[3951] | 1 | // $Id: JUploadApplet.java 750 2009-05-06 14:36:50Z etienne_sf $
|
---|
| 2 | //
|
---|
| 3 | // jupload - A file upload applet.
|
---|
| 4 | // Copyright 2007 The JUpload Team
|
---|
| 5 | //
|
---|
| 6 | // Created: ?
|
---|
| 7 | // Creator: William JinHua Kwong
|
---|
| 8 | // Last modified: $Date: 2009-05-06 16:36:50 +0200 (mer., 06 mai 2009) $
|
---|
| 9 | //
|
---|
| 10 | // This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | // the terms of the GNU General Public License as published by the Free Software
|
---|
| 12 | // Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | // version. This program is distributed in the hope that it will be useful, but
|
---|
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
| 15 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
---|
| 16 | // details. You should have received a copy of the GNU General Public License
|
---|
| 17 | // along with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
| 18 | // 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 19 |
|
---|
| 20 | package wjhk.jupload2.context;
|
---|
| 21 |
|
---|
| 22 | import java.awt.Cursor;
|
---|
| 23 | import java.awt.Frame;
|
---|
| 24 | import java.io.InputStream;
|
---|
| 25 | import java.lang.reflect.InvocationTargetException;
|
---|
| 26 | import java.lang.reflect.Method;
|
---|
| 27 | import java.text.MessageFormat;
|
---|
| 28 | import java.util.ArrayList;
|
---|
| 29 | import java.util.Date;
|
---|
| 30 | import java.util.List;
|
---|
| 31 | import java.util.Locale;
|
---|
| 32 | import java.util.Properties;
|
---|
| 33 | import java.util.Vector;
|
---|
| 34 |
|
---|
| 35 | import javax.swing.JApplet;
|
---|
| 36 | import javax.swing.JOptionPane;
|
---|
| 37 | import javax.swing.RootPaneContainer;
|
---|
| 38 | import javax.swing.SwingUtilities;
|
---|
| 39 |
|
---|
| 40 | import wjhk.jupload2.exception.JUploadException;
|
---|
| 41 | import wjhk.jupload2.gui.JUploadPanel;
|
---|
| 42 | import wjhk.jupload2.gui.JUploadPanelImpl;
|
---|
| 43 | import wjhk.jupload2.gui.JUploadTextArea;
|
---|
| 44 | import wjhk.jupload2.policies.UploadPolicy;
|
---|
| 45 | import wjhk.jupload2.policies.UploadPolicyFactory;
|
---|
| 46 | import wjhk.jupload2.upload.FileUploadManagerThread;
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * The Jupload Context. One such context is created at run time. It can be the
|
---|
| 50 | * Applet, or the 'main' class, depending on the launch type. <BR>
|
---|
| 51 | * It contains the call to the creation of the
|
---|
| 52 | * {@link wjhk.jupload2.gui.JUploadPanel}, which contains the real code, and
|
---|
| 53 | * some technical stuff that depend on the technical context (mainly applet or
|
---|
| 54 | * stand alone application). <BR>
|
---|
| 55 | * The functional control of JUpload is done by using {@link UploadPolicy}. This
|
---|
| 56 | * class should not be changed, in order to remain compatible with next JUpload
|
---|
| 57 | * releases. <BR>
|
---|
| 58 | * <BR>
|
---|
| 59 | * <B>Technical note:</B> This class should be abstract. But it is used by the
|
---|
| 60 | * build.xml file, to load the version. So all methods of the
|
---|
| 61 | * {@link JUploadContext} interface are implemented. Those who actually can't be
|
---|
| 62 | * coded here, just generate a UnsupportedOperationException exception.
|
---|
| 63 | *
|
---|
| 64 | * @author etienne_sf
|
---|
| 65 | * @version $Revision: 750 $
|
---|
| 66 | */
|
---|
| 67 | public class DefaultJUploadContext implements JUploadContext {
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * The final that contains the SVN properties. These properties are
|
---|
| 71 | * generated during compilation, by the build.xml ant file.
|
---|
| 72 | */
|
---|
| 73 | private final static String SVN_PROPERTIES_FILENAME = "/conf/svn.properties";
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Used as default value when calling getProperty, to identify missing
|
---|
| 77 | * properties, and have a correct behavior in this case.
|
---|
| 78 | */
|
---|
| 79 | private final static String DEFAULT_PROP_UNKNOWN = "Unknown";
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * The properties, created at build time, by the build.xml ant file. Or a
|
---|
| 83 | * dummy property set, with 'unknown' values.
|
---|
| 84 | */
|
---|
| 85 | Properties svnProperties = getSvnProperties();
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * The frame that contains the application. Mainly used to attached modal
|
---|
| 89 | * dialog.
|
---|
| 90 | */
|
---|
| 91 | Frame frame = null;
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * variable to hold reference to JavascriptHandler object
|
---|
| 95 | */
|
---|
| 96 | JavascriptHandler jsHandler = null;
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * the mime type list, coming from: http://www.mimetype.org/ Thanks to them!
|
---|
| 100 | */
|
---|
| 101 | Properties mimeTypesProperties = null;
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * The current upload policy. This class is responsible for the call to the
|
---|
| 105 | * UploadPolicyFactory.
|
---|
| 106 | */
|
---|
| 107 | UploadPolicy uploadPolicy = null;
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * The JUploadPanel, which actually contains all the applet components.
|
---|
| 111 | */
|
---|
| 112 | JUploadPanel jUploadPanel = null;
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * The log messages should go there ...
|
---|
| 116 | */
|
---|
| 117 | JUploadTextArea logWindow = null;
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * This class represent the Callback method. It is then possible to run the
|
---|
| 121 | * {@link JUploadContext#registerUnload(Object, String)} method to register
|
---|
| 122 | * new callback methods. These callback methods are executed when the applet
|
---|
| 123 | * or the application closes, by calling the
|
---|
| 124 | * {@link JUploadContext#runUnload()} method.
|
---|
| 125 | */
|
---|
| 126 | static class Callback {
|
---|
| 127 | private String method;
|
---|
| 128 |
|
---|
| 129 | private Object object;
|
---|
| 130 |
|
---|
| 131 | Callback(Object object, String method) {
|
---|
| 132 | this.object = object;
|
---|
| 133 | this.method = method;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void invoke() throws IllegalArgumentException, IllegalAccessException,
|
---|
| 137 | InvocationTargetException, SecurityException {
|
---|
| 138 | Object args[] = {};
|
---|
| 139 | Method methods[] = this.object.getClass().getMethods();
|
---|
| 140 | for (int i = 0; i < methods.length; i++) {
|
---|
| 141 | if (methods[i].getName().equals(this.method)) {
|
---|
| 142 | methods[i].invoke(this.object, args);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * @return the method
|
---|
| 149 | */
|
---|
| 150 | public String getMethod() {
|
---|
| 151 | return method;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * @return the object
|
---|
| 156 | */
|
---|
| 157 | public Object getObject() {
|
---|
| 158 | return object;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * All registered callbacks.
|
---|
| 165 | *
|
---|
| 166 | * @see Callback
|
---|
| 167 | */
|
---|
| 168 | List<Callback> unloadCallbacks = new ArrayList<Callback>(20);
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * Reaction on the start of the applet: creation of each specific item of
|
---|
| 172 | * the GUI, and the upload policy. <BR>
|
---|
| 173 | * This method needs that the initialization of the called is finished. For
|
---|
| 174 | * instance, {@link JUploadContextApplet} needs to have set theApplet, to be
|
---|
| 175 | * able to properly execute some method calls that are in the init() method.
|
---|
| 176 | * So we can not do this initialization in the constructor of
|
---|
| 177 | * DefaultJUploadContext.
|
---|
| 178 | *
|
---|
| 179 | * @param frame
|
---|
| 180 | * The frame that contains the application. Mainly used to
|
---|
| 181 | * attached modal dialog.
|
---|
| 182 | * @param rootPaneContainer
|
---|
| 183 | * The mother window (JApplet, JFrame...), which contains the
|
---|
| 184 | * rootPaneContainer. Used to set the {@link JUploadPanel} in it.
|
---|
| 185 | */
|
---|
| 186 | public void init(Frame frame, RootPaneContainer rootPaneContainer) {
|
---|
| 187 | try {
|
---|
| 188 | this.frame = frame;
|
---|
| 189 |
|
---|
| 190 | // The standard thread name is: thread
|
---|
| 191 | // applet-wjhk.jupload2.JUploadApplet.class
|
---|
| 192 | // Too long ! :-)
|
---|
| 193 | Thread.currentThread().setName(
|
---|
| 194 | rootPaneContainer.getClass().getName());
|
---|
| 195 |
|
---|
| 196 | // The logWindow must exist before the uploadPolicy creation. But it
|
---|
| 197 | // needs the uploadPolicy, to know the logging parameters. We'll set
|
---|
| 198 | // the uploadPolicy just after.
|
---|
| 199 | this.logWindow = new JUploadTextArea(20, 20, this.uploadPolicy);
|
---|
| 200 |
|
---|
| 201 | // Now we can create the upload policy: the logWindow exists.
|
---|
| 202 | this.uploadPolicy = UploadPolicyFactory.getUploadPolicy(this);
|
---|
| 203 | this.uploadPolicy.displayDebug(
|
---|
| 204 | "After UploadPolicyFactory.getUploadPolicy(this)", 80);
|
---|
| 205 |
|
---|
| 206 | // We set the uploadPolicy to the logWindow. The logThread starts,
|
---|
| 207 | // and it register its unload method, to be called when the JUpload
|
---|
| 208 | // finishes.
|
---|
| 209 | this.uploadPolicy.displayDebug(
|
---|
| 210 | "Before this.logWindow.setUploadPolicy(this.uploadPolicy)",
|
---|
| 211 | 80);
|
---|
| 212 | this.logWindow.setUploadPolicy(this.uploadPolicy);
|
---|
| 213 |
|
---|
| 214 | // getMainPanel().setLayout(new BorderLayout());
|
---|
| 215 | this.uploadPolicy
|
---|
| 216 | .displayDebug(
|
---|
| 217 | "Before new JUploadPanelImpl(this.logWindow,this.uploadPolicy)",
|
---|
| 218 | 80);
|
---|
| 219 | this.jUploadPanel = new JUploadPanelImpl(this.logWindow,
|
---|
| 220 | this.uploadPolicy);
|
---|
| 221 |
|
---|
| 222 | // getMainPanel().add(this.jUploadPanel, BorderLayout.CENTER);
|
---|
| 223 | this.uploadPolicy
|
---|
| 224 | .displayDebug(
|
---|
| 225 | "Before rootPaneContainer.setContentPane(this.jUploadPanel);",
|
---|
| 226 | 80);
|
---|
| 227 | rootPaneContainer.setContentPane(this.jUploadPanel.getJComponent());
|
---|
| 228 |
|
---|
| 229 | // We start the jsHandler thread, that allows javascript to send
|
---|
| 230 | // upload command to the applet.
|
---|
| 231 | this.uploadPolicy
|
---|
| 232 | .displayDebug(
|
---|
| 233 | "Before new JavascriptHandler(this.uploadPolicy, this.jUploadPanel)",
|
---|
| 234 | 80);
|
---|
| 235 | this.jsHandler = new JavascriptHandler(this.uploadPolicy,
|
---|
| 236 | this.jUploadPanel);
|
---|
| 237 | this.jsHandler.start();
|
---|
| 238 | // Then we register the unload method, that'll be called like all
|
---|
| 239 | // unload callbacks when the applet is stopped.
|
---|
| 240 | registerUnload(this, "unload");
|
---|
| 241 | } catch (final Exception e) {
|
---|
| 242 | System.out.println(e.getMessage());
|
---|
| 243 | e.printStackTrace();
|
---|
| 244 | // TODO Translate this sentence
|
---|
| 245 | JOptionPane.showMessageDialog(null,
|
---|
| 246 | "Error during applet initialization!\nHave a look in your Java console ("
|
---|
| 247 | + e.getClass().getName() + ")", "Error",
|
---|
| 248 | JOptionPane.ERROR_MESSAGE);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | this.uploadPolicy.displayDebug("Before new Properties();", 80);
|
---|
| 252 | this.mimeTypesProperties = new Properties();
|
---|
| 253 | final String mimetypePropertiesFilename = "/conf/mimetypes.properties";
|
---|
| 254 | try {
|
---|
| 255 | InputStream isProperties = this.getClass().getResourceAsStream(
|
---|
| 256 | mimetypePropertiesFilename);
|
---|
| 257 | this.mimeTypesProperties.load(isProperties);
|
---|
| 258 | isProperties.close();
|
---|
| 259 | this.uploadPolicy.displayDebug("Mime types list loaded Ok ("
|
---|
| 260 | + mimetypePropertiesFilename + ")", 50);
|
---|
| 261 | } catch (Exception e) {
|
---|
| 262 | this.uploadPolicy
|
---|
| 263 | .displayWarn("Unable to load the mime types list ("
|
---|
| 264 | + mimetypePropertiesFilename + "): "
|
---|
| 265 | + e.getClass().getName() + " (" + e.getMessage()
|
---|
| 266 | + ")");
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | this.uploadPolicy.displayDebug("End of DefaultJUploadContext.init()",
|
---|
| 270 | 80);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /**
|
---|
| 274 | * This method is called when the applet is unloaded (actually, when it is
|
---|
| 275 | * stopped). it is registered as a callback in the
|
---|
| 276 | * {@link #init(Frame, RootPaneContainer)}, here above.
|
---|
| 277 | */
|
---|
| 278 | public void unload() {
|
---|
| 279 | if (this.jsHandler != null && this.jsHandler.isAlive()) {
|
---|
| 280 | this.jsHandler.interrupt();
|
---|
| 281 | this.jsHandler = null;
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | /** {@inheritDoc} */
|
---|
| 286 | public String getDetailedVersionMessage() {
|
---|
| 287 | String version = getVersion();
|
---|
| 288 | String svnRevision = getSvnRevision();
|
---|
| 289 | boolean gotSvnRevision = !svnRevision.equals(DEFAULT_PROP_UNKNOWN);
|
---|
| 290 | int buildNumber = getBuildNumber();
|
---|
| 291 | boolean gotBuildNumber = buildNumber > 0;
|
---|
| 292 | String buildDate = getBuildDate();
|
---|
| 293 | boolean gotBuildDate = !buildDate.equals(DEFAULT_PROP_UNKNOWN);
|
---|
| 294 |
|
---|
| 295 | StringBuffer sb = new StringBuffer();
|
---|
| 296 | sb.append(version);
|
---|
| 297 |
|
---|
| 298 | if (gotSvnRevision || gotBuildNumber) {
|
---|
| 299 | sb.append(" [");
|
---|
| 300 | String space = "";
|
---|
| 301 | if (gotSvnRevision) {
|
---|
| 302 | sb.append("SVN-Rev: ");
|
---|
| 303 | sb.append(svnRevision);
|
---|
| 304 | space = " ";
|
---|
| 305 | }
|
---|
| 306 | if (gotBuildNumber) {
|
---|
| 307 | sb.append(space);
|
---|
| 308 | sb.append("build ");
|
---|
| 309 | sb.append(buildNumber);
|
---|
| 310 | }
|
---|
| 311 | sb.append("]");
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | if (gotBuildDate) {
|
---|
| 315 | sb.append(" - ");
|
---|
| 316 | sb.append(buildDate);
|
---|
| 317 | }
|
---|
| 318 | return sb.toString();
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | /** {@inheritDoc} */
|
---|
| 322 | public String getVersion() {
|
---|
| 323 | return getProperty("jupload.version", DEFAULT_PROP_UNKNOWN);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | /** {@inheritDoc} */
|
---|
| 327 | public String getSvnRevision() {
|
---|
| 328 | String svnRevision = getProperty("jupload.svn.revision",
|
---|
| 329 | DEFAULT_PROP_UNKNOWN);
|
---|
| 330 | if (svnRevision.startsWith("{") || svnRevision.startsWith("${")) {
|
---|
| 331 | // This particular case should not happen with standard maven build.
|
---|
| 332 | // But it occurs when launching after an eclipse automatic build.
|
---|
| 333 | // Returning DEFAULT_PROP_UNKNOWN in this case, make the applet
|
---|
| 334 | // behave like if it was built by maven.
|
---|
| 335 | return DEFAULT_PROP_UNKNOWN;
|
---|
| 336 | } else {
|
---|
| 337 | return svnRevision;
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /** {@inheritDoc} */
|
---|
| 342 | public String getLastModified() {
|
---|
| 343 | return getProperty("jupload.lastSrcDirModificationDate",
|
---|
| 344 | DEFAULT_PROP_UNKNOWN);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | /** {@inheritDoc} */
|
---|
| 348 | public String getBuildDate() {
|
---|
| 349 | String timestamp = getProperty("jupload.buildTimestamp",
|
---|
| 350 | DEFAULT_PROP_UNKNOWN);
|
---|
| 351 | if (timestamp.equals(DEFAULT_PROP_UNKNOWN)) {
|
---|
| 352 | return DEFAULT_PROP_UNKNOWN;
|
---|
| 353 | } else {
|
---|
| 354 | Locale locale = Locale.getDefault();
|
---|
| 355 | if (this.uploadPolicy != null) {
|
---|
| 356 | locale = this.uploadPolicy.getLocale();
|
---|
| 357 | }
|
---|
| 358 | MessageFormat msgFormat = new MessageFormat("{0,date,medium}",
|
---|
| 359 | locale);
|
---|
| 360 | try {
|
---|
| 361 | Object[] args = { new Date(Long.parseLong(timestamp)) };
|
---|
| 362 | return msgFormat.format(args);
|
---|
| 363 | } catch (NumberFormatException e) {
|
---|
| 364 | // uploadPolicy is null at startup.
|
---|
| 365 | // TODO Better handling logging, here
|
---|
| 366 | System.out.println("[WARN] The timestamp can not be read ("
|
---|
| 367 | + timestamp + "). Will return '" + DEFAULT_PROP_UNKNOWN
|
---|
| 368 | + "'.");
|
---|
| 369 | return DEFAULT_PROP_UNKNOWN;
|
---|
| 370 | }
|
---|
| 371 | }
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | /** {@inheritDoc} */
|
---|
| 375 | public int getBuildNumber() {
|
---|
| 376 | String valuePropBuildNumber = getProperty("jupload.buildNumber", "-1");
|
---|
| 377 | try {
|
---|
| 378 | return Integer.parseInt(valuePropBuildNumber);
|
---|
| 379 | } catch (java.lang.NumberFormatException e) {
|
---|
| 380 | System.out.println("[WARN] " + e.getClass().getName()
|
---|
| 381 | + " when getting the buildNumber, while parsing '"
|
---|
| 382 | + valuePropBuildNumber + "'). Will return -1");
|
---|
| 383 | return -1;
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | /**
|
---|
| 388 | * Try to get a property from the loaded properties. If the property is not
|
---|
| 389 | * available, the default value is returned.
|
---|
| 390 | *
|
---|
| 391 | * @param propertyName
|
---|
| 392 | * @param defaultValue
|
---|
| 393 | * @return
|
---|
| 394 | */
|
---|
| 395 | private String getProperty(String propertyName, String defaultValue) {
|
---|
| 396 | String value = null;
|
---|
| 397 | try {
|
---|
| 398 | value = this.svnProperties.getProperty(propertyName);
|
---|
| 399 | } catch (Exception e) {
|
---|
| 400 | System.out.println("[WARN] " + e.getClass().getName()
|
---|
| 401 | + " when getting the " + propertyName + " property ("
|
---|
| 402 | + e.getMessage() + "). Will return '" + value + "'");
|
---|
| 403 | }
|
---|
| 404 | return (value == null) ? defaultValue : value;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | /** {@inheritDoc} */
|
---|
| 408 | public JUploadTextArea getLogWindow() {
|
---|
| 409 | return this.logWindow;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | /** {@inheritDoc} */
|
---|
| 413 | public String getMimeType(String fileExtension) {
|
---|
| 414 | String mimeType = this.mimeTypesProperties.getProperty(fileExtension
|
---|
| 415 | .toLowerCase());
|
---|
| 416 | return (mimeType == null) ? "application/octet-stream" : mimeType;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | /** {@inheritDoc} */
|
---|
| 420 | public JUploadPanel getUploadPanel() {
|
---|
| 421 | return this.jUploadPanel;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | /**
|
---|
| 425 | * Retrieves the current upload policy. The JUploadContext is responsible
|
---|
| 426 | * for storing the UploadPolicy associated with the current instance.
|
---|
| 427 | *
|
---|
| 428 | * @return the current upload policy of this instance.
|
---|
| 429 | * @throws JUploadException
|
---|
| 430 | */
|
---|
| 431 | public UploadPolicy getUploadPolicy() throws JUploadException {
|
---|
| 432 | return this.uploadPolicy;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | // ///////////////////////////////////////////////////////////////////////////////////////////////////////:
|
---|
| 436 | // //////////////// FUNCTIONS INTENDED TO BE CALLED BY JAVASCRIPT FUNCTIONS
|
---|
| 437 | // ////////////////////////////:
|
---|
| 438 | // ///////////////////////////////////////////////////////////////////////////////////////////////////////:
|
---|
| 439 |
|
---|
| 440 | /**
|
---|
| 441 | * This allow runtime modifications of properties, from javascript.
|
---|
| 442 | * Currently, this can only be used after full initialization. This method
|
---|
| 443 | * only calls the UploadPolicy.setProperty method. <BR>
|
---|
| 444 | * Ex: document.jupload.setProperty(prop, value);
|
---|
| 445 | *
|
---|
| 446 | * @param prop
|
---|
| 447 | * The property name that must be set.
|
---|
| 448 | * @param value
|
---|
| 449 | * The value of this property.
|
---|
| 450 | */
|
---|
| 451 | public void setProperty(String prop, String value) {
|
---|
| 452 | // FIXME setProperty should use jsHandler
|
---|
| 453 | class PropertySetter implements Runnable {
|
---|
| 454 | String prop;
|
---|
| 455 |
|
---|
| 456 | String value;
|
---|
| 457 |
|
---|
| 458 | PropertySetter(String prop, String value) {
|
---|
| 459 | this.prop = prop;
|
---|
| 460 | this.value = value;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | public void run() {
|
---|
| 464 | try {
|
---|
| 465 | // We'll wait up to 2s until the applet initialized (we need
|
---|
| 466 | // an
|
---|
| 467 | // upload policy).
|
---|
| 468 | // FIXME should be done in a separate thread (block the
|
---|
| 469 | // browser)
|
---|
| 470 | for (int i = 0; i < 20 && uploadPolicy == null; i += 1) {
|
---|
| 471 | this.wait(100);
|
---|
| 472 | }
|
---|
| 473 | if (uploadPolicy == null) {
|
---|
| 474 | System.out
|
---|
| 475 | .println("uploadPolicy is null. Impossible to set "
|
---|
| 476 | + prop + " to " + value);
|
---|
| 477 | } else {
|
---|
| 478 | // FIXME There should be a boolean: initialized, to
|
---|
| 479 | // indicate when property may be set.
|
---|
| 480 | uploadPolicy.setProperty(prop, value);
|
---|
| 481 | }
|
---|
| 482 | } catch (Exception e) {
|
---|
| 483 | uploadPolicy.displayErr(e);
|
---|
| 484 | }
|
---|
| 485 | }
|
---|
| 486 | }
|
---|
| 487 | try {
|
---|
| 488 | SwingUtilities.invokeLater(new PropertySetter(prop, value));
|
---|
| 489 | } catch (Exception e) {
|
---|
| 490 | if (this.uploadPolicy != null) {
|
---|
| 491 | this.uploadPolicy.displayErr(e);
|
---|
| 492 | } else {
|
---|
| 493 | System.out.println(e.getClass().getName() + ": "
|
---|
| 494 | + e.getMessage());
|
---|
| 495 | }
|
---|
| 496 | }
|
---|
| 497 | }
|
---|
| 498 |
|
---|
| 499 | /** {@inheritDoc} */
|
---|
| 500 | public String startUpload() {
|
---|
| 501 | return this.jsHandler.doCommand(JavascriptHandler.COMMAND_START_UPLOAD);
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | /**
|
---|
| 505 | * Call to {@link UploadPolicy#displayErr(Exception)}
|
---|
| 506 | *
|
---|
| 507 | * @param err
|
---|
| 508 | * The error text to be displayed.
|
---|
| 509 | */
|
---|
| 510 | public void displayErr(String err) {
|
---|
| 511 | this.uploadPolicy.displayErr(err);
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | /**
|
---|
| 515 | * Call to {@link UploadPolicy#displayInfo(String)}
|
---|
| 516 | *
|
---|
| 517 | * @param info
|
---|
| 518 | * The info text to display
|
---|
| 519 | */
|
---|
| 520 | public void displayInfo(String info) {
|
---|
| 521 | this.uploadPolicy.displayInfo(info);
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | /**
|
---|
| 525 | * Call to {@link UploadPolicy#displayWarn(String)}
|
---|
| 526 | *
|
---|
| 527 | * @param warn
|
---|
| 528 | * The error text to be displayed.
|
---|
| 529 | */
|
---|
| 530 | public void displayWarn(String warn) {
|
---|
| 531 | this.uploadPolicy.displayWarn(warn);
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | /**
|
---|
| 535 | * Call to {@link UploadPolicy#displayDebug(String, int)}
|
---|
| 536 | *
|
---|
| 537 | * @param debug
|
---|
| 538 | * The debug message.
|
---|
| 539 | * @param minDebugLevel
|
---|
| 540 | * The minimum level that debug level should have, to display
|
---|
| 541 | * this message. Values can go from 0 to 100.
|
---|
| 542 | */
|
---|
| 543 | public void displayDebug(String debug, int minDebugLevel) {
|
---|
| 544 | this.uploadPolicy.displayDebug(debug, minDebugLevel);
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | // /////////////////////////////////////////////////////////////////////////
|
---|
| 548 | // ////////////////////// Helper functions
|
---|
| 549 | // /////////////////////////////////////////////////////////////////////////
|
---|
| 550 |
|
---|
| 551 | /**
|
---|
| 552 | * Helper function, to get the Revision number, if available. The applet
|
---|
| 553 | * must be built from the build.xml ant file.
|
---|
| 554 | *
|
---|
| 555 | * @return The svn properties
|
---|
| 556 | */
|
---|
| 557 | public static Properties getSvnProperties() {
|
---|
| 558 | Properties properties = new Properties();
|
---|
| 559 | Boolean bPropertiesLoaded = false;
|
---|
| 560 |
|
---|
| 561 | // Let's try to load the properties file.
|
---|
| 562 | // The upload policy is not created yet: we can not use its display
|
---|
| 563 | // methods to trace what is happening here.
|
---|
| 564 | try {
|
---|
| 565 | InputStream isProperties = Class.forName(
|
---|
| 566 | "wjhk.jupload2.JUploadApplet").getResourceAsStream(
|
---|
| 567 | SVN_PROPERTIES_FILENAME);
|
---|
| 568 | properties.load(isProperties);
|
---|
| 569 | isProperties.close();
|
---|
| 570 | bPropertiesLoaded = true;
|
---|
| 571 | } catch (Exception e) {
|
---|
| 572 | // An error occurred when reading the file. The applet was
|
---|
| 573 | // probably not built with the build.xml ant file.
|
---|
| 574 | // We'll create a fake property list. See below.
|
---|
| 575 |
|
---|
| 576 | // We can not output to the uploadPolicy display method, as the
|
---|
| 577 | // upload policy is not created yet. We output to the system output.
|
---|
| 578 | // Consequence: if this doesn't work during build, you'll see an
|
---|
| 579 | // error during the build: the generated file name will contain the
|
---|
| 580 | // following error message.
|
---|
| 581 | System.out.println(e.getClass().getName()
|
---|
| 582 | + " in DefaultJUploadContext.getSvnProperties() ("
|
---|
| 583 | + e.getMessage() + ")");
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | // If we could not read the property file. The applet was probably not
|
---|
| 587 | // built with the build.xml ant file, we create a fake property list.
|
---|
| 588 | if (!bPropertiesLoaded) {
|
---|
| 589 | properties.setProperty("buildDate",
|
---|
| 590 | "Unknown build date (please use the build.xml ant script)");
|
---|
| 591 | properties
|
---|
| 592 | .setProperty("lastSrcDirModificationDate",
|
---|
| 593 | "Unknown last modification date (please use the build.xml ant script)");
|
---|
| 594 | properties.setProperty("revision",
|
---|
| 595 | "Unknown revision (please use the build.xml ant script)");
|
---|
| 596 | }
|
---|
| 597 | return properties;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | /** {@inheritDoc} */
|
---|
| 601 | public void registerUnload(Object object, String method) {
|
---|
| 602 | // We insert each item at the beginning, so that the callbacks are
|
---|
| 603 | // called in the reverse order of the order in which they were
|
---|
| 604 | // registered.
|
---|
| 605 | // For instance: the removal of the log file is the first one to be
|
---|
| 606 | // registered ... and must be the last one to be executed.
|
---|
| 607 | this.unloadCallbacks.add(0, new Callback(object, method));
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | /** {@inheritDoc} */
|
---|
| 611 | public synchronized void runUnload() {
|
---|
| 612 | // If an upload is runing on, we have to stop it.
|
---|
| 613 | FileUploadManagerThread fileUploadManagerThread = this.getUploadPanel()
|
---|
| 614 | .getFileUploadManagerThread();
|
---|
| 615 | if (fileUploadManagerThread != null) {
|
---|
| 616 | fileUploadManagerThread.stopUpload();
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | // Then we call all unload callback.
|
---|
| 620 | for (Callback callback : this.unloadCallbacks) {
|
---|
| 621 | try {
|
---|
| 622 | callback.invoke();
|
---|
| 623 | } catch (Exception e) {
|
---|
| 624 | System.out.println(e.getClass().getName()
|
---|
| 625 | + " while calling the callback: "
|
---|
| 626 | + callback.getObject().getClass().getName() + "."
|
---|
| 627 | + callback.getMethod());
|
---|
| 628 | e.printStackTrace();
|
---|
| 629 | }
|
---|
| 630 | }
|
---|
| 631 | this.unloadCallbacks.clear();
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | /**
|
---|
| 635 | * Displays the debug information for the current parameter.
|
---|
| 636 | */
|
---|
| 637 | void displayDebugParameterValue(String key, String value) {
|
---|
| 638 | if (this.uploadPolicy != null
|
---|
| 639 | && this.uploadPolicy.getDebugLevel() >= 80) {
|
---|
| 640 | this.uploadPolicy.displayDebug("Parameter '" + key
|
---|
| 641 | + "' loaded. Value: " + value, 80);
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | /** {@inheritDoc} */
|
---|
| 646 | public int parseInt(String value, int def) {
|
---|
| 647 | int ret = def;
|
---|
| 648 | // Then, parse it as an integer.
|
---|
| 649 | try {
|
---|
| 650 | ret = Integer.parseInt(value);
|
---|
| 651 | } catch (NumberFormatException e) {
|
---|
| 652 | ret = def;
|
---|
| 653 | if (this.uploadPolicy != null) {
|
---|
| 654 | this.uploadPolicy.displayWarn("Invalid int value: " + value
|
---|
| 655 | + ", using default value: " + def);
|
---|
| 656 | }
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 | return ret;
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | /** {@inheritDoc} */
|
---|
| 663 | public float parseFloat(String value, float def) {
|
---|
| 664 | float ret = def;
|
---|
| 665 | // Then, parse it as an integer.
|
---|
| 666 | try {
|
---|
| 667 | ret = Float.parseFloat(value);
|
---|
| 668 | } catch (NumberFormatException e) {
|
---|
| 669 | ret = def;
|
---|
| 670 | if (this.uploadPolicy != null) {
|
---|
| 671 | this.uploadPolicy.displayWarn("Invalid float value: " + value
|
---|
| 672 | + ", using default value: " + def);
|
---|
| 673 | }
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 | return ret;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | /** {@inheritDoc} */
|
---|
| 680 | public long parseLong(String value, long def) {
|
---|
| 681 | long ret = def;
|
---|
| 682 | // Then, parse it as an integer.
|
---|
| 683 | try {
|
---|
| 684 | ret = Long.parseLong(value);
|
---|
| 685 | } catch (NumberFormatException e) {
|
---|
| 686 | ret = def;
|
---|
| 687 | if (this.uploadPolicy != null) {
|
---|
| 688 | this.uploadPolicy.displayWarn("Invalid long value: " + value
|
---|
| 689 | + ", using default value: " + def);
|
---|
| 690 | }
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | return ret;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | /** {@inheritDoc} */
|
---|
| 697 | public boolean parseBoolean(String value, boolean def) {
|
---|
| 698 | // Then, parse it as a boolean.
|
---|
| 699 | if (value.toUpperCase().equals("FALSE")) {
|
---|
| 700 | return false;
|
---|
| 701 | } else if (value.toUpperCase().equals("TRUE")) {
|
---|
| 702 | return true;
|
---|
| 703 | } else {
|
---|
| 704 | if (this.uploadPolicy != null) {
|
---|
| 705 | this.uploadPolicy.displayWarn("Invalid boolean value: " + value
|
---|
| 706 | + ", using default value: " + def);
|
---|
| 707 | }
|
---|
| 708 | return def;
|
---|
| 709 | }
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 | /**
|
---|
| 713 | * @return The cursor that was active before the call to this method
|
---|
| 714 | * @see JUploadContext#setCursor(Cursor)
|
---|
| 715 | */
|
---|
| 716 | public Cursor setWaitCursor() {
|
---|
| 717 | return setCursor(new Cursor(Cursor.WAIT_CURSOR));
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | /**
|
---|
| 721 | * Just throws a UnsupportedOperationException exception.
|
---|
| 722 | *
|
---|
| 723 | * @param url
|
---|
| 724 | * @param success
|
---|
| 725 | */
|
---|
| 726 | public void displayURL(String url, boolean success) {
|
---|
| 727 | throw new UnsupportedOperationException(
|
---|
| 728 | "DefaultJUploadContext.setCursor()");
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | /**
|
---|
| 732 | * Just throws a UnsupportedOperationException exception.
|
---|
| 733 | *
|
---|
| 734 | * @return Not used
|
---|
| 735 | */
|
---|
| 736 | public JApplet getApplet() {
|
---|
| 737 | throw new UnsupportedOperationException(
|
---|
| 738 | "DefaultJUploadContext.setCursor()");
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | /** @see JUploadContext#getFrame() */
|
---|
| 742 | public Frame getFrame() {
|
---|
| 743 | return this.frame;
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 | /**
|
---|
| 747 | * Just throws a UnsupportedOperationException exception.
|
---|
| 748 | *
|
---|
| 749 | * @return Not used.
|
---|
| 750 | */
|
---|
| 751 | public Cursor getCursor() {
|
---|
| 752 | throw new UnsupportedOperationException(
|
---|
| 753 | "DefaultJUploadContext.setCursor()");
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | /**
|
---|
| 757 | * Just throws a UnsupportedOperationException exception.
|
---|
| 758 | *
|
---|
| 759 | * @param key
|
---|
| 760 | * @param def
|
---|
| 761 | * @return Not used
|
---|
| 762 | */
|
---|
| 763 | public String getParameter(String key, String def) {
|
---|
| 764 | throw new UnsupportedOperationException(
|
---|
| 765 | "DefaultJUploadContext.setCursor()");
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | /**
|
---|
| 769 | * Just throws a UnsupportedOperationException exception.
|
---|
| 770 | *
|
---|
| 771 | * @param key
|
---|
| 772 | * @param def
|
---|
| 773 | * @return Not used
|
---|
| 774 | */
|
---|
| 775 | public int getParameter(String key, int def) {
|
---|
| 776 | throw new UnsupportedOperationException(
|
---|
| 777 | "DefaultJUploadContext.setCursor()");
|
---|
| 778 | }
|
---|
| 779 |
|
---|
| 780 | /**
|
---|
| 781 | * Just throws a UnsupportedOperationException exception.
|
---|
| 782 | *
|
---|
| 783 | * @param key
|
---|
| 784 | * @param def
|
---|
| 785 | * @return Not used
|
---|
| 786 | */
|
---|
| 787 | public float getParameter(String key, float def) {
|
---|
| 788 | throw new UnsupportedOperationException(
|
---|
| 789 | "DefaultJUploadContext.setCursor()");
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | /**
|
---|
| 793 | * Just throws a UnsupportedOperationException exception.
|
---|
| 794 | *
|
---|
| 795 | * @param key
|
---|
| 796 | * @param def
|
---|
| 797 | * @return Not used
|
---|
| 798 | */
|
---|
| 799 | public long getParameter(String key, long def) {
|
---|
| 800 | throw new UnsupportedOperationException(
|
---|
| 801 | "DefaultJUploadContext.setCursor()");
|
---|
| 802 | }
|
---|
| 803 |
|
---|
| 804 | /**
|
---|
| 805 | * Just throws a UnsupportedOperationException exception.
|
---|
| 806 | *
|
---|
| 807 | * @param key
|
---|
| 808 | * @param def
|
---|
| 809 | * @return Not used
|
---|
| 810 | */
|
---|
| 811 | public boolean getParameter(String key, boolean def) {
|
---|
| 812 | throw new UnsupportedOperationException(
|
---|
| 813 | "DefaultJUploadContext.setCursor()");
|
---|
| 814 | }
|
---|
| 815 |
|
---|
| 816 | /**
|
---|
| 817 | * Just throws a UnsupportedOperationException exception.
|
---|
| 818 | *
|
---|
| 819 | * @param url
|
---|
| 820 | * @return Not used
|
---|
| 821 | * @throws JUploadException
|
---|
| 822 | */
|
---|
| 823 | public String normalizeURL(String url) throws JUploadException {
|
---|
| 824 | throw new UnsupportedOperationException(
|
---|
| 825 | "DefaultJUploadContext.setCursor()");
|
---|
| 826 | }
|
---|
| 827 |
|
---|
| 828 | /**
|
---|
| 829 | * Just throws a UnsupportedOperationException exception.
|
---|
| 830 | *
|
---|
| 831 | * @param headers
|
---|
| 832 | */
|
---|
| 833 | public void readCookieFromNavigator(Vector<String> headers) {
|
---|
| 834 | throw new UnsupportedOperationException(
|
---|
| 835 | "DefaultJUploadContext.readCookieFromNavigator()");
|
---|
| 836 | }
|
---|
| 837 |
|
---|
| 838 | /**
|
---|
| 839 | * Just throws a UnsupportedOperationException exception.
|
---|
| 840 | *
|
---|
| 841 | * @param headers
|
---|
| 842 | */
|
---|
| 843 | public void readUserAgentFromNavigator(Vector<String> headers) {
|
---|
| 844 | throw new UnsupportedOperationException(
|
---|
| 845 | "DefaultJUploadContext.readUserAgentFromNavigator()");
|
---|
| 846 | }
|
---|
| 847 |
|
---|
| 848 | /**
|
---|
| 849 | * Just throws a UnsupportedOperationException exception.
|
---|
| 850 | *
|
---|
| 851 | * @param cursor
|
---|
| 852 | * @return Not used
|
---|
| 853 | */
|
---|
| 854 | public Cursor setCursor(Cursor cursor) {
|
---|
| 855 | throw new UnsupportedOperationException(
|
---|
| 856 | "DefaultJUploadContext.setCursor()");
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | /**
|
---|
| 860 | * Just throws a UnsupportedOperationException exception.
|
---|
| 861 | *
|
---|
| 862 | * @param status
|
---|
| 863 | */
|
---|
| 864 | public void showStatus(String status) {
|
---|
| 865 | throw new UnsupportedOperationException(
|
---|
| 866 | "DefaultJUploadContext.showStatus()");
|
---|
| 867 | }
|
---|
| 868 | }
|
---|