KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > stateless > StatelessClusterProxyFactory


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.stateless;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import org.jboss.annotation.ejb.Clustered;
26 import org.jboss.annotation.ejb.RemoteBinding;
27 import org.jboss.aop.Advisor;
28 import org.jboss.aop.AspectManager;
29 import org.jboss.aop.advice.AdviceStack;
30 import org.jboss.aspects.remoting.FamilyWrapper;
31 import org.jboss.ejb3.JBossProxy;
32 import org.jboss.ejb3.ProxyFactoryHelper;
33 import org.jboss.ejb3.remoting.RemoteProxyFactory;
34 import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository;
35 import org.jboss.ha.framework.interfaces.HAPartition;
36 import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
37 import org.jboss.ha.framework.interfaces.RandomRobin;
38 import org.jboss.ha.framework.server.HATarget;
39 import org.jboss.remoting.InvokerLocator;
40
41
42 /**
43  * Comment
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @version $Revision: 56516 $
47  */

48 public class StatelessClusterProxyFactory extends BaseStatelessProxyFactory implements RemoteProxyFactory
49 {
50    private RemoteBinding binding;
51    private InvokerLocator locator;
52    private HATarget hatarget;
53    private String JavaDoc proxyFamilyName;
54    private LoadBalancePolicy lbPolicy;
55    private FamilyWrapper wrapper;
56
57    public void setRemoteBinding(RemoteBinding binding)
58    {
59       this.binding = binding;
60    }
61
62    protected Class JavaDoc[] getInterfaces()
63    {
64       Class JavaDoc[] remoteInterfaces = ProxyFactoryHelper.getRemoteInterfaces(container);
65       Class JavaDoc[] interfaces = new Class JavaDoc[remoteInterfaces.length + 1];
66       System.arraycopy(remoteInterfaces, 0, interfaces, 0, remoteInterfaces.length);
67       interfaces[remoteInterfaces.length] = JBossProxy.class;
68       return interfaces;
69    }
70
71    protected void initializeJndiName()
72    {
73       jndiName = ProxyFactoryHelper.getRemoteJndiName(container, binding);
74    }
75
76    public void start() throws Exception JavaDoc
77    {
78       String JavaDoc clientBindUrl = ProxyFactoryHelper.getClientBindUrl(binding);
79       locator = new InvokerLocator(clientBindUrl);
80       Clustered clustered = (Clustered) advisor.resolveAnnotation(Clustered.class);
81       if (clustered == null) throw new RuntimeException JavaDoc("Could not find @Clustered annotation. Cannot deploy.");
82       String JavaDoc partitionName = clustered.partition();
83       proxyFamilyName = container.getEjbName() + locator.getProtocol() + partitionName;
84       HAPartition partition = (HAPartition) container.getInitialContext().lookup("/HAPartition/" + partitionName);
85       hatarget = new HATarget(partition, proxyFamilyName, locator, HATarget.ENABLE_INVOCATIONS);
86       ClusteringTargetsRepository.initTarget(proxyFamilyName, hatarget.getReplicants());
87       ((StatelessContainer) container).getClusterFamilies().put(proxyFamilyName, hatarget);
88       if (clustered.loadBalancePolicy() == null || clustered.loadBalancePolicy().equals(LoadBalancePolicy.class))
89       {
90          lbPolicy = new RandomRobin();
91       }
92       else
93       {
94          lbPolicy = (LoadBalancePolicy) clustered.loadBalancePolicy().newInstance();
95       }
96       wrapper = new FamilyWrapper(proxyFamilyName, hatarget.getReplicants());
97       super.start();
98    }
99
100    public void stop() throws Exception JavaDoc
101    {
102       super.stop();
103       hatarget.destroy();
104       ((StatelessContainer) container).getClusterFamilies().remove(proxyFamilyName);
105    }
106
107    public Object JavaDoc createProxy()
108    {
109 // try
110
{
111          Object JavaDoc containerId = container.getObjectName().getCanonicalName();
112          String JavaDoc stackName = "ClusteredStatelessSessionClientInterceptors";
113          if (binding.interceptorStack() != null && !binding.interceptorStack().equals(""))
114          {
115             stackName = binding.interceptorStack();
116          }
117          AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
118          /*
119          Object[] args = {new StatelessClusteredProxy(containerId, stack.createInterceptors((Advisor) container, null), wrapper, lbPolicy)};
120          return proxyConstructor.newInstance(args);
121          */

122          return constructProxy(new StatelessClusteredProxy(containerId, stack.createInterceptors((Advisor) container, null), wrapper, lbPolicy));
123       }
124       /*
125       catch (InstantiationException e)
126       {
127          throw new RuntimeException(e); //To change body of catch statement use Options | File Templates.
128       }
129       catch (IllegalAccessException e)
130       {
131          throw new RuntimeException(e); //To change body of catch statement use Options | File Templates.
132       }
133       catch (IllegalArgumentException e)
134       {
135          throw new RuntimeException(e); //To change body of catch statement use Options | File Templates.
136       }
137       catch (InvocationTargetException e)
138       {
139          throw new RuntimeException(e.getTargetException()); //To change body of catch statement use Options | File Templates.
140       }
141       */

142    }
143
144    protected StatelessHandleImpl getHandle()
145    {
146       StatelessHandleImpl handle = new StatelessHandleImpl();
147       RemoteBinding remoteBinding = (RemoteBinding)advisor.resolveAnnotation(RemoteBinding.class);
148       if (remoteBinding != null)
149          handle.jndiName = remoteBinding.jndiBinding();
150  
151       return handle;
152    }
153 }
154
Popular Tags