KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30
31 import org.jboss.invocation.Invoker;
32 import org.jboss.invocation.InvokerInterceptor;
33 import org.jboss.invocation.http.interfaces.HttpInvokerProxy;
34 import org.jboss.invocation.http.interfaces.ClientMethodInterceptor;
35 import org.jboss.naming.Util;
36 import org.jboss.proxy.GenericProxyFactory;
37 import org.jboss.system.Registry;
38 import org.jboss.system.ServiceMBeanSupport;
39 import org.jboss.system.server.ServerConfigUtil;
40 import org.jboss.util.StringPropertyReplacer;
41 import org.jboss.metadata.MetaData;
42 import org.w3c.dom.Element JavaDoc;
43
44 /** Create an interface proxy that uses HTTP to communicate with the server
45  * side object that exposes the corresponding JMX invoke operation. Any request
46  * to this servlet receives a serialized object stream containing a
47  * MarshalledValue with the Naming proxy as its content.
48  *
49  * @author Scott.Stark@jboss.org
50  * @version $Revision: 37459 $
51  */

52 public class HttpProxyFactory extends ServiceMBeanSupport
53    implements HttpProxyFactoryMBean
54 {
55    /** The server side mbean that exposes the invoke operation for the
56     exported interface */

57    private ObjectName JavaDoc jmxInvokerName;
58    /** The Proxy object which uses the HttpInvokerProxy as its handler */
59    private Object JavaDoc theProxy;
60    /** The http URL to the InvokerServlet */
61    private String JavaDoc invokerURL;
62    /** The alternative prefix used to build the invokerURL */
63    private String JavaDoc invokerURLPrefix = "http://";
64    /** The alternative suffix used to build the invokerURL */
65    private String JavaDoc invokerURLSuffix = ":8080/invoker/JMXInvokerServlet";
66    /** The alternative host or ip flag used to build the invokerURL */
67    private boolean useHostName = false;
68    /** The JNDI name under which the HttpInvokerProxy will be bound */
69    private String JavaDoc jndiName;
70    /** The interface that the HttpInvokerProxy implements */
71    private Class JavaDoc exportedInterface;
72    private Element JavaDoc interceptorConfig;
73    private ArrayList JavaDoc interceptorClasses;
74
75    public HttpProxyFactory()
76    {
77    }
78
79    public ObjectName JavaDoc getInvokerName()
80    {
81       return jmxInvokerName;
82    }
83    public void setInvokerName(ObjectName JavaDoc jmxInvokerName)
84    {
85       this.jmxInvokerName = jmxInvokerName;
86    }
87
88    public String JavaDoc getJndiName()
89    {
90       return jndiName;
91    }
92    public void setJndiName(String JavaDoc jndiName)
93    {
94       this.jndiName = jndiName;
95    }
96
97    public String JavaDoc getInvokerURL()
98    {
99       return invokerURL;
100    }
101    public void setInvokerURL(String JavaDoc invokerURL)
102    {
103       // Replace any system properties in the URL
104
String JavaDoc tmp = StringPropertyReplacer.replaceProperties(invokerURL);
105       this.invokerURL = tmp;
106       log.debug("Set invokerURL to "+this.invokerURL);
107    }
108
109    public String JavaDoc getInvokerURLPrefix()
110    {
111       return invokerURLPrefix;
112    }
113    public void setInvokerURLPrefix(String JavaDoc invokerURLPrefix)
114    {
115       this.invokerURLPrefix = invokerURLPrefix;
116    }
117
118    public String JavaDoc getInvokerURLSuffix()
119    {
120       return invokerURLSuffix;
121    }
122    public void setInvokerURLSuffix(String JavaDoc invokerURLSuffix)
123    {
124       this.invokerURLSuffix = invokerURLSuffix;
125    }
126
127    public boolean getUseHostName()
128    {
129       return useHostName;
130    }
131    public void setUseHostName(boolean flag)
132    {
133       this.useHostName = flag;
134    }
135
136    public Class JavaDoc getExportedInterface()
137    {
138       return exportedInterface;
139    }
140    public void setExportedInterface(Class JavaDoc exportedInterface)
141    {
142       this.exportedInterface = exportedInterface;
143    }
144
145    public Element JavaDoc getClientInterceptors()
146    {
147       return interceptorConfig;
148    }
149    public void setClientInterceptors(Element JavaDoc config) throws Exception JavaDoc
150    {
151       this.interceptorConfig = config;
152       Iterator JavaDoc interceptorElements = MetaData.getChildrenByTagName(interceptorConfig, "interceptor");
153       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
154       if( interceptorClasses != null )
155          interceptorClasses.clear();
156       else
157          interceptorClasses = new ArrayList JavaDoc();
158       while( interceptorElements != null && interceptorElements.hasNext() )
159       {
160          Element JavaDoc ielement = (Element JavaDoc) interceptorElements.next();
161          String JavaDoc className = null;
162          className = MetaData.getElementContent(ielement);
163          Class JavaDoc clazz = loader.loadClass(className);
164          interceptorClasses.add(clazz);
165       }
166    }
167
168    public Object JavaDoc getProxy()
169    {
170       return theProxy;
171    }
172
173    public Object JavaDoc getProxy(Object JavaDoc id)
174    {
175       Class JavaDoc[] ifaces = {exportedInterface};
176       ArrayList JavaDoc interceptorClasses = null; //defineInterceptors();
177
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
178       GenericProxyFactory proxyFactory = new GenericProxyFactory();
179       Object JavaDoc newProxy = null;
180       /*
181       Object newProxy = proxyFactory.createProxy(id, jmxInvokerName,
182          null, null, null, interceptorClasses, loader, ifaces);
183          */

184       return newProxy;
185    }
186
187    /** Initializes the servlet.
188     */

189    protected void startService() throws Exception JavaDoc
190    {
191       /** Create an HttpInvokerProxy that posts invocations to the
192        externalURL. This proxy will be associated with a naming JMX invoker
193        given by the jmxInvokerName.
194        */

195       Invoker delegateInvoker = createInvoker();
196       Integer JavaDoc nameHash = new Integer JavaDoc(jmxInvokerName.hashCode());
197       log.debug("Bound delegate: "+delegateInvoker
198          +" for invoker="+jmxInvokerName);
199       /* Create a binding betweeh the invoker name hash and the jmx name
200       This is used by the HttpInvoker to map from the Invocation ObjectName
201       hash value to the target JMX ObjectName.
202       */

203       Registry.bind(nameHash, jmxInvokerName);
204
205       Object JavaDoc cacheID = null;
206       String JavaDoc proxyBindingName = null;
207       Class JavaDoc[] ifaces = {exportedInterface};
208       /* Initialize interceptorClasses with default client interceptor list
209          if no client interceptor configuration was provided */

210       if( interceptorClasses == null )
211          interceptorClasses = defineDefaultInterceptors();
212       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
213       GenericProxyFactory proxyFactory = new GenericProxyFactory();
214       theProxy = proxyFactory.createProxy(cacheID, jmxInvokerName,
215          delegateInvoker, jndiName, proxyBindingName, interceptorClasses,
216          loader, ifaces);
217       log.debug("Created HttpInvokerProxy for invoker="+jmxInvokerName
218          +", nameHash="+nameHash);
219
220       if( jndiName != null )
221       {
222          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
223          Util.bind(iniCtx, jndiName, theProxy);
224          log.debug("Bound proxy under jndiName="+jndiName);
225       }
226    }
227
228    protected void stopService() throws Exception JavaDoc
229    {
230       Integer JavaDoc nameHash = new Integer JavaDoc(jmxInvokerName.hashCode());
231       Registry.unbind(jmxInvokerName);
232       Registry.unbind(nameHash);
233       if( jndiName != null )
234       {
235          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
236          Util.unbind(iniCtx, jndiName);
237       }
238    }
239
240    /** Build the default interceptor list. This consists of:
241     * ClientMethodInterceptor
242     * InvokerInterceptor
243     */

244    protected ArrayList JavaDoc defineDefaultInterceptors()
245    {
246       ArrayList JavaDoc tmp = new ArrayList JavaDoc();
247       tmp.add(ClientMethodInterceptor.class);
248       tmp.add(InvokerInterceptor.class);
249       return tmp;
250    }
251
252    /** Create the Invoker
253     */

254    protected Invoker createInvoker() throws Exception JavaDoc
255    {
256       checkInvokerURL();
257       HttpInvokerProxy delegateInvoker = new HttpInvokerProxy(invokerURL);
258       return delegateInvoker;
259    }
260
261    /** Validate that the invokerURL is set, and if not build it from
262     * the invokerURLPrefix + host + invokerURLSuffix. The host value will be
263     * taken from the jboss.bind.address system property if its a valid
264     * address, InetAddress.getLocalHost otherwise.
265     */

266    protected void checkInvokerURL() throws UnknownHostException JavaDoc
267    {
268       if( invokerURL == null )
269       {
270          // First check for a global bind address
271
String JavaDoc host = ServerConfigUtil.getSpecificBindAddress();
272          if( host == null )
273          {
274             InetAddress JavaDoc addr = InetAddress.getLocalHost();
275             host = useHostName ? addr.getHostName() : addr.getHostAddress();
276          }
277          String JavaDoc url = invokerURLPrefix + host + invokerURLSuffix;
278          setInvokerURL(url);
279       }
280    }
281
282 }
283
284
Popular Tags