ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/java/ProxySettings/src/net/oni2/ProxySettings.java
Revision: 852
Committed: Fri May 3 12:25:21 2013 UTC (12 years, 5 months ago) by alloc
Content type: text/x-java
File size: 4273 byte(s)
Log Message:
AEI2.08, AEIUpdater:
- Fixes #3

File Contents

# Content
1 package net.oni2;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.Serializable;
9 import java.net.InetSocketAddress;
10 import java.net.MalformedURLException;
11 import java.net.Proxy;
12 import java.net.Proxy.Type;
13 import java.net.SocketTimeoutException;
14 import java.net.URL;
15 import java.net.URLConnection;
16
17 import com.thoughtworks.xstream.XStream;
18 import com.thoughtworks.xstream.io.xml.StaxDriver;
19
20 /**
21 * @author Christian Illy
22 */
23 public class ProxySettings implements Serializable {
24 private static final long serialVersionUID = 7764207071836441954L;
25
26 private static ProxySettings instance = new ProxySettings();
27
28 private boolean useProxy = false;
29 private String hostOrIp = null;
30 private int port = -1;
31
32 private transient boolean validated = false;
33 private transient boolean validatedOk = false;
34
35 /**
36 * Get the proxy for URL connections. Validates connection first if not
37 * validated yet
38 *
39 * @return Proxy
40 */
41 public Proxy getProxy() {
42 if (!useProxy)
43 return Proxy.NO_PROXY;
44 if (!validated)
45 validate();
46 if (!validatedOk)
47 return Proxy.NO_PROXY;
48
49 return new Proxy(Type.HTTP, new InetSocketAddress(hostOrIp, port));
50 }
51
52 /**
53 * Check if a connection to the given proxy can be established
54 *
55 * @return Data entered and connection Ok?
56 */
57 public boolean validate() {
58 if (validated) {
59 return validatedOk;
60 } else {
61 validated = true;
62 if (isValid()) {
63 try {
64 URLConnection con = new URL(String.format("http://%s:%d",
65 hostOrIp, port)).openConnection();
66 con.setConnectTimeout(1000);
67 con.connect();
68 validatedOk = true;
69 return true;
70 } catch (MalformedURLException e) {
71 e.printStackTrace();
72 } catch (SocketTimeoutException e) {
73 e.printStackTrace();
74 } catch (IOException e) {
75 e.printStackTrace();
76 }
77 }
78 validatedOk = false;
79 return false;
80 }
81 }
82
83 /**
84 * @return Is data for both host and port entered?
85 */
86 public boolean isValid() {
87 return (hostOrIp != null) && (port > 0);
88 }
89
90 /**
91 * @return Should a proxy be used for connections?
92 */
93 public boolean isUseProxy() {
94 return useProxy;
95 }
96
97 /**
98 * @param use
99 * Should a proxy be used for connections?
100 */
101 public void setUseProxy(boolean use) {
102 this.useProxy = use;
103 }
104
105 /**
106 * @return Hostname or IP of host to use as proxy
107 */
108 public String getHostOrIp() {
109 return hostOrIp;
110 }
111
112 /**
113 * @param hoi
114 * Hostname or IP of host to use as proxy
115 */
116 public void setHostOrIp(String hoi) {
117 validated = false;
118 this.hostOrIp = hoi;
119 }
120
121 /**
122 * @return Port number proxy is listening on
123 */
124 public int getPort() {
125 return port;
126 }
127
128 /**
129 * @param port
130 * Port number proxy is listening on
131 */
132 public void setPort(int port) {
133 validated = false;
134 this.port = port;
135 }
136
137 /**
138 * Get the singleton instance
139 *
140 * @return Singleton instance
141 */
142 public static ProxySettings getInstance() {
143 return instance;
144 }
145
146 private static XStream getXStream() {
147 XStream xs = new XStream(new StaxDriver());
148 xs.alias("Proxy", ProxySettings.class);
149 return xs;
150 }
151
152 /**
153 * Serializes the settings to disk
154 *
155 * @param settingsFile
156 * File to write to
157 */
158 public void serializeToFile(File settingsFile) {
159 try {
160 FileOutputStream fos = new FileOutputStream(settingsFile);
161 XStream xs = getXStream();
162 xs.toXML(this, fos);
163 fos.close();
164 } catch (FileNotFoundException e) {
165 e.printStackTrace();
166 } catch (IOException e) {
167 e.printStackTrace();
168 }
169 }
170
171 /**
172 * Deserializes the settings from disk
173 *
174 * @param settingsFile
175 * File to read from
176 */
177 public static void deserializeFromFile(File settingsFile) {
178 try {
179 FileInputStream fis = new FileInputStream(settingsFile);
180 XStream xs = getXStream();
181 Object obj = xs.fromXML(fis);
182 if (obj instanceof ProxySettings)
183 instance = (ProxySettings) obj;
184 fis.close();
185 } catch (FileNotFoundException e) {
186 } catch (IOException e) {
187 }
188 }
189
190 }