001: package org.objectweb.celtix.routing;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.net.URLClassLoader;
008: import java.util.ArrayList;
009: import java.util.List;
010:
011: import javax.wsdl.Definition;
012: import javax.wsdl.WSDLException;
013: import javax.xml.ws.WebServiceException;
014:
015: import org.objectweb.celtix.Bus;
016: import org.objectweb.celtix.BusException;
017: import org.objectweb.celtix.configuration.Configuration;
018: import org.objectweb.celtix.configuration.ConfigurationBuilder;
019: import org.objectweb.celtix.configuration.ConfigurationBuilderFactory;
020: import org.objectweb.celtix.routing.configuration.UrlListPolicy;
021: import org.objectweb.celtix.tools.WSDLToJava;
022: import org.objectweb.celtix.tools.common.ToolConstants;
023: import org.objectweb.celtix.tools.common.toolspec.ToolRunner;
024:
025: public class RouterManager {
026: public static final String ROUTING_CONFIGURATION_URI = "http://celtix.objectweb.org/routing/configuration";
027: public static final String ROUTING_CONFIGURATION_ID = "router";
028: public static final String ROUTING_WSDL_ID = "routesWSDL";
029: public static final String ROUTER_CONFIG_RESOURCE = "config-metadata/router-config.xml";
030: private final Bus bus;
031: private final Configuration config;
032: private URLClassLoader seiClassLoader;
033: private RouterFactory factory;
034: private List<Definition> wsdlModelList;
035: private List<Router> routerList;
036:
037: public RouterManager(Bus b) {
038: bus = b;
039: config = createConfiguration();
040: wsdlModelList = new ArrayList<Definition>();
041: routerList = new ArrayList<Router>();
042: }
043:
044: private Configuration createConfiguration() {
045:
046: Configuration busCfg = bus.getConfiguration();
047: assert null != busCfg;
048: Configuration cfg = null;
049: ConfigurationBuilder cb = ConfigurationBuilderFactory
050: .getBuilder(null);
051: cb.addModel(ROUTER_CONFIG_RESOURCE);
052: cfg = cb.getConfiguration(ROUTING_CONFIGURATION_URI,
053: ROUTING_CONFIGURATION_ID, busCfg);
054: if (null == cfg) {
055: cfg = cb.buildConfiguration(ROUTING_CONFIGURATION_URI,
056: ROUTING_CONFIGURATION_ID, busCfg);
057: }
058: return cfg;
059: }
060:
061: private URLClassLoader createSEIClassLoader(File classDir) {
062:
063: URLClassLoader loader = null;
064: try {
065: loader = URLClassLoader.newInstance(new URL[] { classDir
066: .toURL() }, Thread.currentThread()
067: .getContextClassLoader());
068: } catch (MalformedURLException mue) {
069: throw new WebServiceException(
070: "URLClassLoader creation failed", mue);
071: }
072:
073: return loader;
074: }
075:
076: private void loadWSDL() {
077: try {
078: List<String> wsdlUrlList = getRouteWSDLList();
079: for (String wsdlUrl : wsdlUrlList) {
080: URL url = getClass().getResource(wsdlUrl);
081: //String url = getFile(wsdlUrl);
082: wsdlModelList.add(bus.getWSDLManager().getDefinition(
083: url));
084: }
085: } catch (WSDLException we) {
086: throw new WebServiceException("Could not load router wsdl",
087: we);
088: }
089: }
090:
091: private void addRoutes() {
092: for (Definition def : wsdlModelList) {
093: List<Router> rList = factory.addRoutes(def);
094: routerList.addAll(rList);
095: }
096: }
097:
098: protected void publishRoutes() {
099: for (Router r : routerList) {
100: r.publish();
101: }
102: }
103:
104: protected void invokeWSDLToJava(File srcDir, File classDir) {
105: List<String> wsdlUrlList = getRouteWSDLList();
106:
107: for (String wsdlUrl : wsdlUrlList) {
108: invokeWSDLToJava(wsdlUrl, srcDir, classDir);
109: }
110: }
111:
112: private void invokeWSDLToJava(String wsdlUrl, File srcDir,
113: File classDir) {
114: try {
115: String file = getFile(wsdlUrl);
116: if (null != file) {
117: String[] args = new String[] { "-compile", "-d",
118: srcDir.getCanonicalPath(), "-classdir",
119: classDir.getCanonicalPath(), file };
120:
121: ToolRunner
122: .runTool(
123: WSDLToJava.class,
124: WSDLToJava.class
125: .getResourceAsStream(ToolConstants.TOOLSPECS_BASE
126: + "wsdl2java.xml"),
127: false, 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(
154: "Unable to create directory as a file "
155: + "already exists with that name: "
156: + dir.getAbsolutePath());
157: }
158:
159: if (!dir.exists()) {
160: dir.mkdirs();
161: }
162: }
163:
164: public void init() {
165: factory = new RouterFactory(this );
166: factory.init(bus);
167:
168: loadWSDL();
169:
170: File opDir = new File(System.getProperty("user.dir"),
171: "/celtix-router-tmp");
172: File classDir = new File(opDir, "/classes");
173: mkDir(classDir);
174:
175: invokeWSDLToJava(opDir, classDir);
176: seiClassLoader = createSEIClassLoader(classDir);
177:
178: addRoutes();
179: publishRoutes();
180: }
181:
182: public List<String> getRouteWSDLList() {
183: UrlListPolicy urlList = config.getObject(UrlListPolicy.class,
184: ROUTING_WSDL_ID);
185: if (null == urlList) {
186: throw new WebServiceException("Router WSDL not specified");
187: }
188: return urlList.getUrl();
189: }
190:
191: public RouterFactory getRouterFactory() {
192: return factory;
193: }
194:
195: public List<Router> getRouters() {
196: return routerList;
197: }
198:
199: public URLClassLoader getSEIClassLoader() {
200: return seiClassLoader;
201: }
202:
203: public static void main(String[] args) {
204: try {
205: Bus bus = Bus.init(args);
206: RouterManager rm = new RouterManager(bus);
207: rm.init();
208: bus.run();
209: } catch (BusException be) {
210: throw new WebServiceException("Could not initialize bus",
211: be);
212: }
213: }
214: }
|