
org.apache.geronimo.naming.deployment.LifecycleMethodBuilder Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.geronimo.naming.deployment;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.apache.geronimo.common.DeploymentException;
import org.apache.geronimo.gbean.annotation.GBean;
import org.apache.geronimo.j2ee.annotation.Holder;
import org.apache.geronimo.j2ee.annotation.LifecycleMethod;
import org.apache.geronimo.j2ee.deployment.EARContext;
import org.apache.geronimo.j2ee.deployment.Module;
import org.apache.geronimo.j2ee.deployment.NamingBuilder;
import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
import org.apache.openejb.jee.ApplicationClient;
import org.apache.openejb.jee.EnterpriseBean;
import org.apache.openejb.jee.Interceptor;
import org.apache.openejb.jee.JndiConsumer;
import org.apache.openejb.jee.Lifecycle;
import org.apache.openejb.jee.LifecycleCallback;
import org.apache.xbean.finder.AbstractFinder;
import org.apache.xmlbeans.QNameSet;
import org.apache.xmlbeans.XmlObject;
/**
* @version $Rev: 988780 $ $Date: 2010-08-25 10:32:14 +0800 (Wed, 25 Aug 2010) $
*/
@GBean(j2eeType = NameFactory.MODULE_BUILDER)
public class LifecycleMethodBuilder extends AbstractNamingBuilder {
@Override
public void buildNaming(JndiConsumer specDD, XmlObject plan, Module module, Map sharedContext) throws DeploymentException {
// skip ejb modules... they have already been processed
//skip ears, they have no standalone components
// if (module.getType() == ConfigurationModuleType.EJB || module.getType() == ConfigurationModuleType.EAR) {
// return;
// }
if (!(specDD instanceof Lifecycle)) {
return;
}
Lifecycle lifecycle = (Lifecycle) specDD;
AbstractFinder classFinder = module.getClassFinder();
//TODO Need to double check the LifecycleMethod Scanning, seems we also do it in the OpenEJB
String componentType = null;
if (specDD instanceof EnterpriseBean) {
componentType = ((EnterpriseBean) specDD).getEjbClass();
} else if (specDD instanceof Interceptor) {
componentType = ((Interceptor) specDD).getInterceptorClass();
} else if (specDD instanceof ApplicationClient) {
componentType = ((ApplicationClient) specDD).getMainClass();
}
Map postConstructMap = mapLifecycleCallbacks(lifecycle.getPostConstruct(), componentType);
Map preDestroyMap = mapLifecycleCallbacks(lifecycle.getPreDestroy(), componentType);
if (module.getClassFinder() != null) {
List postConstructs = classFinder.findAnnotatedMethods(PostConstruct.class);
for (Method m : postConstructs) {
String methodName = m.getName();
String className = m.getDeclaringClass().getName();
if (!postConstructMap.containsKey(className)) {
LifecycleCallback callback = new LifecycleCallback();
callback.setLifecycleCallbackClass(className);
callback.setLifecycleCallbackMethod(methodName);
lifecycle.getPostConstruct().add(callback);
postConstructMap.put(className, callback);
}
}
List preDestroys = classFinder.findAnnotatedMethods(PreDestroy.class);
for (Method m : preDestroys) {
String methodName = m.getName();
String className = m.getDeclaringClass().getName();
if (!preDestroyMap.containsKey(className)) {
LifecycleCallback callback = new LifecycleCallback();
callback.setLifecycleCallbackClass(className);
callback.setLifecycleCallbackMethod(methodName);
preDestroyMap.put(className, callback);
lifecycle.getPreDestroy().add(callback);
}
}
}
Map postConstructs = map(postConstructMap);
Map preDestroys = map(preDestroyMap);
Holder holder = NamingBuilder.INJECTION_KEY.get(sharedContext);
holder.addPostConstructs(postConstructs);
holder.addPreDestroys(preDestroys);
}
private Map map(Map LifecycleCallbacks) {
if (LifecycleCallbacks.isEmpty()) {
return null;
}
Map map = new HashMap();
for (Map.Entry entry : LifecycleCallbacks.entrySet()) {
String className = entry.getKey();
LifecycleCallback callback = entry.getValue();
LifecycleMethod method = new LifecycleMethod(className, callback.getLifecycleCallbackMethod().trim());
map.put(className, method);
}
return map;
}
private Map mapLifecycleCallbacks(Collection callbackArray, String componentType) throws DeploymentException {
Map map = new HashMap();
for (LifecycleCallback callback : callbackArray) {
String className;
if (callback.getLifecycleCallbackClass() != null) {
className = callback.getLifecycleCallbackClass().trim();
} else {
if (componentType == null) {
throw new DeploymentException("No component type available and none in lifecycle callback");
}
className = componentType;
}
map.put(className, callback);
}
return map;
}
@Override
public QNameSet getSpecQNameSet() {
return QNameSet.EMPTY;
}
@Override
public QNameSet getPlanQNameSet() {
return QNameSet.EMPTY;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy