source: 3thparty/jupload/src/main/java/wjhk/jupload2/policies/UploadPolicyFactory.java @ 3951

Revision 3951, 7.7 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1709 - Adicao de codigo fonte java do componente jupload

Line 
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 11:49:12 -0300 (Qui, 02 Jul 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
21package wjhk.jupload2.policies;
22
23import java.lang.reflect.Constructor;
24import java.lang.reflect.InvocationTargetException;
25
26import 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 */
45public 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        UploadPolicy uploadPolicy = theAppletContext.getUploadPolicy();
60
61        if (uploadPolicy == null) {
62            // Let's create the update policy.
63            String uploadPolicyStr = theAppletContext.getParameter(
64                    UploadPolicy.PROP_UPLOAD_POLICY,
65                    UploadPolicy.DEFAULT_UPLOAD_POLICY);
66            int debugLevel = theAppletContext.getParameter(
67                    UploadPolicy.PROP_DEBUG_LEVEL,
68                    UploadPolicy.DEFAULT_DEBUG_LEVEL);
69
70            String action = null;
71            boolean usingDefaultUploadPolicy = false;
72            try {
73                logDebug("Trying to load the given uploadPolicy: "
74                        + uploadPolicyStr, debugLevel);
75
76                action = uploadPolicyStr;
77                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                if (!uploadPolicyStr.contains(".")) {
82                    try {
83                        uploadPolicyClass = Class
84                                .forName("wjhk.jupload2.policies."
85                                        + uploadPolicyStr);
86                        logDebug("wjhk.jupload2.policies." + uploadPolicyStr
87                                + " class found.", debugLevel);
88                    } catch (ClassNotFoundException e1) {
89                        logDebug(e1.getClass().getName()
90                                + " when looking for [wjhk.jupload2.policies.]"
91                                + uploadPolicyStr, debugLevel);
92                        uploadPolicyClass = null;
93                    }
94                }
95                if (null == uploadPolicyClass) {
96                    // Let's try without the prefix
97                    try {
98                        uploadPolicyClass = Class.forName(uploadPolicyStr);
99                        logDebug(uploadPolicyStr + " class found.", debugLevel);
100                    } catch (ClassNotFoundException e2) {
101                        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                        usingDefaultUploadPolicy = true;
107                        uploadPolicyClass = Class
108                                .forName("wjhk.jupload2.policies.DefaultUploadPolicy");
109                        logDebug(
110                                "Using default upload policy: wjhk.jupload2.policies.DefaultUploadPolicy",
111                                debugLevel);
112                    }
113                }
114                action = "constructorParameters";
115                Class<?>[] constructorParameters = {
116                    Class.forName("wjhk.jupload2.context.JUploadContext")
117                };
118                Constructor<?> constructor = uploadPolicyClass
119                        .getConstructor(constructorParameters);
120                Object[] params = {
121                    theAppletContext
122                };
123                action = "newInstance";
124                uploadPolicy = (UploadPolicy) constructor.newInstance(params);
125            } catch (Exception e) {
126                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                    Throwable t = ((InvocationTargetException) e)
131                            .getTargetException();
132                    System.out.println("-ERROR- " + e.getClass().getName()
133                            + " (message: " + t.getMessage() + ")");
134                    t.printStackTrace();
135                }
136                System.out.println("-ERROR- " + e.getClass().getName() + " in "
137                        + action + "(error message: " + e.getMessage() + ")");
138                throw e;
139            }
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            uploadPolicy.displayDebug("uploadPolicy parameter = "
147                    + uploadPolicyStr, 1);
148            if (usingDefaultUploadPolicy) {
149                uploadPolicy.displayWarn("Unable to create the '"
150                        + uploadPolicyStr
151                        + "'. Using the DefaultUploadPolicy instead.");
152            } else {
153                uploadPolicy.displayDebug("uploadPolicy = "
154                        + uploadPolicy.getClass().getName(), 20);
155            }
156
157            // Then, we display the applet parameter list.
158            uploadPolicy.displayParameterStatus();
159        }
160
161        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        if (currentDebugLevel > 0) {
172            System.out.println("[DEBUG] " + msg);
173        }
174    }
175}
Note: See TracBrowser for help on using the repository browser.