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.util.Collection;
   22   import java.util.HashMap;
   23   import java.util.Map;
   24   
   25   import javax.xml.namespace.QName;
   26   
   27   import org.apache.geronimo.common.DeploymentException;
   28   import org.apache.geronimo.deployment.DeploymentContext;
   29   import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
   30   import org.apache.geronimo.deployment.xbeans.AttributeType;
   31   import org.apache.geronimo.deployment.xbeans.GbeanDocument;
   32   import org.apache.geronimo.deployment.xbeans.GbeanType;
   33   import org.apache.geronimo.deployment.xbeans.PatternType;
   34   import org.apache.geronimo.deployment.xbeans.ReferenceType;
   35   import org.apache.geronimo.deployment.xbeans.ReferencesType;
   36   import org.apache.geronimo.deployment.xbeans.ServiceDocument;
   37   import org.apache.geronimo.deployment.xbeans.XmlAttributeType;
   38   import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
   39   import org.apache.geronimo.gbean.AbstractName;
   40   import org.apache.geronimo.gbean.GBeanData;
   41   import org.apache.geronimo.gbean.GBeanInfo;
   42   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   43   import org.apache.geronimo.gbean.GBeanInfoFactory;
   44   import org.apache.geronimo.gbean.MultiGBeanInfoFactory;
   45   import org.apache.geronimo.gbean.ReferenceMap;
   46   import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
   47   import org.apache.geronimo.kernel.repository.Environment;
   48   import org.apache.xmlbeans.QNameSet;
   49   import org.apache.xmlbeans.XmlException;
   50   import org.apache.xmlbeans.XmlObject;
   51   
   52   /**
   53    * @version $Rev: 741679 $ $Date: 2009-02-06 11:07:17 -0800 (Fri, 06 Feb 2009) $
   54    */
   55   public class GBeanBuilder implements NamespaceDrivenBuilder {
   56       protected Map attrRefMap;
   57       protected Map refRefMap;
   58       private final GBeanInfoFactory infoFactory;
   59       public static final QName SERVICE_QNAME = ServiceDocument.type.getDocumentElementName();
   60       private static final QName GBEAN_QNAME = GbeanDocument.type.getDocumentElementName();
   61       private static final QNameSet GBEAN_QNAME_SET = QNameSet.singleton(GBEAN_QNAME);
   62   
   63       public GBeanBuilder(Collection xmlAttributeBuilders, Collection xmlReferenceBuilders) {
   64           if (xmlAttributeBuilders != null) {
   65               ReferenceMap.Key key = new ReferenceMap.Key() {
   66   
   67                   public Object getKey(Object object) {
   68                       return ((XmlAttributeBuilder) object).getNamespace();
   69                   }
   70               };
   71               attrRefMap = new ReferenceMap(xmlAttributeBuilders, new HashMap(), key);
   72           } else {
   73               attrRefMap = new HashMap();
   74           }
   75   
   76           if (xmlReferenceBuilders != null) {
   77               ReferenceMap.Key key = new ReferenceMap.Key() {
   78   
   79                   public Object getKey(Object object) {
   80                       return ((XmlReferenceBuilder) object).getNamespace();
   81                   }
   82               };
   83               refRefMap = new ReferenceMap(xmlReferenceBuilders, new HashMap(), key);
   84           }
   85           EnvironmentBuilder environmentBuilder = new EnvironmentBuilder();
   86           attrRefMap.put(environmentBuilder.getNamespace(), environmentBuilder);
   87           
   88           infoFactory = newGBeanInfoFactory();
   89       }
   90   
   91       protected GBeanInfoFactory newGBeanInfoFactory() {
   92           return new MultiGBeanInfoFactory();
   93       }
   94   
   95       public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException {
   96       }
   97   
   98       public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext) throws DeploymentException {
   99           XmlObject[] items = container.selectChildren(GBEAN_QNAME_SET);
  100           for (int i = 0; i < items.length; i++) {
  101               GbeanType gbean;
  102               try {
  103                   gbean = (GbeanType) XmlBeansUtil.typedCopy(items[i], GbeanType.type);
  104               } catch (XmlException e) {
  105                   throw new DeploymentException("Could not validate gbean xml", e);
  106               }
  107               addGBeanData(gbean, moduleContext.getModuleName(), moduleContext.getClassLoader(), moduleContext);
  108           }
  109       }
  110   
  111       private AbstractName addGBeanData(GbeanType gbean, AbstractName moduleName, ClassLoader cl, DeploymentContext context) throws DeploymentException {
  112           GBeanInfo gBeanInfo = infoFactory.getGBeanInfo(gbean.getClass1(), cl);
  113           String namePart = gbean.getName();
  114           String j2eeType = gBeanInfo.getJ2eeType();
  115           AbstractName abstractName = context.getNaming().createChildName(moduleName, namePart, j2eeType);
  116           SingleGBeanBuilder builder = new SingleGBeanBuilder(abstractName, gBeanInfo, cl, context, moduleName, attrRefMap , refRefMap);
  117   
  118           // set up attributes
  119           AttributeType[] attributeArray = gbean.getAttributeArray();
  120           if (attributeArray != null) {
  121               for (int j = 0; j < attributeArray.length; j++) {
  122                   builder.setAttribute(attributeArray[j].getName().trim(), attributeArray[j].getType(), attributeArray[j].getStringValue());
  123               }
  124           }
  125   
  126           XmlAttributeType[] xmlAttributeArray = gbean.getXmlAttributeArray();
  127           if (xmlAttributeArray != null) {
  128               for (int i = 0; i < xmlAttributeArray.length; i++) {
  129                   XmlAttributeType xmlAttributeType = xmlAttributeArray[i];
  130                   String name = xmlAttributeType.getName().trim();
  131                   XmlObject[] anys = xmlAttributeType.selectChildren(XmlAttributeType.type.qnameSetForWildcardElements());
  132                   if (anys.length != 1) {
  133                       throw new DeploymentException("Unexpected count of xs:any elements in xml-attribute " + anys.length + " qnameset: " + XmlAttributeType.type.qnameSetForWildcardElements());
  134                   }
  135                   builder.setXmlAttribute(name, anys[0]);
  136               }
  137           }
  138   
  139           // set up all single pattern references
  140           ReferenceType[] referenceArray = gbean.getReferenceArray();
  141           if (referenceArray != null) {
  142               for (int j = 0; j < referenceArray.length; j++) {
  143                   builder.setReference(referenceArray[j].getName2(), referenceArray[j], moduleName);
  144               }
  145           }
  146   
  147           // set up app multi-patterned references
  148           ReferencesType[] referencesArray = gbean.getReferencesArray();
  149           if (referencesArray != null) {
  150               for (int j = 0; j < referencesArray.length; j++) {
  151                   builder.setReference(referencesArray[j].getName(), referencesArray[j].getPatternArray(), moduleName);
  152               }
  153           }
  154   
  155           XmlAttributeType[] xmlReferenceArray = gbean.getXmlReferenceArray();
  156           if (xmlReferenceArray != null) {
  157               for (int i = 0; i < xmlReferenceArray.length; i++) {
  158                   XmlAttributeType xmlAttributeType = xmlReferenceArray[i];
  159                   String name = xmlAttributeType.getName().trim();
  160                   XmlObject[] anys = xmlAttributeType.selectChildren(XmlAttributeType.type.qnameSetForWildcardElements());
  161                   if (anys.length != 1) {
  162                       throw new DeploymentException("Unexpected count of xs:any elements in xml-attribute " + anys.length + " qnameset: " + XmlAttributeType.type.qnameSetForWildcardElements());
  163                   }
  164                   builder.setXmlReference(name, anys[0]);
  165               }
  166           }
  167   
  168           PatternType[] dependencyArray = gbean.getDependencyArray();
  169           if (dependencyArray != null) {
  170               for (int i = 0; i < dependencyArray.length; i++) {
  171                   PatternType patternType = dependencyArray[i];
  172                   builder.addDependency(patternType);
  173               }
  174           }
  175   
  176           GBeanData gbeanData = builder.getGBeanData();
  177           try {
  178               context.addGBean(gbeanData);
  179           } catch (GBeanAlreadyExistsException e) {
  180               throw new DeploymentException(e);
  181           }
  182           return abstractName;
  183       }
  184   
  185       public QNameSet getSpecQNameSet() {
  186           return QNameSet.EMPTY;
  187       }
  188   
  189       public QNameSet getPlanQNameSet() {
  190           return GBEAN_QNAME_SET;
  191       }
  192   
  193       public QName getBaseQName() {
  194           return SERVICE_QNAME;
  195       }
  196   
  197       public static final GBeanInfo GBEAN_INFO;
  198   
  199       static {
  200           PropertyEditorManager.registerEditor(Environment.class, EnvironmentBuilder.class);
  201   
  202           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(GBeanBuilder.class, "ModuleBuilder");
  203   
  204           infoBuilder.addInterface(NamespaceDrivenBuilder.class);
  205   
  206           infoBuilder.addReference("XmlAttributeBuilders", XmlAttributeBuilder.class, "XmlAttributeBuilder");
  207           infoBuilder.addReference("XmlReferenceBuilders", XmlReferenceBuilder.class, "XmlReferenceBuilder");
  208   
  209           infoBuilder.setConstructor(new String[]{"XmlAttributeBuilders", "XmlReferenceBuilders"});
  210   
  211           GBEAN_INFO = infoBuilder.getBeanInfo();
  212       }
  213   
  214       public static GBeanInfo getGBeanInfo() {
  215           return GBEAN_INFO;
  216       }
  217   
  218   }

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