/** * Doing the start, stop, reload and lazy unload of webapps inside all hosts respectively when * getting request. * * @param nameOfOperation the operation to be performed in oder to hot update the host * @throws CarbonException if errors occurs when hot update the host */ private void handleHotUpdateToHost(String nameOfOperation) throws CarbonException { if (DataHolder.getHotUpdateService() != null) { List<String> mappings = URLMappingHolder.getInstance().getUrlMappingsPerApplication(this.context.getName()); Engine engine = DataHolder.getCarbonTomcatService().getTomcat().getEngine(); Context hostContext; Host host; for (String hostName : mappings) { host = (Host) engine.findChild(hostName); if (host != null) { hostContext = (Context) host.findChild("/"); if (hostContext != null) { if (nameOfOperation.equalsIgnoreCase("start")) { start(hostContext); } else if (nameOfOperation.equalsIgnoreCase("stop")) { stop(hostContext); } else if (nameOfOperation.equalsIgnoreCase("reload")) { reload(hostContext); } else if (nameOfOperation.equalsIgnoreCase("lazyunload")) { lazyUnload(hostContext); DataHolder.getHotUpdateService().removeHost(hostName); } else if (nameOfOperation.equalsIgnoreCase("delete")) { DataHolder.getHotUpdateService().deleteHost(hostName); } } } } } }
public void deploy(String url, File dir) { if (host.findChild(url) != null) return; if (dir == null || !dir.exists() || !dir.isDirectory()) return; log.debug("deploy new context:" + url + ", dir:" + dir.getAbsolutePath()); this.host.addChild(this.embedded.createContext(url, dir.getAbsolutePath())); }
/** Undeploy all deployed applications. */ protected void undeployApps() { if (log.isDebugEnabled()) log.debug(sm.getString("hostConfig.undeploying")); // Soft undeploy all contexts we have deployed DeployedApplication[] apps = (DeployedApplication[]) deployed.values().toArray(new DeployedApplication[0]); for (int i = 0; i < apps.length; i++) { try { host.removeChild(host.findChild(apps[i].name)); } catch (Throwable t) { log.warn(sm.getString("hostConfig.context.remove", apps[i].name), t); } } deployed.clear(); }
@Override public synchronized void start() throws Exception { Host host = ServerUtil.getDefaultHost().getHost(); _serverContext = (StandardContext) host.findChild("/" + _contextName); if (_serverContext == null) { _serverContext = new StandardContext(); _serverContext.setPath("/" + _contextName); File docBase = new File(SERVER_TEMP_DIR, _contextName); if (!docBase.exists()) { if (!docBase.mkdirs()) { throw new RuntimeException("Unable to create temp directory " + docBase.getPath()); } } _serverContext.setDocBase(docBase.getPath()); _serverContext.addLifecycleListener(new ContextConfig()); final Loader loader = new WebCtxLoader(Thread.currentThread().getContextClassLoader()); loader.setContainer(host); _serverContext.setLoader(loader); _serverContext.setInstanceManager(new LocalInstanceManager()); Wrapper wrapper = _serverContext.createWrapper(); wrapper.setName(SERVLET_NAME); wrapper.setServletClass(SwitchYardRemotingServlet.class.getName()); wrapper.setLoadOnStartup(1); _serverContext.addChild(wrapper); _serverContext.addServletMapping("/*", SERVLET_NAME); host.addChild(_serverContext); _serverContext.create(); _serverContext.start(); SwitchYardRemotingServlet remotingServlet = (SwitchYardRemotingServlet) wrapper.getServlet(); remotingServlet.setEndpointPublisher(this); _log.info("Published Remote Service Endpoint " + _serverContext.getPath()); } else { throw new RuntimeException("Context " + _contextName + " already exists!"); } }
protected Realm getRealm(HttpServletRequest request) { String serverName = request.getServerName(); String contextPath = request.getContextPath(); Host host = (Host) engine.findChild(serverName); if (host == null) { // if it cannot find host, then use the default host. host = (Host) engine.findChild(engine.getDefaultHost()); if (host == null) throw new NullPointerException( "Could not find Tomcat host for: " + serverName + " or: " + engine.getDefaultHost()); } Context context = (Context) host.findChild(contextPath); if (context == null) throw new NullPointerException("Could not find Tomcat context for: " + contextPath); Realm realm = context.getRealm(); if (realm == null) throw new NullPointerException( "Could not find Tomcat realm for: " + serverName + "" + contextPath); return realm; }
public void undeploy(String url) { if (host.findChild(url) != null) { this.host.removeChild(host.findChild(url)); log.debug("undeploy web context:" + url); } }
/** Check resources for redeployment and reloading. */ protected synchronized void checkResources(DeployedApplication app) { String[] resources = (String[]) app.redeployResources.keySet().toArray(new String[0]); for (int i = 0; i < resources.length; i++) { File resource = new File(resources[i]); if (log.isDebugEnabled()) log.debug("Checking context[" + app.name + "] redeploy resource " + resource); if (resource.exists()) { long lastModified = ((Long) app.redeployResources.get(resources[i])).longValue(); if ((!resource.isDirectory()) && resource.lastModified() > lastModified) { // Undeploy application if (log.isInfoEnabled()) log.info(sm.getString("hostConfig.undeploy", app.name)); ContainerBase context = (ContainerBase) host.findChild(app.name); try { host.removeChild(context); } catch (Throwable t) { log.warn(sm.getString("hostConfig.context.remove", app.name), t); } try { context.destroy(); } catch (Throwable t) { log.warn(sm.getString("hostConfig.context.destroy", app.name), t); } // Delete other redeploy resources for (int j = i + 1; j < resources.length; j++) { try { File current = new File(resources[j]); current = current.getCanonicalFile(); if ((current .getAbsolutePath() .startsWith(appBase().getAbsolutePath() + File.separator)) || (current.getAbsolutePath().startsWith(configBase().getAbsolutePath()))) { if (log.isDebugEnabled()) log.debug("Delete " + current); ExpandWar.delete(current); } } catch (IOException e) { log.warn(sm.getString("hostConfig.canonicalizing", app.name), e); } } deployed.remove(app.name); return; } } else { // There is a chance the the resource was only missing // temporarily eg renamed during a text editor save try { Thread.sleep(500); } catch (InterruptedException e1) { // Ignore } // Recheck the resource to see if it was really deleted if (resource.exists()) { continue; } long lastModified = ((Long) app.redeployResources.get(resources[i])).longValue(); if (lastModified == 0L) { continue; } // Undeploy application if (log.isInfoEnabled()) log.info(sm.getString("hostConfig.undeploy", app.name)); ContainerBase context = (ContainerBase) host.findChild(app.name); try { host.removeChild(context); } catch (Throwable t) { log.warn(sm.getString("hostConfig.context.remove", app.name), t); } try { context.destroy(); } catch (Throwable t) { log.warn(sm.getString("hostConfig.context.destroy", app.name), t); } // Delete all redeploy resources for (int j = i + 1; j < resources.length; j++) { try { File current = new File(resources[j]); current = current.getCanonicalFile(); if ((current.getAbsolutePath().startsWith(appBase().getAbsolutePath() + File.separator)) || (current.getAbsolutePath().startsWith(configBase().getAbsolutePath()))) { if (log.isDebugEnabled()) log.debug("Delete " + current); ExpandWar.delete(current); } } catch (IOException e) { log.warn(sm.getString("hostConfig.canonicalizing", app.name), e); } } // Delete reload resources as well (to remove any remaining .xml descriptor) String[] resources2 = (String[]) app.reloadResources.keySet().toArray(new String[0]); for (int j = 0; j < resources2.length; j++) { try { File current = new File(resources2[j]); current = current.getCanonicalFile(); if ((current.getAbsolutePath().startsWith(appBase().getAbsolutePath() + File.separator)) || ((current.getAbsolutePath().startsWith(configBase().getAbsolutePath()) && (current.getAbsolutePath().endsWith(".xml"))))) { if (log.isDebugEnabled()) log.debug("Delete " + current); ExpandWar.delete(current); } } catch (IOException e) { log.warn(sm.getString("hostConfig.canonicalizing", app.name), e); } } deployed.remove(app.name); return; } } resources = (String[]) app.reloadResources.keySet().toArray(new String[0]); for (int i = 0; i < resources.length; i++) { File resource = new File(resources[i]); if (log.isDebugEnabled()) log.debug("Checking context[" + app.name + "] reload resource " + resource); long lastModified = ((Long) app.reloadResources.get(resources[i])).longValue(); if ((!resource.exists() && lastModified != 0L) || (resource.lastModified() != lastModified)) { // Reload application if (log.isInfoEnabled()) log.info(sm.getString("hostConfig.reload", app.name)); Container context = host.findChild(app.name); try { ((Lifecycle) context).stop(); } catch (Exception e) { log.warn(sm.getString("hostConfig.context.restart", app.name), e); } // If the context was not started (for example an error // in web.xml) we'll still get to try to start try { ((Lifecycle) context).start(); } catch (Exception e) { log.warn(sm.getString("hostConfig.context.restart", app.name), e); } // Update times app.reloadResources.put(resources[i], new Long(resource.lastModified())); app.timestamp = System.currentTimeMillis(); return; } } }
/** * Check if a webapp is already deployed in this host. * * @param contextPath of the context which will be checked */ protected boolean deploymentExists(String contextPath) { return (deployed.containsKey(contextPath) || (host.findChild(contextPath) != null)); }
/** * @param contextPath * @param contextXml * @param file */ protected void deployDescriptor(String contextPath, File contextXml, String file) { if (deploymentExists(contextPath)) { return; } DeployedApplication deployedApp = new DeployedApplication(contextPath); // Assume this is a configuration descriptor and deploy it if (log.isInfoEnabled()) { log.info(sm.getString("hostConfig.deployDescriptor", file)); } Context context = null; try { synchronized (digester) { try { context = (Context) digester.parse(contextXml); if (context == null) { log.error(sm.getString("hostConfig.deployDescriptor.error", file)); return; } } finally { digester.reset(); } } if (context instanceof Lifecycle) { Class clazz = Class.forName(host.getConfigClass()); LifecycleListener listener = (LifecycleListener) clazz.newInstance(); ((Lifecycle) context).addLifecycleListener(listener); } context.setConfigFile(contextXml.getAbsolutePath()); context.setPath(contextPath); // Add the associated docBase to the redeployed list if it's a WAR boolean isExternalWar = false; boolean isExternal = false; if (context.getDocBase() != null) { File docBase = new File(context.getDocBase()); if (!docBase.isAbsolute()) { docBase = new File(appBase(), context.getDocBase()); } // If external docBase, register .xml as redeploy first if (!docBase.getCanonicalPath().startsWith(appBase().getAbsolutePath() + File.separator)) { isExternal = true; deployedApp.redeployResources.put( contextXml.getAbsolutePath(), new Long(contextXml.lastModified())); deployedApp.redeployResources.put( docBase.getAbsolutePath(), new Long(docBase.lastModified())); if (docBase.getAbsolutePath().toLowerCase().endsWith(".war")) { isExternalWar = true; } } else { log.warn(sm.getString("hostConfig.deployDescriptor.localDocBaseSpecified", docBase)); // Ignore specified docBase context.setDocBase(null); } } host.addChild(context); // Get paths for WAR and expanded WAR in appBase String name = null; String path = context.getPath(); if (path.equals("")) { name = "ROOT"; } else { if (path.startsWith("/")) { name = path.substring(1); } else { name = path; } } File expandedDocBase = new File(appBase(), name); if (context.getDocBase() != null) { // first assume docBase is absolute expandedDocBase = new File(context.getDocBase()); if (!expandedDocBase.isAbsolute()) { // if docBase specified and relative, it must be relative to appBase expandedDocBase = new File(appBase(), context.getDocBase()); } } // Add the eventual unpacked WAR and all the resources which will be // watched inside it if (isExternalWar && unpackWARs) { deployedApp.redeployResources.put( expandedDocBase.getAbsolutePath(), new Long(expandedDocBase.lastModified())); deployedApp.redeployResources.put( contextXml.getAbsolutePath(), new Long(contextXml.lastModified())); addWatchedResources(deployedApp, expandedDocBase.getAbsolutePath(), context); } else { // Find an existing matching war and expanded folder if (!isExternal) { File warDocBase = new File(expandedDocBase.getAbsolutePath() + ".war"); if (warDocBase.exists()) { deployedApp.redeployResources.put( warDocBase.getAbsolutePath(), new Long(warDocBase.lastModified())); } } if (expandedDocBase.exists()) { deployedApp.redeployResources.put( expandedDocBase.getAbsolutePath(), new Long(expandedDocBase.lastModified())); addWatchedResources(deployedApp, expandedDocBase.getAbsolutePath(), context); } else { addWatchedResources(deployedApp, null, context); } // Add the context XML to the list of files which should trigger a redeployment if (!isExternal) { deployedApp.redeployResources.put( contextXml.getAbsolutePath(), new Long(contextXml.lastModified())); } } } catch (Throwable t) { log.error(sm.getString("hostConfig.deployDescriptor.error", file), t); } if (context != null && host.findChild(context.getName()) != null) { deployed.put(contextPath, deployedApp); } }
/** * Remove a webapp from our control. Entry point for the admin webapp, and other JMX Context * controlers. */ public void unmanageApp(String contextPath) { if (isServiced(contextPath)) { deployed.remove(contextPath); host.removeChild(host.findChild(contextPath)); } }
protected Context findContextInternal(String name) { return (Context) host.findChild(name); }