📄 hostconfig.java
字号:
* @param file
*/
protected void deployDirectory(String contextPath, File dir, String file) {
DeployedApplication deployedApp = new DeployedApplication(contextPath);
if (deploymentExists(contextPath))
return;
// Deploy the application in this directory
if( log.isDebugEnabled() )
log.debug(sm.getString("hostConfig.deployDir", file));
try {
Context context = (Context) Class.forName(contextClass).newInstance();
if (context instanceof Lifecycle) {
Class clazz = Class.forName(host.getConfigClass());
LifecycleListener listener =
(LifecycleListener) clazz.newInstance();
((Lifecycle) context).addLifecycleListener(listener);
}
context.setPath(contextPath);
context.setDocBase(file);
File configFile = new File(dir, Constants.ApplicationContextXml);
if (deployXML) {
context.setConfigFile(configFile.getAbsolutePath());
}
host.addChild(context);
deployedApp.redeployResources.put(dir.getAbsolutePath(),
new Long(dir.lastModified()));
if (deployXML) {
deployedApp.redeployResources.put(configFile.getAbsolutePath(),
new Long(configFile.lastModified()));
}
addWatchedResources(deployedApp, dir.getAbsolutePath(), context);
} catch (Throwable t) {
log.error(sm.getString("hostConfig.deployDir.error", file), t);
}
deployed.put(contextPath, deployedApp);
}
/**
* 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));
}
/**
* Add watched resources to the specified Context.
* @param app HostConfig deployed app
* @param docBase web app docBase
* @param context web application context
*/
protected void addWatchedResources(DeployedApplication app, String docBase, Context context) {
// FIXME: Feature idea. Add support for patterns (ex: WEB-INF/*, WEB-INF/*.xml), where
// we would only check if at least one resource is newer than app.timestamp
File docBaseFile = null;
if (docBase != null) {
docBaseFile = new File(docBase);
if (!docBaseFile.isAbsolute()) {
docBaseFile = new File(appBase(), docBase);
}
}
String[] watchedResources = context.findWatchedResources();
for (int i = 0; i < watchedResources.length; i++) {
File resource = new File(watchedResources[i]);
if (!resource.isAbsolute()) {
if (docBase != null) {
resource = new File(docBaseFile, watchedResources[i]);
} else {
continue;
}
}
app.reloadResources.put(resource.getAbsolutePath(),
new Long(resource.lastModified()));
}
}
/**
* 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()))
|| (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 {
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()))
|| (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()))
|| ((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;
}
}
}
/**
* Process a "start" event for this Host.
*/
public void start() {
if (log.isDebugEnabled())
log.debug(sm.getString("hostConfig.start"));
try {
ObjectName hostON = new ObjectName(host.getObjectName());
oname = new ObjectName
(hostON.getDomain() + ":type=Deployer,host=" + host.getName());
Registry.getRegistry(null, null).registerComponent
(this, oname, this.getClass().getName());
} catch (Exception e) {
log.error(sm.getString("hostConfig.jmx.register", oname), e);
}
if (host.getDeployOnStartup())
deployApps();
}
/**
* Process a "stop" event for this Host.
*/
public void stop() {
if (log.isDebugEnabled())
log.debug(sm.getString("hostConfig.stop"));
undeployApps();
if (oname != null) {
try {
Registry.getRegistry(null, null).unregisterComponent(oname);
} catch (Exception e) {
log.error(sm.getString("hostConfig.jmx.unregister", oname), e);
}
}
oname = null;
appBase = null;
configBase = null;
}
/**
* 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();
}
/**
* Check status of all webapps.
*/
protected void check() {
if (host.getAutoDeploy()) {
// Check for resources modification to trigger redeployment
DeployedApplication[] apps =
(DeployedApplication[]) deployed.values().toArray(new DeployedApplication[0]);
for (int i = 0; i < apps.length; i++) {
if (!isServiced(apps[i].name))
checkResources(apps[i]);
}
// Hotdeploy applications
deployApps();
}
}
/**
* Check status of a specific webapp, for use with stuff like management webapps.
*/
public void check(String name) {
DeployedApplication app = (DeployedApplication) deployed.get(name);
if (app != null) {
checkResources(app);
} else {
deployApps(name);
}
}
/**
* Add a new Context to be managed by us.
* Entry point for the admin webapp, and other JMX Context controlers.
*/
public void manageApp(Context context) {
String contextPath = context.getPath();
if (deployed.containsKey(contextPath))
return;
DeployedApplication deployedApp = new DeployedApplication(contextPath);
// Add the associated docBase to the redeployed list if it's a WAR
boolean isWar = false;
if (context.getDocBase() != null) {
File docBase = new File(context.getDocBase());
if (!docBase.isAbsolute()) {
docBase = new File(appBase(), context.getDocBase());
}
deployedApp.redeployResources.put(docBase.getAbsolutePath(),
new Long(docBase.lastModified()));
if (docBase.getAbsolutePath().toLowerCase().endsWith(".war")) {
isWar = true;
}
}
host.addChild(context);
// Add the eventual unpacked WAR and all the resources which will be
// watched inside it
if (isWar && unpackWARs) {
String name = null;
String path = context.getPath();
if (path.equals("")) {
name = "ROOT";
} else {
if (path.startsWith("/")) {
name = path.substring(1);
} else {
name = path;
}
}
File docBase = new File(name);
if (!docBase.isAbsolute()) {
docBase = new File(appBase(), name);
}
deployedApp.redeployResources.put(docBase.getAbsolutePath(),
new Long(docBase.lastModified()));
addWatchedResources(deployedApp, docBase.getAbsolutePath(), context);
} else {
addWatchedResources(deployedApp, null, context);
}
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));
}
}
// ----------------------------------------------------- Instance Variables
/**
* This class represents the state of a deployed application, as well as
* the monitored resources.
*/
protected class DeployedApplication {
public DeployedApplication(String name) {
this.name = name;
}
/**
* Application context path. The assertion is that
* (host.getChild(name) != null).
*/
public String name;
/**
* Any modification of the specified (static) resources will cause a
* redeployment of the application. If any of the specified resources is
* removed, the application will be undeployed. Typically, this will
* contain resources like the context.xml file, a compressed WAR path.
* The value is the last modification time.
*/
public LinkedHashMap redeployResources = new LinkedHashMap();
/**
* Any modification of the specified (static) resources will cause a
* reload of the application. This will typically contain resources
* such as the web.xml of a webapp, but can be configured to contain
* additional descriptors.
* The value is the last modification time.
*/
public HashMap reloadResources = new HashMap();
/**
* Instant where the application was last put in service.
*/
public long timestamp = System.currentTimeMillis();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -