1 17 package org.apache.servicemix.sca; 18 19 import java.io.File ; 20 import java.io.FilenameFilter ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.Iterator ; 24 25 import javax.wsdl.Definition; 26 import javax.wsdl.factory.WSDLFactory; 27 28 import org.apache.servicemix.common.ServiceUnit; 29 import org.apache.servicemix.sca.assembly.JbiBinding; 30 import org.apache.servicemix.sca.tuscany.CommonsLoggingMonitorFactory; 31 import org.apache.servicemix.sca.tuscany.TuscanyRuntime; 32 import org.apache.tuscany.model.assembly.Binding; 33 import org.apache.tuscany.model.assembly.EntryPoint; 34 import org.apache.tuscany.model.assembly.Module; 35 36 public class ScaServiceUnit extends ServiceUnit { 37 38 protected static final ThreadLocal <ScaServiceUnit> SERVICE_UNIT = new ThreadLocal <ScaServiceUnit>(); 39 40 public static ScaServiceUnit getCurrentScaServiceUnit() { 41 return SERVICE_UNIT.get(); 42 } 43 44 protected TuscanyRuntime tuscanyRuntime; 45 protected ClassLoader classLoader; 46 47 public void init() throws Exception { 48 SERVICE_UNIT.set(this); 49 createScaRuntime(); 50 createEndpoints(); 51 SERVICE_UNIT.set(null); 52 } 53 54 protected void createScaRuntime() throws Exception { 55 File root = new File (getRootPath()); 56 File [] files = root.listFiles(new JarFileFilter()); 57 URL [] urls = new URL [files.length + 1]; 58 for (int i = 0; i < files.length; i++) { 59 urls[i] = files[i].toURL(); 60 } 61 urls[urls.length - 1] = root.toURL(); 62 classLoader = new URLClassLoader (urls, getClass().getClassLoader()); 63 64 tuscanyRuntime = new TuscanyRuntime(getName(), getRootPath(), classLoader, new CommonsLoggingMonitorFactory()); 65 } 66 67 protected void createEndpoints() throws Exception { 68 Module module = tuscanyRuntime.getModuleComponent().getModuleImplementation(); 69 for (Iterator i = module.getEntryPoints().iterator(); i.hasNext();) { 70 EntryPoint entryPoint = (EntryPoint) i.next(); 71 Binding binding = (Binding) entryPoint.getBindings().get(0); 72 if (binding instanceof JbiBinding) { 73 JbiBinding jbiBinding = (JbiBinding) binding; 74 ScaEndpoint endpoint = new ScaEndpoint(entryPoint); 75 endpoint.setServiceUnit(this); 76 endpoint.setService(jbiBinding.getServiceName()); 77 endpoint.setEndpoint(jbiBinding.getEndpointName()); 78 endpoint.setInterfaceName(jbiBinding.getInterfaceName()); 79 Definition definition = jbiBinding.getDefinition(); 80 if (definition != null) { 81 endpoint.setDefinition(definition); 82 endpoint.setDescription(WSDLFactory.newInstance().newWSDLWriter().getDocument(definition)); 83 } 84 addEndpoint(endpoint); 85 } 86 } 87 } 88 89 private static class JarFileFilter implements FilenameFilter { 90 public boolean accept(File dir, String name) { 91 return name.endsWith(".jar"); 92 } 93 } 94 95 public TuscanyRuntime getTuscanyRuntime() { 96 return tuscanyRuntime; 97 } 98 99 @Override 100 public void start() throws Exception { 101 tuscanyRuntime.start(); 102 super.start(); 103 } 104 105 @Override 106 public void stop() throws Exception { 107 super.stop(); 108 tuscanyRuntime.stop(); 109 } 110 111 } 112 | Popular Tags |