private StandardContext createContext(String path, String docBase) { StandardContext context = new StandardContext(); context.setUnpackWAR(false); context.setDocBase(docBase); context.setPath(path); ContextConfig config = new ContextConfig(); context.addLifecycleListener(config); return (context); } }
private StandardContext createContext(String path, String docBase, ClassLoader classLoader, Map<String, List<Injector<?>>> injectors) { StandardContext context = new StandardContext(); context.setDocBase(docBase); context.setPath(path); ContextConfig config = new ContextConfig(); context.addLifecycleListener(config); context.setParentClassLoader(classLoader); Fabric3Loader loader = new Fabric3Loader(classLoader); context.setLoader(loader); // turn off unpacking so Tomcat does not copy the extracted WAR to the /webapps directory and autodeploy it on a subsequent boot context.setUnpackWAR(false); Fabric3AnnotationProcessor annotationProcessor = new Fabric3AnnotationProcessor(injectors); context.setAnnotationProcessor(annotationProcessor); context.getServletContext().setAttribute(AnnotationProcessor.class.getName(), annotationProcessor); return (context); }
/** * Adds the given child Container to this context. * * @param child the child Container to add * * @exception IllegalArgumentException if the given child Container is * not an instance of Wrapper */ @Override public void addChild(Container child) { addChild(child, false, true); }
@Test public void testAuthenticate() throws Exception { InMemoryRealm realm = new InMemoryRealm(); StandardContext ctx = new StandardContext(); ctx.setName("OryxTest"); realm.setContainer(ctx); realm.start(); realm.addUser("foo", "bar"); Principal authPrincipal = realm.authenticate("foo", "bar"); assertNotNull(authPrincipal); assertEquals("foo", authPrincipal.getName()); assertNull(realm.authenticate("foo", "baz")); assertNull(realm.authenticate("bar", "foo")); assertEquals("bar", realm.getPassword("foo")); assertEquals("foo", realm.getPrincipal("foo").getName()); }
public static StandardContext createContext() { final StandardEngine engine = new StandardEngine(); engine.setService( new StandardService() ); final StandardContext context = new StandardContext(); context.setPath( "/" ); context.setSessionCookiePath( "/" ); final WebappLoader webappLoader = new WebappLoader() { @Override public ClassLoader getClassLoader() { return Thread.currentThread().getContextClassLoader(); } }; context.setLoader( webappLoader ); final StandardHost host = new StandardHost(); engine.addChild( host ); host.addChild( context ); return context; }
public void setMaxAge(int maxAge) { if (ctx.isContextInitializedCalled()) { String msg = MessageFormat.format(rb.getString(LogFacade.SESSION_COOKIE_CONFIG_ALREADY_INIT), new Object[] {"maxAge", ctx.getName()}); throw new IllegalStateException(msg); } this.maxAge = maxAge; }
@Override public void declareRoles(String... roleNames) { if (isContextInitializedCalled) { String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_CONTEXT_ALREADY_INIT_EXCEPTION), new Object[] {"declareRoles", getName()}); throw new IllegalStateException(msg); } for (String roleName : roleNames) { addSecurityRole(roleName); } }
/** * @return true if the context initialization parameter with the given * name and value was set successfully on this ServletContext, and false * if it was not set because this ServletContext already contains a * context initialization parameter with a matching name */ public boolean setInitParameter(String name, String value) { if (isContextInitializedCalled) { String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_CONTEXT_ALREADY_INIT_EXCEPTION), new Object[] {"setInitParameter", getName()}); throw new IllegalStateException(msg); } return context.setInitParameter(name, value); }
protected void stopInternal() throws LifecycleException { if (log.isDebugEnabled()) log.debug(sm.getString("webappLoader.stopping")); ((Context) container).getServletContext(); servletContext.removeAttribute(Globals.CLASS_PATH_ATTR); String contextName = ctx.getName(); if (!contextName.startsWith("/")) { contextName = "/" + contextName; + contextName + ",host=" + ctx.getParent().getName()); Registry.getRegistry(null, null).unregisterComponent(cloname); } catch (Exception e) { log.error("LifecycleException ", e);
if (!getState().isAvailable()) throw new IllegalStateException (sm.getString("standardContext.notStarted", getName())); if(log.isInfoEnabled()) log.info(sm.getString("standardContext.reloadingStarted", getName())); setPaused(true); stop(); } catch (LifecycleException e) { log.error( sm.getString("standardContext.stoppingContext", getName()), e); start(); } catch (LifecycleException e) { log.error( sm.getString("standardContext.startingContext", getName()), e); setPaused(false); if(log.isInfoEnabled()) log.info(sm.getString("standardContext.reloadingCompleted", getName()));
@Override public boolean fireRequestInitEvent(ServletRequest request) { Object instances[] = getApplicationEventListeners(); if ((instances != null) && (instances.length > 0)) { ServletRequestEvent event = new ServletRequestEvent(getServletContext(), request); for (int i = 0; i < instances.length; i++) { if (instances[i] == null) continue; if (!(instances[i] instanceof ServletRequestListener)) continue; ServletRequestListener listener = (ServletRequestListener) instances[i]; try { listener.requestInitialized(event); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); getLogger().error(sm.getString( "standardContext.requestListener.requestInit", instances[i].getClass().getName()), t); request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t); return false; } } } return true; }
private ServletRegistration.Dynamic addServlet(String servletName, String servletClass, Servlet servlet) throws IllegalStateException { if (!context.getState().equals(LifecycleState.STARTING_PREP)) { sm.getString("applicationContext.addServlet.ise", getContextPath())); Wrapper wrapper = (Wrapper) context.findChild(servletName); wrapper = context.createWrapper(); wrapper.setName(servletName); context.addChild(wrapper); } else { if (wrapper.getName() != null && return context.dynamicServletAdded(wrapper);
private StandardContext startWebApp(Host host) throws Exception { StandardContext context = new StandardContext(); try { context.setPath(contextRoot); context.addLifecycleListener(new ContextConfig()); File docBase = new File(serverTempDir, contextRoot); if (!docBase.exists()) { docBase.mkdirs(); context.setDocBase(docBase.getPath()); context.setLoader(loader); context.setInstanceManager(new LocalInstanceManager()); final Wrapper wsfsWrapper = context.createWrapper(); wsfsWrapper.setName(servletName); wsfsWrapper.setServlet((Servlet) clazz.newInstance()); wsfsWrapper.setServletClass(servletClass); context.addChild(wsfsWrapper); context.addServletMapping(urlPattern, servletName); context.create(); } catch (Exception e) { throw MESSAGES.createContextPhaseFailed(e); context.start(); } catch (LifecycleException e) { throw MESSAGES.startContextPhaseFailed(e);
/** * Add a URL for a JAR that contains static resources in a * META-INF/resources directory that should be included in the static * resources for this context. */ public void addResourcesDirContext(DirContext altDirContext) { if (webappResources instanceof BaseDirContext) { ((BaseDirContext) webappResources).addAltDirContext(altDirContext); } else { log.error(sm.getString("standardContext.noResourceJar", altDirContext, getName())); } }
@Override public Context createContext(String path, String docBase) { if( log.isDebugEnabled() ) log.debug("Creating context '" + path + "' with docBase '" + docBase + "'"); context = new StandardContext(); context.setDocBase(docBase); context.setPath(path); ContextConfig config = new ContextConfig(); config.setCustomAuthenticators(authenticators); ((Lifecycle) context).addLifecycleListener(config); return (context); }
@Override public void unbind(boolean usePrivilegedAction, ClassLoader originalClassLoader) { if (originalClassLoader == null) { return; } if (threadBindingListener != null) { try { threadBindingListener.unbind(); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); log.error(sm.getString( "standardContext.threadBindingListenerError", getName()), t); } } if (usePrivilegedAction) { PrivilegedAction<Void> pa = new PrivilegedSetTccl(originalClassLoader); AccessController.doPrivileged(pa); } else { Thread.currentThread().setContextClassLoader(originalClassLoader); } }
private String getObjectKeyPropertiesNameOnly() { StringBuilder result = new StringBuilder("name=//"); String hostname = getParent().getName(); if (hostname == null) { result.append("DEFAULT"); } else { result.append(hostname); } String contextName = getName(); if (!contextName.startsWith("/")) { result.append('/'); } result.append(contextName); return result.toString(); }
Object listeners[] = context.getApplicationEventListeners(); if ((listeners == null) || (listeners.length == 0)) return; ServletContextAttributeEvent event = new ServletContextAttributeEvent(context.getServletContext(), name, value); for (int i = 0; i < listeners.length; i++) { (ServletContextAttributeListener) listeners[i]; try { context.fireContainerEvent("beforeContextAttributeRemoved", listener); listener.attributeRemoved(event); context.fireContainerEvent("afterContextAttributeRemoved", listener); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); context.fireContainerEvent("afterContextAttributeRemoved", listener); log(sm.getString("applicationContext.attributeEvent"), t);
private static StandardContext startWebApp(Host host, WSEndpointDeploymentUnit unit) throws Exception { StandardContext context = new StandardContext(); try { JBossWebMetaData jbwebMD = unit.getAttachment(WSAttachmentKeys.JBOSSWEB_METADATA_KEY); context.setPath(jbwebMD.getContextRoot()); context.addLifecycleListener(new ContextConfig()); ServerConfigService config = (ServerConfigService)unit.getServiceRegistry().getService(WSServices.CONFIG_SERVICE).getService(); File docBase = new File(config.getValue().getServerTempDir(), jbwebMD.getContextRoot()); if (!docBase.exists()) { docBase.mkdirs(); } context.setDocBase(docBase.getPath()); final Loader loader = new WebCtxLoader(unit.getAttachment(WSAttachmentKeys.CLASSLOADER_KEY)); loader.setContainer(host); context.setLoader(loader); context.setInstanceManager(new LocalInstanceManager()); addServlets(jbwebMD, context); host.addChild(context); context.create(); } catch (Exception e) { throw MESSAGES.createContextPhaseFailed(e); } try { context.start(); } catch (LifecycleException e) { throw MESSAGES.startContextPhaseFailed(e); } return context; }
public void deployWebApps(AppInfo appInfo, ClassLoader classLoader) throws Exception { for (WebAppInfo webApp : appInfo.webApps) { if (getContextInfo(webApp) == null) { StandardContext standardContext = new StandardContext(); String contextXmlFile = webApp.codebase + "/META-INF/context.xml"; if (new File(contextXmlFile).exists()) { standardContext.setConfigFile(contextXmlFile); standardContext.setOverride(true); } ContextConfig contextConfig = new ContextConfig(); standardContext.addLifecycleListener(contextConfig); standardContext.setPath("/" + webApp.contextRoot); standardContext.setDocBase(webApp.codebase); standardContext.setParentClassLoader(classLoader); standardContext.setDelegate(true); String host = webApp.host; if (host == null) host = "localhost"; HostConfig deployer = deployers.get(host); if (deployer != null) { // host isn't set until we call deployer.manageApp, so pass it ContextInfo contextInfo = addContextInfo(host, standardContext); contextInfo.appInfo = appInfo; contextInfo.deployer = deployer; contextInfo.standardContext = standardContext; deployer.manageApp(standardContext); } } } }