| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
package org.webmacro.util; |
| 24 | |
|
| 25 | |
import java.util.Arrays; |
| 26 | |
import java.util.Collections; |
| 27 | |
import java.util.List; |
| 28 | |
|
| 29 | |
import org.slf4j.Logger; |
| 30 | |
import org.slf4j.LoggerFactory; |
| 31 | |
import org.webmacro.Broker; |
| 32 | |
import org.webmacro.WebMacroException; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
final public class Instantiator |
| 49 | |
{ |
| 50 | 0 | static Logger _log = LoggerFactory.getLogger(Instantiator.class); |
| 51 | |
|
| 52 | |
private static final String IMPLIED_PACKAGES = "ImpliedPackages"; |
| 53 | |
|
| 54 | |
private static final String ALLOWED_PACKAGES = "AllowedPackages"; |
| 55 | |
|
| 56 | |
private static final String DEPRECATED_IMPLIED_PACKAGES = "BeanDirective.ImpliedPackages"; |
| 57 | |
|
| 58 | |
private static final String DEPRECATED_ALLOWED_PACKAGES = "BeanDirective.AllowedPackages"; |
| 59 | |
|
| 60 | |
private static final String INSTANTIATOR_KEY = "org.webmacro.util.Instantiator"; |
| 61 | |
|
| 62 | |
private List _impliedPackages; |
| 63 | |
|
| 64 | |
private List _allowedPackages; |
| 65 | |
|
| 66 | |
private Broker _broker; |
| 67 | |
|
| 68 | |
private Instantiator(Broker b) |
| 69 | 0 | { |
| 70 | 0 | _broker = b; |
| 71 | 0 | String s = b.getSetting(IMPLIED_PACKAGES); |
| 72 | 0 | if (s == null) |
| 73 | |
{ |
| 74 | 0 | s = b.getSetting(DEPRECATED_IMPLIED_PACKAGES); |
| 75 | 0 | if (s != null) |
| 76 | |
{ |
| 77 | 0 | _log.warn( |
| 78 | |
"The configuration parameter \"" |
| 79 | |
+ DEPRECATED_IMPLIED_PACKAGES |
| 80 | |
+ "\" has been deprecated! Use \"" |
| 81 | |
+ IMPLIED_PACKAGES + "\" instead."); |
| 82 | |
} |
| 83 | |
} |
| 84 | 0 | if (s == null) |
| 85 | |
{ |
| 86 | 0 | _impliedPackages = Collections.EMPTY_LIST; |
| 87 | |
} |
| 88 | |
else |
| 89 | |
{ |
| 90 | 0 | _impliedPackages = Arrays.asList(org.webmacro.servlet.TextTool.split( |
| 91 | |
s, ",")); |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | 0 | s = b.getSetting(ALLOWED_PACKAGES); |
| 96 | 0 | if (s == null) |
| 97 | |
{ |
| 98 | 0 | s = b.getSetting(DEPRECATED_ALLOWED_PACKAGES); |
| 99 | 0 | if (s != null) |
| 100 | |
{ |
| 101 | 0 | _log.warn( |
| 102 | |
"The configuration parameter \"" |
| 103 | |
+ DEPRECATED_ALLOWED_PACKAGES |
| 104 | |
+ "\" has been deprecated! Use \"" |
| 105 | |
+ ALLOWED_PACKAGES + "\" instead."); |
| 106 | |
} |
| 107 | |
} |
| 108 | 0 | if (s == null) |
| 109 | |
{ |
| 110 | 0 | _allowedPackages = Collections.EMPTY_LIST; |
| 111 | |
} |
| 112 | |
else |
| 113 | |
{ |
| 114 | 0 | _allowedPackages = Arrays.asList(org.webmacro.servlet.TextTool.split( |
| 115 | |
s, ",")); |
| 116 | |
} |
| 117 | 0 | } |
| 118 | |
|
| 119 | |
public List getImpliedPackages() |
| 120 | |
{ |
| 121 | 0 | return _impliedPackages; |
| 122 | |
} |
| 123 | |
|
| 124 | |
public List getAllowedPackages() |
| 125 | |
{ |
| 126 | 0 | return _allowedPackages; |
| 127 | |
} |
| 128 | |
|
| 129 | |
public Class classForName(String className) throws WebMacroException |
| 130 | |
{ |
| 131 | 0 | Class c = null; |
| 132 | 0 | if (className.indexOf('.') >= 0) |
| 133 | |
{ |
| 134 | |
try |
| 135 | |
{ |
| 136 | 0 | c = _broker.classForName(className); |
| 137 | |
} |
| 138 | 0 | catch (ClassNotFoundException e) |
| 139 | |
{ |
| 140 | 0 | throw new WebMacroException("Unable to load class " + className, e); |
| 141 | 0 | } |
| 142 | |
} |
| 143 | |
else |
| 144 | |
{ |
| 145 | 0 | ClassNotFoundException exception = null; |
| 146 | |
|
| 147 | 0 | for (int i = 0; i < _impliedPackages.size(); i++) |
| 148 | |
{ |
| 149 | 0 | String s = (String) _impliedPackages.get(i); |
| 150 | |
try |
| 151 | |
{ |
| 152 | 0 | c = _broker.classForName(s + "." + className); |
| 153 | 0 | break; |
| 154 | |
} |
| 155 | 0 | catch (ClassNotFoundException e) |
| 156 | |
{ |
| 157 | 0 | exception = e; |
| 158 | |
} |
| 159 | |
} |
| 160 | 0 | if (c == null) { |
| 161 | 0 | if (exception == null) |
| 162 | 0 | throw new WebMacroException("Unable to load class " + className + |
| 163 | |
", property " + IMPLIED_PACKAGES + " contains " + |
| 164 | |
_impliedPackages.size() + " items"); |
| 165 | |
else |
| 166 | 0 | throw new WebMacroException("Unable to load class " + className, exception); |
| 167 | |
} |
| 168 | |
} |
| 169 | |
|
| 170 | 0 | if (!_allowedPackages.isEmpty()) |
| 171 | |
{ |
| 172 | |
|
| 173 | 0 | String pkg = c.getPackage().getName(); |
| 174 | 0 | if (!_allowedPackages.contains(pkg)) |
| 175 | |
{ |
| 176 | 0 | throw new WebMacroException( |
| 177 | |
"You are not permitted to load classes from this package (" |
| 178 | |
+ pkg + "). Check the \"" + ALLOWED_PACKAGES |
| 179 | |
+ "\" parameter in the WebMacro configuration."); |
| 180 | |
} |
| 181 | |
} |
| 182 | |
|
| 183 | |
|
| 184 | 0 | return c; |
| 185 | |
} |
| 186 | |
|
| 187 | |
public Object instantiate(Class c, Object[] args) throws Exception |
| 188 | |
{ |
| 189 | 0 | Object o = null; |
| 190 | 0 | if (args == null) |
| 191 | |
{ |
| 192 | 0 | o = c.newInstance(); |
| 193 | |
} |
| 194 | |
else |
| 195 | |
{ |
| 196 | 0 | java.lang.reflect.Constructor[] cons = c.getConstructors(); |
| 197 | 0 | for (int i = 0; i < cons.length; i++) |
| 198 | |
{ |
| 199 | 0 | if (cons[i].getParameterTypes().length == args.length) |
| 200 | |
{ |
| 201 | |
|
| 202 | |
try |
| 203 | |
{ |
| 204 | 0 | o = cons[i].newInstance(args); |
| 205 | 0 | break; |
| 206 | |
} |
| 207 | 0 | catch (Exception e) |
| 208 | |
{ |
| 209 | |
|
| 210 | |
} |
| 211 | |
} |
| 212 | |
} |
| 213 | 0 | if (o == null) |
| 214 | |
{ |
| 215 | 0 | throw new InstantiationException( |
| 216 | |
"Unable to construct object of type " + c.getName() |
| 217 | |
+ " using the supplied arguments: " |
| 218 | |
+ java.util.Arrays.asList(args).toString()); |
| 219 | |
} |
| 220 | |
} |
| 221 | 0 | return o; |
| 222 | |
} |
| 223 | |
|
| 224 | |
synchronized public static Instantiator getInstance(Broker b) |
| 225 | |
{ |
| 226 | 0 | Instantiator instantiator = (Instantiator) b |
| 227 | |
.getBrokerLocal(INSTANTIATOR_KEY); |
| 228 | 0 | if (instantiator == null) |
| 229 | |
{ |
| 230 | 0 | instantiator = new Instantiator(b); |
| 231 | 0 | b.setBrokerLocal(INSTANTIATOR_KEY, instantiator); |
| 232 | |
} |
| 233 | 0 | return instantiator; |
| 234 | |
} |
| 235 | |
} |
| 236 | |
|