View Javadoc
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  import wjhk.jupload2.gui.filepanel.FilePanel.FileListViewMode;
38  
39  /**
40   * Implementation of the {@link JUploadContext}, for an executable, that is: for a stand alone application. One such
41   * context is created at run time. Its main capabilities, is to load the properties either by a file in the jar file
42   * (see DAEMON_PROPERTIES_FILE), or an URL given to the {@link JUploadDaemon#main(String[])} method.
43   * 
44   * @see DefaultJUploadContext
45   * @see JUploadDaemon
46   * @author etienne_sf
47   * @version $Revision: 750 $
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       * The main window of the application.
57       */
58      private JFrame jframe = null;
59  
60      /**
61       * Content of the /conf/default_deamon.properties file. These value override default value, that would be wrong
62       * values for the daemon standalone application.
63       */
64      protected Properties defaultProperties = null;
65  
66      /**
67       * Content of the /conf/_deamon.properties file. These value are the properties given to parameterize the daemon,
68       * according to the specific needs of the project.
69       */
70      protected Properties daemonProperties = null;
71  
72      /**
73       * This constructor does nothing. It should be used by test case only.
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       * The constructor of the context, which needs the top level container to be created.
84       * 
85       * @param jframe The owner TopLevelWindow
86       * @param propertiesURL The URL where the configuration properties for the daemon can be read. If null, the daemon
87       *            try to read the /conf/daemon.properties file, in the current jar.
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          // Load default properties
96          this.defaultProperties = loadPropertiesFromFileInJar(DEFAULT_PROPERTIES_FILE, null);
97  
98          // Load daemon properties: from the given URL or from the file.
99          if (propertiesURL == null) {
100             // No URL given. We load properties from the 'standard' file, in the
101             // jar.
102             this.daemonProperties = loadPropertiesFromFileInJar(DAEMON_PROPERTIES_FILE, this.defaultProperties);
103         } else {
104             // Let's load the properties from this URL.
105             this.daemonProperties = loadPropertiesFromURL(propertiesURL, this.defaultProperties);
106         }
107 
108         // Now, we're ready. Let's initialize the DefaultJUploadContext.
109         init(jframe, this.jframe);
110     }
111 
112     /**
113      * Creates and loads a property file, and return the loaded result.
114      * 
115      * @param filename The name of the file, which contains the properties to load
116      * @param defaultProperties The default properties value. Put null if no default Properties should be used.
117      * @return The loaded properties. It's empty if an error occurs.
118      */
119     Properties loadPropertiesFromFileInJar(String filename, Properties defaultProperties) {
120         Properties properties = new Properties(defaultProperties);
121         try {
122             // TODO use this.getClass() ?
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      * Creates and loads a property file, and return the loaded result.
139      * 
140      * @param propertiesURL The url that points to the properties configuration file for the daemon.
141      * @param defaultProperties The default properties value. Put null if no default Properties should be used.
142      * @return The loaded properties. It's empty if an error occurs.
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      * Get a String parameter value from applet properties or System properties.
164      * 
165      * @param key The name of the parameter to fetch.
166      * @param def A default value which is used, when the specified parameter is not set.
167      * @return The value of the applet parameter (resp. system property). If the parameter was not specified or no such
168      *         system property exists, returns the given default value.
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     }// getParameter(boolean)
218 
219     /** {@inheritDoc} */
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     }// getParameter(FileListViewMode)
228 
229     /** {@inheritDoc} */
230     @Override
231     public void displayURL(String url, boolean success) {
232         throw new UnsupportedOperationException("JUploadContextExecution.displayURL(): Not implemented yet!");
233     }
234 
235     /** {@inheritDoc} */
236     @Override
237     public JApplet getApplet() {
238         throw new UnsupportedOperationException("Can't use getApplet(), when using the JUploadDaemon!");
239     }
240 
241     /** {@inheritDoc} */
242     @Override
243     public Cursor getCursor() {
244         return this.jframe.getCursor();
245     }
246 
247     /**
248      * This class doesn't control the URL. It expects it to be already normalized. No work here. {@inheritDoc}
249      */
250     @Override
251     public String normalizeURL(String url) throws JUploadException {
252         return url;
253     }
254 
255     /** {@inheritDoc} */
256     @Override
257     public void readCookieFromNavigator(Vector<String> headers) {
258         throw new UnsupportedOperationException("Can't use readCookieFromNavigator(), when using the JUploadDaemon!");
259     }
260 
261     /** {@inheritDoc} */
262     @Override
263     public void readUserAgentFromNavigator(Vector<String> headers) {
264         throw new UnsupportedOperationException("Can't use readUserAgentFromNavigator(), when using the JUploadDaemon!");
265     }
266 
267     /** {@inheritDoc} */
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     /** {@inheritDoc} */
276     @Override
277     public void showStatus(String status) {
278         // TODO Auto-generated method stub
279 
280     }
281 
282 }