KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > unified > server > UnifiedInvokerHA


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.invocation.unified.server;
23
24 import java.rmi.MarshalledObject JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import org.jboss.ha.framework.interfaces.GenericClusteringException;
31 import org.jboss.ha.framework.interfaces.HARMIResponse;
32 import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
33 import org.jboss.ha.framework.server.HATarget;
34 import org.jboss.invocation.Invocation;
35 import org.jboss.invocation.Invoker;
36 import org.jboss.invocation.InvokerHA;
37 import org.jboss.invocation.unified.interfaces.UnifiedInvokerHAProxy;
38 import org.jboss.mx.util.JMXExceptionDecoder;
39 import org.jboss.remoting.InvocationRequest;
40 import org.jboss.system.Registry;
41
42 /**
43  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
44  */

45 public class UnifiedInvokerHA extends UnifiedInvoker implements InvokerHA
46 {
47    private HashMap JavaDoc beanMap = new HashMap JavaDoc();
48
49    public UnifiedInvokerHA()
50    {
51       super();
52       setSubSystem("invokerha");
53    }
54
55    protected void jmxBind()
56    {
57       Registry.bind(getServiceName(), this);
58    }
59
60    public java.io.Serializable JavaDoc getStub()
61    {
62       return getInvoker().getLocator();
63    }
64
65    public void registerBean(ObjectName JavaDoc beanName, HATarget target) throws Exception JavaDoc
66    {
67       Integer JavaDoc hash = new Integer JavaDoc(beanName.hashCode());
68
69       if(beanMap.containsKey(hash))
70       {
71          throw new IllegalStateException JavaDoc("Trying to register bean (" + beanName + ") with an hash code that already exists");
72       }
73       beanMap.put(hash, target);
74    }
75
76    public Invoker createProxy(ObjectName JavaDoc beanName, LoadBalancePolicy policy, String JavaDoc proxyFamilyName)
77          throws Exception JavaDoc
78    {
79       Integer JavaDoc hash = new Integer JavaDoc(beanName.hashCode());
80       HATarget target = (HATarget) beanMap.get(hash);
81       if(target == null)
82       {
83          throw new IllegalStateException JavaDoc("The bean hashCode not found");
84       }
85
86       String JavaDoc familyName = proxyFamilyName;
87       if(familyName == null)
88       {
89          familyName = target.getAssociatedPartition().getPartitionName() + "/" + beanName;
90       }
91
92       UnifiedInvokerHAProxy proxy = new UnifiedInvokerHAProxy(getInvoker().getLocator(), getStrictRMIException(),
93                                                               target.getReplicants(),
94                                                               policy, proxyFamilyName, target.getCurrentViewId());
95       return proxy;
96
97    }
98
99    public void unregisterBean(ObjectName JavaDoc beanName) throws Exception JavaDoc
100    {
101       Integer JavaDoc hash = new Integer JavaDoc(beanName.hashCode());
102       beanMap.remove(hash);
103    }
104
105    /**
106     * Implementation of the server invoker handler interface. Will take the invocation request
107     * and invoke down the interceptor chain.
108     *
109     * @param invocationReq
110     * @return
111     * @throws Throwable
112     */

113    public Object JavaDoc invoke(InvocationRequest invocationReq) throws Throwable JavaDoc
114    {
115       Invocation invocation = (Invocation) invocationReq.getParameter();
116       Thread JavaDoc currentThread = Thread.currentThread();
117       ClassLoader JavaDoc oldCl = currentThread.getContextClassLoader();
118       ObjectName JavaDoc mbean = null;
119       try
120       {
121          mbean = (ObjectName JavaDoc) Registry.lookup(invocation.getObjectName());
122
123          /** Clustering **/
124          long clientViewId = ((Long JavaDoc) invocation.getValue("CLUSTER_VIEW_ID")).longValue();
125          HATarget target = (HATarget) beanMap.get(invocation.getObjectName());
126          if(target == null)
127          {
128             // We could throw IllegalStateException but we have a race condition that could occur:
129
// when we undeploy a bean, the cluster takes some time to converge
130
// and to recalculate a new viewId and list of replicant for each HATarget.
131
// Consequently, a client could own an up-to-date list of the replicants
132
// (before the cluster has converged) and try to perform an invocation
133
// on this node where the HATarget no more exist, thus receiving a
134
// wrong exception and no failover is performed with an IllegalStateException
135
//
136
throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO,
137                                                  "target is not/no more registered on this node");
138          }
139
140          if(!target.invocationsAllowed())
141          {
142             throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO,
143                                                  "invocations are currently not allowed on this target");
144          }
145          /** End Clustering **/
146
147          // The cl on the thread should be set in another interceptor
148
Object JavaDoc obj = getServer().invoke(mbean,
149                                          "invoke",
150                                          new Object JavaDoc[]{invocation},
151                                          Invocation.INVOKE_SIGNATURE);
152
153          /** Clustering **/
154
155          HARMIResponse haResponse = new HARMIResponse();
156
157          if(clientViewId != target.getCurrentViewId())
158          {
159             haResponse.newReplicants = new ArrayList JavaDoc(target.getReplicants());
160             haResponse.currentViewId = target.getCurrentViewId();
161          }
162          haResponse.response = obj;
163
164          /** End Clustering **/
165
166          return new MarshalledObject JavaDoc(haResponse);
167       }
168       catch(Exception JavaDoc e)
169       {
170          Throwable JavaDoc th = JMXExceptionDecoder.decode(e);
171          if(log.isTraceEnabled())
172          {
173             log.trace("Failed to invoke on mbean: " + mbean, th);
174          }
175
176          if(th instanceof Exception JavaDoc)
177          {
178             e = (Exception JavaDoc) th;
179          }
180
181          throw e;
182       }
183       finally
184       {
185          currentThread.setContextClassLoader(oldCl);
186          Thread.interrupted(); // clear interruption because this thread may be pooled.
187
}
188
189    }
190
191    @Override JavaDoc
192    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
193    {
194       ObjectName JavaDoc result = super.preRegister(server, name);
195       log.info("Service name is " + getServiceName());
196       return result;
197    }
198    
199    
200
201 }
Popular Tags