1 23 24 package com.sun.enterprise.web; 25 26 import java.io.File ; 27 import java.util.HashMap ; 28 import java.util.logging.Logger ; 29 import java.util.logging.Level ; 30 31 import org.apache.catalina.Connector; 32 import org.apache.catalina.ContainerListener; 33 import org.apache.catalina.Context; 34 import org.apache.catalina.Host; 35 import org.apache.catalina.Engine; 36 import org.apache.catalina.InstanceListener; 37 import org.apache.catalina.Lifecycle; 38 import org.apache.catalina.Realm; 39 import org.apache.catalina.core.StandardContext; 40 import org.apache.catalina.core.StandardEngine; 41 import org.apache.catalina.startup.Embedded; 42 import org.apache.catalina.startup.ContextConfig; 43 import org.apache.catalina.mbeans.MBeanUtils; 44 import org.apache.catalina.net.ServerSocketFactory; 45 import org.apache.catalina.logger.FileLogger; 46 import org.apache.tomcat.util.IntrospectionUtils; 47 48 import com.sun.enterprise.deployment.WebBundleDescriptor; 49 import com.sun.enterprise.web.connector.coyote.PECoyoteConnector; 50 import com.sun.web.server.WebContainerListener; 51 import com.sun.enterprise.server.ServerContext; 52 import com.sun.enterprise.server.pluggable.WebContainerFeatureFactory; 53 import com.sun.enterprise.config.serverbeans.ElementProperty; 54 import com.sun.enterprise.config.ConfigBean; 55 56 65 public final class EmbeddedWebContainer extends Embedded { 66 67 70 private Logger _logger = null; 71 72 private WebContainerFeatureFactory webContainerFeatureFactory; 73 74 private WebContainer webContainer; 75 76 79 private String logServiceFile; 80 81 82 84 public EmbeddedWebContainer(Logger webLogger, 85 ServerContext serverContext, 86 WebContainer webContainer, 87 String logServiceFile) { 88 super(); 89 _logger = webLogger; 90 this.webContainer = webContainer; 91 this.logServiceFile = logServiceFile; 92 webContainerFeatureFactory = 93 serverContext.getPluggableFeatureFactory().getWebContainerFeatureFactory(); 94 } 95 96 97 99 109 public Host createHost( 110 String vsID, 111 com.sun.enterprise.config.serverbeans.VirtualServer vsBean, 112 String vsDocroot, 113 String vsLogFile, 114 MimeMap vsMimeMap) { 115 116 VirtualServer vs = webContainerFeatureFactory.getVirtualServer(); 117 vs.setDebug(debug); 118 vs.setAppBase(vsDocroot); 119 vs.setName(vsID); 120 vs.setID(vsID); 121 vs.setBean(vsBean); 122 vs.setMimeMap(vsMimeMap); 123 124 String defaultWebXmlLocation = Constants.DEFAULT_WEB_XML; 125 boolean allowLinking = true; 126 String state = null; 127 128 if (vsBean != null) { 129 130 state = vsBean.getState(); 131 132 ElementProperty prop = null; 133 134 prop = vsBean.getElementPropertyByName("default-web-xml"); 136 if (prop != null) { 137 defaultWebXmlLocation = prop.getValue(); 138 } 139 141 prop = vsBean.getElementPropertyByName("allowLinking"); 143 if (prop != null) { 144 allowLinking = ConfigBean.toBoolean(prop.getValue()); 145 } 146 } 147 148 vs.setDefaultWebXmlLocation(defaultWebXmlLocation); 149 150 if (state == null) { 152 state = WebContainer.ON; 153 } 154 if (WebContainer.DISABLED.equalsIgnoreCase(state)) { 155 vs.setIsActive(false); 156 } else { 157 vs.setIsActive(ConfigBean.toBoolean(state)); 158 } 159 160 vs.setAllowLinking(allowLinking); 161 162 if (vsLogFile != null && !vsLogFile.equals(logServiceFile)) { 163 168 setLogFile(vs, vsLogFile); 169 } 170 171 ContainerListener listener = loadListener 172 ("com.sun.enterprise.web.connector.grizzly.CatalinaListener"); 173 if ( listener != null ) 174 vs.addContainerListener(listener); 175 176 return vs; 177 } 178 179 180 189 public Context createContext(String ctxPath, String location, 190 String defaultWebXmlLocation, 191 boolean useDOLforDeployment, 192 WebBundleDescriptor wbd) { 193 194 boolean hasWebXml = (wbd == null ? false : true); 195 196 WebModule context = new WebModule(webContainer); 197 context.setDebug(debug); 198 context.setPath(ctxPath); 199 context.setDocBase(location); 200 context.setCrossContext(true); 201 context.setUseNaming(isUseNaming()); 202 context.setHasWebXml(hasWebXml); 203 context.setManagerChecksFrequency(1); 204 205 ContextConfig config; 206 WebModule defaultWebModule; 207 if (useDOLforDeployment) { 208 config = new WebModuleContextConfig(); 209 ((WebModuleContextConfig)config).setDescriptor(wbd); 210 } else { 211 config = new ContextConfig(); 212 } 213 214 config.setDefaultWebXml(defaultWebXmlLocation); 215 ((Lifecycle) context).addLifecycleListener(config); 216 217 context.addLifecycleListener(new WebModuleListener( 218 webContainer.getInstanceClassPath(), location, wbd)); 219 220 context.addInstanceListener(Constants.J2EE_INSTANCE_LISTENER); 221 222 context.addContainerListener(new WebContainerListener()); 223 224 context.addInstanceListener( 225 "com.sun.enterprise.admin.monitor.callflow.WebContainerListener"); 226 227 return context; 228 } 229 230 231 235 private ContainerListener loadListener(String className){ 236 try{ 237 Class clazz = Class.forName(className); 238 return (ContainerListener)clazz.newInstance(); 239 } catch (Throwable ex){ 240 _logger.log(Level.SEVERE,ex.getMessage() + ":" + className, ex); 241 } 242 return null; 243 } 244 245 246 249 public Engine[] getEngines() { 250 return engines; 251 } 252 253 260 public Connector[] getConnectors() { 261 return connectors; 262 } 263 264 271 protected void setLogFile(Host vs, String logFile) { 272 273 String logPrefix = logFile; 274 String logDir = null; 275 String logSuffix = null; 276 277 if (logPrefix == null || logPrefix.equals("")) { 278 return; 279 } 280 281 int index = logPrefix.lastIndexOf(File.separatorChar); 282 if (index != -1) { 283 logDir = logPrefix.substring(0, index); 284 logPrefix = logPrefix.substring(index+1); 285 } 286 287 index = logPrefix.indexOf('.'); 288 if (index != -1) { 289 logSuffix = logPrefix.substring(index); 290 logPrefix = logPrefix.substring(0, index); 291 } 292 293 logPrefix += "_"; 294 295 FileLogger contextLogger = new FileLogger(); 296 if (logDir != null) { 297 contextLogger.setDirectory(logDir); 298 } 299 contextLogger.setPrefix(logPrefix); 300 if (logSuffix != null) { 301 contextLogger.setSuffix(logSuffix); 302 } 303 contextLogger.setTimestamp(true); 304 305 vs.setLogger(contextLogger); 306 } 307 308 309 318 public Connector createConnector(String address, int port, 319 String protocol) { 320 321 if (address != null) { 322 328 int index = address.indexOf('/'); 329 if (index != -1) { 330 address = address.substring(index + 1); 331 } 332 } 333 334 _logger.log(Level.FINE,"Creating connector for address='" + 335 ((address == null) ? "ALL" : address) + 336 "' port='" + port + "' protocol='" + protocol + "'"); 337 338 PECoyoteConnector connector = new PECoyoteConnector(); 339 340 if (address != null) { 341 connector.setAddress(address); 342 } 343 344 connector.setPort(port); 345 346 if (protocol.equals("ajp")) { 347 connector.setProtocolHandlerClassName( 348 "org.apache.jk.server.JkCoyoteHandler"); 349 } else if (protocol.equals("memory")) { 350 connector.setProtocolHandlerClassName( 351 "org.apache.coyote.memory.MemoryProtocolHandler"); 352 } else if (protocol.equals("https")) { 353 connector.setScheme("https"); 354 connector.setSecure(true); 355 } 356 357 return (connector); 358 359 } 360 361 362 370 public Engine createEngine() { 371 372 StandardEngine engine = new StandardEngine(){ 373 public Realm getRealm(){ 374 return null; 375 } 376 }; 377 378 engine.setDebug(debug); 379 engine.setLogger(logger); engine.setRealm(null); 383 ContainerListener listener = loadListener 384 ("com.sun.enterprise.admin.monitor.callflow.WebContainerListener"); 385 if ( listener != null ) { 386 engine.addContainerListener(listener); 387 } 388 return (engine); 389 390 } 391 } 392 | Popular Tags |