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) throws DeploymentException {
130 String key = EndpointSupport.getKey(endpoint);
131 if (this.endpoints.put(key, endpoint) != null) {
132 throw new DeploymentException(
133 "More than one endpoint found in the SU for key: " + key);
134 }
135 if (this.status == LifeCycleMBean.STARTED) {
136 try {
137 endpoint.activate();
138 } catch (Exception e) {
139 throw new DeploymentException(e);
140 }
141 }
142 }
143
144 public void removeEndpoint(Endpoint endpoint) throws DeploymentException {
145 if (this.endpoints.remove(endpoint) != null) {
146 throw new DeploymentException("Endpoint not found in the SU for key: " + EndpointSupport.getKey(endpoint));
147 }
148 if (this.status == LifeCycleMBean.STARTED) {
149 try {
150 endpoint.deactivate();
151 } catch (Exception e) {
152 throw new DeploymentException(e);
153 }
154 }
155 }
156
157 public Endpoint getEndpoint(String key) {
158 return this.endpoints.get(key);
159 }
160
161 public ClassLoader getConfigurationClassLoader() {
162 return component.getClass().getClassLoader();
163 }
164
165 }