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
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *  http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   
   21   package org.apache.geronimo.naming.deployment;
   22   
   23   import java.lang.reflect.Method;
   24   import java.util.HashMap;
   25   import java.util.List;
   26   import java.util.Map;
   27   
   28   import javax.annotation.PostConstruct;
   29   import javax.annotation.PreDestroy;
   30   
   31   import org.apache.geronimo.common.DeploymentException;
   32   import org.apache.geronimo.gbean.GBeanInfo;
   33   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   34   import org.apache.geronimo.j2ee.annotation.Holder;
   35   import org.apache.geronimo.j2ee.annotation.LifecycleMethod;
   36   import org.apache.geronimo.j2ee.deployment.Module;
   37   import org.apache.geronimo.j2ee.deployment.NamingBuilder;
   38   import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedApp;
   39   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   40   import org.apache.geronimo.kernel.config.ConfigurationModuleType;
   41   import org.apache.geronimo.xbeans.javaee.FullyQualifiedClassType;
   42   import org.apache.geronimo.xbeans.javaee.JavaIdentifierType;
   43   import org.apache.geronimo.xbeans.javaee.LifecycleCallbackType;
   44   import org.apache.xbean.finder.ClassFinder;
   45   import org.apache.xmlbeans.QNameSet;
   46   import org.apache.xmlbeans.XmlObject;
   47   
   48   /**
   49    * @version $Rev: 541492 $ $Date: 2007-05-24 17:09:32 -0700 (Thu, 24 May 2007) $
   50    */
   51   public class LifecycleMethodBuilder extends AbstractNamingBuilder {
   52       public void buildNaming(XmlObject specDD, XmlObject plan, Module module, Map componentContext) throws DeploymentException {
   53           // skip ejb modules... they have alreayd been processed
   54           if (module.getType() == ConfigurationModuleType.EJB) {
   55               return;
   56           }
   57   
   58           ClassFinder classFinder = module.getClassFinder();
   59           AnnotatedApp annotatedApp = module.getAnnotatedApp();
   60           if (annotatedApp == null) {
   61               throw new NullPointerException("No AnnotatedApp supplied");
   62           }
   63           Map<String, LifecycleCallbackType> postConstructMap = mapLifecycleCallbacks(annotatedApp.getPostConstructArray(), annotatedApp.getComponentType());
   64           Map<String, LifecycleCallbackType> preDestroyMap = mapLifecycleCallbacks(annotatedApp.getPreDestroyArray(), annotatedApp.getComponentType());
   65           if (module.getClassFinder() != null) {
   66               List<Method> postConstructs = classFinder.findAnnotatedMethods(PostConstruct.class);
   67               for (Method m : postConstructs) {
   68                   String methodName = m.getName();
   69                   String className = m.getDeclaringClass().getName();
   70                   if (!postConstructMap.containsKey(className)) {
   71                       LifecycleCallbackType callback = annotatedApp.addPostConstruct();
   72                       FullyQualifiedClassType classType = callback.addNewLifecycleCallbackClass();
   73                       classType.setStringValue(className);
   74                       JavaIdentifierType method = callback.addNewLifecycleCallbackMethod();
   75                       method.setStringValue(methodName);
   76                       postConstructMap.put(className, callback);
   77                   }
   78               }
   79               List<Method> preDestroys = classFinder.findAnnotatedMethods(PreDestroy.class);
   80               for (Method m : preDestroys) {
   81                   String methodName = m.getName();
   82                   String className = m.getDeclaringClass().getName();
   83                   if (!preDestroyMap.containsKey(className)) {
   84                       LifecycleCallbackType callback = annotatedApp.addPreDestroy();
   85                       FullyQualifiedClassType classType = callback.addNewLifecycleCallbackClass();
   86                       classType.setStringValue(className);
   87                       JavaIdentifierType method = callback.addNewLifecycleCallbackMethod();
   88                       method.setStringValue(methodName);
   89                       preDestroyMap.put(className, callback);
   90                   }
   91               }
   92           }
   93           Map<String, LifecycleMethod> postConstructs = map(postConstructMap);
   94           Map<String, LifecycleMethod> preDestroys = map(preDestroyMap);
   95           Holder holder = NamingBuilder.INJECTION_KEY.get(componentContext);
   96           holder.addPostConstructs(postConstructs);
   97           holder.addPreDestroys(preDestroys);
   98       }
   99   
  100       private Map<String, LifecycleMethod> map(Map<String, LifecycleCallbackType> lifecycleCallbackTypes) {
  101           if (lifecycleCallbackTypes.isEmpty()) {
  102               return null;
  103           }
  104           Map<String, LifecycleMethod> map = new HashMap<String, LifecycleMethod>();
  105           for (Map.Entry<String, LifecycleCallbackType> entry : lifecycleCallbackTypes.entrySet()) {
  106               String className = entry.getKey();
  107               LifecycleCallbackType callback = entry.getValue();
  108               LifecycleMethod method = new LifecycleMethod(className, callback.getLifecycleCallbackMethod().getStringValue().trim());
  109               map.put(className, method);
  110           }
  111           return map;
  112       }
  113   
  114       private Map<String, LifecycleCallbackType> mapLifecycleCallbacks(LifecycleCallbackType[] callbackArray, String componentType) throws DeploymentException {
  115           Map<String, LifecycleCallbackType> map = new HashMap<String, LifecycleCallbackType>();
  116           for (LifecycleCallbackType callback : callbackArray) {
  117               String className;
  118               if (callback.isSetLifecycleCallbackClass()) {
  119                   className = callback.getLifecycleCallbackClass().getStringValue().trim();
  120               } else {
  121                   if (componentType == null) {
  122                       throw new DeploymentException("No component type available and none in  lifecycle callback");
  123                   }
  124                   className = componentType;
  125               }
  126               map.put(className, callback);
  127           }
  128           return map;
  129       }
  130   
  131       public QNameSet getSpecQNameSet() {
  132           return QNameSet.EMPTY;
  133       }
  134   
  135       public QNameSet getPlanQNameSet() {
  136           return QNameSet.EMPTY;
  137       }
  138   
  139       public static final GBeanInfo GBEAN_INFO;
  140   
  141       static {
  142           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(LifecycleMethodBuilder.class, NameFactory.MODULE_BUILDER);
  143   
  144           GBEAN_INFO = infoBuilder.getBeanInfo();
  145       }
  146   
  147       public static GBeanInfo getGBeanInfo() {
  148           return GBEAN_INFO;
  149       }
  150   }

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