Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.service » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package org.apache.geronimo.deployment.service;
   19   
   20   import java.beans.PropertyEditorManager;
   21   import java.io.File;
   22   import java.io.FileNotFoundException;
   23   import java.io.IOException;
   24   import java.net.URI;
   25   import java.net.URL;
   26   import java.util.Collection;
   27   import java.util.Collections;
   28   import java.util.Map;
   29   import java.util.HashMap;
   30   import java.util.jar.JarFile;
   31   
   32   import javax.xml.namespace.QName;
   33   
   34   import org.apache.geronimo.common.DeploymentException;
   35   import org.apache.geronimo.deployment.ConfigurationBuilder;
   36   import org.apache.geronimo.deployment.DeploymentContext;
   37   import org.apache.geronimo.deployment.ModuleIDBuilder;
   38   import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
   39   import org.apache.geronimo.deployment.NamespaceDrivenBuilderCollection;
   40   import org.apache.geronimo.deployment.util.DeploymentUtil;
   41   import org.apache.geronimo.deployment.xbeans.ArtifactType;
   42   import org.apache.geronimo.deployment.xbeans.EnvironmentType;
   43   import org.apache.geronimo.deployment.xbeans.ModuleDocument;
   44   import org.apache.geronimo.deployment.xbeans.ModuleType;
   45   import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
   46   import org.apache.geronimo.gbean.AbstractName;
   47   import org.apache.geronimo.gbean.GBeanInfo;
   48   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   49   import org.apache.geronimo.gbean.GBeanLifecycle;
   50   import org.apache.geronimo.kernel.Kernel;
   51   import org.apache.geronimo.kernel.Naming;
   52   import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
   53   import org.apache.geronimo.kernel.config.ConfigurationManager;
   54   import org.apache.geronimo.kernel.config.ConfigurationModuleType;
   55   import org.apache.geronimo.kernel.config.ConfigurationStore;
   56   import org.apache.geronimo.kernel.config.ConfigurationUtil;
   57   import org.apache.geronimo.kernel.config.SimpleConfigurationManager;
   58   import org.apache.geronimo.kernel.repository.Artifact;
   59   import org.apache.geronimo.kernel.repository.ArtifactResolver;
   60   import org.apache.geronimo.kernel.repository.Environment;
   61   import org.apache.geronimo.kernel.repository.Repository;
   62   import org.apache.xmlbeans.XmlCursor;
   63   import org.apache.xmlbeans.XmlException;
   64   import org.apache.xmlbeans.XmlObject;
   65   
   66   /**
   67    * @version $Rev: 741679 $ $Date: 2009-02-06 11:07:17 -0800 (Fri, 06 Feb 2009) $
   68    */
   69   public class ServiceConfigBuilder implements ConfigurationBuilder, GBeanLifecycle {
   70       private final Environment defaultEnvironment;
   71       private final Collection repositories;
   72   
   73       private static final QName MODULE_QNAME = ModuleDocument.type.getDocumentElementName();
   74       public static final String SERVICE_MODULE = "ServiceModule";
   75       private static final Map<String, String> NAMESPACE_UPDATES = new HashMap<String, String>();
   76       static {
   77           NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment", "http://geronimo.apache.org/xml/ns/deployment-1.2");
   78           NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment-1.1", "http://geronimo.apache.org/xml/ns/deployment-1.2");
   79           NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment/javabean", "http://geronimo.apache.org/xml/ns/deployment/javabean-1.0");
   80       }
   81   
   82       private final Naming naming;
   83       private final ConfigurationManager configurationManager;
   84       private final NamespaceDrivenBuilderCollection serviceBuilders;
   85   
   86       public ServiceConfigBuilder(Environment defaultEnvironment, Collection repositories, Naming naming) {
   87           this(defaultEnvironment, repositories, Collections.EMPTY_LIST, naming, null);
   88       }
   89   
   90       public ServiceConfigBuilder(Environment defaultEnvironment, Collection repositories, Collection<NamespaceDrivenBuilder> serviceBuilders, Kernel kernel) {
   91           this(defaultEnvironment, repositories, serviceBuilders, kernel.getNaming(), ConfigurationUtil.getConfigurationManager(kernel));
   92       }
   93   
   94       public ServiceConfigBuilder(Environment defaultEnvironment, Collection repositories, Collection<NamespaceDrivenBuilder> serviceBuilders, Naming naming) {
   95           this(defaultEnvironment, repositories, serviceBuilders, naming, null);
   96       }
   97   
   98       public void doStart() throws Exception {
   99           XmlBeansUtil.registerNamespaceUpdates(NAMESPACE_UPDATES);
  100       }
  101   
  102       public void doStop() {
  103           XmlBeansUtil.unregisterNamespaceUpdates(NAMESPACE_UPDATES);
  104       }
  105   
  106       public void doFail() {
  107           doStop();
  108       }
  109   
  110       private ServiceConfigBuilder(Environment defaultEnvironment, Collection repositories, Collection<NamespaceDrivenBuilder> serviceBuilders, Naming naming, ConfigurationManager configurationManager) {
  111           this.naming = naming;
  112           this.configurationManager = configurationManager;
  113   
  114           EnvironmentBuilder environmentBuilder = new EnvironmentBuilder();
  115           this.defaultEnvironment = defaultEnvironment;
  116   
  117           this.repositories = repositories;
  118           this.serviceBuilders = new NamespaceDrivenBuilderCollection(serviceBuilders);
  119       }
  120   
  121       public Object getDeploymentPlan(File planFile, JarFile jarFile, ModuleIDBuilder idBuilder) throws DeploymentException {
  122           if (planFile == null && jarFile == null) {
  123               return null;
  124           }
  125   
  126           try {
  127               XmlObject xmlObject;
  128               if (planFile != null) {
  129                   xmlObject = XmlBeansUtil.parse(planFile.toURL(), getClass().getClassLoader());
  130               } else {
  131                   URL path = DeploymentUtil.createJarURL(jarFile, "META-INF/geronimo-service.xml");
  132                   try {
  133                       xmlObject = XmlBeansUtil.parse(path, getClass().getClassLoader());
  134                   } catch (FileNotFoundException e) {
  135                       // It has a JAR but no plan, and nothing at META-INF/geronimo-service.xml,
  136                       // therefore it's not a service deployment
  137                       return null;
  138                   }
  139               }
  140               if(xmlObject == null) {
  141                   return null;
  142               }
  143   
  144               XmlCursor cursor = xmlObject.newCursor();
  145               try {
  146                   cursor.toFirstChild();
  147                   if (!MODULE_QNAME.equals(cursor.getName())) {
  148                       return null;
  149                   }
  150               } finally {
  151                   cursor.dispose();
  152               }
  153               ModuleDocument moduleDoc;
  154               if (xmlObject instanceof ModuleDocument) {
  155                   moduleDoc = (ModuleDocument) xmlObject;
  156               } else {
  157                   moduleDoc = (ModuleDocument) xmlObject.changeType(ModuleDocument.type);
  158               }
  159               XmlBeansUtil.validateDD(moduleDoc);
  160               // If there's no artifact ID and we won't be able to figure one out later, use the plan file name.  Bit of a hack.
  161               if(jarFile == null && (moduleDoc.getModule().getEnvironment() == null ||
  162                           moduleDoc.getModule().getEnvironment().getModuleId() == null ||
  163                           moduleDoc.getModule().getEnvironment().getModuleId().getArtifactId() == null)) {
  164                   if(moduleDoc.getModule().getEnvironment() == null) {
  165                       moduleDoc.getModule().addNewEnvironment();
  166                   }
  167                   if(moduleDoc.getModule().getEnvironment().getModuleId() == null) {
  168                       moduleDoc.getModule().getEnvironment().addNewModuleId();
  169                   }
  170                   String name = planFile.getName();
  171                   int pos = name.lastIndexOf('.');
  172                   if(pos > -1) {
  173                       name = name.substring(0, pos);
  174                   }
  175                   moduleDoc.getModule().getEnvironment().getModuleId().setArtifactId(name);
  176               }
  177               return moduleDoc.getModule();
  178           } catch (XmlException e) {
  179               throw new DeploymentException("Could not parse xml in plan", e);
  180           } catch (IOException e) {
  181               throw new DeploymentException("no plan at " + planFile, e);
  182           }
  183       }
  184   
  185       public Artifact getConfigurationID(Object plan, JarFile module, ModuleIDBuilder idBuilder) throws IOException, DeploymentException {
  186           ModuleType configType = (ModuleType) plan;
  187           EnvironmentType environmentType = configType.getEnvironment();
  188           Environment environment = EnvironmentBuilder.buildEnvironment(environmentType, defaultEnvironment);
  189           idBuilder.resolve(environment, module == null ? "" : new File(module.getName()).getName(), "car");
  190           if(!environment.getConfigId().isResolved()) {
  191               throw new IllegalStateException("Service Module ID is not fully populated ("+environment.getConfigId()+")");
  192           }
  193           return environment.getConfigId();
  194       }
  195   
  196       public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, Object plan, JarFile jar, Collection configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws IOException, DeploymentException {
  197           ModuleType configType = (ModuleType) plan;
  198   
  199           return buildConfiguration(inPlaceDeployment, configId, configType, jar, configurationStores, artifactResolver, targetConfigurationStore);
  200       }
  201   
  202       public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, ModuleType moduleType, JarFile jar, Collection configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws DeploymentException, IOException {
  203           ArtifactType type = moduleType.getEnvironment().isSetModuleId() ? moduleType.getEnvironment().getModuleId() : moduleType.getEnvironment().addNewModuleId();
  204           type.setArtifactId(configId.getArtifactId());
  205           type.setGroupId(configId.getGroupId());
  206           type.setType(configId.getType());
  207           type.setVersion(configId.getVersion().toString());
  208           Environment environment = EnvironmentBuilder.buildEnvironment(moduleType.getEnvironment(), defaultEnvironment);
  209           if(!environment.getConfigId().isResolved()) {
  210               throw new IllegalStateException("Module ID should be fully resolved by now (not "+environment.getConfigId()+")");
  211           }
  212           File outfile;
  213           try {
  214               outfile = targetConfigurationStore.createNewConfigurationDir(configId);
  215           } catch (ConfigurationAlreadyExistsException e) {
  216               throw new DeploymentException(e);
  217           }
  218   
  219           DeploymentContext context = null;
  220           try {
  221               ConfigurationManager configurationManager = this.configurationManager;
  222               if (configurationManager == null) {
  223                   configurationManager = new SimpleConfigurationManager(configurationStores, artifactResolver, repositories);
  224               }
  225   
  226               AbstractName moduleName = naming.createRootName(configId, configId.toString(), SERVICE_MODULE);
  227               context = new DeploymentContext(outfile,
  228                       inPlaceDeployment && null != jar ? DeploymentUtil.toFile(jar) : null,
  229                       environment,
  230                       moduleName,
  231                       ConfigurationModuleType.SERVICE,
  232                       naming,
  233                       configurationManager,
  234                       repositories);
  235               if(jar != null) {
  236                   File file = new File(jar.getName());
  237                   context.addIncludeAsPackedJar(URI.create(file.getName()), jar);
  238               }
  239   
  240               serviceBuilders.build(moduleType, context, context);
  241               return context;
  242           } catch (DeploymentException de) {
  243               cleanupAfterFailedBuild(context, outfile);
  244               throw de;
  245           } catch (IOException ie) {
  246               cleanupAfterFailedBuild(context, outfile);
  247               throw ie;
  248           } catch (RuntimeException re) {
  249               cleanupAfterFailedBuild(context, outfile);
  250               throw re;
  251           } catch (Error e) {
  252               cleanupAfterFailedBuild(context, outfile);
  253               throw e;
  254           }
  255       }
  256   
  257       private void cleanupAfterFailedBuild(DeploymentContext context, File directory) {
  258           try {
  259               if (context !=null) {
  260                   context.close();
  261               }
  262           } catch (DeploymentException de) {
  263               // ignore error on cleanup
  264           } catch (IOException ioe) {
  265               // ignore error on cleanu
  266           }
  267           if (directory != null) {
  268               DeploymentUtil.recursiveDelete(directory);
  269           }
  270       }
  271   
  272       public static final GBeanInfo GBEAN_INFO;
  273   
  274       static {
  275           PropertyEditorManager.registerEditor(Environment.class, EnvironmentBuilder.class);
  276   
  277           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(ServiceConfigBuilder.class, CONFIG_BUILDER);
  278   
  279           infoBuilder.addInterface(ConfigurationBuilder.class);
  280   
  281           infoBuilder.addAttribute("defaultEnvironment", Environment.class, true);
  282           infoBuilder.addReference("Repository", Repository.class, "Repository");
  283           infoBuilder.addReference("ServiceBuilders", NamespaceDrivenBuilder.class, "ModuleBuilder");
  284           infoBuilder.addAttribute("kernel", Kernel.class, false, false);
  285   
  286           infoBuilder.setConstructor(new String[]{"defaultEnvironment", "Repository", "ServiceBuilders", "kernel"});
  287   
  288           GBEAN_INFO = infoBuilder.getBeanInfo();
  289       }
  290   
  291       public static GBeanInfo getGBeanInfo() {
  292           return GBEAN_INFO;
  293       }
  294   
  295   }

Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.service » [javadoc | source]