1 22 package org.objectweb.petals.binding.axis2.listener.soap; 23 24 import java.net.SocketException ; 25 26 import javax.xml.namespace.QName ; 27 28 import org.apache.axis2.AxisFault; 29 import org.apache.axis2.Constants; 30 import org.apache.axis2.addressing.EndpointReference; 31 import org.apache.axis2.context.ConfigurationContext; 32 import org.apache.axis2.description.Parameter; 33 import org.apache.axis2.description.TransportInDescription; 34 import org.apache.axis2.engine.ListenerManager; 35 import org.apache.axis2.transport.TransportListener; 36 import org.apache.axis2.transport.http.server.HttpUtils; 37 import org.mortbay.jetty.Server; 38 import org.mortbay.jetty.handler.ContextHandlerCollection; 39 import org.mortbay.jetty.servlet.Context; 40 import org.mortbay.jetty.servlet.ServletHolder; 41 42 50 public class AxisJettyServer implements TransportListener { 51 52 55 private Server server; 56 57 60 private String hostAddress = null; 61 62 65 protected int port; 66 67 70 protected ConfigurationContext configurationContext; 71 72 78 public AxisJettyServer(ConfigurationContext configurationContext, int port) 79 throws AxisFault { 80 this.port = port; 81 this.configurationContext = configurationContext; 82 this.server = new Server(this.port); 83 84 ContextHandlerCollection contexts = new ContextHandlerCollection(); 86 server.setHandler(contexts); 87 88 Context axis2 = new Context(contexts, "/axis2", Context.SESSIONS); 90 91 ServletHolder axisServlet = new ServletHolder(new AxisJettyServlet( 93 configurationContext)); 94 axisServlet.setName("AxisServlet"); 95 axisServlet.setInitOrder(1); 96 97 103 104 110 111 axis2.addServlet(axisServlet, "/services/*"); 113 axis2.addServlet(axisServlet, "/servlet/AxisServlet"); 115 117 ListenerManager listenerManager = configurationContext 119 .getListenerManager(); 120 TransportInDescription trsIn = new TransportInDescription(new QName ( 121 Constants.TRANSPORT_HTTP)); 122 trsIn.setReceiver(this); 123 if (listenerManager == null) { 124 listenerManager = new ListenerManager(); 125 listenerManager.init(configurationContext); 126 } 127 listenerManager.addListener(trsIn, true); 128 } 129 130 135 public void start() throws AxisFault { 136 Runnable httpListenerRunnable = new Runnable () { 138 public void run() { 139 try { 140 server.start(); 141 } catch (Exception e) { 142 e.printStackTrace(System.err); 143 } 144 } 145 }; 146 Thread httpThread = new Thread (httpListenerRunnable, 147 "Jetty started Thread"); 148 149 try { 150 httpThread.start(); 151 httpThread.join(); 152 } catch (InterruptedException e) { 153 throw new AxisFault(e.getMessage()); 154 } catch (Exception e) { 155 throw new AxisFault(e.getMessage()); 156 } 157 } 158 159 164 public void stop() throws AxisFault { 165 try { 166 server.stop(); 168 } catch (InterruptedException e) { 169 throw AxisFault.makeFault(e); 170 } catch (Exception e) { 171 throw AxisFault.makeFault(e); 172 } 173 } 174 175 181 public void init(ConfigurationContext axisConf, 182 TransportInDescription transprtIn) throws AxisFault { 183 try { 184 this.configurationContext = axisConf; 185 186 Parameter param = transprtIn.getParameter(PARAM_PORT); 187 188 if (param != null) { 189 this.port = Integer.parseInt((String ) param.getValue()); 190 } 191 param = transprtIn.getParameter(HOST_ADDRESS); 192 if (param != null) { 193 hostAddress = ((String ) param.getValue()).trim(); 194 } 195 } catch (Exception e) { 196 throw AxisFault.makeFault(e); 197 } 198 } 199 200 206 public EndpointReference[] getEPRsForService(String serviceName, String ip) 207 throws AxisFault { 208 if (hostAddress != null) { 210 if (server != null) { 211 return new EndpointReference[] {new EndpointReference( 212 hostAddress + "/" 213 + configurationContext.getServiceContextPath() + "/" 214 + serviceName)}; 215 } else { 216 throw new AxisFault( 217 "Unable to generate EPR for the transport : http"); 218 } 219 } 220 String localAddress; 222 if (ip != null) { 223 localAddress = ip; 224 } else { 225 try { 226 localAddress = HttpUtils.getIpAddress(); 227 if (localAddress == null) { 228 localAddress = "127.0.0.1"; 229 } 230 } catch (SocketException e) { 231 throw AxisFault.makeFault(e); 232 } 233 } 234 if (server != null) { 235 return new EndpointReference[] {new EndpointReference("http://" 236 + localAddress + ":" + port + "/" 237 + configurationContext.getServiceContextPath() + "/" 238 + serviceName)}; 239 } else { 240 throw new AxisFault( 241 "Unable to generate EPR for the transport : http"); 242 } 243 } 244 245 249 public ConfigurationContext getConfigurationContext() { 250 return this.configurationContext; 251 } 252 253 259 public EndpointReference getEPRForService(String serviceName, String ip) 260 throws AxisFault { 261 return getEPRsForService(serviceName, ip)[0]; 262 } 263 } 264 | Popular Tags |