1 19 20 package org.netbeans.modules.httpserver; 21 22 import java.io.Externalizable ; 23 import java.io.File ; 24 import java.net.MalformedURLException ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.Vector ; 28 import javax.servlet.ServletContext ; 29 import org.apache.tomcat.context.WebXmlReader; 30 import org.apache.tomcat.startup.EmbededTomcat; 31 import org.apache.tomcat.core.ContextManager; 32 import org.apache.tomcat.core.Context; 33 import org.apache.tomcat.logging.TomcatLogger; 34 import org.apache.tomcat.service.PoolTcpConnector; 35 import org.openide.ErrorManager; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.Repository; 38 import org.openide.modules.ModuleInstall; 39 import org.openide.util.Lookup; 40 import org.openide.util.LookupEvent; 41 import org.openide.util.LookupListener; 42 import org.openide.util.NbBundle; 43 import org.openide.util.RequestProcessor; 44 import org.openide.util.SharedClassObject; 45 46 51 public class HttpServerModule extends ModuleInstall implements Externalizable { 52 53 54 private static ContextManager server; 55 56 57 private static ContextReloader reloader; 58 59 private static Thread serverThread; 60 private static boolean inSetRunning = false; 61 62 63 public void close () { 64 synchronized (HttpServerSettings.httpLock ()) { 66 stopHTTPServer(); 67 } 68 } 69 70 71 static void initHTTPServer() { 72 if (inSetRunning) 73 return; 74 synchronized (HttpServerSettings.httpLock ()) { 75 if (inSetRunning) 76 return; 77 inSetRunning = true; 78 try { 79 if ((serverThread != null) && (!httpserverSettings ().running)) { 80 try { 82 Thread.currentThread().sleep(2000); 83 } 84 catch (InterruptedException e) {} 85 if ((serverThread != null) && (!httpserverSettings ().running)) { 86 serverThread.stop(); 87 serverThread = null; 88 } 89 } 90 if (serverThread == null) { 91 serverThread = new Thread ("HTTPServer") { public void run() { 93 try { 94 server = buildServer(); 95 server.start(); 96 httpserverSettings ().runSuccess(); 97 reloader.activate (); 98 if (httpserverSettings ().isStartStopMessages()) 100 System.out.println(NbBundle.getMessage(HttpServerModule.class, "CTL_ServerStarted", new Object [] {new Integer (httpserverSettings ().getPort())})); 101 } 102 catch (ThreadDeath td) { 103 throw td; 104 } 105 catch (Throwable ex) { 106 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, ex); 107 serverThread = null; 109 inSetRunning = false; 110 httpserverSettings ().runFailure(ex); 111 } 112 finally { 113 httpserverSettings ().setStartStopMessages(true); 114 } 115 } 116 }; 117 serverThread.start(); 118 } 119 try { 121 HttpServerSettings.httpLock ().wait(HttpServerSettings.SERVER_STARTUP_TIMEOUT); 122 } 123 catch (Exception e) { 124 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e); 125 } 126 } 127 finally { 128 inSetRunning = false; 129 } 130 } 131 } 132 133 public void uninstalled () { 134 stopHTTPServer(); 135 } 136 137 138 @SuppressWarnings ("deprecation") 139 static void stopHTTPServer() { 140 if (inSetRunning) 141 return; 142 synchronized (HttpServerSettings.httpLock ()) { 143 if (inSetRunning) 144 return; 145 inSetRunning = true; 146 try { 147 if (reloader != null) { 148 reloader.deactivate (); 149 reloader = null; 150 } 151 152 if ((serverThread != null) && (server != null)) { 153 try { 154 server.stop(); 155 serverThread.join(); 156 } 157 catch (InterruptedException e) { 158 serverThread.stop(); 159 161 } 162 catch (Exception e) { 163 serverThread.stop(); 165 167 } 168 serverThread = null; 169 if (httpserverSettings ().isStartStopMessages()) 171 System.out.println(NbBundle.getBundle(HttpServerModule.class). 172 getString("CTL_ServerStopped")); 173 } 174 } 175 finally { 176 inSetRunning = false; 177 } 178 } 179 } 180 181 182 private static ContextManager getContextManager(EmbededTomcat tc) { 183 try { 184 java.lang.reflect.Field fm = EmbededTomcat.class.getDeclaredField("contextM"); fm.setAccessible(true); 186 return (ContextManager)fm.get(tc); 187 } 188 catch (NoSuchFieldException e) { 189 return null; 190 } 191 catch (IllegalAccessException e) { 192 return null; 193 } 194 } 195 196 197 200 private static void removeWebXmlReader (EmbededTomcat tc) { 201 try { 202 java.lang.reflect.Field fm = EmbededTomcat.class.getDeclaredField("contextInt"); fm.setAccessible(true); 204 Vector contextInt = (Vector )fm.get(tc); 205 Iterator it = contextInt.iterator (); 206 while (it.hasNext ()) { 207 Object o = it.next (); 208 if (o instanceof WebXmlReader) { 209 contextInt.remove (o); 210 break; 211 } 212 } 213 } 214 catch (NoSuchFieldException e) { 215 return; 216 } 217 catch (IllegalAccessException e) { 218 return; 219 } 220 } 221 222 223 private static ContextManager buildServer() throws Exception { 224 HttpServerSettings op = httpserverSettings (); 225 226 NbLogger logger = new NbLogger(); 227 logger.setName("tc_log"); 229 final EmbededTomcat tc=new EmbededTomcat(); 230 231 File wd = FileUtil.toFile ( 232 Repository.getDefault ().getDefaultFileSystem().getRoot()); 233 wd = new File (wd, "httpwork"); tc.setWorkDir(wd.getAbsolutePath()); 235 236 NbLoaderInterceptor nbL =new NbLoaderInterceptor(); 238 tc.addContextInterceptor( nbL ); 239 240 tc.addApplicationAdapter(null); 242 243 NbServletsInterceptor nbI =new NbServletsInterceptor(); 245 tc.addContextInterceptor( nbI ); 246 247 removeWebXmlReader (tc); 248 249 ServletContext sctx; 250 sctx=tc.addContext("", wd.toURI().toURL()); tc.initContext( sctx ); 252 254 tc.addEndpoint( op.getPort(), null, null); 255 256 final ContextManager cm = getContextManager(tc); 257 258 reloader = new ContextReloader (tc, cm, sctx); 259 260 Enumeration e = cm.getConnectors (); 262 while (e.hasMoreElements ()) { 263 Object o = e.nextElement (); 264 if (o instanceof PoolTcpConnector) { 265 org.apache.tomcat.core.ServerConnector conn = (PoolTcpConnector)o; 266 conn.setAttribute (PoolTcpConnector.MIN_SPARE_THREADS, "0"); conn.setAttribute (PoolTcpConnector.MAX_SPARE_THREADS, "1"); conn.setAttribute (PoolTcpConnector.MAX_THREADS, "3"); } 270 } 271 272 return cm; 273 274 } 275 276 private static class NbLogger extends TomcatLogger { 277 public NbLogger() { 278 super(); 279 } 280 281 protected void realLog(String message) { 282 } 283 284 protected void realLog(String message, Throwable t) { 285 } 286 287 public void flush() { 288 } 289 } 290 291 294 static HttpServerSettings httpserverSettings () { 295 return HttpServerSettings.getDefault(); 296 } 297 298 303 private static class ContextReloader implements LookupListener, Runnable { 304 305 private ServletContext ide_ctx; 306 307 private EmbededTomcat tc; 308 309 private ContextManager cm; 310 311 private Lookup.Result<ClassLoader > res; 312 313 public ContextReloader (EmbededTomcat tc, ContextManager cm, ServletContext ctx) { 314 ide_ctx = ctx; 315 this.tc = tc; 316 this.cm = cm; 317 } 318 319 320 public void activate () { 321 res = Lookup.getDefault().lookup(new Lookup.Template<ClassLoader > (ClassLoader .class)); 322 res.addLookupListener (this); 323 } 324 325 326 public void deactivate () { 327 if (res != null) { 328 res.removeLookupListener (this); 329 res = null; 330 } 331 } 332 333 public void resultChanged (LookupEvent evt) { 334 RequestProcessor.getDefault ().post (this); 335 } 336 337 public void run () { 338 ClassLoader cl = (ClassLoader )res.allInstances ().iterator ().next (); 339 cm.setParentClassLoader (cl); 340 341 File wd = FileUtil.toFile ( 342 Repository.getDefault ().getDefaultFileSystem().getRoot()); 343 wd = new File (wd, "httpwork"); 345 Enumeration e = cm.getContexts (); 346 while (e.hasMoreElements ()) { 347 Object o = e.nextElement (); 348 if (o instanceof Context ) { 349 Context ctx = (Context )o; 350 tc.removeContext (ide_ctx); 352 try { 353 ide_ctx=tc.addContext ("", wd.toURI().toURL ()); } 355 catch (MalformedURLException ex) { 356 } 358 tc.initContext ( ide_ctx ); 359 } 360 } 361 362 } 363 364 } 365 } 366 367 | Popular Tags |