1 22 package org.jboss.invocation.http.server; 23 24 import java.net.InetAddress ; 25 import java.net.UnknownHostException ; 26 import javax.management.MBeanException ; 27 import javax.management.ObjectName ; 28 import javax.management.RuntimeMBeanException ; 29 import javax.management.RuntimeOperationsException ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 import javax.transaction.Transaction ; 33 34 import org.jboss.invocation.Invocation; 35 import org.jboss.invocation.Invoker; 36 import org.jboss.invocation.MarshalledInvocation; 37 import org.jboss.invocation.http.interfaces.HttpInvokerProxy; 38 import org.jboss.system.Registry; 39 import org.jboss.system.ServiceMBeanSupport; 40 import org.jboss.system.server.ServerConfigUtil; 41 import org.jboss.util.StringPropertyReplacer; 42 43 49 public class HttpInvoker extends ServiceMBeanSupport 50 implements HttpInvokerMBean 51 { 52 private String invokerURL; 53 private String invokerURLPrefix = "http://"; 54 private String invokerURLSuffix = ":8080/invoker/JMXInvokerServlet"; 55 private boolean useHostName = false; 56 57 59 public String getInvokerURL() 60 { 61 return invokerURL; 62 } 63 public void setInvokerURL(String invokerURL) 64 { 65 String tmp = StringPropertyReplacer.replaceProperties(invokerURL); 67 this.invokerURL = tmp; 68 log.debug("Set invokerURL to "+this.invokerURL); 69 } 70 71 public String getInvokerURLPrefix() 72 { 73 return invokerURLPrefix; 74 } 75 public void setInvokerURLPrefix(String invokerURLPrefix) 76 { 77 this.invokerURLPrefix = invokerURLPrefix; 78 } 79 80 public String getInvokerURLSuffix() 81 { 82 return invokerURLSuffix; 83 } 84 public void setInvokerURLSuffix(String invokerURLSuffix) 85 { 86 this.invokerURLSuffix = invokerURLSuffix; 87 } 88 89 public boolean getUseHostName() 90 { 91 return useHostName; 92 } 93 public void setUseHostName(boolean flag) 94 { 95 this.useHostName = flag; 96 } 97 98 protected void startService() 99 throws Exception 100 { 101 checkInvokerURL(); 102 Invoker delegateInvoker = new HttpInvokerProxy(invokerURL); 103 104 ObjectName name = super.getServiceName(); 106 Registry.bind(name, delegateInvoker); 107 log.debug("Bound Http invoker for JMX node"); 108 } 109 110 protected void stopService() 111 { 112 try 113 { 114 InitialContext ctx = new InitialContext (); 115 ctx.unbind("invokers/"+InetAddress.getLocalHost().getHostName()+"/http"); 116 } 117 catch (NamingException ignore) 118 { 119 } 120 catch (Throwable e) 121 { 122 log.error("Failed", e); 123 return; 124 } 125 } 126 127 protected void destroyService() 128 { 129 Registry.unbind(serviceName); 131 } 132 133 136 public Object invoke(Invocation invocation) 137 throws Exception 138 { 139 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 140 try 141 { 142 MarshalledInvocation mi = (MarshalledInvocation) invocation; 144 Object tpc = mi.getTransactionPropagationContext(); 145 Transaction tx = importTPC(tpc); 146 invocation.setTransaction(tx); 147 148 Integer nameHash = (Integer ) invocation.getObjectName(); 149 ObjectName mbean = (ObjectName ) Registry.lookup(nameHash); 150 151 Object [] args = {invocation}; 153 String [] sig = {"org.jboss.invocation.Invocation"}; 154 Object obj = super.getServer().invoke(mbean, 155 "invoke", args, sig); 156 157 return obj; 159 } 160 catch (Exception e) 161 { 162 if (e instanceof MBeanException ) 163 e = ((MBeanException )e).getTargetException(); 164 165 if (e instanceof RuntimeMBeanException ) 166 e = ((RuntimeMBeanException )e).getTargetException(); 167 168 if (e instanceof RuntimeOperationsException ) 169 e = ((RuntimeOperationsException )e).getTargetException(); 170 171 if( log.isTraceEnabled() ) 173 log.trace("operation failed", e); 174 throw e; 175 } 176 finally 177 { 178 Thread.currentThread().setContextClassLoader(oldCl); 179 } 180 } 181 182 184 186 188 190 protected Transaction importTPC(Object tpc) 191 { 192 return null; 193 } 194 195 200 protected void checkInvokerURL() throws UnknownHostException 201 { 202 if( invokerURL == null ) 203 { 204 InetAddress addr = InetAddress.getLocalHost(); 205 String host = ServerConfigUtil.getSpecificBindAddress(); 207 if( host == null ) 208 { 209 host = useHostName ? addr.getHostName() : addr.getHostAddress(); 210 } 211 String url = invokerURLPrefix + host + invokerURLSuffix; 212 setInvokerURL(url); 213 } 214 } 215 } 216 217 | Popular Tags |