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.PropertyEditorSupport;
   21   import java.util.ArrayList;
   22   import java.util.Collection;
   23   import java.util.HashSet;
   24   import java.util.Iterator;
   25   import java.util.LinkedHashSet;
   26   import java.util.List;
   27   import java.util.Set;
   28   
   29   import javax.xml.namespace.QName;
   30   
   31   import org.apache.geronimo.common.DeploymentException;
   32   import org.apache.geronimo.common.propertyeditor.PropertyEditorException;
   33   import org.apache.geronimo.deployment.xbeans.ArtifactType;
   34   import org.apache.geronimo.deployment.xbeans.ClassFilterType;
   35   import org.apache.geronimo.deployment.xbeans.DependenciesType;
   36   import org.apache.geronimo.deployment.xbeans.EnvironmentDocument;
   37   import org.apache.geronimo.deployment.xbeans.EnvironmentType;
   38   import org.apache.geronimo.deployment.xbeans.ImportType;
   39   import org.apache.geronimo.deployment.xbeans.DependencyType;
   40   import org.apache.geronimo.kernel.repository.Artifact;
   41   import org.apache.geronimo.kernel.repository.ClassLoadingRule;
   42   import org.apache.geronimo.kernel.repository.ClassLoadingRules;
   43   import org.apache.geronimo.kernel.repository.Dependency;
   44   import org.apache.geronimo.kernel.repository.Environment;
   45   import org.apache.xmlbeans.XmlException;
   46   import org.apache.xmlbeans.XmlObject;
   47   import org.apache.xmlbeans.XmlOptions;
   48   
   49   /**
   50    * @version $Rev: 723643 $ $Date: 2008-12-05 00:02:26 -0800 (Fri, 05 Dec 2008) $
   51    */
   52   public class EnvironmentBuilder extends PropertyEditorSupport implements XmlAttributeBuilder {
   53       private final static QName QNAME = EnvironmentDocument.type.getDocumentElementName();
   54       private final static String NAMESPACE = QNAME.getNamespaceURI();
   55   
   56       public static Environment buildEnvironment(EnvironmentType environmentType) {
   57           Environment environment = new Environment();
   58           if (environmentType != null) {
   59               if (environmentType.isSetModuleId()) {
   60                   environment.setConfigId(toArtifact(environmentType.getModuleId(), null));
   61               }
   62   
   63               if (environmentType.isSetDependencies()) {
   64                   DependencyType[] dependencyArray = environmentType.getDependencies().getDependencyArray();
   65                   LinkedHashSet dependencies = toDependencies(dependencyArray);
   66                   environment.setDependencies(dependencies);
   67               }
   68               environment.setSuppressDefaultEnvironment(environmentType.isSetSuppressDefaultEnvironment());
   69               
   70               ClassLoadingRulesUtil.configureRules(environment.getClassLoadingRules(), environmentType);
   71           }
   72   
   73           return environment;
   74       }
   75   
   76       public static void mergeEnvironments(Environment environment, Environment additionalEnvironment) {
   77           if (additionalEnvironment != null) {
   78               //TODO merge configIds??
   79               if (environment.getConfigId() == null) {
   80                   environment.setConfigId(additionalEnvironment.getConfigId());
   81               }
   82               environment.addDependencies(additionalEnvironment.getDependencies());
   83               environment.setSuppressDefaultEnvironment(environment.isSuppressDefaultEnvironment() || additionalEnvironment.isSuppressDefaultEnvironment());
   84               
   85               ClassLoadingRules classLoadingRules = environment.getClassLoadingRules();
   86               ClassLoadingRules additionalClassLoadingRules = additionalEnvironment.getClassLoadingRules();
   87               classLoadingRules.merge(additionalClassLoadingRules);
   88           }
   89       }
   90   
   91       public static Environment buildEnvironment(EnvironmentType environmentType, Environment defaultEnvironment) {
   92           Environment environment = buildEnvironment(environmentType);
   93           if (!environment.isSuppressDefaultEnvironment()) {
   94               mergeEnvironments(environment, defaultEnvironment);
   95           }
   96           return environment;
   97       }
   98   
   99       public static EnvironmentType buildEnvironmentType(Environment environment) {
  100           EnvironmentType environmentType = EnvironmentType.Factory.newInstance();
  101           if (environment.getConfigId() != null) {
  102               ArtifactType configId = toArtifactType(environment.getConfigId());
  103               environmentType.setModuleId(configId);
  104           }
  105   
  106           List dependencies = toDependencyTypes(environment.getDependencies());
  107           DependencyType[] dependencyTypes = (DependencyType[]) dependencies.toArray(new DependencyType[dependencies.size()]);
  108           DependenciesType dependenciesType = environmentType.addNewDependencies();
  109           dependenciesType.setDependencyArray(dependencyTypes);
  110           
  111           ClassLoadingRules classLoadingRules = environment.getClassLoadingRules();
  112           if (classLoadingRules.isInverseClassLoading()) {
  113               environmentType.addNewInverseClassloading();
  114           }
  115           
  116           if (environment.isSuppressDefaultEnvironment()) {
  117               environmentType.addNewSuppressDefaultEnvironment();
  118           }
  119           
  120           ClassLoadingRule classLoadingRule = classLoadingRules.getHiddenRule();
  121           environmentType.setHiddenClasses(toFilterType(classLoadingRule.getClassPrefixes()));
  122           
  123           classLoadingRule = classLoadingRules.getNonOverrideableRule();
  124           environmentType.setNonOverridableClasses(toFilterType(classLoadingRule.getClassPrefixes()));
  125   
  126           classLoadingRule = classLoadingRules.getPrivateRule();
  127           environmentType.setPrivateClasses(toFilterType(classLoadingRule.getClassPrefixes()));
  128           
  129           return environmentType;
  130       }
  131   
  132       private static ClassFilterType toFilterType(Set filters) {
  133           String[] classFilters = (String[]) filters.toArray(new String[filters.size()]);
  134           ClassFilterType classFilter = ClassFilterType.Factory.newInstance();
  135           classFilter.setFilterArray(classFilters);
  136           return classFilter;
  137       }
  138   
  139       private static List toDependencyTypes(Collection artifacts) {
  140           List dependencies = new ArrayList();
  141           for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) {
  142               Dependency dependency = (Dependency) iterator.next();
  143               ArtifactType artifactType = toDependencyType(dependency);
  144               dependencies.add(artifactType);
  145           }
  146           return dependencies;
  147       }
  148   
  149       private static ArtifactType toArtifactType(Artifact artifact) {
  150           ArtifactType artifactType = ArtifactType.Factory.newInstance();
  151           fillArtifactType(artifact, artifactType);
  152           return artifactType;
  153       }
  154   
  155       private static void fillArtifactType(Artifact artifact, ArtifactType artifactType) {
  156           if (artifact.getGroupId() != null) {
  157               artifactType.setGroupId(artifact.getGroupId());
  158           }
  159           if (artifact.getArtifactId() != null) {
  160               artifactType.setArtifactId(artifact.getArtifactId());
  161           }
  162           if (artifact.getVersion() != null) {
  163               artifactType.setVersion(artifact.getVersion().toString());
  164           }
  165           if (artifact.getType() != null) {
  166               artifactType.setType(artifact.getType());
  167           }
  168       }
  169   
  170       private static DependencyType toDependencyType(Dependency dependency) {
  171           DependencyType dependencyType = DependencyType.Factory.newInstance();
  172           fillArtifactType(dependency.getArtifact(), dependencyType);
  173   
  174           org.apache.geronimo.kernel.repository.ImportType importType = dependency.getImportType();
  175           if (importType == org.apache.geronimo.kernel.repository.ImportType.CLASSES) {
  176               dependencyType.setImport(ImportType.CLASSES);
  177           } else if (importType == org.apache.geronimo.kernel.repository.ImportType.SERVICES) {
  178               dependencyType.setImport(ImportType.SERVICES);
  179           }
  180   
  181           return dependencyType;
  182       }
  183   
  184       private static Set toFilters(ClassFilterType filterType) {
  185           Set filters = new HashSet();
  186           if (filterType != null) {
  187               String[] filterArray = filterType.getFilterArray();
  188               for (int i = 0; i < filterArray.length; i++) {
  189                   String filter = filterArray[i].trim();
  190                   filters.add(filter);
  191               }
  192           }
  193           return filters;
  194       }
  195   
  196       //package level for testing
  197       static LinkedHashSet toArtifacts(ArtifactType[] artifactTypes) {
  198           LinkedHashSet artifacts = new LinkedHashSet();
  199           for (int i = 0; i < artifactTypes.length; i++) {
  200               ArtifactType artifactType = artifactTypes[i];
  201               Artifact artifact = toArtifact(artifactType, "jar");
  202               artifacts.add(artifact);
  203           }
  204           return artifacts;
  205       }
  206   
  207       private static LinkedHashSet toDependencies(DependencyType[] dependencyArray) {
  208           LinkedHashSet dependencies = new LinkedHashSet();
  209           for (int i = 0; i < dependencyArray.length; i++) {
  210               DependencyType artifactType = dependencyArray[i];
  211               Dependency dependency = toDependency(artifactType);
  212               dependencies.add(dependency);
  213           }
  214           return dependencies;
  215       }
  216   
  217       private static Dependency toDependency(DependencyType dependencyType) {
  218           Artifact artifact = toArtifact(dependencyType, null);
  219           if (ImportType.CLASSES.equals(dependencyType.getImport())) {
  220               return new Dependency(artifact, org.apache.geronimo.kernel.repository.ImportType.CLASSES);
  221           } else if (ImportType.SERVICES.equals(dependencyType.getImport())) {
  222               return new Dependency(artifact, org.apache.geronimo.kernel.repository.ImportType.SERVICES);
  223           } else if (dependencyType.getImport() == null) {
  224               return new Dependency(artifact, org.apache.geronimo.kernel.repository.ImportType.ALL);
  225           } else {
  226               throw new IllegalArgumentException("Unknown import type: " + dependencyType.getImport());
  227           }
  228       }
  229   
  230       private static Artifact toArtifact(ArtifactType artifactType, String defaultType) {
  231           String groupId = artifactType.isSetGroupId() ? trim(artifactType.getGroupId()) : null;
  232           String type = artifactType.isSetType() ? trim(artifactType.getType()) : defaultType;
  233           String artifactId = trim(artifactType.getArtifactId());
  234           String version = artifactType.isSetVersion() ? trim(artifactType.getVersion()) : null;
  235           return new Artifact(groupId, artifactId, version, type);
  236       }
  237   
  238       private static String trim(String string) {
  239           if (string == null || string.length() == 0) {
  240           return null;
  241           }
  242           return string.trim();
  243       }
  244   
  245   
  246       public String getNamespace() {
  247           return NAMESPACE;
  248       }
  249   
  250       public Object getValue(XmlObject xmlObject, String type, ClassLoader cl) throws DeploymentException {
  251   
  252           EnvironmentType environmentType;
  253           if (xmlObject instanceof EnvironmentType) {
  254               environmentType = (EnvironmentType) xmlObject;
  255           } else {
  256               environmentType = (EnvironmentType) xmlObject.copy().changeType(EnvironmentType.type);
  257           }
  258           try {
  259               XmlOptions xmlOptions = new XmlOptions();
  260               xmlOptions.setLoadLineNumbers();
  261               Collection errors = new ArrayList();
  262               xmlOptions.setErrorListener(errors);
  263               if (!environmentType.validate(xmlOptions)) {
  264                   throw new XmlException("Invalid deployment descriptor: " + errors + "\nDescriptor: " + environmentType.toString(), null, errors);
  265               }
  266           } catch (XmlException e) {
  267               throw new DeploymentException(e);
  268           }
  269   
  270           return buildEnvironment(environmentType);
  271       }
  272   
  273       public String getAsText() {
  274           Environment environment = (Environment) getValue();
  275           EnvironmentType environmentType = buildEnvironmentType(environment);
  276           XmlOptions xmlOptions = new XmlOptions();
  277           xmlOptions.setSaveSyntheticDocumentElement(QNAME);
  278           xmlOptions.setSavePrettyPrint();
  279           return environmentType.xmlText(xmlOptions);
  280       }
  281   
  282       public void setAsText(String text) {
  283           try {
  284               EnvironmentDocument environmentDocument = EnvironmentDocument.Factory.parse(text);
  285               EnvironmentType environmentType = environmentDocument.getEnvironment();
  286               Environment environment = (Environment) getValue(environmentType, null, null);
  287               setValue(environment);
  288   
  289           } catch (XmlException e) {
  290               throw new PropertyEditorException(e);
  291           } catch (DeploymentException e) {
  292               throw new PropertyEditorException(e);
  293           }
  294       }
  295   
  296   
  297       //This is added by hand to the xmlAttributeBuilders since it is needed to bootstrap the ServiceConfigBuilder.
  298   }

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