KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > http > server > HttpProxyFactoryHA


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.http.server;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 import org.jboss.invocation.Invoker;
33 import org.jboss.invocation.InvokerInterceptor;
34 import org.jboss.invocation.http.interfaces.ClientMethodInterceptorHA;
35 import org.jboss.invocation.http.interfaces.HttpInvokerProxyHA;
36 import org.jboss.system.server.ServerConfigUtil;
37 import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
38 import org.jboss.ha.framework.interfaces.HAPartition;
39 import org.jboss.ha.framework.server.HATarget;
40
41 /** An extension of HttpProxyFactory that supports clustering of invoker proxies.
42  * It does this by creating a HATarget that monitors the replication of the
43  * invoker url and creates a HAInvokerWrapper that handles wrapping the
44  * underlying invocation result with changes to the HATarget replication
45  * view.
46  *
47  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
48  * @version $Revision: 37459 $
49  */

50 public class HttpProxyFactoryHA extends HttpProxyFactory
51    implements HttpProxyFactoryHAMBean
52 {
53    private ObjectName JavaDoc realJmxInvokerName;
54    private ObjectName JavaDoc wrappedJmxInvokerName;
55    private String JavaDoc partitionName = ServerConfigUtil.getDefaultPartitionName();
56    private Class JavaDoc policyClass;
57    private HAInvokerWrapper invokerWrapper;
58    private HATarget invokerTarget;
59
60    /** Get the server side mbean that exposes the invoke operation for the
61     exported interface */

62    public Class JavaDoc getLoadBalancePolicy()
63    {
64       return this.policyClass;
65    }
66    /** Set the server side mbean that exposes the invoke operation for the
67     exported interface */

68    public void setLoadBalancePolicy(Class JavaDoc policyClass)
69    {
70       this.policyClass = policyClass;
71    }
72
73    /** Get the name of the cluster partition the invoker is deployed in
74     */

75    public String JavaDoc getPartitionName()
76    {
77       return this.partitionName;
78    }
79    /** Set the name of the cluster partition the invoker is deployed in
80     */

81    public void setPartitionName(String JavaDoc name)
82    {
83       this.partitionName = name;
84    }
85
86    /** Override the superclass method to create a wrapped ObjectName for the
87     * HAInvokerWrapper mbean. This will be the name of the invoker as known
88     * by the proxy. The HAInvokerWrapper is the HttpProxyFactoryHA name +
89     * wrapperType=httpHA
90     *
91     * @param jmxInvokerName
92     */

93    public void setInvokerName(ObjectName JavaDoc jmxInvokerName)
94    {
95       realJmxInvokerName = jmxInvokerName;
96       ObjectName JavaDoc factoryName = getServiceName();
97       Hashtable JavaDoc props = factoryName.getKeyPropertyList();
98       props.put("wrapperType", "httpHA");
99       try
100       {
101          wrappedJmxInvokerName = new ObjectName JavaDoc(factoryName.getDomain(), props);
102          super.setInvokerName(wrappedJmxInvokerName);
103       }
104       catch(MalformedObjectNameException JavaDoc e)
105       {
106          throw new IllegalStateException JavaDoc("Was not able to create wrapped ObjectName");
107       }
108    }
109
110    /**
111     * @return the ObjectName the HAInvokerWrapper delegates to
112     */

113    public ObjectName JavaDoc getRealJmxInvokerName()
114    {
115       return realJmxInvokerName;
116    }
117
118    /**
119     *
120     */

121    protected ArrayList JavaDoc defineInterceptors()
122    {
123       ArrayList JavaDoc interceptorClasses = new ArrayList JavaDoc();
124       interceptorClasses.add(ClientMethodInterceptorHA.class);
125       interceptorClasses.add(InvokerInterceptor.class);
126       return interceptorClasses;
127    }
128
129    /** Override the HttpProxyFactory method to create a HttpInvokerProxyHA.
130     * @return
131     * @throws Exception
132     */

133    protected Invoker createInvoker() throws Exception JavaDoc
134    {
135       InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
136       HAPartition partition = (HAPartition) iniCtx.lookup("/HAPartition/" + partitionName);
137
138       /* Create a HATarget for the local invoker mbean stub. The name passed
139       to the HATarget must be the wrappedJmxInvokerName so that different
140       more than one HttpProxyFactoryHA may be configured for the same
141       realJmxInvokerName.
142       */

143       checkInvokerURL();
144       Serializable JavaDoc invokerStub = super.getInvokerURL();
145       invokerTarget = new HATarget(partition, wrappedJmxInvokerName.toString(),
146          invokerStub, HATarget.MAKE_INVOCATIONS_WAIT);
147       log.debug("Created invoker: "+invokerTarget);
148       // Create and register the invoker wrapper
149
MBeanServer JavaDoc mbeanServer = super.getServer();
150       invokerWrapper = new HAInvokerWrapper(mbeanServer, realJmxInvokerName, invokerTarget);
151       mbeanServer.registerMBean(invokerWrapper, wrappedJmxInvokerName);
152
153       // Create the LoadBalancePolicy instance
154
LoadBalancePolicy policy = (LoadBalancePolicy) policyClass.newInstance();
155
156       // Finally, create the invoker proxy, a HttpInvokerProxyHA
157
String JavaDoc clusterFamilyName = partitionName + "/" + wrappedJmxInvokerName.toString();
158       Invoker delegateInvoker = new HttpInvokerProxyHA(invokerTarget.getReplicants(), invokerTarget.getCurrentViewId (),
159                                                        policy, clusterFamilyName);
160       return delegateInvoker;
161    }
162
163    /** Override the HttpProxyFactory stop to unregister the HAInvokerWrapper
164     * mbean
165     *
166     * @throws Exception
167     */

168    protected void stopService() throws Exception JavaDoc
169    {
170       try
171       {
172          MBeanServer JavaDoc mbeanServer = super.getServer();
173          mbeanServer.unregisterMBean(wrappedJmxInvokerName);
174       }
175       catch(Exception JavaDoc e)
176       {
177          log.debug("Failed to unregister HAInvokerWrapper: "+wrappedJmxInvokerName, e);
178       }
179       super.stopService();
180    }
181
182    /** Destroys the HATarget
183     */

184    public void destroy()
185    {
186       super.destroy();
187       try
188       {
189          invokerTarget.destroy();
190       }
191       catch(Exception JavaDoc ignore)
192       {
193       }
194    }
195
196 }
197
Popular Tags