001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: jonas-team@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
019: *
020: * --------------------------------------------------------------------------
021: * $Id: CmiLifeCycleCallback.java 2163 2007-12-11 18:19:23Z loris $
022: * --------------------------------------------------------------------------
023: */package org.ow2.easybeans.component.cmi;
024:
025: import org.ow2.carol.cmi.annotation.Cluster;
026: import org.ow2.carol.cmi.info.CMIInfoExtractor;
027: import org.ow2.carol.cmi.info.CMIInfoExtractorException;
028: import org.ow2.carol.cmi.info.CMIInfoRepository;
029: import org.ow2.carol.cmi.info.ClusteredObjectInfo;
030: import org.ow2.easybeans.api.EZBContainerCallbackInfo;
031: import org.ow2.easybeans.api.Factory;
032: import org.ow2.easybeans.api.LifeCycleCallbackException;
033: import org.ow2.easybeans.api.binding.EZBRef;
034: import org.ow2.easybeans.container.EmptyLifeCycleCallBack;
035: import org.ow2.easybeans.container.session.stateless.StatelessSessionFactory;
036: import org.ow2.easybeans.proxy.reference.RemoteCallRef;
037: import org.ow2.util.log.Log;
038: import org.ow2.util.log.LogFactory;
039:
040: /**
041: * Provide a callback for CMI.
042: * @author WEI Zhouyue & ZHU Ning
043: */
044: public class CmiLifeCycleCallback extends EmptyLifeCycleCallBack {
045:
046: /**
047: * Logger.
048: */
049: private static Log logger = LogFactory
050: .getLog(CmiLifeCycleCallback.class);
051:
052: /**
053: * Called before binding a reference into the registry.
054: * Extract the informations on clustering and register it in the repository of CMI.
055: * @param info some information on the container which is running.
056: * @param reference a reference on the bean that will be bound
057: * @throws LifeCycleCallbackException if the invocation of the callback failed
058: */
059: @Override
060: public void beforeBind(final EZBContainerCallbackInfo info,
061: final EZBRef reference) throws LifeCycleCallbackException {
062: // CMI only works on remote interfaces
063: if (reference instanceof RemoteCallRef) {
064: Factory<?, ?> factory = reference.getFactory();
065: // Only for stateless beans
066: // TODO Support of stateful beans
067: if (factory instanceof StatelessSessionFactory) {
068: Class<?> beanClass = ((StatelessSessionFactory) factory)
069: .getBeanClass();
070: if (beanClass.isAnnotationPresent(Cluster.class)) {
071: Class<?> itfClass = null;
072: String jndiName = reference.getJNDIName();
073: String itfClassname = reference.getItfClassName();
074: logger
075: .info(
076: "The bean with the jndi name {0} and the interface name {1} is clustered.",
077: jndiName, itfClassname);
078: for (Class<?> klass : beanClass.getInterfaces()) {
079: if (klass.getName().equals(itfClassname)) {
080: itfClass = klass;
081: }
082: }
083: if (itfClass == null) {
084: logger
085: .error(
086: "Cannot find the interface for name {0}",
087: itfClassname);
088: throw new LifeCycleCallbackException(
089: "Cannot find the interface for name "
090: + itfClassname);
091: }
092: // Extract the informations on clustering
093: ClusteredObjectInfo infos;
094: try {
095: infos = CMIInfoExtractor
096: .extractClusteringInfoFromAnnotatedPOJO(
097: itfClass, beanClass, false);
098: } catch (CMIInfoExtractorException e) {
099: logger
100: .error(
101: "Cannot extract infos for the class with name {0}",
102: beanClass.getName(), e);
103: throw new LifeCycleCallbackException(
104: "Cannot extract infos for the class with name "
105: + beanClass.getName(), e);
106: }
107: // Register the informations in the repository of CMI
108: CMIInfoRepository.addClusteredObjectInfo(jndiName,
109: infos);
110: }
111: }
112: }
113: }
114:
115: }
|