KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > common > ServiceUnit


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.common;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.jbi.management.LifeCycleMBean;
28
29 public class ServiceUnit {
30
31     protected BaseComponent component;
32     protected String JavaDoc name;
33     protected String JavaDoc rootPath;
34     protected String JavaDoc status = LifeCycleMBean.SHUTDOWN;
35     protected Map JavaDoc endpoints = new HashMap JavaDoc();
36     
37     public ServiceUnit() {
38     }
39     
40     public ServiceUnit(BaseComponent component) {
41         this.component = component;
42     }
43     
44     public void start() throws Exception JavaDoc {
45         // Activate endpoints
46
List JavaDoc activated = new ArrayList JavaDoc();
47         try {
48             for (Iterator JavaDoc iter = getEndpoints().iterator(); iter.hasNext();) {
49                 Endpoint endpoint = (Endpoint) iter.next();
50                 endpoint.activate();
51                 activated.add(endpoint);
52             }
53             this.status = LifeCycleMBean.STARTED;
54         } catch (Exception JavaDoc e) {
55             // Deactivate activated endpoints
56
for (Iterator JavaDoc iter = activated.iterator(); iter.hasNext();) {
57                 try {
58                     Endpoint endpoint = (Endpoint) iter.next();
59                     endpoint.deactivate();
60                 } catch (Exception JavaDoc e2) {
61                     // do nothing
62
}
63             }
64             throw e;
65         }
66     }
67     
68     public void stop() throws Exception JavaDoc {
69         this.status = LifeCycleMBean.STOPPED;
70         // Deactivate endpoints
71
Exception JavaDoc exception = null;
72         for (Iterator JavaDoc iter = getEndpoints().iterator(); iter.hasNext();) {
73             Endpoint endpoint = (Endpoint) iter.next();
74             try {
75                 endpoint.deactivate();
76             } catch (Exception JavaDoc e) {
77                 exception = e;
78             }
79         }
80         if (exception != null) {
81             throw exception;
82         }
83     }
84     
85     public void shutDown() throws JBIException {
86         this.status = LifeCycleMBean.SHUTDOWN;
87     }
88     
89     public String JavaDoc getCurrentState() {
90         return status;
91     }
92
93     public String JavaDoc getName() {
94         return name;
95     }
96
97     public void setName(String JavaDoc name) {
98         this.name = name;
99     }
100
101     public String JavaDoc getRootPath() {
102         return rootPath;
103     }
104
105     public void setRootPath(String JavaDoc rootPath) {
106         this.rootPath = rootPath;
107     }
108     
109     /**
110      * @return Returns the component.
111      */

112     public BaseComponent getComponent() {
113         return component;
114     }
115     
116     /**
117      * @param component The component to set.
118      */

119     public void setComponent(BaseComponent component) {
120         this.component = component;
121     }
122     
123     public Collection JavaDoc getEndpoints() {
124         return this.endpoints.values();
125     }
126     
127     public void addEndpoint(Endpoint endpoint) {
128         String JavaDoc key = EndpointSupport.getKey(endpoint);
129         if (this.endpoints.put(key, endpoint) != null) {
130             throw new IllegalStateException JavaDoc("More than one endpoint found in the SU for key: " + key);
131         }
132     }
133     
134     public Endpoint getEndpoint(String JavaDoc key) {
135         return (Endpoint) this.endpoints.get(key);
136     }
137
138
139 }
140
Popular Tags