use of org.apache.catalina.core.ContainerBase in project tomcat by apache.
the class HostManagerServlet method remove.
/**
* Remove the specified host.
*
* @param writer Writer to render results to
* @param name host name
* @param smClient StringManager for the client's locale
*/
protected synchronized void remove(PrintWriter writer, String name, StringManager smClient) {
if (debug >= 1) {
log(sm.getString("hostManagerServlet.remove", name));
}
// Validate the requested host name
if ((name == null) || name.length() == 0) {
writer.println(smClient.getString("hostManagerServlet.invalidHostName", name));
return;
}
// Check if host exists
if (engine.findChild(name) == null) {
writer.println(smClient.getString("hostManagerServlet.noHost", name));
return;
}
// Prevent removing our own host
if (engine.findChild(name) == installedHost) {
writer.println(smClient.getString("hostManagerServlet.cannotRemoveOwnHost", name));
return;
}
// Note that the host will not get physically removed
try {
Container child = engine.findChild(name);
engine.removeChild(child);
if (child instanceof ContainerBase)
((ContainerBase) child).destroy();
} catch (Exception e) {
writer.println(smClient.getString("hostManagerServlet.exception", e.toString()));
return;
}
Host host = (StandardHost) engine.findChild(name);
if (host == null) {
writer.println(smClient.getString("hostManagerServlet.remove", name));
} else {
// Something failed
writer.println(smClient.getString("hostManagerServlet.removeFailed", name));
}
}
use of org.apache.catalina.core.ContainerBase in project tomcat by apache.
the class ContainerMBean method addChild.
/**
* Add a new child Container to those associated with this Container,
* if supported. Won't start the child yet. Has to be started with a call to
* Start method after necessary configurations are done.
*
* @param type ClassName of the child to be added
* @param name Name of the child to be added
*
* @exception MBeanException if the child cannot be added
*/
public void addChild(String type, String name) throws MBeanException {
Container contained = (Container) newInstance(type);
contained.setName(name);
if (contained instanceof StandardHost) {
HostConfig config = new HostConfig();
contained.addLifecycleListener(config);
} else if (contained instanceof StandardContext) {
ContextConfig config = new ContextConfig();
contained.addLifecycleListener(config);
}
boolean oldValue = true;
ContainerBase container = doGetManagedResource();
try {
oldValue = container.getStartChildren();
container.setStartChildren(false);
container.addChild(contained);
contained.init();
} catch (LifecycleException e) {
throw new MBeanException(e);
} finally {
if (container != null) {
container.setStartChildren(oldValue);
}
}
}
use of org.apache.catalina.core.ContainerBase in project tomee by apache.
the class GlobalListenerSupport method addContextListener.
/**
* Setting monitoreable child field.
*
* @param containerBase host or engine
*/
@SuppressWarnings("unchecked")
private void addContextListener(final ContainerBase containerBase) {
boolean accessible = false;
Field field = null;
try {
field = ContainerBase.class.getDeclaredField("children");
accessible = field.isAccessible();
field.setAccessible(true);
Map<Object, Object> children = (Map<Object, Object>) field.get(containerBase);
if (children instanceof GlobalListenerSupport.MoniterableHashMap) {
return;
}
children = new GlobalListenerSupport.MoniterableHashMap(children, containerBase, "children", this);
field.set(containerBase, children);
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (field != null) {
if (!accessible) {
field.setAccessible(false);
}
}
}
}
use of org.apache.catalina.core.ContainerBase in project tomee by apache.
the class TomcatWebAppBuilder method checkHost.
/**
* {@inheritDoc}
*/
@Override
public synchronized void checkHost(final StandardHost standardHost) {
if (noHostCheck) {
return;
}
if (standardHost.getAutoDeploy()) {
// Undeploy any modified application
for (final Iterator<Map.Entry<String, DeployedApplication>> iterator = deployedApps.entrySet().iterator(); iterator.hasNext(); ) {
final Map.Entry<String, DeployedApplication> entry = iterator.next();
final DeployedApplication deployedApplication = entry.getValue();
if (deployedApplication.isModified()) {
// TODO: for war use StandardContext.redeploy()
if (deployedApplication.appInfo != null) {
// can happen with badly formed config
try {
getAssembler().destroyApplication(deployedApplication.appInfo.path);
} catch (final Exception e) {
logger.error("Unable to application " + deployedApplication.appInfo.path, e);
}
} else {
logger.error("appinfo is null for " + deployedApplication);
}
iterator.remove();
}
}
// Deploy new applications
final File appBase = appBase(standardHost);
final File[] files = appBase.listFiles();
if (null != files) {
for (File file : files) {
if (file.getName().endsWith(".tmp")) {
// tomcat is uploading, see org.apache.catalina.manager.ManagerServlet.deploy(java.io.PrintWriter, org.apache.catalina.util.ContextName, java.lang.String, boolean, javax.servlet.http.HttpServletRequest, org.apache.tomcat.util.res.StringManager)
continue;
}
final String name = file.getName();
// ignore war files
if (name.toLowerCase().endsWith(".war") || isRoot(name) || name.equalsIgnoreCase("META-INF") || name.equalsIgnoreCase("WEB-INF")) {
continue;
}
// Simple fix for TOMEE-23
if (name.toLowerCase().equals(".ds_store")) {
continue;
}
// ignore unpacked web apps
if (file.isDirectory() && new File(file, "WEB-INF").exists()) {
continue;
}
// ignore unpacked apps where packed version is present (packed version is owner)
if (file.isDirectory() && (new File(file.getParent(), file.getName() + ".ear").exists() || new File(file.getParent(), file.getName() + ".war").exists() || new File(file.getParent(), file.getName() + ".rar").exists())) {
continue;
}
// ignore already deployed apps
if (isDeployed(file, standardHost)) {
continue;
}
final AppInfo appInfo;
try {
file = file.getCanonicalFile().getAbsoluteFile();
final AppModule appModule = deploymentLoader.load(file, null);
// Ignore any standalone web modules - this happens when the app is unpaked and doesn't have a WEB-INF dir
if (appModule.getDeploymentModule().size() == 1 && appModule.getWebModules().size() == 1) {
final WebModule webModule = appModule.getWebModules().iterator().next();
if (file.getAbsolutePath().equals(webModule.getJarLocation())) {
continue;
}
}
// tell web modules to deploy using this host
for (final WebModule webModule : appModule.getWebModules()) {
webModule.setHost(standardHost.getName());
}
appInfo = configurationFactory.configureApplication(appModule);
// if this is an unpacked dir, tomcat will pick it up as a webapp so undeploy it first
if (file.isDirectory()) {
final ContainerBase context = (ContainerBase) standardHost.findChild("/" + name);
if (context != null) {
try {
standardHost.removeChild(context);
} catch (final Throwable t) {
logger.warning("Error undeploying wep application from Tomcat " + name, t);
}
try {
context.destroy();
} catch (final Throwable t) {
logger.warning("Error destroying Tomcat web context " + name, t);
}
}
}
getAssembler().createApplication(appInfo);
deployedApps.put(file.getAbsolutePath(), new DeployedApplication(file, appInfo));
} catch (final Throwable e) {
logger.warning("Error deploying application " + file.getAbsolutePath(), e);
}
}
}
}
}
Aggregations