KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > stateful > StatefulClusterProxyFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.stateful;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25
26 import javax.naming.NamingException JavaDoc;
27
28 import org.jboss.annotation.ejb.Clustered;
29 import org.jboss.annotation.ejb.RemoteBinding;
30 import org.jboss.aop.Advisor;
31 import org.jboss.aop.AspectManager;
32 import org.jboss.aop.Dispatcher;
33 import org.jboss.aop.advice.AdviceStack;
34 import org.jboss.aspects.remoting.FamilyWrapper;
35 import org.jboss.aspects.remoting.Remoting;
36 import org.jboss.ejb3.JBossProxy;
37 import org.jboss.ejb3.ProxyFactory;
38 import org.jboss.ejb3.ProxyFactoryHelper;
39 import org.jboss.ejb3.remoting.RemoteProxyFactory;
40 import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
41 import org.jboss.ha.framework.interfaces.FirstAvailable;
42 import org.jboss.ha.framework.interfaces.HAPartition;
43 import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
44 import org.jboss.ha.framework.server.HATarget;
45 import org.jboss.naming.Util;
46 import org.jboss.remoting.InvokerLocator;
47
48
49 /**
50  * Comment
51  *
52  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
53  * @version $Revision: 56518 $
54  */

55 public class StatefulClusterProxyFactory extends BaseStatefulProxyFactory implements RemoteProxyFactory
56 {
57    private RemoteBinding binding;
58    private InvokerLocator locator;
59    private HATarget hatarget;
60    private String JavaDoc proxyFamilyName;
61    private LoadBalancePolicy lbPolicy;
62    private FamilyWrapper wrapper;
63
64    public void setRemoteBinding(RemoteBinding binding)
65    {
66       this.binding = binding;
67    }
68
69    protected Class JavaDoc[] getInterfaces()
70    {
71       Class JavaDoc[] remoteInterfaces = ProxyFactoryHelper.getRemoteInterfaces(container);
72       Class JavaDoc[] interfaces = new Class JavaDoc[remoteInterfaces.length + 1];
73       System.arraycopy(remoteInterfaces, 0, interfaces, 0, remoteInterfaces.length);
74       interfaces[remoteInterfaces.length] = JBossProxy.class;
75       return interfaces;
76    }
77
78    protected void initializeJndiName()
79    {
80       jndiName = ProxyFactoryHelper.getRemoteJndiName(container, binding);
81    }
82
83    public void start() throws Exception JavaDoc
84    {
85       String JavaDoc clientBindUrl = ProxyFactoryHelper.getClientBindUrl(binding);
86       locator = new InvokerLocator(clientBindUrl);
87       Clustered clustered = (Clustered) advisor.resolveAnnotation(Clustered.class);
88       if (clustered == null) throw new RuntimeException JavaDoc("Could not find @Clustered annotation. Cannot deploy.");
89       String JavaDoc partitionName = clustered.partition();
90       proxyFamilyName = container.getEjbName() + locator.getProtocol() + partitionName;
91       HAPartition partition = (HAPartition) container.getInitialContext().lookup("/HAPartition/" + partitionName);
92       hatarget = new HATarget(partition, proxyFamilyName, locator, HATarget.ENABLE_INVOCATIONS);
93       ClusteringTargetsRepository.initTarget(proxyFamilyName, hatarget.getReplicants());
94       ((StatefulContainer) container).getClusterFamilies().put(proxyFamilyName, hatarget);
95       if (clustered.loadBalancePolicy() == null || clustered.loadBalancePolicy().equals(LoadBalancePolicy.class))
96       {
97          lbPolicy = new FirstAvailable();
98       }
99       else
100       {
101          lbPolicy = (LoadBalancePolicy) clustered.loadBalancePolicy().newInstance();
102       }
103       wrapper = new FamilyWrapper(proxyFamilyName, hatarget.getReplicants());
104       super.start();
105       Class JavaDoc[] interfaces = {ProxyFactory.class};
106       Object JavaDoc factoryProxy = Remoting.createPojiProxy(jndiName + PROXY_FACTORY_NAME, interfaces, ProxyFactoryHelper.getClientBindUrl(binding));
107       try
108       {
109          Util.rebind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME, factoryProxy);
110       } catch (NamingException JavaDoc e)
111       {
112          NamingException JavaDoc namingException = new NamingException JavaDoc("Could not bind stateful cluster proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName + PROXY_FACTORY_NAME);
113          namingException.setRootCause(e);
114          throw namingException;
115       }
116       Dispatcher.singleton.registerTarget(jndiName + PROXY_FACTORY_NAME, this);
117
118    }
119
120    public Object JavaDoc createProxy()
121    {
122       try
123       {
124          Object JavaDoc containerId = container.getObjectName().getCanonicalName();
125          String JavaDoc stackName = "ClusteredStatefulSessionClientInterceptors";
126          if (binding.interceptorStack() != null && !binding.interceptorStack().equals(""))
127          {
128             stackName = binding.interceptorStack();
129          }
130          AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
131          Object JavaDoc[] args = {new StatefulClusteredProxy(containerId, stack.createInterceptors((Advisor) container, null), wrapper, lbPolicy)};
132          return proxyConstructor.newInstance(args);
133       }
134       catch (InstantiationException JavaDoc e)
135       {
136          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
137
}
138       catch (IllegalAccessException JavaDoc e)
139       {
140          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
141
}
142       catch (IllegalArgumentException JavaDoc e)
143       {
144          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
145
}
146       catch (InvocationTargetException JavaDoc e)
147       {
148          throw new RuntimeException JavaDoc(e.getTargetException()); //To change body of catch statement use Options | File Templates.
149
}
150    }
151
152    public Object JavaDoc createProxy(Object JavaDoc id)
153    {
154       throw new RuntimeException JavaDoc("NYI");
155    }
156    
157    public void stop() throws Exception JavaDoc
158    {
159       super.stop();
160       hatarget.destroy();
161       ((StatefulContainer) container).getClusterFamilies().remove(proxyFamilyName);
162       Util.unbind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME);
163
164    }
165    
166    protected StatefulHandleImpl getHandle()
167    {
168       StatefulHandleImpl handle = new StatefulHandleImpl();
169       RemoteBinding remoteBinding = (RemoteBinding)advisor.resolveAnnotation(RemoteBinding.class);
170       if (remoteBinding != null)
171          handle.jndiName = remoteBinding.jndiBinding();
172  
173       return handle;
174    }
175
176 }
177
Popular Tags