1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package wjhk.jupload2.context;
22
23 import java.awt.Cursor;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.net.URLConnection;
29 import java.util.Properties;
30 import java.util.Vector;
31
32 import javax.swing.JApplet;
33 import javax.swing.JFrame;
34
35 import wjhk.jupload2.JUploadDaemon;
36 import wjhk.jupload2.exception.JUploadException;
37 import wjhk.jupload2.gui.filepanel.FilePanel.FileListViewMode;
38
39
40
41
42
43
44
45
46
47
48
49 public class JUploadContextExecutable extends DefaultJUploadContext {
50
51 final static String DEFAULT_PROPERTIES_FILE = "/conf/default_daemon.properties";
52
53 final static String DAEMON_PROPERTIES_FILE = "/conf/daemon.properties";
54
55
56
57
58 private JFrame jframe = null;
59
60
61
62
63
64 protected Properties defaultProperties = null;
65
66
67
68
69
70 protected Properties daemonProperties = null;
71
72
73
74
75 protected JUploadContextExecutable(JFrame jframe) {
76 if (jframe == null) {
77 throw new IllegalArgumentException("theApplet may not be null");
78 }
79 this.jframe = jframe;
80 }
81
82
83
84
85
86
87
88
89 public JUploadContextExecutable(JFrame jframe, String propertiesURL) {
90 if (jframe == null) {
91 throw new IllegalArgumentException("The jframe may not be null");
92 }
93 this.jframe = jframe;
94
95
96 this.defaultProperties = loadPropertiesFromFileInJar(DEFAULT_PROPERTIES_FILE, null);
97
98
99 if (propertiesURL == null) {
100
101
102 this.daemonProperties = loadPropertiesFromFileInJar(DAEMON_PROPERTIES_FILE, this.defaultProperties);
103 } else {
104
105 this.daemonProperties = loadPropertiesFromURL(propertiesURL, this.defaultProperties);
106 }
107
108
109 init(jframe, this.jframe);
110 }
111
112
113
114
115
116
117
118
119 Properties loadPropertiesFromFileInJar(String filename, Properties defaultProperties) {
120 Properties properties = new Properties(defaultProperties);
121 try {
122
123 InputStream isProperties = Class.forName("wjhk.jupload2.JUploadApplet").getResourceAsStream(filename);
124 properties.load(isProperties);
125 isProperties.close();
126 } catch (IOException e1) {
127 System.out.println("Error while loading " + filename + " (" + e1.getClass().getName() + ")");
128 e1.printStackTrace();
129 } catch (ClassNotFoundException e1) {
130 System.out.println("Error while loading " + filename + " (" + e1.getClass().getName() + ")");
131 e1.printStackTrace();
132 }
133
134 return properties;
135 }
136
137
138
139
140
141
142
143
144 private Properties loadPropertiesFromURL(String propertiesURL, Properties defaultProperties) {
145 Properties properties = new Properties(defaultProperties);
146 URL url;
147 try {
148 url = new URL(propertiesURL);
149 URLConnection urlConnection = url.openConnection();
150 properties.load(urlConnection.getInputStream());
151 } catch (MalformedURLException e) {
152 System.out.println("Error while loading url " + propertiesURL + " (" + e.getClass().getName() + ")");
153 e.printStackTrace();
154 } catch (IOException e) {
155 System.out.println("Error while loading url " + propertiesURL + " (" + e.getClass().getName() + ")");
156 e.printStackTrace();
157 }
158
159 return properties;
160 }
161
162
163
164
165
166
167
168
169
170 @Override
171 public String getParameter(String key, String def) {
172 String paramStr = (this.daemonProperties.getProperty(key) != null ? this.daemonProperties.getProperty(key)
173 : def);
174 displayDebugParameterValue(key, paramStr);
175 return paramStr;
176 }
177
178
179
180 @Override
181 public int getParameter(String key, int def) {
182 String paramDef = Integer.toString(def);
183 String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties.getProperty(key)
184 : paramDef;
185 displayDebugParameterValue(key, paramStr);
186 return parseInt(paramStr, def);
187 }
188
189
190 @Override
191 public float getParameter(String key, float def) {
192 String paramDef = Float.toString(def);
193 String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties.getProperty(key)
194 : paramDef;
195 displayDebugParameterValue(key, paramStr);
196 return parseFloat(paramStr, def);
197 }
198
199
200 @Override
201 public long getParameter(String key, long def) {
202 String paramDef = Long.toString(def);
203 String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties.getProperty(key)
204 : paramDef;
205 displayDebugParameterValue(key, paramStr);
206 return parseLong(paramStr, def);
207 }
208
209
210 @Override
211 public boolean getParameter(String key, boolean def) {
212 String paramDef = (def ? "true" : "false");
213 String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties.getProperty(key)
214 : paramDef;
215 displayDebugParameterValue(key, paramStr);
216 return parseBoolean(paramStr, def);
217 }
218
219
220 @Override
221 public FileListViewMode getParameter(String key, FileListViewMode def) {
222 String paramDef = def.toString();
223 String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties.getProperty(key)
224 : paramDef;
225 displayDebugParameterValue(key, paramStr);
226 return parseFileListViewMode(paramStr, def);
227 }
228
229
230 @Override
231 public void displayURL(String url, boolean success) {
232 throw new UnsupportedOperationException("JUploadContextExecution.displayURL(): Not implemented yet!");
233 }
234
235
236 @Override
237 public JApplet getApplet() {
238 throw new UnsupportedOperationException("Can't use getApplet(), when using the JUploadDaemon!");
239 }
240
241
242 @Override
243 public Cursor getCursor() {
244 return this.jframe.getCursor();
245 }
246
247
248
249
250 @Override
251 public String normalizeURL(String url) throws JUploadException {
252 return url;
253 }
254
255
256 @Override
257 public void readCookieFromNavigator(Vector<String> headers) {
258 throw new UnsupportedOperationException("Can't use readCookieFromNavigator(), when using the JUploadDaemon!");
259 }
260
261
262 @Override
263 public void readUserAgentFromNavigator(Vector<String> headers) {
264 throw new UnsupportedOperationException("Can't use readUserAgentFromNavigator(), when using the JUploadDaemon!");
265 }
266
267
268 @Override
269 public Cursor setCursor(Cursor cursor) {
270 Cursor previousCursor = this.jframe.getCursor();
271 this.jframe.setCursor(cursor);
272 return previousCursor;
273 }
274
275
276 @Override
277 public void showStatus(String status) {
278
279
280 }
281
282 }