geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject cvs commit: incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/xml LocalEntityResolver.java ParserFactory.java ParserFactoryImpl.java
Date Mon, 19 Jan 2004 06:40:07 GMT
djencks     2004/01/18 22:40:07

  Modified:    modules/deployment/src/java/org/apache/geronimo/deployment/service
                        ServiceDeployer.java ServiceModule.java
               modules/deployment/src/java/org/apache/geronimo/deployment/tools
                        DeployCommand.java
  Added:       modules/deployment/src/java/org/apache/geronimo/deployment
                        BatchDeployerFactory.java
               modules/deployment/src/java/org/apache/geronimo/deployment/util
                        ExplicitDeployment.java
               modules/deployment/src/java/org/apache/geronimo/deployment/xml
                        LocalEntityResolver.java ParserFactory.java
                        ParserFactoryImpl.java
  Log:
  infrastructure and updates for working deployer test framework.  Several of these classes
may be temporary
  
  Revision  Changes    Path
  1.1                  incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/BatchDeployerFactory.java
  
  Index: BatchDeployerFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.deployment;
  
  import java.util.Collection;
  import java.util.ArrayList;
  import java.net.URI;
  import java.io.File;
  
  import org.apache.geronimo.gbean.GBeanInfo;
  import org.apache.geronimo.gbean.GBeanInfoFactory;
  import org.apache.geronimo.gbean.GEndpointInfo;
  import org.apache.geronimo.gbean.GOperationInfo;
  import org.apache.geronimo.kernel.config.ConfigurationParent;
  
  /**
   * TODO this does not put the deployers in any particular order.  This may be a problem.
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/19 06:40:07 $
   *
   * */
  public class BatchDeployerFactory {
  
      private static final GBeanInfo GBEAN_INFO;
  
      private int id = 0;
  
      private static File tmpDir = new File(System.getProperty("java.io.tmpdir"), "geronimo");
  
  
      private Collection deployers;
  
      public Collection getDeployers() {
          return deployers;
      }
  
      public void setDeployers(Collection deployers) {
          this.deployers = deployers;
      }
  
      public BatchDeployer getBatchDeployer(ConfigurationParent configurationParent, URI configID,
File workingDir) {
          return new BatchDeployer(configurationParent, configID, new ArrayList(deployers),
workingDir);
      }
  
      public synchronized File createWorkDir() {
          while (true) {
              File result = new File(tmpDir, "package" + id++);
              if (!result.exists()) {
                  result.mkdirs();
                  return result;
              }
          }
      }
  
  
      static {
          GBeanInfoFactory infoFactory = new GBeanInfoFactory(BatchDeployerFactory.class.getName());
          infoFactory.addOperation(new GOperationInfo("getBatchDeployer", new String[] {ConfigurationParent.class.getName(),
URI.class.getName(), File.class.getName()}));
          infoFactory.addOperation(new GOperationInfo("createWorkDir"));
          infoFactory.addEndpoint(new GEndpointInfo("Deployers", ModuleFactory.class.getName()));
          GBEAN_INFO = infoFactory.getBeanInfo();
      }
  
      public static GBeanInfo getGBeanInfo() {
          return GBEAN_INFO;
      }
  
  }
  
  
  
  1.6       +3 -5      incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/service/ServiceDeployer.java
  
  Index: ServiceDeployer.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/service/ServiceDeployer.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ServiceDeployer.java	17 Jan 2004 17:00:31 -0000	1.5
  +++ ServiceDeployer.java	19 Jan 2004 06:40:07 -0000	1.6
  @@ -55,6 +55,7 @@
    */
   package org.apache.geronimo.deployment.service;
   
  +import java.beans.PropertyEditor;
   import java.net.URI;
   import java.net.URISyntaxException;
   import java.util.ArrayList;
  @@ -64,21 +65,18 @@
   import java.util.List;
   import java.util.Map;
   import java.util.Set;
  -import java.beans.PropertyEditor;
  -import java.beans.PropertyEditorManager;
   
   import javax.management.MalformedObjectNameException;
   import javax.management.ObjectName;
   import javax.xml.parsers.DocumentBuilder;
   
  +import org.apache.geronimo.common.propertyeditor.PropertyEditors;
   import org.apache.geronimo.deployment.DeploymentModule;
   import org.apache.geronimo.deployment.ModuleFactory;
   import org.apache.geronimo.deployment.util.DeploymentHelper;
   import org.apache.geronimo.kernel.deployment.DeploymentException;
   import org.apache.geronimo.kernel.deployment.scanner.URLInfo;
   import org.apache.geronimo.kernel.deployment.service.XMLUtil;
  -import org.apache.geronimo.common.Classes;
  -import org.apache.geronimo.common.propertyeditor.PropertyEditors;
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
   import org.w3c.dom.Node;
  
  
  
  1.3       +8 -8      incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/service/ServiceModule.java
  
  Index: ServiceModule.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/service/ServiceModule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ServiceModule.java	16 Jan 2004 22:19:51 -0000	1.2
  +++ ServiceModule.java	19 Jan 2004 06:40:07 -0000	1.3
  @@ -56,16 +56,16 @@
   package org.apache.geronimo.deployment.service;
   
   import java.io.BufferedInputStream;
  -import java.io.IOException;
  -import java.io.InputStream;
   import java.io.File;
   import java.io.FileInputStream;
  +import java.io.IOException;
  +import java.io.InputStream;
   import java.net.URI;
   import java.net.URL;
   import java.util.Iterator;
  +import java.util.LinkedList;
   import java.util.List;
   import java.util.Map;
  -import java.util.LinkedList;
   import java.util.Set;
   import java.util.zip.ZipEntry;
   import java.util.zip.ZipInputStream;
  @@ -75,12 +75,12 @@
   
   import org.apache.geronimo.deployment.ConfigurationCallback;
   import org.apache.geronimo.deployment.DeploymentModule;
  -import org.apache.geronimo.kernel.deployment.DeploymentException;
  -import org.apache.geronimo.kernel.deployment.scanner.URLInfo;
  -import org.apache.geronimo.kernel.deployment.scanner.URLType;
   import org.apache.geronimo.gbean.GBeanInfo;
   import org.apache.geronimo.gbean.InvalidConfigurationException;
   import org.apache.geronimo.gbean.jmx.GBeanMBean;
  +import org.apache.geronimo.kernel.deployment.DeploymentException;
  +import org.apache.geronimo.kernel.deployment.scanner.URLInfo;
  +import org.apache.geronimo.kernel.deployment.scanner.URLType;
   
   /**
    *
  @@ -203,7 +203,7 @@
   
               GBeanMBean gbean;
               try {
  -                gbean = new GBeanMBean(gbeanInfo);
  +                gbean = new GBeanMBean(gbeanInfo, cl);
               } catch (InvalidConfigurationException e) {
                   throw new DeploymentException("Unable to create GMBean", e);
               }
  
  
  
  1.3       +9 -25     incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/tools/DeployCommand.java
  
  Index: DeployCommand.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/tools/DeployCommand.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DeployCommand.java	17 Jan 2004 03:44:38 -0000	1.2
  +++ DeployCommand.java	19 Jan 2004 06:40:07 -0000	1.3
  @@ -65,20 +65,22 @@
   import java.util.ArrayList;
   import java.util.List;
   import java.util.jar.JarOutputStream;
  +
   import javax.xml.parsers.DocumentBuilder;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
   
   import org.apache.geronimo.deployment.BatchDeployer;
   import org.apache.geronimo.deployment.NoDeployerException;
  +import org.apache.geronimo.deployment.util.FileUtil;
   import org.apache.geronimo.deployment.service.ServiceDeployer;
   import org.apache.geronimo.kernel.deployment.DeploymentException;
   import org.apache.geronimo.kernel.deployment.scanner.URLInfo;
   import org.apache.geronimo.kernel.deployment.scanner.URLType;
   
   /**
  - * 
  - * 
  + *
  + *
    * @version $Revision$ $Date$
    */
   public class DeployCommand {
  @@ -105,7 +107,7 @@
   
       public static void main(String[] args) {
           if (args.length < 3) {
  -            System.err.println("usage: "+DeployCommand.class.getName()+" <configID>
<outfile> <url>+");
  +            System.err.println("usage: " + DeployCommand.class.getName() + " <configID>
<outfile> <url>+");
               System.exit(1);
               throw new AssertionError();
           }
  @@ -134,7 +136,7 @@
           DeployCommand deployer = new DeployCommand(configFile, configID, workDir, deployers);
           int status = 0;
           try {
  -            for (int i=2; i < args.length; i++) {
  +            for (int i = 2; i < args.length; i++) {
                   File source = new File(args[i]);
                   deployer.add(source.toURL());
               }
  @@ -143,11 +145,7 @@
               e.printStackTrace();
               status = 2;
           } finally {
  -            try {
  -                recursiveDelete(workDir);
  -            } catch (IOException e) {
  -                // ignore
  -            }
  +            FileUtil.recursiveDelete(workDir);
           }
           System.exit(status);
       }
  @@ -159,22 +157,8 @@
               deployers.add(new ServiceDeployer(parser));
               return deployers;
           } catch (ParserConfigurationException e) {
  -            throw new AssertionError("Unable to instanciate XML Parser");
  +            throw new AssertionError("Unable to instantiate XML Parser");
           }
       }
   
  -    private static void recursiveDelete(File root) throws IOException {
  -        File[] files = root.listFiles();
  -        if (files != null) {
  -            for (int i = 0; i < files.length; i++) {
  -                File file = files[i];
  -                if (file.isDirectory()) {
  -                    recursiveDelete(file);
  -                } else {
  -                    file.delete();
  -                }
  -            }
  -        }
  -        root.delete();
  -    }
   }
  
  
  
  1.1                  incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/util/ExplicitDeployment.java
  
  Index: ExplicitDeployment.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.deployment.util;
  
  import java.io.File;
  import java.net.URI;
  import java.net.URL;
  
  import javax.management.ObjectName;
  
  import org.apache.geronimo.deployment.BatchDeployer;
  import org.apache.geronimo.deployment.BatchDeployerFactory;
  import org.apache.geronimo.gbean.GAttributeInfo;
  import org.apache.geronimo.gbean.GBean;
  import org.apache.geronimo.gbean.GBeanInfo;
  import org.apache.geronimo.gbean.GBeanInfoFactory;
  import org.apache.geronimo.gbean.GConstructorInfo;
  import org.apache.geronimo.gbean.GEndpointInfo;
  import org.apache.geronimo.gbean.WaitingException;
  import org.apache.geronimo.kernel.KernelMBean;
  import org.apache.geronimo.kernel.config.ConfigurationParent;
  import org.apache.geronimo.kernel.config.NoSuchConfigException;
  import org.apache.geronimo.kernel.deployment.scanner.URLInfo;
  import org.apache.geronimo.kernel.deployment.scanner.URLType;
  
  /**
   *
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/19 06:40:07 $
   *
   * */
  public class ExplicitDeployment implements GBean {
  
      private final static GBeanInfo GBEAN_INFO;
  
      private ConfigurationParent configurationParent;
  
      private URI configID;
  
      private URL packageURL;
  
      private BatchDeployerFactory batchDeployerFactory;
  
      private KernelMBean kernel;
      private ObjectName configName;
  
      public ExplicitDeployment(URI configID, URL packageURL, ConfigurationParent configurationParent,
BatchDeployerFactory batchDeployerFactory, KernelMBean kernel) {
          this.configID = configID;
          this.packageURL = packageURL;
          this.configurationParent = configurationParent;
          this.batchDeployerFactory = batchDeployerFactory;
          this.kernel = kernel;
      }
  
      public ConfigurationParent getConfigurationParent() {
          return configurationParent;
      }
  
      public void setConfigurationParent(ConfigurationParent configurationParent) {
          this.configurationParent = configurationParent;
      }
  
      public URI getConfigID() {
          return configID;
      }
  
      public void setConfigID(URI configID) {
          this.configID = configID;
      }
  
      public URL getPackageURL() {
          return packageURL;
      }
  
      public void setPackageURL(URL packageURL) {
          this.packageURL = packageURL;
      }
  
      public BatchDeployerFactory getBatchDeployerFactory() {
          return batchDeployerFactory;
      }
  
      public void setBatchDeployerFactory(BatchDeployerFactory batchDeployerFactory) {
          this.batchDeployerFactory = batchDeployerFactory;
      }
  
      public KernelMBean getKernel() {
          return kernel;
      }
  
      public void doStart() throws WaitingException, Exception {
          File workDir = batchDeployerFactory.createWorkDir();
          BatchDeployer batchDeployer = batchDeployerFactory.getBatchDeployer(configurationParent,
configID, workDir);
          batchDeployer.addSource(new URLInfo(packageURL, URLType.getType(packageURL)));
          batchDeployer.deploy();
          configName = kernel.load(batchDeployer.getConfiguration(), workDir.toURL());
          kernel.getMBeanServer().invoke(configName, "startRecursive", null, null);
      }
  
      public void doStop() {
          try {
              kernel.getMBeanServer().invoke(configName,  "stopRecursive", null, null);
          } catch (Exception e) {
              //log.info(e);
          }
          try {
              kernel.unload(configName);
          } catch (NoSuchConfigException e) {
              //log.info(e);
          }
          configName = null;
      }
  
      public void doFail() {
          if (configName != null) {
              doStop();
          }
      }
  
      static {
          GBeanInfoFactory infoFactory = new GBeanInfoFactory(ExplicitDeployment.class.getName());
          infoFactory.addAttribute(new GAttributeInfo("ConfigID", true));
          infoFactory.addAttribute(new GAttributeInfo("PackageURL", true));
          infoFactory.addEndpoint(new GEndpointInfo("ConfigurationParent", ConfigurationParent.class.getName()));
          infoFactory.addEndpoint(new GEndpointInfo("BatchDeployerFactory", BatchDeployerFactory.class.getName()));
          infoFactory.addEndpoint(new GEndpointInfo("Kernel", KernelMBean.class.getName()));
          infoFactory.setConstructor(new GConstructorInfo(
                  new String[] {"ConfigID", "PackageURL", "ConfigurationParent", "BatchDeployerFactory",
"Kernel"},
                  new Class[] {URI.class, URL.class, ConfigurationParent.class, BatchDeployerFactory.class,
KernelMBean.class}));
          GBEAN_INFO = infoFactory.getBeanInfo();
      }
  
      public static GBeanInfo getGBeanInfo() {
          return GBEAN_INFO;
      }
  
  }
  
  
  
  1.1                  incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/xml/LocalEntityResolver.java
  
  Index: LocalEntityResolver.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  package org.apache.geronimo.deployment.xml;
  
  import java.io.BufferedInputStream;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.net.URI;
  import java.util.Vector;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.geronimo.gbean.GAttributeInfo;
  import org.apache.geronimo.gbean.GBeanInfo;
  import org.apache.geronimo.gbean.GBeanInfoFactory;
  import org.apache.geronimo.gbean.GConstructorInfo;
  import org.apache.geronimo.gbean.GOperationInfo;
  import org.apache.xml.resolver.Catalog;
  import org.apache.xml.resolver.CatalogEntry;
  import org.apache.xml.resolver.CatalogException;
  import org.apache.xml.resolver.CatalogManager;
  import org.xml.sax.EntityResolver;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  /**
   * Implementation of EntityResolver that looks to the local filesystem.
   *
   * The implementation tries to resolve an entity via the following steps:
   *
   * <ul>
   *   <li>using a catalog file</li>
   *   <li>using a local repository</li>
   *   <li>using JAR files in Classpath</li>
   * </ul>
   *
   * The catalog resolving is based on the OASIS XML Catalog Standard.
   * (see http://www.oasis-open.org/committees/entity/spec-2001-08-01.html
   * and http://www.oasis-open.org/html/a401.htm)
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/19 06:40:07 $
   */
  public class LocalEntityResolver implements EntityResolver {
  
      private static final GBeanInfo GBEAN_INFO;
  
      private static final String INTERNAL_CATALOG = "resolver-catalog.xml";
      /**
       * used Logger
       */
      private static final Log log = LogFactory.getLog(LocalEntityResolver.class);
  
      /**
       * The used Catalog Manager
       */
      private final CatalogManager manager = new CatalogManager();
  
      /**
       * the XML Catalog
       */
      private Catalog catalog = null;
  
      /**
       * the URI of the catalog file
       */
      private URI catalogFileURI = null;
  
      /**
       * Local Repository where DTDs and Schemas are located
       */
      private String localRepository = null;
  
      /**
       * Flag indicating if this resolver may return null to signal
       * the parser to open a regular URI connection to the system
       * identifier. Otherwise an exception is thrown.
       */
      private boolean failOnUnresolvable = false;
  
  
      public LocalEntityResolver(URI catalogFileURI, boolean failOnUnresolvable) {
          this(catalogFileURI, INTERNAL_CATALOG, failOnUnresolvable);
      }
  
      public LocalEntityResolver(URI catalogFileURI, String localRepository, boolean failOnUnresolvable)
{
          this.catalogFileURI = catalogFileURI;
          setLocalRepository(localRepository);
          setFailOnUnresolvable(failOnUnresolvable);
          //setCatalogFile(catalogFile);
          init();
      }
  
      /**
       * Sets the setFailOnUnresolvable flag.
       *
       * @param b value (true means that a SAXException is thrown
       * if the entity could not be resolved)
       */
      public void setFailOnUnresolvable(final boolean b) {
          failOnUnresolvable = b;
      }
  
      public boolean isFailOnUnresolvable() {
          return failOnUnresolvable;
      }
  
      public void setCatalogFileURI(final URI catalogFileURI) {
          this.catalogFileURI = catalogFileURI;
          /*
          try {
              URL url = new URL(catalogFile);
              this.catalogFileURI = new URI(url.toExternalForm());
          } catch (MalformedURLException e) {
              throw new IllegalArgumentException("could not parse url: " + catalogFile);
          } catch (URISyntaxException e) {
              throw new IllegalArgumentException("could not parse url: " + catalogFile);
          }
          */
          init();
      }
  
      public URI getCatalogFileURI() {
          return this.catalogFileURI;
      }
  
      public String getLocalRepository() {
          return localRepository;
      }
  
      public void setLocalRepository(String string) {
          localRepository = string;
      }
  
      public void addPublicMapping(final String publicId, final String uri) {
  
          Vector args = new Vector();
          args.add(publicId);
          args.add(uri);
  
          addEntry("PUBLIC", args);
  
      }
  
      public void addSystemMapping(final String systemId, final String uri) {
  
          Vector args = new Vector();
          args.add(systemId);
          args.add(uri);
  
          addEntry("SYSTEM", args);
  
      }
  
      /**
       * Attempt to resolve the entity based on the supplied publicId and systemId.
       * First the catalog is queried with both publicId and systemId.
       * Then the local repository is queried with the file name part of the systemId
       * Then the classpath is queried with  the file name part of the systemId.
       *
       * Then, if failOnUnresolvable is true, an exception is thrown: otherwise null is returned.
       * @param publicId
       * @param systemId
       * @return
       * @throws SAXException
       * @throws IOException
       */
      public InputSource resolveEntity(
              final String publicId,
              final String systemId)
              throws SAXException, IOException {
  
          if (log.isTraceEnabled()) {
              log.trace(
                      "start resolving for "
                      + entityMessageString(publicId, systemId));
          }
  
          InputSource source = resolveWithCatalog(publicId, systemId);
          if (source != null) {
              return source;
          }
  
          source = resolveWithRepository(publicId, systemId);
          if (source != null) {
              return source;
          }
  
          source = resolveWithClasspath(publicId, systemId);
          if (source != null) {
              return source;
          }
  
          String message =
                  "could not resolve " + entityMessageString(publicId, systemId);
  
          if (failOnUnresolvable) {
              throw new SAXException(message);
          } else {
              log.debug(message);
          }
  
          return null;
      }
  
      /**
       * Try to resolve using the catalog file
       *
       * @param publicId the PublicId
       * @param systemId the SystemId
       * @return InputSource if the entity could be resolved. null otherwise
       * @throws MalformedURLException
       * @throws IOException
       */
      InputSource resolveWithCatalog(
              final String publicId,
              final String systemId)
              throws MalformedURLException, IOException {
  
          if (catalogFileURI == null) {
              return null;
          }
  
          String resolvedSystemId =
                  catalog.resolvePublic(guaranteeNotNull(publicId), systemId);
  
          if (resolvedSystemId != null) {
              if (log.isTraceEnabled()) {
                  log.trace(
                          "resolved "
                          + entityMessageString(publicId, systemId)
                          + " using the catalog file. result: "
                          + resolvedSystemId);
              }
              return new InputSource(resolvedSystemId);
          }
  
          return null;
      }
  
      /**
       * Try to resolve using the local repository and only the supplied systemID filename.
       * Any path in the systemID will be removed.
       *
       * @param publicId the PublicId
       * @param systemId the SystemId
       * @return InputSource if the entity could be resolved. null otherwise
       */
      InputSource resolveWithRepository(
              final String publicId,
              final String systemId) {
  
          if (localRepository == null) {
              return null;
          }
  
          String fileName = getSystemIdFileName(systemId);
  
          if (fileName == null) {
              return null;
          }
  
          String resolvedSystemId = null;
  
          File file = new File(localRepository, fileName);
          if (file.exists()) {
              resolvedSystemId = file.getAbsolutePath();
              if (log.isTraceEnabled()) {
                  log.trace(
                          "resolved "
                          + entityMessageString(publicId, systemId)
                          + "with file relative to "
                          + localRepository
                          + resolvedSystemId);
              }
              return new InputSource(resolvedSystemId);
          }
          return null;
      }
  
      /**
       * Try to resolve using the the classpath and only the supplied systemID.
       * Any path in the systemID will be removed.
       *
       * @param publicId the PublicId
       * @param systemId the SystemId
       * @return InputSource if the entity could be resolved. null otherwise
       */
      InputSource resolveWithClasspath(
              final String publicId,
              final String systemId) {
  
          String fileName = getSystemIdFileName(systemId);
  
          if (fileName == null) {
              return null;
          }
  
          InputStream in =
                  getClass().getClassLoader().getResourceAsStream(fileName);
          if (in != null) {
              if (log.isTraceEnabled()) {
                  log.trace(
                          "resolved "
                          + entityMessageString(publicId, systemId)
                          + " via file found file on classpath: "
                          + fileName);
              }
              InputSource is = new InputSource(new BufferedInputStream(in));
              is.setSystemId(systemId);
              return is;
          }
  
          return null;
      }
  
      /**
       * Guarantees a not null value
       */
      private String guaranteeNotNull(final String string) {
          return string != null ? string : "";
      }
  
      /**
       * Returns the SystemIds filename
       *
       * @param systemId SystemId
       * @return filename
       */
      private String getSystemIdFileName(final String systemId) {
  
          if (systemId == null) {
              return null;
          }
  
          int indexBackSlash = systemId.lastIndexOf("\\");
          int indexSlash = systemId.lastIndexOf("/");
  
          int index = Math.max(indexBackSlash, indexSlash);
  
          String fileName = systemId.substring(index + 1);
          return fileName;
      }
  
      /**
       * Constructs a debugging message string
       */
      private String entityMessageString(
              final String publicId,
              final String systemId) {
  
          StringBuffer buffer = new StringBuffer("entity with publicId '");
          buffer.append(publicId);
          buffer.append("' and systemId '");
          buffer.append(systemId);
          buffer.append("'");
          return buffer.toString();
  
      }
  
      /**
       * Adds a new Entry to the catalog
       */
      private void addEntry(String type, Vector args) {
          try {
              CatalogEntry entry = new CatalogEntry(type, args);
              catalog.addEntry(entry);
          } catch (CatalogException e) {
              throw new RuntimeException(e);
          }
      }
  
      /**
       * Loads mappings from configuration file
       */
      private void init() {
  
          if (log.isDebugEnabled()) {
              log.debug("init catalog file " + this.catalogFileURI);
          }
  
          manager.setUseStaticCatalog(false);
          manager.setCatalogFiles(this.catalogFileURI.toString());
          manager.setIgnoreMissingProperties(true);
          catalog = manager.getCatalog();
      }
  
      static {
          GBeanInfoFactory infoFactory = new GBeanInfoFactory("configurable local entity resolver",
LocalEntityResolver.class.getName());
          infoFactory.addAttribute(new GAttributeInfo("CatalogFileURI", true));
          infoFactory.addAttribute(new GAttributeInfo("LocalRepository", true));
          infoFactory.addAttribute(new GAttributeInfo("FailOnUnresolvable", true));
          infoFactory.addOperation(new GOperationInfo("resolveEntity", new String[]{String.class.getName(),
String.class.getName()}));
          infoFactory.addOperation(new GOperationInfo("addPublicMapping", new String[]{String.class.getName(),
String.class.getName()}));
          infoFactory.addOperation(new GOperationInfo("addSystemMapping", new String[]{String.class.getName(),
String.class.getName()}));
          infoFactory.setConstructor(new GConstructorInfo(new String[]{"CatalogFileURI", "LocalRepository",
"FailOnUnresolvable"},
                  new Class[]{URI.class, String.class, Boolean.TYPE}));
          GBEAN_INFO = infoFactory.getBeanInfo();
      }
  
      public static GBeanInfo getGBeanInfo() {
          return GBEAN_INFO;
      }
  
  }
  
  
  
  1.1                  incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/xml/ParserFactory.java
  
  Index: ParserFactory.java
  ===================================================================
  package org.apache.geronimo.deployment.xml;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.ParserConfigurationException;
  
  /**
   *
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/19 06:40:07 $
   *
   * */
  public interface ParserFactory {
      DocumentBuilder getParser()
              throws ParserConfigurationException;
  }
  
  
  
  1.1                  incubator-geronimo/modules/deployment/src/java/org/apache/geronimo/deployment/xml/ParserFactoryImpl.java
  
  Index: ParserFactoryImpl.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.deployment.xml;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.geronimo.gbean.GBeanInfo;
  import org.apache.geronimo.gbean.GBeanInfoFactory;
  import org.apache.geronimo.gbean.GOperationInfo;
  import org.apache.geronimo.gbean.GEndpointInfo;
  import org.apache.geronimo.gbean.GConstructorInfo;
  import org.xml.sax.EntityResolver;
  import org.xml.sax.ErrorHandler;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  
  /**
   *
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/19 06:40:07 $
   *
   * */
  public class ParserFactoryImpl implements ParserFactory {
  
      private static final Log log = LogFactory.getLog(ParserFactoryImpl.class);
  
      private final static GBeanInfo GBEAN_INFO;
  
      private final DocumentBuilderFactory factory;
      private EntityResolver entityResolver;
  
      public ParserFactoryImpl(EntityResolver entityResolver) {
          this.entityResolver = entityResolver;
          factory = DocumentBuilderFactory.newInstance();
          //sets "http://xml.org/sax/features/namespaces"
          factory.setNamespaceAware(true);
          //sets "http://xml.org/sax/features/validation"
          factory.setValidating(true);
          factory.setAttribute(
                  "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                  "http://www.w3.org/2001/XMLSchema");
          factory.setAttribute("http://apache.org/xml/features/validation/schema",
                  Boolean.TRUE);
      }
  
      public DocumentBuilder getParser()
              throws ParserConfigurationException {
          DocumentBuilder builder = factory.newDocumentBuilder();
          builder.setEntityResolver(entityResolver);
          builder.setErrorHandler(new ErrorHandler() {
              public void error(SAXParseException exception)
                      throws SAXException {
                  log.warn("SAX parse error (ignored)", exception);
                  //throw exception;
              }
  
              public void fatalError(SAXParseException exception)
                      throws SAXException {
                  log.warn("Fatal SAX parse error (ignored)", exception);
                  //throw exception;
              }
  
              public void warning(SAXParseException exception)
                      throws SAXException {
                  log.warn("SAX parse warning", exception);
              }
          });
          return builder;
      }
  
      public EntityResolver getEntityResolver() {
          return entityResolver;
      }
  
      public void setEntityResolver(EntityResolver entityResolver) {
          this.entityResolver = entityResolver;
      }
  
      static {
          GBeanInfoFactory infoFactory = new GBeanInfoFactory("Factory for constructing suitable
configured xml parsers", ParserFactoryImpl.class.getName());
          infoFactory.addOperation(new GOperationInfo("getParser"));
          infoFactory.addEndpoint(new GEndpointInfo("EntityResolver", EntityResolver.class.getName()));
          infoFactory.setConstructor(new GConstructorInfo(
                  new String[] {"EntityResolver"},
                  new Class[] {EntityResolver.class}));
          GBEAN_INFO = infoFactory.getBeanInfo();
      }
  
      public static GBeanInfo getGBeanInfo() {
          return GBEAN_INFO;
      }
  }
  
  
  

Mime
View raw message