1 22 package org.jboss.invocation.http.server; 23 24 import java.net.InetAddress ; 25 import java.net.UnknownHostException ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import javax.management.ObjectName ; 29 import javax.naming.InitialContext ; 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 ; 43 44 52 public class HttpProxyFactory extends ServiceMBeanSupport 53 implements HttpProxyFactoryMBean 54 { 55 57 private ObjectName jmxInvokerName; 58 59 private Object theProxy; 60 61 private String invokerURL; 62 63 private String invokerURLPrefix = "http://"; 64 65 private String invokerURLSuffix = ":8080/invoker/JMXInvokerServlet"; 66 67 private boolean useHostName = false; 68 69 private String jndiName; 70 71 private Class exportedInterface; 72 private Element interceptorConfig; 73 private ArrayList interceptorClasses; 74 75 public HttpProxyFactory() 76 { 77 } 78 79 public ObjectName getInvokerName() 80 { 81 return jmxInvokerName; 82 } 83 public void setInvokerName(ObjectName jmxInvokerName) 84 { 85 this.jmxInvokerName = jmxInvokerName; 86 } 87 88 public String getJndiName() 89 { 90 return jndiName; 91 } 92 public void setJndiName(String jndiName) 93 { 94 this.jndiName = jndiName; 95 } 96 97 public String getInvokerURL() 98 { 99 return invokerURL; 100 } 101 public void setInvokerURL(String invokerURL) 102 { 103 String tmp = StringPropertyReplacer.replaceProperties(invokerURL); 105 this.invokerURL = tmp; 106 log.debug("Set invokerURL to "+this.invokerURL); 107 } 108 109 public String getInvokerURLPrefix() 110 { 111 return invokerURLPrefix; 112 } 113 public void setInvokerURLPrefix(String invokerURLPrefix) 114 { 115 this.invokerURLPrefix = invokerURLPrefix; 116 } 117 118 public String getInvokerURLSuffix() 119 { 120 return invokerURLSuffix; 121 } 122 public void setInvokerURLSuffix(String 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 getExportedInterface() 137 { 138 return exportedInterface; 139 } 140 public void setExportedInterface(Class exportedInterface) 141 { 142 this.exportedInterface = exportedInterface; 143 } 144 145 public Element getClientInterceptors() 146 { 147 return interceptorConfig; 148 } 149 public void setClientInterceptors(Element config) throws Exception 150 { 151 this.interceptorConfig = config; 152 Iterator interceptorElements = MetaData.getChildrenByTagName(interceptorConfig, "interceptor"); 153 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 154 if( interceptorClasses != null ) 155 interceptorClasses.clear(); 156 else 157 interceptorClasses = new ArrayList (); 158 while( interceptorElements != null && interceptorElements.hasNext() ) 159 { 160 Element ielement = (Element ) interceptorElements.next(); 161 String className = null; 162 className = MetaData.getElementContent(ielement); 163 Class clazz = loader.loadClass(className); 164 interceptorClasses.add(clazz); 165 } 166 } 167 168 public Object getProxy() 169 { 170 return theProxy; 171 } 172 173 public Object getProxy(Object id) 174 { 175 Class [] ifaces = {exportedInterface}; 176 ArrayList interceptorClasses = null; ClassLoader loader = Thread.currentThread().getContextClassLoader(); 178 GenericProxyFactory proxyFactory = new GenericProxyFactory(); 179 Object newProxy = null; 180 184 return newProxy; 185 } 186 187 189 protected void startService() throws Exception 190 { 191 195 Invoker delegateInvoker = createInvoker(); 196 Integer nameHash = new Integer (jmxInvokerName.hashCode()); 197 log.debug("Bound delegate: "+delegateInvoker 198 +" for invoker="+jmxInvokerName); 199 203 Registry.bind(nameHash, jmxInvokerName); 204 205 Object cacheID = null; 206 String proxyBindingName = null; 207 Class [] ifaces = {exportedInterface}; 208 210 if( interceptorClasses == null ) 211 interceptorClasses = defineDefaultInterceptors(); 212 ClassLoader 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 iniCtx = new InitialContext (); 223 Util.bind(iniCtx, jndiName, theProxy); 224 log.debug("Bound proxy under jndiName="+jndiName); 225 } 226 } 227 228 protected void stopService() throws Exception 229 { 230 Integer nameHash = new Integer (jmxInvokerName.hashCode()); 231 Registry.unbind(jmxInvokerName); 232 Registry.unbind(nameHash); 233 if( jndiName != null ) 234 { 235 InitialContext iniCtx = new InitialContext (); 236 Util.unbind(iniCtx, jndiName); 237 } 238 } 239 240 244 protected ArrayList defineDefaultInterceptors() 245 { 246 ArrayList tmp = new ArrayList (); 247 tmp.add(ClientMethodInterceptor.class); 248 tmp.add(InvokerInterceptor.class); 249 return tmp; 250 } 251 252 254 protected Invoker createInvoker() throws Exception 255 { 256 checkInvokerURL(); 257 HttpInvokerProxy delegateInvoker = new HttpInvokerProxy(invokerURL); 258 return delegateInvoker; 259 } 260 261 266 protected void checkInvokerURL() throws UnknownHostException 267 { 268 if( invokerURL == null ) 269 { 270 String host = ServerConfigUtil.getSpecificBindAddress(); 272 if( host == null ) 273 { 274 InetAddress addr = InetAddress.getLocalHost(); 275 host = useHostName ? addr.getHostName() : addr.getHostAddress(); 276 } 277 String url = invokerURLPrefix + host + invokerURLSuffix; 278 setInvokerURL(url); 279 } 280 } 281 282 } 283 284 | Popular Tags |