KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > geronimo > ServiceMixGBean


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.geronimo;
18
19 import java.io.File JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import javax.jbi.JBIException;
23 import javax.resource.spi.work.WorkManager JavaDoc;
24 import javax.transaction.TransactionManager JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.geronimo.gbean.GBeanInfo;
29 import org.apache.geronimo.gbean.GBeanInfoBuilder;
30 import org.apache.geronimo.gbean.GBeanLifecycle;
31 import org.apache.geronimo.kernel.Kernel;
32 import org.apache.geronimo.transaction.context.GeronimoTransactionManager;
33 import org.apache.geronimo.transaction.context.TransactionContextManager;
34 import org.apache.servicemix.jbi.container.ComponentEnvironment;
35 import org.apache.servicemix.jbi.container.JBIContainer;
36 import org.apache.servicemix.jbi.container.ServiceAssemblyEnvironment;
37 import org.apache.servicemix.jbi.framework.ComponentContextImpl;
38 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl;
39 import org.apache.servicemix.jbi.framework.ComponentNameSpace;
40 import org.apache.servicemix.jbi.framework.ServiceAssemblyLifeCycle;
41
42 public class ServiceMixGBean implements GBeanLifecycle, Container {
43
44     private Log log = LogFactory.getLog(getClass().getName());
45     
46     private JBIContainer container;
47     private String JavaDoc name;
48     private String JavaDoc directory;
49     private TransactionContextManager transactionContextManager;
50     private WorkManager JavaDoc workManager;
51     private Kernel kernel;
52     private Collection JavaDoc jndiResources;
53
54     public static final GBeanInfo GBEAN_INFO;
55
56     static {
57         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ServiceMix JBI Container", ServiceMixGBean.class, "JBIContainer");
58         infoFactory.addInterface(Container.class);
59         infoFactory.addAttribute("name", String JavaDoc.class, true);
60         infoFactory.addAttribute("directory", String JavaDoc.class, true);
61         infoFactory.addReference("transactionContextManager", TransactionContextManager.class);
62         infoFactory.addReference("workManager", WorkManager JavaDoc.class);
63         infoFactory.addAttribute("kernel", Kernel.class, false);
64         infoFactory.setConstructor(new String JavaDoc[]{"name", "directory", "transactionContextManager", "workManager", "kernel"});
65         GBEAN_INFO = infoFactory.getBeanInfo();
66     }
67
68     public static GBeanInfo getGBeanInfo() {
69         return GBEAN_INFO;
70     }
71
72     public ServiceMixGBean(String JavaDoc name,
73                            String JavaDoc directory,
74                            TransactionContextManager transactionContextManager,
75                            WorkManager JavaDoc workManager,
76                            Kernel kernel) {
77         this.name = name;
78         this.directory = directory;
79         this.transactionContextManager = transactionContextManager;
80         this.workManager = workManager;
81         this.kernel = kernel;
82         if (log.isDebugEnabled()) {
83             log.debug("ServiceMixGBean created");
84         }
85         /*
86         // Print available jndi resources
87         Set patterns = new HashSet();
88         this.jndiResources = kernel.listGBeans(patterns);
89         for (Iterator it = jndiResources.iterator(); it.hasNext();) {
90             ObjectName name = (ObjectName) it.next();
91             log.info("Resource name: " + name);
92             log.info("Resource inst: " + kernel.)
93         }
94         */

95     }
96     
97     /**
98      * Starts the GBean. This informs the GBean that it is about to transition to the running state.
99      *
100      * @throws Exception if the target failed to start; this will cause a transition to the failed state
101      */

102     public void doStart() throws Exception JavaDoc {
103         if (log.isDebugEnabled()) {
104             log.debug("ServiceMixGBean doStart");
105         }
106         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
107         Thread.currentThread().setContextClassLoader(ServiceMixGBean.class.getClassLoader());
108         try {
109             if (container == null) {
110                 container = createContainer();
111                 container.init();
112                 container.start();
113             }
114         } finally {
115             Thread.currentThread().setContextClassLoader(old);
116         }
117     }
118
119     /**
120      * Stops the target. This informs the GBean that it is about to transition to the stopped state.
121      *
122      * @throws Exception if the target failed to stop; this will cause a transition to the failed state
123      */

124     public void doStop() throws Exception JavaDoc {
125         if (log.isDebugEnabled()) {
126             log.debug("ServiceMixGBean doStop");
127         }
128         try {
129             if (container != null) {
130                 container.shutDown();
131             }
132         } finally {
133             container = null;
134         }
135     }
136
137     /**
138      * Fails the GBean. This informs the GBean that it is about to transition to the failed state.
139      */

140     public void doFail() {
141         if (log.isDebugEnabled()) {
142             log.debug("ServiceMixGBean doFail");
143         }
144         try {
145             if (container != null) {
146                 try {
147                     container.shutDown();
148                 }
149                 catch (JBIException e) {
150                     log.info("Caught while closing due to failure: " + e, e);
151                 }
152             }
153         } finally {
154             container = null;
155         }
156     }
157
158     private JBIContainer createContainer() {
159         JBIContainer container = new JBIContainer();
160         container.setName(name);
161         container.setRootDir(directory);
162         container.setTransactionManager(getTransactionManager());
163         container.setMonitorInstallationDirectory(false);
164         container.setMonitorDeploymentDirectory(false);
165         container.setWorkManager(workManager);
166         return container;
167     }
168     
169     public TransactionManager JavaDoc getTransactionManager() {
170         if (transactionContextManager != null) {
171             return new GeronimoTransactionManager(transactionContextManager);
172         }
173         return null;
174     }
175     
176     public void register(Component component) throws Exception JavaDoc {
177         ComponentNameSpace cns = new ComponentNameSpace(container.getName(), component.getName());
178         ComponentContextImpl context = new ComponentContextImpl(container, cns);
179         ComponentEnvironment env = new ComponentEnvironment();
180         env.setComponentRoot(new File JavaDoc(component.getRootDir()));
181         env.setInstallRoot(new File JavaDoc(component.getInstallDir()));
182         env.setWorkspaceRoot(new File JavaDoc(component.getWorkDir()));
183         context.setEnvironment(env);
184         
185         container.activateComponent(null,
186                                     component.getComponent(),
187                                     component.getDescription(),
188                                     context,
189                                     component.getType().equals("binding-component"),
190                                     component.getType().equals("service-engine"),
191                                     null);
192         ComponentMBeanImpl cmb = container.getComponent(component.getName());
193         File JavaDoc stateFile = cmb.getContext().getEnvironment().getStateFile();
194         if (stateFile.isFile()) {
195             cmb.setInitialRunningState();
196         } else {
197             cmb.start();
198         }
199     }
200
201     public void unregister(Component component) throws Exception JavaDoc {
202         container.deactivateComponent(component.getName());
203     }
204     
205     public void register(ServiceAssembly assembly) throws Exception JavaDoc {
206         File JavaDoc rootDir = new File JavaDoc(assembly.getRootDir());
207         ServiceAssemblyEnvironment env = new ServiceAssemblyEnvironment();
208         env.setRootDir(rootDir);
209         env.setInstallDir(new File JavaDoc(rootDir, "install"));
210         env.setSusDir(new File JavaDoc(rootDir, "sus"));
211         env.setStateFile(new File JavaDoc(rootDir, "state.xml"));
212         ServiceAssemblyLifeCycle salc = container.getRegistry().registerServiceAssembly(assembly.getDescriptor().getServiceAssembly(), env);
213         if (env.getStateFile().isFile()) {
214             salc.restore();
215         } else {
216             salc.start();
217         }
218     }
219     
220     public void unregister(ServiceAssembly assembly) throws Exception JavaDoc {
221         ServiceAssemblyLifeCycle salc = container.getRegistry().getServiceAssembly(assembly.getName());
222         salc.shutDown(false);
223         container.getRegistry().unregisterServiceAssembly(assembly.getName());
224     }
225
226 }
227
Popular Tags