use of org.apache.catalina.core.StandardContext in project tomee by apache.
the class TomcatWebAppBuilder method deployWebApps.
//
// OpenEJB WebAppBuilder
//
/**
* {@inheritDoc}
*/
@Override
public void deployWebApps(final AppInfo appInfo, final ClassLoader classLoader) throws Exception {
try {
for (final WebAppInfo webApp : appInfo.webApps) {
// look for context.xml
final File war = new File(webApp.path);
URL contextXmlUrl = null;
if (war.isDirectory()) {
final File cXml = new File(war, Constants.ApplicationContextXml).getAbsoluteFile();
if (cXml.exists()) {
contextXmlUrl = cXml.toURI().toURL();
logger.info("using context file " + cXml.getAbsolutePath());
}
} else {
// war
try (final JarFile warAsJar = new JarFile(war)) {
final JarEntry entry = warAsJar.getJarEntry(Constants.ApplicationContextXml);
if (entry != null) {
contextXmlUrl = new URL("jar:" + war.getAbsoluteFile().toURI().toURL().toExternalForm() + "!/" + Constants.ApplicationContextXml);
}
}
}
if (isAlreadyDeployed(appInfo, webApp)) {
continue;
}
StandardContext standardContext;
{
final ClassLoader containerLoader = Helper.get();
final Host host = hosts.getDefault();
if (StandardHost.class.isInstance(host) && !StandardContext.class.getName().equals(StandardHost.class.cast(host).getContextClass())) {
try {
standardContext = StandardContext.class.cast(containerLoader.loadClass(StandardHost.class.cast(host).getContextClass()).newInstance());
} catch (final Throwable th) {
logger.warning("Can't use context class specified, using default StandardContext", th);
standardContext = new StandardContext();
}
} else {
standardContext = new StandardContext();
}
// should be optional but in maven parent is app loader and not maven loader which is the real parent
final ClassLoader currentParent = standardContext.getParentClassLoader();
if (currentParent == null || isParent(currentParent, containerLoader)) {
standardContext.setParentClassLoader(containerLoader);
}
}
standardContext.setUnpackWAR(!"false".equalsIgnoreCase(appInfo.properties.getProperty("tomcat.unpackWar")));
if (contextXmlUrl != null) {
standardContext.setConfigFile(contextXmlUrl);
}
if (standardContext.getPath() != null) {
webApp.contextRoot = standardContext.getPath();
}
if (webApp.contextRoot.startsWith("/") || webApp.contextRoot.startsWith(File.separator)) {
webApp.contextRoot = webApp.contextRoot.substring(1);
}
if (webApp.contextRoot.startsWith(File.separator)) {
webApp.contextRoot = webApp.contextRoot.replaceFirst(File.separator, "");
}
// /!\ take care, StandardContext default host = "_" and not null or localhost
final String hostname = Contexts.getHostname(standardContext);
if (hostname != null && !"_".equals(hostname)) {
webApp.host = hostname;
}
final ApplicationParameter appParam = new ApplicationParameter();
appParam.setName(OPENEJB_WEBAPP_MODULE_ID);
appParam.setValue(webApp.moduleId);
standardContext.addApplicationParameter(appParam);
if (!isAlreadyDeployed(appInfo, webApp)) {
if (standardContext.getPath() == null) {
if (webApp.contextRoot != null && webApp.contextRoot.startsWith("/")) {
standardContext.setPath(webApp.contextRoot);
} else if (isRoot(webApp.contextRoot)) {
standardContext.setPath("");
} else {
standardContext.setPath("/" + webApp.contextRoot);
}
}
if (standardContext.getDocBase() == null) {
standardContext.setDocBase(webApp.path);
}
String docBase = standardContext.getDocBase();
File docBaseFile = new File(docBase);
if (docBase != null && docBaseFile.isFile() && docBase.endsWith(".war")) {
DeploymentLoader.unpack(docBaseFile);
if (standardContext.getPath().endsWith(".war")) {
standardContext.setPath(removeFirstSlashAndWar("/" + standardContext.getPath()));
standardContext.setName(standardContext.getPath());
webApp.contextRoot = standardContext.getPath();
}
standardContext.setDocBase(docBase.substring(0, docBase.length() - 4));
}
if (isRoot(standardContext.getName())) {
standardContext.setName("");
webApp.contextRoot = "";
}
if (isAlreadyDeployed(appInfo, webApp)) {
// possible because of the previous renaming
continue;
}
// but here we have all the classloading logic
if (classLoader != null) {
standardContext.setParentClassLoader(classLoader);
standardContext.setDelegate(true);
}
String host = webApp.host;
if (host == null) {
host = hosts.getDefaultHost();
logger.info("using default host: " + host);
}
if (classLoader != null) {
appInfo.autoDeploy = false;
deployWar(standardContext, host, appInfo);
} else {
// force a normal deployment with lazy building of AppInfo
deployWar(standardContext, host, null);
}
}
}
} finally {
// cleanup temp var passing
for (final WebAppInfo webApp : appInfo.webApps) {
appInfo.properties.remove(webApp);
}
}
}
use of org.apache.catalina.core.StandardContext in project tomee by apache.
the class TomcatWebAppBuilder method undeployWebApps.
/**
* {@inheritDoc}
*/
@Override
public void undeployWebApps(final AppInfo appInfo) throws Exception {
final String version = appVersion(appInfo);
for (final WebAppInfo webApp : appInfo.webApps) {
final ContextInfo contextInfo = getContextInfo(webApp.host, webApp.contextRoot, version);
if (contextInfo != null) {
final StandardContext standardContext = contextInfo.standardContext;
if (!appInfo.webAppAlone || !appInfo.properties.containsKey("tomee.destroying")) {
undeploy(standardContext, contextInfo);
final File extracted = Contexts.warPath(standardContext);
if (isExtracted(extracted)) {
deleteDir(extracted);
}
removeContextInfo(standardContext);
}
}
}
}
use of org.apache.catalina.core.StandardContext in project tomee by apache.
the class OpenEJBListener method findOpenEjbWar.
private static File findOpenEjbWar(final StandardHost standardHost) {
// look for openejb war in a Tomcat context
for (final Container container : standardHost.findChildren()) {
if (container instanceof StandardContext) {
final StandardContext standardContext = (StandardContext) container;
File contextDocBase = new File(standardContext.getDocBase());
if (!contextDocBase.isDirectory() && standardContext.getOriginalDocBase() != null) {
contextDocBase = new File(standardContext.getOriginalDocBase());
}
if (contextDocBase.isDirectory()) {
final File openEjbWar = findOpenEjbWarInContext(contextDocBase);
if (openEjbWar != null) {
return openEjbWar;
}
}
}
}
return null;
}
use of org.apache.catalina.core.StandardContext in project tomee by apache.
the class TomcatHessianRegistry method createNewContext.
private static Context createNewContext(final ClassLoader classLoader, final String rAuthMethod, final String rTransportGuarantee, final String realmName, final String name) {
String path = name;
if (path == null) {
path = "/";
}
if (!path.startsWith("/")) {
path = "/" + path;
}
final StandardContext context = new IgnoredStandardContext();
context.setPath(path);
context.setDocBase("");
context.setParentClassLoader(classLoader);
context.setDelegate(true);
context.setName(name);
TomcatWebAppBuilder.class.cast(SystemInstance.get().getComponent(WebAppBuilder.class)).initJ2EEInfo(context);
// Configure security
String authMethod = rAuthMethod;
if (authMethod != null) {
authMethod = authMethod.toUpperCase();
}
String transportGuarantee = rTransportGuarantee;
if (transportGuarantee != null) {
transportGuarantee = transportGuarantee.toUpperCase();
}
if (authMethod != null & !"NONE".equals(authMethod)) {
if ("BASIC".equals(authMethod) || "DIGEST".equals(authMethod) || "CLIENT-CERT".equals(authMethod)) {
// Setup a login configuration
final LoginConfig loginConfig = new LoginConfig();
loginConfig.setAuthMethod(authMethod);
loginConfig.setRealmName(realmName);
context.setLoginConfig(loginConfig);
// Setup a default Security Constraint
final String securityRole = SystemInstance.get().getProperty(TOMEE_HESSIAN_SECURITY_ROLE_PREFIX + name, "default");
for (final String role : securityRole.split(",")) {
final SecurityCollection collection = new SecurityCollection();
collection.addMethod("GET");
collection.addMethod("POST");
collection.addPattern("/*");
collection.setName(role);
final SecurityConstraint sc = new SecurityConstraint();
sc.addAuthRole("*");
sc.addCollection(collection);
sc.setAuthConstraint(true);
sc.setUserConstraint(transportGuarantee);
context.addConstraint(sc);
context.addSecurityRole(role);
}
}
// Set the proper authenticator
switch(authMethod) {
case "BASIC":
context.addValve(new BasicAuthenticator());
break;
case "DIGEST":
context.addValve(new DigestAuthenticator());
break;
case "CLIENT-CERT":
context.addValve(new SSLAuthenticator());
break;
case "NONE":
context.addValve(new NonLoginAuthenticator());
break;
}
context.getPipeline().addValve(new OpenEJBValve());
} else {
throw new IllegalArgumentException("Invalid authMethod: " + authMethod);
}
return context;
}
use of org.apache.catalina.core.StandardContext in project tomee by apache.
the class GlobalListenerSupport method hostAdded.
/**
* Host is added.
*
* @param host tomcat host.
*/
private void hostAdded(final StandardHost host) {
addContextListener(host);
host.addLifecycleListener(this);
for (final Container child : host.findChildren()) {
if (child instanceof StandardContext) {
final StandardContext context = (StandardContext) child;
contextAdded(context);
}
}
}
Aggregations