KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > sca > ScaServiceUnit


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.sca;
18
19 import java.io.File JavaDoc;
20 import java.io.FilenameFilter JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.Iterator JavaDoc;
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 JavaDoc<ScaServiceUnit> SERVICE_UNIT = new ThreadLocal JavaDoc<ScaServiceUnit>();
39     
40     public static ScaServiceUnit getCurrentScaServiceUnit() {
41         return SERVICE_UNIT.get();
42     }
43     
44     protected TuscanyRuntime tuscanyRuntime;
45     protected ClassLoader JavaDoc classLoader;
46     
47     public void init() throws Exception JavaDoc {
48         SERVICE_UNIT.set(this);
49         createScaRuntime();
50         createEndpoints();
51         SERVICE_UNIT.set(null);
52     }
53     
54     protected void createScaRuntime() throws Exception JavaDoc {
55         File JavaDoc root = new File JavaDoc(getRootPath());
56         File JavaDoc[] files = root.listFiles(new JarFileFilter());
57         URL JavaDoc[] urls = new URL JavaDoc[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 JavaDoc(urls, getClass().getClassLoader());
63         
64         tuscanyRuntime = new TuscanyRuntime(getName(), getRootPath(), classLoader, new CommonsLoggingMonitorFactory());
65     }
66     
67     protected void createEndpoints() throws Exception JavaDoc {
68         Module module = tuscanyRuntime.getModuleComponent().getModuleImplementation();
69         for (Iterator JavaDoc 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 JavaDoc {
90         public boolean accept(File JavaDoc dir, String JavaDoc name) {
91             return name.endsWith(".jar");
92         }
93     }
94
95     public TuscanyRuntime getTuscanyRuntime() {
96         return tuscanyRuntime;
97     }
98
99     @Override JavaDoc
100     public void start() throws Exception JavaDoc {
101         tuscanyRuntime.start();
102         super.start();
103     }
104
105     @Override JavaDoc
106     public void stop() throws Exception JavaDoc {
107         super.stop();
108         tuscanyRuntime.stop();
109     }
110
111 }
112
Popular Tags