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: package org.apache.geronimo.myfaces;
021:
022: import java.lang.reflect.InvocationTargetException;
023: import java.util.Map;
024:
025: import javax.naming.NamingException;
026: import javax.naming.Context;
027:
028: import org.apache.myfaces.config.annotation.LifecycleProvider;
029: import org.apache.geronimo.j2ee.annotation.Holder;
030: import org.apache.geronimo.j2ee.annotation.LifecycleMethod;
031: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
032: import org.apache.geronimo.gbean.GBeanLifecycle;
033: import org.apache.geronimo.gbean.GBeanInfo;
034: import org.apache.geronimo.gbean.GBeanInfoBuilder;
035: import org.apache.geronimo.kernel.Kernel;
036: import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
037:
038: /**
039: * @version $Rev: 518635 $ $Date: 2007-03-15 07:15:53 -0700 (Thu, 15 Mar 2007) $
040: */
041: public class LifecycleProviderGBean implements LifecycleProvider,
042: GBeanLifecycle {
043:
044: private final Holder holder;
045: private final Context context;
046: private final ApplicationIndexedLifecycleProviderFactory factory;
047: private final ClassLoader classLoader;
048:
049: public LifecycleProviderGBean(Holder holder, Map componentContext,
050: LifecycleProviderFactorySource factory, Kernel kernel,
051: ClassLoader classLoader) throws NamingException {
052: this .holder = holder;
053: // GeronimoUserTransaction userTransaction = new GeronimoUserTransaction(transactionManager);
054: context = EnterpriseNamingContext
055: .createEnterpriseNamingContext(componentContext, null,
056: kernel, classLoader);
057: this .factory = factory.getLifecycleProviderFactory();
058: this .classLoader = classLoader;
059: }
060:
061: public Object newInstance(String className)
062: throws ClassNotFoundException, IllegalAccessException,
063: InstantiationException, NamingException,
064: InvocationTargetException {
065: if (className == null) {
066: throw new InstantiationException("no class name provided");
067: }
068: return holder.newInstance(className, classLoader, context);
069: }
070:
071: public void destroyInstance(Object o)
072: throws IllegalAccessException, InvocationTargetException {
073: Class clazz = o.getClass();
074: if (holder != null) {
075: Map<String, LifecycleMethod> preDestroy = holder
076: .getPreDestroy();
077: if (preDestroy != null) {
078: Holder.apply(o, clazz, preDestroy);
079: }
080: }
081: }
082:
083: public void doStart() {
084: factory.registerLifecycleProvider(classLoader, this );
085: }
086:
087: public void doStop() {
088: factory.unregisterLifecycleProvider(classLoader);
089: }
090:
091: public void doFail() {
092: doStop();
093: }
094:
095: public static final GBeanInfo GBEAN_INFO;
096:
097: static {
098: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
099: LifecycleProviderGBean.class,
100: NameFactory.GERONIMO_SERVICE);
101: infoBuilder.addAttribute("holder", Holder.class, true);
102: infoBuilder.addAttribute("componentContext", Map.class, true);
103:
104: infoBuilder.addReference("LifecycleProviderFactory",
105: LifecycleProviderFactorySource.class);
106: // infoBuilder.addReference("TransactionManager", TransactionManager.class);
107: infoBuilder.addAttribute("kernel", Kernel.class, false);
108: infoBuilder.addAttribute("classLoader", ClassLoader.class,
109: false);
110:
111: infoBuilder.setConstructor(new String[] { "holder",
112: "componentContext", "LifecycleProviderFactory",
113: "kernel", "classLoader" });
114:
115: GBEAN_INFO = infoBuilder.getBeanInfo();
116: }
117:
118: public static GBeanInfo getGBeanInfo() {
119: return GBEAN_INFO;
120: }
121: }
|