001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.common;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.LinkedHashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.jbi.JBIException;
026: import javax.jbi.management.DeploymentException;
027: import javax.jbi.management.LifeCycleMBean;
028:
029: public class ServiceUnit {
030:
031: protected ServiceMixComponent component;
032:
033: protected String name;
034:
035: protected String rootPath;
036:
037: protected String status = LifeCycleMBean.SHUTDOWN;
038:
039: protected Map<String, Endpoint> endpoints = new LinkedHashMap<String, Endpoint>();
040:
041: public ServiceUnit() {
042: }
043:
044: public ServiceUnit(ServiceMixComponent component) {
045: this .component = component;
046: }
047:
048: public void start() throws Exception {
049: // Activate endpoints
050: List<Endpoint> activated = new ArrayList<Endpoint>();
051: try {
052: for (Endpoint endpoint : getEndpoints()) {
053: endpoint.activate();
054: activated.add(endpoint);
055: }
056: this .status = LifeCycleMBean.STARTED;
057: } catch (Exception e) {
058: // Deactivate activated endpoints
059: for (Endpoint endpoint : activated) {
060: try {
061: endpoint.deactivate();
062: } catch (Exception e2) {
063: // do nothing
064: }
065: }
066: throw e;
067: }
068: }
069:
070: public void stop() throws Exception {
071: this .status = LifeCycleMBean.STOPPED;
072: // Deactivate endpoints
073: Exception exception = null;
074: for (Endpoint endpoint : getEndpoints()) {
075: try {
076: endpoint.deactivate();
077: } catch (Exception e) {
078: exception = e;
079: }
080: }
081: if (exception != null) {
082: throw exception;
083: }
084: }
085:
086: public void shutDown() throws JBIException {
087: this .status = LifeCycleMBean.SHUTDOWN;
088: }
089:
090: public String getCurrentState() {
091: return status;
092: }
093:
094: public String getName() {
095: return name;
096: }
097:
098: public void setName(String name) {
099: this .name = name;
100: }
101:
102: public String getRootPath() {
103: return rootPath;
104: }
105:
106: public void setRootPath(String rootPath) {
107: this .rootPath = rootPath;
108: }
109:
110: /**
111: * @return Returns the component.
112: */
113: public ServiceMixComponent getComponent() {
114: return component;
115: }
116:
117: /**
118: * @param component
119: * The component to set.
120: */
121: public void setComponent(ServiceMixComponent component) {
122: this .component = component;
123: }
124:
125: public Collection<Endpoint> getEndpoints() {
126: return this .endpoints.values();
127: }
128:
129: public void addEndpoint(Endpoint endpoint)
130: throws DeploymentException {
131: String key = EndpointSupport.getKey(endpoint);
132: if (this .endpoints.put(key, endpoint) != null) {
133: throw new DeploymentException(
134: "More than one endpoint found in the SU for key: "
135: + key);
136: }
137: if (this .status == LifeCycleMBean.STARTED) {
138: try {
139: endpoint.activate();
140: } catch (Exception e) {
141: throw new DeploymentException(e);
142: }
143: }
144: }
145:
146: public void removeEndpoint(Endpoint endpoint)
147: throws DeploymentException {
148: if (this .endpoints.remove(endpoint) != null) {
149: throw new DeploymentException(
150: "Endpoint not found in the SU for key: "
151: + EndpointSupport.getKey(endpoint));
152: }
153: if (this .status == LifeCycleMBean.STARTED) {
154: try {
155: endpoint.deactivate();
156: } catch (Exception e) {
157: throw new DeploymentException(e);
158: }
159: }
160: }
161:
162: public Endpoint getEndpoint(String key) {
163: return this .endpoints.get(key);
164: }
165:
166: public ClassLoader getConfigurationClassLoader() {
167: return component.getClass().getClassLoader();
168: }
169:
170: }
|