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.lang.reflect.Field;
   21   import java.lang.reflect.Method;
   22   import java.util.List;
   23   import java.util.Map;
   24   import java.util.HashMap;
   25   import java.util.ArrayList;
   26   
   27   import javax.annotation.Resource;
   28   import javax.xml.namespace.QName;
   29   
   30   import org.slf4j.Logger;
   31   import org.slf4j.LoggerFactory;
   32   import org.apache.geronimo.common.DeploymentException;
   33   import org.apache.geronimo.gbean.GBeanInfo;
   34   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   35   import org.apache.geronimo.gbean.GBeanLifecycle;
   36   import org.apache.geronimo.j2ee.deployment.Module;
   37   import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedApp;
   38   import org.apache.geronimo.j2ee.deployment.annotation.ResourceAnnotationHelper;
   39   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   40   import org.apache.geronimo.naming.reference.KernelReference;
   41   import org.apache.geronimo.xbeans.geronimo.naming.GerEnvEntryDocument;
   42   import org.apache.geronimo.xbeans.geronimo.naming.GerEnvEntryType;
   43   import org.apache.geronimo.xbeans.javaee.DescriptionType;
   44   import org.apache.geronimo.xbeans.javaee.EnvEntryType;
   45   import org.apache.geronimo.xbeans.javaee.EnvEntryTypeValuesType;
   46   import org.apache.geronimo.xbeans.javaee.InjectionTargetType;
   47   import org.apache.geronimo.xbeans.javaee.JndiNameType;
   48   import org.apache.geronimo.xbeans.javaee.XsdStringType;
   49   import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
   50   import org.apache.xmlbeans.QNameSet;
   51   import org.apache.xmlbeans.XmlObject;
   52   
   53   /**
   54    * @version $Rev: 758570 $ $Date: 2009-03-26 02:25:50 -0700 (Thu, 26 Mar 2009) $
   55    */
   56   public class EnvironmentEntryBuilder extends AbstractNamingBuilder implements GBeanLifecycle {
   57   
   58       private static final Logger log = LoggerFactory.getLogger(EnvironmentEntryBuilder.class);
   59       private static final Map<String, String> NAMESPACE_UPDATES = new HashMap<String, String>();
   60       static {
   61           NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/naming", "http://geronimo.apache.org/xml/ns/naming-1.2");
   62           NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/naming-1.1", "http://geronimo.apache.org/xml/ns/naming-1.2");
   63       }
   64   
   65       private static final QName GER_ENV_ENTRY_QNAME = GerEnvEntryDocument.type.getDocumentElementName();
   66       private static final QNameSet GER_ENV_ENTRY_QNAME_SET = QNameSet.singleton(GER_ENV_ENTRY_QNAME);
   67       private final QNameSet envEntryQNameSet;
   68   
   69       public EnvironmentEntryBuilder(String[] eeNamespaces) {
   70           envEntryQNameSet = buildQNameSet(eeNamespaces, "env-entry");
   71       }
   72   
   73       public void doStart() throws Exception {
   74           XmlBeansUtil.registerNamespaceUpdates(NAMESPACE_UPDATES);
   75       }
   76   
   77       public void doStop() {
   78           XmlBeansUtil.unregisterNamespaceUpdates(NAMESPACE_UPDATES);
   79       }
   80   
   81       public void doFail() {
   82           doStop();
   83       }
   84   
   85       public void buildNaming(XmlObject specDD, XmlObject plan, Module module, Map componentContext) throws DeploymentException {
   86   
   87           // Discover and process any @Resource annotations (if !metadata-complete)
   88           if ((module != null) && (module.getClassFinder() != null)) {
   89   
   90               // Process all the annotations for this naming builder type
   91               try {
   92                   ResourceAnnotationHelper.processAnnotations(module.getAnnotatedApp(), module.getClassFinder(), EnvEntryRefProcessor.INSTANCE);
   93               }
   94               catch (Exception e) {
   95                   log.warn("Unable to process @Resource annotations for module" + module.getName(), e);
   96               }
   97           }
   98   
   99           List<EnvEntryType> envEntriesUntyped = convert(specDD.selectChildren(envEntryQNameSet), JEE_CONVERTER, EnvEntryType.class, EnvEntryType.type);
  100           XmlObject[] gerEnvEntryUntyped = plan == null ? NO_REFS : plan.selectChildren(GER_ENV_ENTRY_QNAME_SET);
  101           Map<String, String> envEntryMap = mapEnvEntries(gerEnvEntryUntyped);
  102           for (EnvEntryType envEntry: envEntriesUntyped) {
  103               String name = getStringValue(envEntry.getEnvEntryName());
  104               String type = getStringValue(envEntry.getEnvEntryType());
  105               String text = envEntryMap.remove(name);
  106               if (text == null) {
  107                   text = getStringValue(envEntry.getEnvEntryValue());
  108               }
  109               try {
  110                   Object value;
  111                   if (text == null) {
  112                       if ("org.apache.geronimo.kernel.Kernel".equals(type)) {
  113                           value = new KernelReference();
  114                       } else {
  115                           value = null;
  116                       }
  117                   } else if ("java.lang.String".equals(type)) {
  118                       value = text;
  119                   } else if ("java.lang.Character".equals(type)) {
  120                       value = text.charAt(0);
  121                   } else if ("java.lang.Boolean".equals(type)) {
  122                       value = Boolean.valueOf(text);
  123                   } else if ("java.lang.Byte".equals(type)) {
  124                       value = Byte.valueOf(text);
  125                   } else if ("java.lang.Short".equals(type)) {
  126                       value = Short.valueOf(text);
  127                   } else if ("java.lang.Integer".equals(type)) {
  128                       value = Integer.valueOf(text);
  129                   } else if ("java.lang.Long".equals(type)) {
  130                       value = Long.valueOf(text);
  131                   } else if ("java.lang.Float".equals(type)) {
  132                       value = Float.valueOf(text);
  133                   } else if ("java.lang.Double".equals(type)) {
  134                       value = Double.valueOf(text);
  135                   } else {
  136                       throw new DeploymentException("unrecognized type: " + type);
  137                   }
  138                   // perform resource injection only if there is a value specified
  139                   // see Java EE 5 spec, section EE.5.4.1.3
  140                   if (value != null) {
  141                       addInjections(name, envEntry.getInjectionTargetArray(), componentContext);
  142                       getJndiContextMap(componentContext).put(ENV + name, value);
  143                   }
  144               } catch (NumberFormatException e) {
  145                   throw new DeploymentException("Invalid env-entry value for name: " + name, e);
  146               }
  147           }
  148           if (!envEntryMap.isEmpty()) {
  149               throw new DeploymentException("Unknown env-entry elements in geronimo plan: " + envEntryMap);
  150           }
  151   
  152       }
  153   
  154       private Map<String, String> mapEnvEntries(XmlObject[] refs) {
  155           Map<String, String> envEntryMap = new HashMap<String, String>();
  156           if (refs != null) {
  157               for (XmlObject ref1 : refs) {
  158                   GerEnvEntryType ref = (GerEnvEntryType) ref1.copy().changeType(GerEnvEntryType.type);
  159                   envEntryMap.put(ref.getEnvEntryName().trim(), ref.getEnvEntryValue().trim());
  160               }
  161           }
  162           return envEntryMap;
  163       }
  164       public QNameSet getSpecQNameSet() {
  165           return envEntryQNameSet;
  166       }
  167   
  168       public QNameSet getPlanQNameSet() {
  169           return QNameSet.EMPTY;
  170       }
  171   
  172       public static class EnvEntryRefProcessor extends ResourceAnnotationHelper.ResourceProcessor {
  173   
  174           public static final EnvEntryRefProcessor INSTANCE = new EnvEntryRefProcessor();
  175   
  176           private EnvEntryRefProcessor() {
  177           }
  178   
  179           public boolean processResource(AnnotatedApp annotatedApp, Resource annotation, Class cls, Method method, Field field) {
  180               String resourceName = getResourceName(annotation, method, field);
  181               String resourceType = getResourceType(annotation, method, field);
  182               if (resourceType.equals("java.lang.String") ||
  183                       resourceType.equals("java.lang.Character") ||
  184                       resourceType.equals("java.lang.Integer") ||
  185                       resourceType.equals("java.lang.Boolean") ||
  186                       resourceType.equals("java.lang.Double") ||
  187                       resourceType.equals("java.lang.Byte") ||
  188                       resourceType.equals("java.lang.Short") ||
  189                       resourceType.equals("java.lang.Long") ||
  190                       resourceType.equals("java.lang.Float")) {
  191   
  192                   log.debug("addResource(): <env-entry> found");
  193   
  194                   boolean exists = false;
  195                   EnvEntryType[] envEntries = annotatedApp.getEnvEntryArray();
  196                   for (EnvEntryType envEntry : envEntries) {
  197                       if (getStringValue(envEntry.getEnvEntryName()).equals(resourceName)) {
  198                           exists = true;
  199                           if (method != null || field != null) {
  200                               InjectionTargetType[] targets = envEntry.getInjectionTargetArray();
  201                               if (!hasTarget(method, field, targets)) {
  202                                   configureInjectionTarget(envEntry.addNewInjectionTarget(), method, field);
  203                               }
  204                           }
  205                           break;
  206                       }
  207                   }
  208                   if (!exists) {
  209                       try {
  210   
  211                           log.debug("addResource(): Does not exist in DD: " + resourceName);
  212   
  213                           // Doesn't exist in deployment descriptor -- add new
  214                           EnvEntryType envEntry = annotatedApp.addNewEnvEntry();
  215   
  216                           //------------------------------------------------------------------------------
  217                           // <env-entry> required elements:
  218                           //------------------------------------------------------------------------------
  219   
  220                           // env-entry-name
  221                           JndiNameType envEntryName = envEntry.addNewEnvEntryName();
  222                           envEntryName.setStringValue(resourceName);
  223   
  224                           if (!resourceType.equals("")) {
  225                               // env-entry-type
  226                               EnvEntryTypeValuesType envEntryType = envEntry.addNewEnvEntryType();
  227                               envEntryType.setStringValue(resourceType);
  228                           }                         
  229                           if (method != null || field != null) {
  230                               // injectionTarget
  231                               InjectionTargetType injectionTarget = envEntry.addNewInjectionTarget();
  232                               configureInjectionTarget(injectionTarget, method, field);
  233                           }
  234   
  235                           // mappedName
  236                           String mappdedNameAnnotation = annotation.mappedName();
  237                           if (!mappdedNameAnnotation.equals("")) {
  238                               XsdStringType mappedName = envEntry.addNewMappedName();
  239                               mappedName.setStringValue(mappdedNameAnnotation);
  240                               envEntry.setMappedName(mappedName);
  241                           }
  242   
  243                           //------------------------------------------------------------------------------
  244                           // <env-entry> optional elements:
  245                           //------------------------------------------------------------------------------
  246   
  247                           // description
  248                           String descriptionAnnotation = annotation.description();
  249                           if (!descriptionAnnotation.equals("")) {
  250                               DescriptionType description = envEntry.addNewDescription();
  251                               description.setStringValue(descriptionAnnotation);
  252                           }
  253   
  254                       }
  255                       catch (Exception anyException) {
  256                           log.debug("ResourceAnnotationHelper: Exception caught while processing <env-entry>");
  257                       }
  258                   }
  259               }
  260               return false;
  261           }
  262       }
  263   
  264       public static final GBeanInfo GBEAN_INFO;
  265   
  266       static {
  267           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(EnvironmentEntryBuilder.class, NameFactory.MODULE_BUILDER);
  268           infoBuilder.addAttribute("eeNamespaces", String[].class, true, true);
  269           infoBuilder.setConstructor(new String[] {"eeNamespaces"});
  270   
  271           GBEAN_INFO = infoBuilder.getBeanInfo();
  272       }
  273   
  274       public static GBeanInfo getGBeanInfo() {
  275           return GBEAN_INFO;
  276       }
  277   
  278   }

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