1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class UploadPolicyFactory {
46
47
48
49
50
51
52
53
54
55
56
57 public static UploadPolicy getUploadPolicy(JUploadContext theAppletContext)
58 throws Exception {
59 UploadPolicy uploadPolicy = theAppletContext.getUploadPolicy();
60
61 if (uploadPolicy == null) {
62
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
79
80
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
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
105
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
128
129
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
142
143
144
145
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
158 uploadPolicy.displayParameterStatus();
159 }
160
161 return uploadPolicy;
162 }
163
164
165
166
167
168
169
170 private static void logDebug(String msg, int currentDebugLevel) {
171 if (currentDebugLevel > 0) {
172 System.out.println("[DEBUG] " + msg);
173 }
174 }
175 }