1 | //
|
---|
2 | // $Id: JUploadApplet.java 750 2009-05-06 14:36:50Z etienne_sf $
|
---|
3 | //
|
---|
4 | // jupload - A file upload applet.
|
---|
5 | // Copyright 2007 The JUpload Team
|
---|
6 | //
|
---|
7 | // Created: ?
|
---|
8 | // Creator: William JinHua Kwong
|
---|
9 | // Last modified: $Date: 2009-05-06 16:36:50 +0200 (mer., 06 mai 2009) $
|
---|
10 | //
|
---|
11 | // This program is free software; you can redistribute it and/or modify it under
|
---|
12 | // the terms of the GNU General Public License as published by the Free Software
|
---|
13 | // Foundation; either version 2 of the License, or (at your option) any later
|
---|
14 | // version. This program is distributed in the hope that it will be useful, but
|
---|
15 | // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
16 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
---|
17 | // details. You should have received a copy of the GNU General Public License
|
---|
18 | // along with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
19 | // 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
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 |
|
---|
38 | /**
|
---|
39 | * Implementation of the {@link JUploadContext}, for an executable, that is: for
|
---|
40 | * a stand alone application. One such context is created at run time. Its main
|
---|
41 | * capabilities, is to load the properties either by a file in the jar file (see
|
---|
42 | * DAEMON_PROPERTIES_FILE), or an URL given to the
|
---|
43 | * {@link JUploadDaemon#main(String[])} method.
|
---|
44 | *
|
---|
45 | * @see DefaultJUploadContext
|
---|
46 | * @see JUploadDaemon
|
---|
47 | * @author etienne_sf
|
---|
48 | * @version $Revision: 750 $
|
---|
49 | */
|
---|
50 | public class JUploadContextExecutable extends DefaultJUploadContext {
|
---|
51 |
|
---|
52 | final static String DEFAULT_PROPERTIES_FILE = "/conf/default_daemon.properties";
|
---|
53 |
|
---|
54 | final static String DAEMON_PROPERTIES_FILE = "/conf/daemon.properties";
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * The main window of the application.
|
---|
58 | */
|
---|
59 | private JFrame jframe = null;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Content of the /conf/default_deamon.properties file. These value override
|
---|
63 | * default value, that would be wrong values for the daemon standalone
|
---|
64 | * application.
|
---|
65 | */
|
---|
66 | protected Properties defaultProperties = null;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Content of the /conf/_deamon.properties file. These value are the
|
---|
70 | * properties given to parameterize the daemon, according to the specific
|
---|
71 | * needs of the project.
|
---|
72 | */
|
---|
73 | protected Properties daemonProperties = null;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * This constructor does nothing. It should be used by test case only.
|
---|
77 | */
|
---|
78 | protected JUploadContextExecutable(JFrame jframe) {
|
---|
79 | if (jframe == null) {
|
---|
80 | throw new IllegalArgumentException("theApplet may not be null");
|
---|
81 | }
|
---|
82 | this.jframe = jframe;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * The constructor of the context, which needs the top level container to be
|
---|
87 | * created.
|
---|
88 | *
|
---|
89 | * @param jframe The owner TopLevelWindow
|
---|
90 | * @param propertiesURL The URL where the configuration properties for the
|
---|
91 | * daemon can be read. If null, the daemon try to read the
|
---|
92 | * /conf/daemon.properties file, in the current jar.
|
---|
93 | */
|
---|
94 | public JUploadContextExecutable(JFrame jframe, String propertiesURL) {
|
---|
95 | if (jframe == null) {
|
---|
96 | throw new IllegalArgumentException("The jframe may not be null");
|
---|
97 | }
|
---|
98 | this.jframe = jframe;
|
---|
99 |
|
---|
100 | // Load default properties
|
---|
101 | this.defaultProperties = loadPropertiesFromFileInJar(
|
---|
102 | DEFAULT_PROPERTIES_FILE, null);
|
---|
103 |
|
---|
104 | // Load daemon properties: from the given URL or from the file.
|
---|
105 | if (propertiesURL == null) {
|
---|
106 | // No URL given. We load properties from the 'standard' file, in the
|
---|
107 | // jar.
|
---|
108 | this.daemonProperties = loadPropertiesFromFileInJar(
|
---|
109 | DAEMON_PROPERTIES_FILE, this.defaultProperties);
|
---|
110 | } else {
|
---|
111 | // Let's load the properties from this URL.
|
---|
112 | this.daemonProperties = loadPropertiesFromURL(propertiesURL,
|
---|
113 | this.defaultProperties);
|
---|
114 | }
|
---|
115 |
|
---|
116 | // Now, we're ready. Let's initialize the DefaultJUploadContext.
|
---|
117 | init(jframe, this.jframe);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Creates and loads a property file, and return the loaded result.
|
---|
122 | *
|
---|
123 | * @param filename The name of the file, which contains the properties to
|
---|
124 | * load
|
---|
125 | * @param defaultProperties The default properties value. Put null if no
|
---|
126 | * default Properties should be used.
|
---|
127 | * @return The loaded properties. It's empty if an error occurs.
|
---|
128 | */
|
---|
129 | Properties loadPropertiesFromFileInJar(String filename,
|
---|
130 | Properties defaultProperties) {
|
---|
131 | Properties properties = new Properties(defaultProperties);
|
---|
132 | try {
|
---|
133 | // TODO use this.getClass() ?
|
---|
134 | InputStream isProperties = Class.forName(
|
---|
135 | "wjhk.jupload2.JUploadApplet")
|
---|
136 | .getResourceAsStream(filename);
|
---|
137 | properties.load(isProperties);
|
---|
138 | isProperties.close();
|
---|
139 | } catch (IOException e1) {
|
---|
140 | System.out.println("Error while loading " + filename + " ("
|
---|
141 | + e1.getClass().getName() + ")");
|
---|
142 | e1.printStackTrace();
|
---|
143 | } catch (ClassNotFoundException e1) {
|
---|
144 | System.out.println("Error while loading " + filename + " ("
|
---|
145 | + e1.getClass().getName() + ")");
|
---|
146 | e1.printStackTrace();
|
---|
147 | }
|
---|
148 |
|
---|
149 | return properties;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Creates and loads a property file, and return the loaded result.
|
---|
154 | *
|
---|
155 | * @param propertiesURL The url that points to the properties configuration
|
---|
156 | * file for the daemon.
|
---|
157 | * @param defaultProperties The default properties value. Put null if no
|
---|
158 | * default Properties should be used.
|
---|
159 | * @return The loaded properties. It's empty if an error occurs.
|
---|
160 | */
|
---|
161 | private Properties loadPropertiesFromURL(String propertiesURL,
|
---|
162 | Properties defaultProperties) {
|
---|
163 | Properties properties = new Properties(defaultProperties);
|
---|
164 | URL url;
|
---|
165 | try {
|
---|
166 | url = new URL(propertiesURL);
|
---|
167 | URLConnection urlConnection = url.openConnection();
|
---|
168 | properties.load(urlConnection.getInputStream());
|
---|
169 | } catch (MalformedURLException e) {
|
---|
170 | System.out.println("Error while loading url " + propertiesURL
|
---|
171 | + " (" + e.getClass().getName() + ")");
|
---|
172 | e.printStackTrace();
|
---|
173 | } catch (IOException e) {
|
---|
174 | System.out.println("Error while loading url " + propertiesURL
|
---|
175 | + " (" + e.getClass().getName() + ")");
|
---|
176 | e.printStackTrace();
|
---|
177 | }
|
---|
178 |
|
---|
179 | return properties;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Get a String parameter value from applet properties or System properties.
|
---|
184 | *
|
---|
185 | * @param key The name of the parameter to fetch.
|
---|
186 | * @param def A default value which is used, when the specified parameter is
|
---|
187 | * not set.
|
---|
188 | * @return The value of the applet parameter (resp. system property). If the
|
---|
189 | * parameter was not specified or no such system property exists,
|
---|
190 | * returns the given default value.
|
---|
191 | */
|
---|
192 | @Override
|
---|
193 | public String getParameter(String key, String def) {
|
---|
194 | String paramStr = (this.daemonProperties.getProperty(key) != null ? this.daemonProperties
|
---|
195 | .getProperty(key)
|
---|
196 | : def);
|
---|
197 | displayDebugParameterValue(key, paramStr);
|
---|
198 | return paramStr;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /** {@inheritDoc} */
|
---|
202 |
|
---|
203 | @Override
|
---|
204 | public int getParameter(String key, int def) {
|
---|
205 | String paramDef = Integer.toString(def);
|
---|
206 | String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties
|
---|
207 | .getProperty(key)
|
---|
208 | : paramDef;
|
---|
209 | displayDebugParameterValue(key, paramStr);
|
---|
210 | return parseInt(paramStr, def);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** {@inheritDoc} */
|
---|
214 | @Override
|
---|
215 | public float getParameter(String key, float def) {
|
---|
216 | String paramDef = Float.toString(def);
|
---|
217 | String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties
|
---|
218 | .getProperty(key)
|
---|
219 | : paramDef;
|
---|
220 | displayDebugParameterValue(key, paramStr);
|
---|
221 | return parseFloat(paramStr, def);
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** {@inheritDoc} */
|
---|
225 | @Override
|
---|
226 | public long getParameter(String key, long def) {
|
---|
227 | String paramDef = Long.toString(def);
|
---|
228 | String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties
|
---|
229 | .getProperty(key)
|
---|
230 | : paramDef;
|
---|
231 | displayDebugParameterValue(key, paramStr);
|
---|
232 | return parseLong(paramStr, def);
|
---|
233 | }
|
---|
234 |
|
---|
235 | /** {@inheritDoc} */
|
---|
236 | @Override
|
---|
237 | public boolean getParameter(String key, boolean def) {
|
---|
238 | String paramDef = (def ? "true" : "false");
|
---|
239 | String paramStr = this.daemonProperties.getProperty(key) != null ? this.daemonProperties
|
---|
240 | .getProperty(key)
|
---|
241 | : paramDef;
|
---|
242 | displayDebugParameterValue(key, paramStr);
|
---|
243 | return parseBoolean(paramStr, def);
|
---|
244 | }// getParameter(boolean)
|
---|
245 |
|
---|
246 | /** {@inheritDoc} */
|
---|
247 | @Override
|
---|
248 | public void displayURL(String url, boolean success) {
|
---|
249 | throw new UnsupportedOperationException(
|
---|
250 | "JUploadContextExecution.displayURL(): Not implemented yet!");
|
---|
251 | }
|
---|
252 |
|
---|
253 | /** {@inheritDoc} */
|
---|
254 | @Override
|
---|
255 | public JApplet getApplet() {
|
---|
256 | throw new UnsupportedOperationException(
|
---|
257 | "Can't use getApplet(), when using the JUploadDaemon!");
|
---|
258 | }
|
---|
259 |
|
---|
260 | /** {@inheritDoc} */
|
---|
261 | @Override
|
---|
262 | public Cursor getCursor() {
|
---|
263 | return this.jframe.getCursor();
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * This class doesn't control the URL. It expects it to be already
|
---|
268 | * normalized. No work here. {@inheritDoc}
|
---|
269 | */
|
---|
270 | @Override
|
---|
271 | public String normalizeURL(String url) throws JUploadException {
|
---|
272 | return url;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /** {@inheritDoc} */
|
---|
276 | @Override
|
---|
277 | public void readCookieFromNavigator(Vector<String> headers) {
|
---|
278 | throw new UnsupportedOperationException(
|
---|
279 | "Can't use readCookieFromNavigator(), when using the JUploadDaemon!");
|
---|
280 | }
|
---|
281 |
|
---|
282 | /** {@inheritDoc} */
|
---|
283 | @Override
|
---|
284 | public void readUserAgentFromNavigator(Vector<String> headers) {
|
---|
285 | throw new UnsupportedOperationException(
|
---|
286 | "Can't use readUserAgentFromNavigator(), when using the JUploadDaemon!");
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** {@inheritDoc} */
|
---|
290 | @Override
|
---|
291 | public Cursor setCursor(Cursor cursor) {
|
---|
292 | Cursor previousCursor = this.jframe.getCursor();
|
---|
293 | this.jframe.setCursor(cursor);
|
---|
294 | return previousCursor;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** {@inheritDoc} */
|
---|
298 | @Override
|
---|
299 | public void showStatus(String status) {
|
---|
300 | // TODO Auto-generated method stub
|
---|
301 |
|
---|
302 | }
|
---|
303 |
|
---|
304 | }
|
---|