Coverage Report - wjhk.jupload2.policies.UploadPolicyFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
UploadPolicyFactory
5 %
3/57
8 %
1/12
6
 
 1  
 //
 2  
 // $Id: UploadPolicyFactory.java 822 2009-07-02 14:49:12Z etienne_sf $
 3  
 //
 4  
 // jupload - A file upload applet.
 5  
 // Copyright 2007 The JUpload Team
 6  
 //
 7  
 // Created: 2006-05-06
 8  
 // Creator: etienne_sf
 9  
 // Last modified: $Date: 2009-07-02 16:49:12 +0200 (jeu., 02 juil. 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.policies;
 22  
 
 23  
 import java.lang.reflect.Constructor;
 24  
 import java.lang.reflect.InvocationTargetException;
 25  
 
 26  
 import wjhk.jupload2.context.JUploadContext;
 27  
 
 28  
 /**
 29  
  * This class is used to control creation of the uploadPolicy instance,
 30  
  * according to applet parameters (or System properties). <BR>
 31  
  * <BR>
 32  
  * The used parameters are:
 33  
  * <UL>
 34  
  * <LI>postURL: The URL where files are to be uploaded. This parameter is
 35  
  * mandatory if called from a servlet.
 36  
  * <LI>uploadPolicy: the class name to be used as a policy. Currently available
 37  
  * : not defined (then use DefaultUploadPolicy),
 38  
  * {@link wjhk.jupload2.policies.DefaultUploadPolicy},
 39  
  * {@link wjhk.jupload2.policies.CoppermineUploadPolicy}
 40  
  * </UL>
 41  
  * 
 42  
  * @author etienne_sf
 43  
  * @version $Revision: 822 $
 44  
  */
 45  0
 public class UploadPolicyFactory {
 46  
 
 47  
     /**
 48  
      * Returns an upload Policy for the given applet and URL. All other
 49  
      * parameters for the uploadPolicy are take from avaiable applet parameters
 50  
      * (or from system properties, if it is not run as an applet).
 51  
      * 
 52  
      * @param theAppletContext if not null : use this Applet Parameters. If
 53  
      *            null, use System properties.
 54  
      * @return The newly created UploadPolicy.
 55  
      * @throws Exception
 56  
      */
 57  
     public static UploadPolicy getUploadPolicy(JUploadContext theAppletContext)
 58  
             throws Exception {
 59  33
         UploadPolicy uploadPolicy = theAppletContext.getUploadPolicy();
 60  
 
 61  33
         if (uploadPolicy == null) {
 62  
             // Let's create the update policy.
 63  0
             String uploadPolicyStr = theAppletContext.getParameter(
 64  
                     UploadPolicy.PROP_UPLOAD_POLICY,
 65  
                     UploadPolicy.DEFAULT_UPLOAD_POLICY);
 66  0
             int debugLevel = theAppletContext.getParameter(
 67  
                     UploadPolicy.PROP_DEBUG_LEVEL,
 68  
                     UploadPolicy.DEFAULT_DEBUG_LEVEL);
 69  
 
 70  0
             String action = null;
 71  0
             boolean usingDefaultUploadPolicy = false;
 72  
             try {
 73  0
                 logDebug("Trying to load the given uploadPolicy: "
 74  
                         + uploadPolicyStr, debugLevel);
 75  
 
 76  0
                 action = uploadPolicyStr;
 77  0
                 Class<?> uploadPolicyClass = null;
 78  
                 // Our default is "DefaultUploadPolicy", (without prefix)
 79  
                 // so we try the prefixed variant first. But only, if the
 80  
                 // user had specified an unqualified class name.
 81  0
                 if (!uploadPolicyStr.contains(".")) {
 82  
                     try {
 83  0
                         uploadPolicyClass = Class
 84  0
                                 .forName("wjhk.jupload2.policies."
 85  
                                         + uploadPolicyStr);
 86  0
                         logDebug("wjhk.jupload2.policies." + uploadPolicyStr
 87  
                                 + " class found.", debugLevel);
 88  0
                     } catch (ClassNotFoundException e1) {
 89  0
                         logDebug(e1.getClass().getName()
 90  
                                 + " when looking for [wjhk.jupload2.policies.]"
 91  
                                 + uploadPolicyStr, debugLevel);
 92  0
                         uploadPolicyClass = null;
 93  0
                     }
 94  
                 }
 95  0
                 if (null == uploadPolicyClass) {
 96  
                     // Let's try without the prefix
 97  
                     try {
 98  0
                         uploadPolicyClass = Class.forName(uploadPolicyStr);
 99  0
                         logDebug(uploadPolicyStr + " class found.", debugLevel);
 100  0
                     } catch (ClassNotFoundException e2) {
 101  0
                         logDebug(e2.getClass().getName()
 102  
                                 + " when looking for the given uploadPolicy ("
 103  
                                 + uploadPolicyStr + ")", debugLevel);
 104  
                         // Too bad, we don't know how to create this class.
 105  
                         // Fall back to built-in default.
 106  0
                         usingDefaultUploadPolicy = true;
 107  0
                         uploadPolicyClass = Class
 108  0
                                 .forName("wjhk.jupload2.policies.DefaultUploadPolicy");
 109  0
                         logDebug(
 110  
                                 "Using default upload policy: wjhk.jupload2.policies.DefaultUploadPolicy",
 111  
                                 debugLevel);
 112  0
                     }
 113  
                 }
 114  0
                 action = "constructorParameters";
 115  0
                 Class<?>[] constructorParameters = {
 116  0
                     Class.forName("wjhk.jupload2.context.JUploadContext")
 117  
                 };
 118  0
                 Constructor<?> constructor = uploadPolicyClass
 119  0
                         .getConstructor(constructorParameters);
 120  0
                 Object[] params = {
 121  
                     theAppletContext
 122  
                 };
 123  0
                 action = "newInstance";
 124  0
                 uploadPolicy = (UploadPolicy) constructor.newInstance(params);
 125  0
             } catch (Exception e) {
 126  0
                 if (e instanceof InvocationTargetException) {
 127  
                     // If the policy's constructor has thrown an exception,
 128  
                     // Get that "real" exception and print its details and
 129  
                     // stack trace
 130  0
                     Throwable t = ((InvocationTargetException) e)
 131  0
                             .getTargetException();
 132  0
                     System.out.println("-ERROR- " + e.getClass().getName()
 133  0
                             + " (message: " + t.getMessage() + ")");
 134  0
                     t.printStackTrace();
 135  
                 }
 136  0
                 System.out.println("-ERROR- " + e.getClass().getName() + " in "
 137  0
                         + action + "(error message: " + e.getMessage() + ")");
 138  0
                 throw e;
 139  0
             }
 140  
 
 141  
             // The current values are displayed here, after the full
 142  
             // initialization of all classes.
 143  
             // It could also be displayed in the DefaultUploadPolicy (for
 144  
             // instance), but then, the
 145  
             // display wouldn't show the modifications done by superclasses.
 146  0
             uploadPolicy.displayDebug("uploadPolicy parameter = "
 147  
                     + uploadPolicyStr, 1);
 148  0
             if (usingDefaultUploadPolicy) {
 149  0
                 uploadPolicy.displayWarn("Unable to create the '"
 150  
                         + uploadPolicyStr
 151  
                         + "'. Using the DefaultUploadPolicy instead.");
 152  
             } else {
 153  0
                 uploadPolicy.displayDebug("uploadPolicy = "
 154  0
                         + uploadPolicy.getClass().getName(), 20);
 155  
             }
 156  
 
 157  
             // Then, we display the applet parameter list.
 158  0
             uploadPolicy.displayParameterStatus();
 159  
         }
 160  
 
 161  33
         return uploadPolicy;
 162  
     }
 163  
 
 164  
     /**
 165  
      * Help to log debug information to the user. Use of log4j, one day in the
 166  
      * future, would help.
 167  
      * 
 168  
      * @param currentDebugLevel
 169  
      */
 170  
     private static void logDebug(String msg, int currentDebugLevel) {
 171  0
         if (currentDebugLevel > 0) {
 172  0
             System.out.println("[DEBUG] " + msg);
 173  
         }
 174  0
     }
 175  
 }