1 package org.objectweb.celtix.routing; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.net.MalformedURLException ; 6 import java.net.URL ; 7 import java.net.URLClassLoader ; 8 import java.util.ArrayList ; 9 import java.util.List ; 10 11 import javax.wsdl.Definition; 12 import javax.wsdl.WSDLException; 13 import javax.xml.ws.WebServiceException; 14 15 import org.objectweb.celtix.Bus; 16 import org.objectweb.celtix.BusException; 17 import org.objectweb.celtix.configuration.Configuration; 18 import org.objectweb.celtix.configuration.ConfigurationBuilder; 19 import org.objectweb.celtix.configuration.ConfigurationBuilderFactory; 20 import org.objectweb.celtix.routing.configuration.UrlListPolicy; 21 import org.objectweb.celtix.tools.WSDLToJava; 22 import org.objectweb.celtix.tools.common.ToolConstants; 23 import org.objectweb.celtix.tools.common.toolspec.ToolRunner; 24 25 public class RouterManager { 26 public static final String ROUTING_CONFIGURATION_URI = 27 "http://celtix.objectweb.org/routing/configuration"; 28 public static final String ROUTING_CONFIGURATION_ID = 29 "router"; 30 public static final String ROUTING_WSDL_ID = 31 "routesWSDL"; 32 public static final String ROUTER_CONFIG_RESOURCE = 33 "config-metadata/router-config.xml"; 34 private final Bus bus; 35 private final Configuration config; 36 private URLClassLoader seiClassLoader; 37 private RouterFactory factory; 38 private List <Definition> wsdlModelList; 39 private List <Router> routerList; 40 41 42 public RouterManager(Bus b) { 43 bus = b; 44 config = createConfiguration(); 45 wsdlModelList = new ArrayList <Definition>(); 46 routerList = new ArrayList <Router>(); 47 } 48 49 private Configuration createConfiguration() { 50 51 Configuration busCfg = bus.getConfiguration(); 52 assert null != busCfg; 53 Configuration cfg = null; 54 ConfigurationBuilder cb = ConfigurationBuilderFactory.getBuilder(null); 55 cb.addModel(ROUTER_CONFIG_RESOURCE); 56 cfg = cb.getConfiguration(ROUTING_CONFIGURATION_URI, 57 ROUTING_CONFIGURATION_ID, 58 busCfg); 59 if (null == cfg) { 60 cfg = cb.buildConfiguration(ROUTING_CONFIGURATION_URI, 61 ROUTING_CONFIGURATION_ID, 62 busCfg); 63 } 64 return cfg; 65 } 66 67 private URLClassLoader createSEIClassLoader(File classDir) { 68 69 URLClassLoader loader = null; 70 try { 71 loader = URLClassLoader.newInstance(new URL []{classDir.toURL()}, 72 Thread.currentThread().getContextClassLoader()); 73 } catch (MalformedURLException mue) { 74 throw new WebServiceException("URLClassLoader creation failed", mue); 75 } 76 77 return loader; 78 } 79 80 private void loadWSDL() { 81 try { 82 List <String > wsdlUrlList = getRouteWSDLList(); 83 for (String wsdlUrl : wsdlUrlList) { 84 URL url = getClass().getResource(wsdlUrl); 85 wsdlModelList.add(bus.getWSDLManager().getDefinition(url)); 87 } 88 } catch (WSDLException we) { 89 throw new WebServiceException("Could not load router wsdl", we); 90 } 91 } 92 93 private void addRoutes() { 94 for (Definition def : wsdlModelList) { 95 List <Router> rList = factory.addRoutes(def); 96 routerList.addAll(rList); 97 } 98 } 99 100 protected void publishRoutes() { 101 for (Router r : routerList) { 102 r.publish(); 103 } 104 } 105 106 protected void invokeWSDLToJava(File srcDir, File classDir) { 107 List <String > wsdlUrlList = getRouteWSDLList(); 108 109 for (String wsdlUrl : wsdlUrlList) { 110 invokeWSDLToJava(wsdlUrl, srcDir, classDir); 111 } 112 } 113 114 private void invokeWSDLToJava(String wsdlUrl, File srcDir, File classDir) { 115 try { 116 String file = getFile(wsdlUrl); 117 if (null != file) { 118 String [] args = new String []{"-compile", 119 "-d", srcDir.getCanonicalPath(), 120 "-classdir", classDir.getCanonicalPath(), 121 file}; 122 123 ToolRunner.runTool(WSDLToJava.class, 124 WSDLToJava.class.getResourceAsStream(ToolConstants.TOOLSPECS_BASE 125 + "wsdl2java.xml"), 126 false, 127 args); 128 } 129 } catch (Exception ex) { 130 throw new WebServiceException("wsdl2java exception", ex); 131 } 132 } 133 134 private String getFile(String wsdlUrl) { 135 try { 136 URL url = getClass().getResource(wsdlUrl); 137 File f = new File (url.getFile()); 138 if (f.exists()) { 139 wsdlUrl = f.getCanonicalPath(); 140 } 141 } catch (IOException ioe) { 142 throw new WebServiceException("Could not load wsdl", ioe); 143 } 144 return wsdlUrl; 145 } 146 147 private void mkDir(File dir) { 148 if (dir == null) { 149 throw new WebServiceException("Could not create dir"); 150 } 151 152 if (dir.isFile()) { 153 throw new WebServiceException("Unable to create directory as a file " 154 + "already exists with that name: " 155 + dir.getAbsolutePath()); 156 } 157 158 if (!dir.exists()) { 159 dir.mkdirs(); 160 } 161 } 162 163 public void init() { 164 factory = new RouterFactory(this); 165 factory.init(bus); 166 167 loadWSDL(); 168 169 File opDir = new File (System.getProperty("user.dir"), "/celtix-router-tmp"); 170 File classDir = new File (opDir, "/classes"); 171 mkDir(classDir); 172 173 invokeWSDLToJava(opDir, classDir); 174 seiClassLoader = createSEIClassLoader(classDir); 175 176 addRoutes(); 177 publishRoutes(); 178 } 179 180 public List <String > getRouteWSDLList() { 181 UrlListPolicy urlList = config.getObject(UrlListPolicy.class, ROUTING_WSDL_ID); 182 if (null == urlList) { 183 throw new WebServiceException("Router WSDL not specified"); 184 } 185 return urlList.getUrl(); 186 } 187 188 public RouterFactory getRouterFactory() { 189 return factory; 190 } 191 192 public List <Router> getRouters() { 193 return routerList; 194 } 195 196 public URLClassLoader getSEIClassLoader() { 197 return seiClassLoader; 198 } 199 200 public static void main(String [] args) { 201 try { 202 Bus bus = Bus.init(args); 203 RouterManager rm = new RouterManager(bus); 204 rm.init(); 205 bus.run(); 206 } catch (BusException be) { 207 throw new WebServiceException("Could not initialize bus", be); 208 } 209 } 210 } 211 | Popular Tags |