001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020
021 package org.apache.geronimo.myfaces;
022
023 import java.lang.reflect.InvocationTargetException;
024 import java.util.Map;
025
026 import javax.naming.NamingException;
027 import javax.naming.Context;
028
029 import org.apache.myfaces.config.annotation.LifecycleProvider;
030 import org.apache.geronimo.j2ee.annotation.Holder;
031 import org.apache.geronimo.j2ee.annotation.LifecycleMethod;
032 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
033 import org.apache.geronimo.gbean.GBeanLifecycle;
034 import org.apache.geronimo.gbean.GBeanInfo;
035 import org.apache.geronimo.gbean.GBeanInfoBuilder;
036 import org.apache.geronimo.kernel.Kernel;
037 import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
038
039 /**
040 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
041 */
042 public class LifecycleProviderGBean implements LifecycleProvider, GBeanLifecycle {
043
044 private final Holder holder;
045 private final Context context;
046 private final ApplicationIndexedLifecycleProviderFactory factory;
047 private final ClassLoader classLoader;
048
049
050 public LifecycleProviderGBean(Holder holder, Map componentContext, LifecycleProviderFactorySource factory, Kernel kernel, ClassLoader classLoader) throws NamingException {
051 this.holder = holder;
052 // GeronimoUserTransaction userTransaction = new GeronimoUserTransaction(transactionManager);
053 context = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext, null, kernel, classLoader);
054 this.factory = factory.getLifecycleProviderFactory();
055 this.classLoader = classLoader;
056 }
057
058 public Object newInstance(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException, InvocationTargetException {
059 if (className == null) {
060 throw new InstantiationException("no class name provided");
061 }
062 return holder.newInstance(className, classLoader, context);
063 }
064
065 public void destroyInstance(Object o) throws IllegalAccessException, InvocationTargetException {
066 Class clazz = o.getClass();
067 if (holder != null) {
068 Map<String, LifecycleMethod> preDestroy = holder.getPreDestroy();
069 if (preDestroy != null) {
070 Holder.apply(o, clazz, preDestroy);
071 }
072 }
073 }
074
075 public void doStart() {
076 factory.registerLifecycleProvider(classLoader, this);
077 }
078
079 public void doStop() {
080 factory.unregisterLifecycleProvider(classLoader);
081 }
082
083 public void doFail() {
084 doStop();
085 }
086
087 public static final GBeanInfo GBEAN_INFO;
088
089 static {
090 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(LifecycleProviderGBean.class, NameFactory.GERONIMO_SERVICE);
091 infoBuilder.addAttribute("holder", Holder.class, true);
092 infoBuilder.addAttribute("componentContext", Map.class, true);
093
094 infoBuilder.addReference("LifecycleProviderFactory", LifecycleProviderFactorySource.class);
095 // infoBuilder.addReference("TransactionManager", TransactionManager.class);
096 infoBuilder.addAttribute("kernel", Kernel.class, false);
097 infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
098
099 infoBuilder.setConstructor(new String[] {"holder", "componentContext", "LifecycleProviderFactory", "kernel", "classLoader"});
100
101 GBEAN_INFO = infoBuilder.getBeanInfo();
102 }
103
104 public static GBeanInfo getGBeanInfo() {
105 return GBEAN_INFO;
106 }
107 }