| 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.servlet; |
| 24 | |
|
| 25 | |
import org.slf4j.Logger; |
| 26 | |
import org.slf4j.LoggerFactory; |
| 27 | |
|
| 28 | |
import org.webmacro.Broker; |
| 29 | |
import org.webmacro.InitException; |
| 30 | |
|
| 31 | |
import javax.servlet.Servlet; |
| 32 | |
import javax.servlet.ServletContext; |
| 33 | |
import java.io.InputStream; |
| 34 | |
import java.net.URL; |
| 35 | |
import java.util.Properties; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public class Servlet20Broker extends ServletBroker |
| 53 | |
{ |
| 54 | |
|
| 55 | 2 | static Logger _log = LoggerFactory.getLogger(Servlet20Broker.class); |
| 56 | |
|
| 57 | |
protected ClassLoader _servletClassLoader; |
| 58 | |
|
| 59 | |
protected Servlet20Broker (ServletContext sc, |
| 60 | |
ClassLoader cl, |
| 61 | |
Properties additionalProperties) throws InitException |
| 62 | |
{ |
| 63 | 2 | super(sc); |
| 64 | 2 | _servletClassLoader = cl; |
| 65 | |
|
| 66 | 2 | String propertySource = WEBMACRO_DEFAULTS + ", " + WEBMACRO_PROPERTIES; |
| 67 | 2 | loadDefaultSettings(); |
| 68 | 2 | loadSettings(WEBMACRO_PROPERTIES, true); |
| 69 | 2 | if (additionalProperties != null && additionalProperties.keySet().size() > 0) |
| 70 | |
{ |
| 71 | 0 | propertySource += ", (additional Properties)"; |
| 72 | 0 | loadSettings(additionalProperties); |
| 73 | |
} |
| 74 | 2 | propertySource += ", (System Properties)"; |
| 75 | 2 | loadSystemSettings(); |
| 76 | |
|
| 77 | 2 | _log.info("Loaded settings from " + propertySource); |
| 78 | 2 | init(); |
| 79 | 2 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public static Broker getBroker (Servlet s, Properties additionalProperties) throws InitException |
| 89 | |
{ |
| 90 | 10 | ServletContext sc = s.getServletConfig().getServletContext(); |
| 91 | 10 | ClassLoader cl = s.getClass().getClassLoader(); |
| 92 | 10 | return _getBroker(sc, cl, additionalProperties, true, |
| 93 | |
s.getClass().getName()); |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public static Broker getBroker (ServletContext sc, ClassLoader cl, |
| 106 | |
Properties additionalProperties) throws InitException |
| 107 | |
{ |
| 108 | 0 | return _getBroker(sc, cl, additionalProperties, false, sc.toString()); |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
protected static Broker _getBroker (ServletContext sc, |
| 129 | |
ClassLoader cl, Properties additionalProperties, boolean fromServlet, |
| 130 | |
String servletOrContextName) throws InitException |
| 131 | |
{ |
| 132 | |
try |
| 133 | |
{ |
| 134 | 10 | Object key = cl; |
| 135 | 10 | if (additionalProperties != null && additionalProperties.keySet().size() > 0) |
| 136 | 0 | key = new PropertiesPair(cl, additionalProperties); |
| 137 | |
|
| 138 | 10 | Broker b = findBroker(key); |
| 139 | 10 | if (b == null) |
| 140 | |
{ |
| 141 | 2 | b = new Servlet20Broker(sc, cl, additionalProperties); |
| 142 | 2 | register(key, b); |
| 143 | |
} |
| 144 | |
else |
| 145 | 8 | _log.info( |
| 146 | |
(fromServlet ? "Servlet " : "ServletContext ") |
| 147 | |
+ servletOrContextName |
| 148 | |
+ " joining Broker" + " " + b.getName()); |
| 149 | 10 | return b; |
| 150 | |
} |
| 151 | 0 | catch (InitException e) |
| 152 | |
{ |
| 153 | 0 | _log.error("Failed to initialized WebMacro from "+ |
| 154 | |
(fromServlet ? "Servlet " : "ServletContext ") |
| 155 | |
+ servletOrContextName); |
| 156 | 0 | throw e; |
| 157 | |
} |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
public URL getResource (String name) |
| 162 | |
{ |
| 163 | 10 | URL u = _servletClassLoader.getResource(name); |
| 164 | 10 | if (u == null) |
| 165 | 4 | u = super.getResource(name); |
| 166 | 10 | return u; |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public InputStream getResourceAsStream (String name) |
| 173 | |
{ |
| 174 | 0 | InputStream is = _servletClassLoader.getResourceAsStream(name); |
| 175 | 0 | if (is == null) |
| 176 | 0 | is = super.getResourceAsStream(name); |
| 177 | 0 | return is; |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public Class classForName (String name) throws ClassNotFoundException |
| 186 | |
{ |
| 187 | 94 | Class cls = null; |
| 188 | |
try |
| 189 | |
{ |
| 190 | 94 | cls = _servletClassLoader.loadClass(name); |
| 191 | |
} |
| 192 | 0 | catch (ClassNotFoundException e) |
| 193 | |
{ |
| 194 | 94 | } |
| 195 | |
|
| 196 | 94 | if (cls == null) |
| 197 | 0 | cls = super.classForName(name); |
| 198 | |
|
| 199 | 94 | return cls; |
| 200 | |
} |
| 201 | |
|
| 202 | |
} |