1 package com.sslexplorer.testcontainer; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.lang.reflect.Method ; 8 import java.net.InetAddress ; 9 import java.net.URL ; 10 import java.net.URLClassLoader ; 11 import java.util.ArrayList ; 12 import java.util.Collection ; 13 import java.util.Enumeration ; 14 import java.util.List ; 15 import java.util.Properties ; 16 import java.util.prefs.Preferences ; 17 18 import javax.net.ssl.TrustManager; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import com.meterware.servletunit.ServletRunner; 26 import com.sslexplorer.boot.BootProgressMonitor; 27 import com.sslexplorer.boot.Context; 28 import com.sslexplorer.boot.ContextHolder; 29 import com.sslexplorer.boot.ContextListener; 30 import com.sslexplorer.boot.LogBootProgressMonitor; 31 import com.sslexplorer.boot.PropertyClass; 32 import com.sslexplorer.boot.PropertyPreferences; 33 import com.sslexplorer.boot.RequestHandler; 34 import com.sslexplorer.boot.RequestHandlerRequest; 35 import com.sslexplorer.boot.RequestHandlerResponse; 36 import com.sslexplorer.boot.StopContextListenerThread; 37 import com.sslexplorer.boot.VersionInfo; 38 import com.sslexplorer.boot.VersionInfo.Version; 39 40 42 public class TestContext implements Context { 43 final static Log log = LogFactory.getLog(TestContext.class); 44 45 static { 46 File f = new File ("tmp/db"); 47 f.mkdirs(); 48 } 49 50 final static File TMP_DIR = new File ("tmp"); 51 final static File DB_DIR = new File ("tmp/db"); 52 final static File LOG_DIR = new File ("logs"); 53 54 private File appDir = null; 55 private List <ContextListener> listeners = new ArrayList <ContextListener>(); 56 private File confDir; 57 private String hostname; 58 private Thread mainThread; 59 private Preferences pref; 60 private boolean setup; 61 private ServletRunner runner; 62 private BootProgressMonitor bootProgressMonitor = new LogBootProgressMonitor();; 63 64 69 public TestContext(File confDir, boolean setup) throws Exception { 70 ContextHolder.setContext(this); 71 this.confDir = confDir; 72 loadSystemProperties(); 73 if (!"".equals(System.getProperty("sslexplorer.extensions", ""))) { 74 appDir = new File (System.getProperty("sslexplorer.extensions")); 75 } 76 hostname = InetAddress.getLocalHost().getCanonicalHostName(); 77 mainThread = Thread.currentThread(); 78 this.setup = setup; 79 pref = PropertyPreferences.SYSTEM_ROOT; 80 log.info("Creating runner"); 81 runner = new ServletRunner(new File ("webapp/WEB-INF/web.xml"), "/webapp"); 82 } 98 99 private void loadSystemProperties() { 100 101 105 InputStream in = null; 106 try { 107 File f = new File (getConfDirectory(), "system.properties"); 108 in = new FileInputStream (f); 109 Properties p = new Properties (); 110 p.load(in); 111 for (Enumeration e = p.keys(); e.hasMoreElements();) { 112 String k = (String ) e.nextElement(); 113 114 if(!k.equals("sslexplorer.devExtensions") || System.getProperty("sslexplorer.devExtensions") == null) { 116 System.getProperties().setProperty(k, p.getProperty(k).trim()); 117 } 118 } 119 } catch (IOException e) { 120 } finally { 122 if (in != null) { 123 try { 124 in.close(); 125 } catch (IOException ioe) { 126 127 } 128 } 129 } 130 } 131 132 136 public static void main(String [] args) throws Exception { 137 File conf = new File ("conf"); 138 boolean setup = false; 139 for (int i = 0; i < args.length; i++) { 140 if (args[i].equals("--conf")) { 141 conf = new File (args[++i]); 142 } else if (args[i].equals("--install") || args[i].equals("--setup")) { 143 setup = true; 144 } else { 145 throw new Exception (TestContext.class.getName() + " only supports --conf=<confdir> or --install arguments"); 146 } 147 } 148 new TestContext(conf, setup); 149 } 150 151 public void addContextListener(ContextListener contextListener) { 152 listeners.add(contextListener); 153 154 } 155 156 public void addContextLoaderURL(URL u) { 157 try { 158 URLClassLoader sysloader = (URLClassLoader ) getClass().getClassLoader(); 159 Class sysclass = URLClassLoader .class; 160 Method method = sysclass.getDeclaredMethod("addURL", new Class [] { URL .class }); 161 method.setAccessible(true); 162 method.invoke(sysloader, new Object [] { u }); 163 if (log.isInfoEnabled()) 164 log.info(u.toExternalForm() + " added to context classloader"); 165 } catch (Exception e) { 166 log.error("Failed to add to classpath.", e); 167 } 168 } 169 170 public void addResourceBase(URL url) { 171 } 173 174 public void addWebApp(String contextPathSpec, String webApp) throws Exception { 175 } 177 178 public Collection <URL > getResourceBases() { 179 return new ArrayList <URL >(); 180 } 181 182 public String deobfuscatePassword(String s) { 183 if (s.startsWith("OBF:")) 184 s = s.substring(4); 185 186 byte[] b = new byte[s.length() / 2]; 187 int l = 0; 188 for (int i = 0; i < s.length(); i += 4) { 189 String x = s.substring(i, i + 4); 190 int i0 = Integer.parseInt(x, 36); 191 int i1 = (i0 / 256); 192 int i2 = (i0 % 256); 193 b[l++] = (byte) ((i1 + i2 - 254) / 2); 194 } 195 196 return new String (b, 0, l); 197 } 198 199 public void deregisterRequestHandler(RequestHandler requestHandler) { 200 } 202 203 public File getApplicationDirectory() { 204 return appDir == null ? new File (getConfDirectory(), "extensions") : appDir; 205 } 206 207 public File getConfDirectory() { 208 return confDir; 209 } 210 211 public File getDBDirectory() { 212 return DB_DIR; 213 } 214 215 public String getHostname() { 216 return hostname; 217 } 218 219 public File getLogDirectory() { 220 return LOG_DIR; 221 } 222 223 public Thread getMainThread() { 224 return mainThread; 225 } 226 227 public int getPort() { 228 return 0; 230 } 231 232 public Preferences getPreferences() { 233 return pref; 234 } 235 236 public File getTempDirectory() { 237 return TMP_DIR; 238 } 239 240 public Version getVersion() { 241 return VersionInfo.getVersion(); 242 } 243 244 public boolean isRestartAvailableMode() { 245 return false; 246 } 247 248 public boolean isSetupMode() { 249 return setup; 250 } 251 252 public String obfuscatePassword(String s) { 253 StringBuffer buf = new StringBuffer (); 254 byte[] b = s.getBytes(); 255 256 synchronized (buf) { 257 buf.append("OBF:"); 258 for (int i = 0; i < b.length; i++) { 259 byte b1 = b[i]; 260 byte b2 = b[s.length() - (i + 1)]; 261 int i1 = (int) b1 + (int) b2 + 127; 262 int i2 = (int) b1 - (int) b2 + 127; 263 int i0 = i1 * 256 + i2; 264 String x = Integer.toString(i0, 36); 265 266 switch (x.length()) { 267 case 1: 268 buf.append('0'); 269 case 2: 270 buf.append('0'); 271 case 3: 272 buf.append('0'); 273 default: 274 buf.append(x); 275 } 276 } 277 return buf.toString(); 278 } 279 } 280 281 public void registerRequestHandler(RequestHandler requestHandler) { 282 } 284 285 public void removeContextListener(ContextListener contextListener) { 286 listeners.remove(contextListener); 287 } 288 289 public void removeResourceBase(URL url) { 290 } 292 293 298 public String setContextProperty(String key, String value) { 299 return null; 301 } 302 303 public void setTrustMananger(TrustManager trustManager, boolean require) { 304 } 306 307 public void shutdown(boolean restart) { 308 for (ContextListener l : listeners) { 310 new StopContextListenerThread(l).waitForStop(); 311 } 312 runner.shutDown(); 313 } 314 315 public URL [] getContextLoaderClassPath() { 316 return ((URLClassLoader ) getClass().getClassLoader()).getURLs(); 317 } 318 319 public ClassLoader getContextLoader() { 320 return getClass().getClassLoader(); 321 } 322 323 327 public static Context getTestContext() throws Exception { 328 return new TestContext(new File ("devconf"), false); 329 } 330 331 public void setResourceAlias(String uri, String location) { 332 } 334 335 public PropertyClass getConfig() { 336 return null; 337 } 338 339 public void removeResourceAlias(String uri) { 340 } 342 343 public BootProgressMonitor getBootProgressMonitor() { 344 return bootProgressMonitor; 345 } 346 347 public void registerRequestHandler(RequestHandler requestHandler, HandlerProtocol protocol) { 348 350 } 351 352 public HttpServletRequest createServletRequest(RequestHandlerRequest request) { 353 return null; 355 } 356 357 public HttpServletResponse createServletResponse(RequestHandlerResponse response, HttpServletRequest request) { 358 return null; 360 } 361 } | Popular Tags |