📄 uploadpolicyfactory.java
字号:
//
// $Id: UploadPolicyFactory.java 629 2009-02-23 16:25:14Z etienne_sf $
//
// jupload - A file upload applet.
// Copyright 2007 The JUpload Team
//
// Created: 2006-05-06
// Creator: etienne_sf
// Last modified: $Date: 2009-02-23 17:25:14 +0100 (lun., 23 févr. 2009) $
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version. This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details. You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation, Inc.,
// 675 Mass Ave, Cambridge, MA 02139, USA.
package wjhk.jupload2.policies;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import wjhk.jupload2.JUploadApplet;
/**
* This class is used to control creation of the uploadPolicy instance,
* according to applet parameters (or System properties). <BR>
* <BR>
* The used parameters are:
* <UL>
* <LI>postURL: The URL where files are to be uploaded. This parameter is
* mandatory if called from a servlet.
* <LI>uploadPolicy: the class name to be used as a policy. Currently available
* : not defined (then use DefaultUploadPolicy),
* {@link wjhk.jupload2.policies.DefaultUploadPolicy},
* {@link wjhk.jupload2.policies.CoppermineUploadPolicy}
* </UL>
*
* @author etienne_sf
* @version $Revision: 629 $
*/
public class UploadPolicyFactory {
/**
* Returns an upload Policy for the given applet and URL. All other
* parameters for the uploadPolicy are take from avaiable applet parameters
* (or from system properties, if it is not run as an applet).
*
* @param theApplet if not null : use this Applet Parameters. If null, use
* System properties.
* @return The newly created UploadPolicy.
* @throws Exception
*/
public static UploadPolicy getUploadPolicy(JUploadApplet theApplet)
throws Exception {
UploadPolicy uploadPolicy = theApplet.getUploadPolicy();
if (uploadPolicy == null) {
// Let's create the update policy.
String uploadPolicyStr = getParameter(theApplet,
UploadPolicy.PROP_UPLOAD_POLICY,
UploadPolicy.DEFAULT_UPLOAD_POLICY, null);
int debugLevel = getParameter(theApplet,
UploadPolicy.PROP_DEBUG_LEVEL,
UploadPolicy.DEFAULT_DEBUG_LEVEL, null);
String action = null;
boolean usingDefaultUploadPolicy = false;
try {
logDebug("Trying to load the given uploadPolicy: "
+ uploadPolicyStr, debugLevel);
action = uploadPolicyStr;
Class<?> uploadPolicyClass = null;
// Our default is "DefaultUploadPolicy", (without prefix)
// so we try the prefixed variant first. But only, if the
// user had specified an unqualified class name.
if (!uploadPolicyStr.contains(".")) {
try {
uploadPolicyClass = Class
.forName("wjhk.jupload2.policies."
+ uploadPolicyStr);
logDebug("wjhk.jupload2.policies." + uploadPolicyStr
+ " successfully loaded.", debugLevel);
} catch (ClassNotFoundException e1) {
logDebug(e1.getClass().getName()
+ " when looking for [wjhk.jupload2.policies.]"
+ uploadPolicyStr, debugLevel);
uploadPolicyClass = null;
}
}
if (null == uploadPolicyClass) {
// Let's try without the prefix
try {
uploadPolicyClass = Class.forName(uploadPolicyStr);
logDebug(uploadPolicyStr + " successfully loaded.",
debugLevel);
} catch (ClassNotFoundException e2) {
logDebug(e2.getClass().getName()
+ " when looking for the given uploadPolicy ("
+ uploadPolicyStr + ")", debugLevel);
// Too bad, we don't know how to create this class.
// Fall back to builtin default.
usingDefaultUploadPolicy = true;
uploadPolicyClass = Class
.forName("wjhk.jupload2.policies.DefaultUploadPolicy");
logDebug(
"Using default upload policy: wjhk.jupload2.policies.DefaultUploadPolicy",
debugLevel);
}
}
action = "constructorParameters";
Class<?>[] constructorParameters = {
Class.forName("wjhk.jupload2.JUploadApplet")
};
Constructor<?> constructor = uploadPolicyClass
.getConstructor(constructorParameters);
Object[] params = {
theApplet
};
action = "newInstance";
uploadPolicy = (UploadPolicy) constructor.newInstance(params);
} catch (Exception e) {
if (e instanceof InvocationTargetException) {
// If the policy's constructor has thrown an exception,
// Get that "real" exception and print its details and
// stacktrace
Throwable t = ((InvocationTargetException) e)
.getTargetException();
System.out.println("-ERROR- " + t.getMessage());
t.printStackTrace();
}
System.out.println("-ERROR- " + e.getClass().getName() + " in "
+ action + "(error message: " + e.getMessage() + ")");
throw e;
}
// The current values are displayed here, after the full
// initialization of all classes.
// It could also be displayed in the DefaultUploadPolicy (for
// instance), but then, the
// display wouldn't show the modifications done by superclasses.
uploadPolicy.displayDebug("uploadPolicy parameter = "
+ uploadPolicyStr, 1);
if (usingDefaultUploadPolicy) {
uploadPolicy.displayWarn("Unable to create the '"
+ uploadPolicyStr
+ "'. Using the DefaultUploadPolicy instead.");
} else {
uploadPolicy.displayDebug("uploadPolicy = "
+ uploadPolicy.getClass().getName(), 20);
}
// Then, we display the applet parameter list.
uploadPolicy.displayParameterStatus();
}
return uploadPolicy;
}
/**
* Get a String parameter value from applet properties or System properties.
*
* @param theApplet The applet which provides the parameter. If null, the
* parameter is retrieved from the system property.
* @param key The name of the parameter to fetch.
* @param def A default value which is used, when the specified parameter is
* not set.
* @param uploadPolicy Unused
* @return The value of the applet parameter (resp. system property). If the
* parameter was not specified or no such system property exists,
* returns the given default value.
*/
static public String getParameter(JUploadApplet theApplet, String key,
String def, UploadPolicy uploadPolicy) {
String paramStr;
if (theApplet == null) {
paramStr = (System.getProperty(key) != null ? System
.getProperty(key) : def);
} else {
paramStr = (theApplet.getParameter(key) != null ? theApplet
.getParameter(key) : def);
}
displayDebugParameterValue(uploadPolicy, key, paramStr);
return paramStr;
}
/**
* Get a String parameter value from applet properties or System properties.
*
* @param theApplet The current applet
* @param key The parameter name
* @param def The default value
* @param uploadPolicy The current upload policy
*
* @return the parameter value, or the default, if the system is not set.
*/
static public int getParameter(JUploadApplet theApplet, String key,
int def, UploadPolicy uploadPolicy) {
String paramStr;
String paramDef = Integer.toString(def);
// First, read the parameter as a String
if (theApplet == null) {
paramStr = System.getProperty(key) != null ? System
.getProperty(key) : paramDef;
} else {
paramStr = theApplet.getParameter(key) != null ? theApplet
.getParameter(key) : paramDef;
}
displayDebugParameterValue(uploadPolicy, key, paramStr);
return parseInt(paramStr, def, uploadPolicy);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -