示例#1
0
  /**
   * @param contextPath
   * @param war
   * @param file
   */
  protected void deployWAR(String contextPath, File war, String file) {

    if (deploymentExists(contextPath)) return;

    // Checking for a nested /META-INF/context.xml
    JarFile jar = null;
    JarEntry entry = null;
    InputStream istream = null;
    BufferedOutputStream ostream = null;
    File xml = new File(configBase, file.substring(0, file.lastIndexOf(".")) + ".xml");
    if (deployXML && !xml.exists()) {
      try {
        jar = new JarFile(war);
        entry = jar.getJarEntry(Constants.ApplicationContextXml);
        if (entry != null) {
          istream = jar.getInputStream(entry);

          configBase.mkdirs();

          ostream = new BufferedOutputStream(new FileOutputStream(xml), 1024);
          byte buffer[] = new byte[1024];
          while (true) {
            int n = istream.read(buffer);
            if (n < 0) {
              break;
            }
            ostream.write(buffer, 0, n);
          }
          ostream.flush();
          ostream.close();
          ostream = null;
          istream.close();
          istream = null;
          entry = null;
          jar.close();
          jar = null;
        }
      } catch (Exception e) {
        // Ignore and continue
        if (ostream != null) {
          try {
            ostream.close();
          } catch (Throwable t) {;
          }
          ostream = null;
        }
        if (istream != null) {
          try {
            istream.close();
          } catch (Throwable t) {;
          }
          istream = null;
        }
      } finally {
        entry = null;
        if (jar != null) {
          try {
            jar.close();
          } catch (Throwable t) {;
          }
          jar = null;
        }
      }
    }

    DeployedApplication deployedApp = new DeployedApplication(contextPath);

    // Deploy the application in this WAR file
    if (log.isInfoEnabled()) log.info(sm.getString("hostConfig.deployJar", file));

    try {
      Context context = null;
      if (deployXML && xml.exists()) {
        synchronized (digester) {
          try {
            context = (Context) digester.parse(xml);
            if (context == null) {
              log.error(sm.getString("hostConfig.deployDescriptor.error", file));
              return;
            }
          } finally {
            digester.reset();
          }
        }
        context.setConfigFile(xml.getAbsolutePath());
      } else {
        context = (Context) Class.forName(contextClass).newInstance();
      }

      // Populate redeploy resources with the WAR file
      deployedApp.redeployResources.put(war.getAbsolutePath(), new Long(war.lastModified()));

      if (deployXML && xml.exists()) {
        deployedApp.redeployResources.put(xml.getAbsolutePath(), new Long(xml.lastModified()));
      }

      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);
      host.addChild(context);
      // If we're unpacking WARs, the docBase will be mutated after
      // starting the context
      if (unpackWARs && (context.getDocBase() != null)) {
        String name = null;
        String path = context.getPath();
        if (path.equals("")) {
          name = "ROOT";
        } else {
          if (path.startsWith("/")) {
            name = path.substring(1);
          } else {
            name = path;
          }
        }
        name = name.replace('/', '#');
        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);
      }
    } catch (Throwable t) {
      log.error(sm.getString("hostConfig.deployJar.error", file), t);
    }

    deployed.put(contextPath, deployedApp);
  }
示例#2
0
  /**
   * @param contextPath
   * @param dir
   * @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.isInfoEnabled()) log.info(sm.getString("hostConfig.deployDir", file));
    try {
      Context context = null;
      File xml = new File(dir, Constants.ApplicationContextXml);
      File xmlCopy = null;
      if (deployXML && xml.exists()) {
        // Will only do this on initial deployment. On subsequent
        // deployments the copied xml file means we'll use
        // deployDescriptor() instead
        synchronized (digester) {
          try {
            context = (Context) digester.parse(xml);
            if (context == null) {
              log.error(sm.getString("hostConfig.deployDescriptor.error", xml));
              return;
            }
          } finally {
            digester.reset();
          }
        }
        configBase.mkdirs();
        xmlCopy = new File(configBase, file + ".xml");
        InputStream is = null;
        OutputStream os = null;
        try {
          is = new FileInputStream(xml);
          os = new FileOutputStream(xmlCopy);
          IOTools.flow(is, os);
          // Don't catch IOE - let the outer try/catch handle it
        } finally {
          try {
            if (is != null) is.close();
          } catch (IOException e) {
            // Ignore
          }
          try {
            if (os != null) os.close();
          } catch (IOException e) {
            // Ignore
          }
        }
        context.setConfigFile(xmlCopy.getAbsolutePath());
      } else {
        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);
      host.addChild(context);
      deployedApp.redeployResources.put(dir.getAbsolutePath(), new Long(dir.lastModified()));
      if (xmlCopy != null) {
        deployedApp.redeployResources.put(
            xmlCopy.getAbsolutePath(), new Long(xmlCopy.lastModified()));
      }
      addWatchedResources(deployedApp, dir.getAbsolutePath(), context);
    } catch (Throwable t) {
      log.error(sm.getString("hostConfig.deployDir.error", file), t);
    }

    deployed.put(contextPath, deployedApp);
  }
示例#3
0
  /**
   * @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);
    }
  }