Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.naming.deployment » [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.naming.deployment;
   19   
   20   import java.util.ArrayList;
   21   import java.util.Collections;
   22   import java.util.HashMap;
   23   import java.util.HashSet;
   24   import java.util.Iterator;
   25   import java.util.List;
   26   import java.util.Map;
   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.deployment.service.EnvironmentBuilder;
   33   import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
   34   import org.apache.geronimo.gbean.AbstractName;
   35   import org.apache.geronimo.gbean.AbstractNameQuery;
   36   import org.apache.geronimo.j2ee.annotation.Holder;
   37   import org.apache.geronimo.j2ee.annotation.Injection;
   38   import org.apache.geronimo.j2ee.deployment.Module;
   39   import org.apache.geronimo.j2ee.deployment.NamingBuilder;
   40   import org.apache.geronimo.kernel.config.Configuration;
   41   import org.apache.geronimo.kernel.repository.Artifact;
   42   import org.apache.geronimo.kernel.repository.Dependency;
   43   import org.apache.geronimo.kernel.repository.Environment;
   44   import org.apache.geronimo.kernel.repository.ImportType;
   45   import org.apache.geronimo.schema.NamespaceElementConverter;
   46   import org.apache.geronimo.xbeans.geronimo.naming.GerAbstractNamingEntryDocument;
   47   import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
   48   import org.apache.geronimo.xbeans.geronimo.naming.GerAbstractNamingEntryType;
   49   import org.apache.geronimo.xbeans.javaee.InjectionTargetType;
   50   import org.apache.geronimo.xbeans.javaee.XsdStringType;
   51   import org.apache.xmlbeans.QNameSet;
   52   import org.apache.xmlbeans.SchemaType;
   53   import org.apache.xmlbeans.XmlCursor;
   54   import org.apache.xmlbeans.XmlException;
   55   import org.apache.xmlbeans.XmlObject;
   56   
   57   /**
   58    * @version $Rev: 741679 $ $Date: 2009-02-06 11:07:17 -0800 (Fri, 06 Feb 2009) $
   59    */
   60   public abstract class AbstractNamingBuilder implements NamingBuilder {
   61       protected static final QName BASE_NAMING_QNAME = GerAbstractNamingEntryType.type.getDocumentElementName();
   62       protected static final String J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee";
   63       protected static final String JEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee";
   64       protected static final NamespaceElementConverter J2EE_CONVERTER = new NamespaceElementConverter(J2EE_NAMESPACE);
   65       protected static final NamespaceElementConverter JEE_CONVERTER = new NamespaceElementConverter(JEE_NAMESPACE);
   66       protected static final NamespaceElementConverter NAMING_CONVERTER = new NamespaceElementConverter(GerAbstractNamingEntryDocument.type.getDocumentElementName().getNamespaceURI());
   67   
   68       private final Environment defaultEnvironment;
   69   
   70       protected AbstractNamingBuilder() {
   71           defaultEnvironment = null;
   72       }
   73   
   74       protected AbstractNamingBuilder(Environment defaultEnvironment) {
   75           this.defaultEnvironment = defaultEnvironment;
   76       }
   77   
   78       public Environment getEnvironment() {
   79           return this.defaultEnvironment;
   80       }
   81   
   82       public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) throws DeploymentException {
   83           // TODO Currently this method is called before the xml is metadata complete, so will not contain all refs
   84           // Just always call mergeEnvironment until this is fixed
   85           //
   86           // if (willMergeEnvironment(specDD, plan)) {
   87           EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
   88           // }
   89       }
   90   
   91       protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) throws DeploymentException {
   92           return false;
   93       }
   94   
   95       protected boolean matchesDefaultEnvironment(Environment environment) {
   96           for (Iterator iterator = defaultEnvironment.getDependencies().iterator(); iterator.hasNext();) {
   97               Dependency defaultDependency = (Dependency) iterator.next();
   98               boolean matches = false;
   99               for (Iterator iterator1 = environment.getDependencies().iterator(); iterator1.hasNext();) {
  100                   Dependency actualDependency = (Dependency) iterator1.next();
  101                   if (matches(defaultDependency, actualDependency)) {
  102                       matches = true;
  103                       break;
  104                   }
  105               }
  106               if (!matches) {
  107                   return false;
  108               }
  109           }
  110           return true;
  111       }
  112   
  113       private boolean matches(Dependency defaultDependency, Dependency actualDependency) {
  114           if (defaultDependency.getArtifact().matches(actualDependency.getArtifact())
  115                   || actualDependency.getArtifact().matches(defaultDependency.getArtifact())) {
  116               return defaultDependency.getImportType() == actualDependency.getImportType()
  117                       || actualDependency.getImportType() == ImportType.ALL;
  118           }
  119           return false;
  120       }
  121   
  122       public void initContext(XmlObject specDD, XmlObject plan, Module module) throws DeploymentException {
  123       }
  124       
  125       public int getPriority() {
  126           return NORMAL_PRIORITY;
  127       }
  128       
  129       protected Object lookupJndiContextMap(Map sharedContext, String name) {
  130           Map<String, Object> jndiContext = getJndiContextMap(sharedContext);
  131           return jndiContext.get(name);
  132       }
  133       
  134       protected Map<String, Object> getJndiContextMap(Map sharedContext) {
  135           return NamingBuilder.JNDI_KEY.get(sharedContext);
  136       }
  137   
  138       protected AbstractName getGBeanName(Map sharedContext) {
  139           return GBEAN_NAME_KEY.get(sharedContext);
  140       }
  141   
  142       protected static QNameSet buildQNameSet(String[] eeNamespaces, String localPart) {
  143           Set qnames = new HashSet(eeNamespaces.length);
  144           for (int i = 0; i < eeNamespaces.length; i++) {
  145               String namespace = eeNamespaces[i];
  146               qnames.add(new QName(namespace, localPart));
  147           }
  148           //xmlbeans 2.0 has a bug so forArray doesn't work.  Don't know if it's fixed in later xmlbeans versions
  149           //return QNameSet.forArray(qnames);
  150           return QNameSet.forSets(null, Collections.EMPTY_SET, Collections.EMPTY_SET, qnames);
  151       }
  152   
  153       /**
  154        * @param xmlObjects
  155        * @param converter
  156        * @param type
  157        * @return
  158        * @throws DeploymentException
  159        * @deprecated
  160        */
  161       protected static XmlObject[] convert(XmlObject[] xmlObjects, NamespaceElementConverter converter, SchemaType type) throws DeploymentException {
  162           //bizarre ArrayStoreException if xmlObjects is loaded by the wrong classloader
  163           XmlObject[] converted = new XmlObject[xmlObjects.length];
  164           for (int i = 0; i < xmlObjects.length; i++) {
  165               XmlObject xmlObject = xmlObjects[i].copy();
  166               if (xmlObject.schemaType() != type) {
  167                   converter.convertElement(xmlObject);
  168                   converted[i] = xmlObject.changeType(type);
  169               } else {
  170                   converted[i] = xmlObject;
  171               }
  172               try {
  173                   XmlBeansUtil.validateDD(converted[i]);
  174               } catch (XmlException e) {
  175                   throw new DeploymentException("Could not validate xmlObject of type " + type, e);
  176               }
  177           }
  178           return converted;
  179   
  180       }
  181   
  182       protected static <T extends XmlObject> List<T> convert(XmlObject[] xmlObjects, NamespaceElementConverter converter, Class<T> c, SchemaType type) throws DeploymentException {
  183           //there's probably a better way to say T extends XmlObject and get the type from that
  184           List<T> result = new ArrayList<T>(xmlObjects.length);
  185           for (XmlObject xmlObject : xmlObjects) {
  186               xmlObject = convert(xmlObject, converter, type);
  187               result.add((T) xmlObject);
  188           }
  189           return result;
  190       }
  191   
  192       protected static XmlObject convert(XmlObject xmlObject, NamespaceElementConverter converter, SchemaType type) throws DeploymentException {
  193           Map ns = new HashMap();
  194           XmlCursor cursor = xmlObject.newCursor();
  195           try {
  196               cursor.getAllNamespaces(ns);
  197           } finally {
  198               cursor.dispose();
  199           }
  200           xmlObject = xmlObject.copy();
  201           cursor = xmlObject.newCursor();
  202           cursor.toNextToken();
  203           try {
  204               for (Object o : ns.entrySet()) {
  205                   Map.Entry entry = (Map.Entry) o;
  206                   cursor.insertNamespace((String) entry.getKey(), (String) entry.getValue());
  207               }
  208           } finally {
  209               cursor.dispose();
  210           }
  211   
  212           if (xmlObject.schemaType() != type) {
  213               converter.convertElement(xmlObject);
  214               xmlObject = xmlObject.changeType(type);
  215           }
  216           try {
  217               XmlBeansUtil.validateDD(xmlObject);
  218           } catch (XmlException e) {
  219               throw new DeploymentException("Could not validate xmlObject of type " + type, e);
  220           }
  221           return xmlObject;
  222       }
  223   
  224       protected static String getStringValue(org.apache.geronimo.xbeans.javaee.String string) {
  225           if (string == null) {
  226               return null;
  227           }
  228           String s = string.getStringValue();
  229           return s == null ? null : s.trim();
  230       }
  231   
  232       protected static String getStringValue(XsdStringType string) {
  233           if (string == null) {
  234               return null;
  235           }
  236           String s = string.getStringValue();
  237           return s == null ? null : s.trim();
  238       }
  239   
  240       public static AbstractNameQuery buildAbstractNameQuery(GerPatternType pattern, String type, String moduleType, Set interfaceTypes) {
  241           return ENCConfigBuilder.buildAbstractNameQueryFromPattern(pattern, null, type, moduleType, interfaceTypes);
  242       }
  243   
  244       public static AbstractNameQuery buildAbstractNameQuery(Artifact configId, String module, String name, String type, String moduleType) {
  245           return ENCConfigBuilder.buildAbstractNameQuery(configId, module, name, type, moduleType);
  246       }
  247   
  248       public static Class assureInterface(String interfaceName, String superInterfaceName, String interfaceType, ClassLoader cl) throws DeploymentException {
  249           if (interfaceName == null || interfaceName.equals("")) {
  250               throw new DeploymentException("interface name cannot be blank");
  251           }
  252           Class clazz;
  253           try {
  254               clazz = cl.loadClass(interfaceName);
  255           } catch (ClassNotFoundException e) {
  256               throw new DeploymentException(interfaceType + " interface class not found: " + interfaceName, e);
  257           }
  258           if (!clazz.isInterface()) {
  259               throw new DeploymentException(interfaceType + " interface is not an interface: " + interfaceName);
  260           }
  261           Class superInterface;
  262           try {
  263               superInterface = cl.loadClass(superInterfaceName);
  264           } catch (ClassNotFoundException e) {
  265               throw new DeploymentException("Class " + superInterfaceName + " could not be loaded", e);
  266           }
  267           if (!superInterface.isAssignableFrom(clazz)) {
  268               throw new DeploymentException(interfaceType + " interface does not extend " + superInterfaceName + ": " + interfaceName);
  269           }
  270           return clazz;
  271       }
  272   
  273   
  274       protected void addInjections(String jndiName, InjectionTargetType[] injectionTargetArray, Map sharedContext) {
  275           Holder holder = NamingBuilder.INJECTION_KEY.get(sharedContext);
  276           for (InjectionTargetType injectionTarget : injectionTargetArray) {
  277               String targetName = injectionTarget.getInjectionTargetName().getStringValue().trim();
  278               String targetClassName = injectionTarget.getInjectionTargetClass().getStringValue().trim();
  279               holder.addInjection(targetClassName, new Injection(targetClassName, targetName, jndiName));
  280           }
  281       }
  282   
  283       protected static Artifact[] getConfigId(Configuration localConfiguration, Configuration earConfiguration) {
  284           if (localConfiguration == earConfiguration) {
  285               return new Artifact[] {earConfiguration.getId()};
  286           }
  287           return new Artifact[] {earConfiguration.getId(),localConfiguration.getId()};
  288       }
  289   
  290   
  291       public QName getBaseQName() {
  292           return BASE_NAMING_QNAME;
  293       }
  294   }
  295   
  296   

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.naming.deployment » [javadoc | source]