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:
018: /*
019: * ChainBuilder ESB
020: * Visual Enterprise Integration
021: *
022: * Copyright (C) 2006 Bostech Corporation
023: *
024: * This program is free software; you can redistribute it and/or modify it
025: * under the terms of the GNU General Public License as published by the
026: * Free Software Foundation; either version 2 of the License, or (at your option)
027: * any later version.
028: *
029: * This program is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
031: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
032: * for more details.
033: *
034: * You should have received a copy of the GNU General Public License along with
035: * this program; if not, write to the Free Software Foundation, Inc.,
036: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
037: *
038: * $Id: CbServiceUnit.java 11961 2008-02-15 18:55:09Z mpreston $
039: */
040: package com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging;
041:
042: import java.util.ArrayList;
043: import java.util.Collection;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.Map;
048:
049: import javax.jbi.JBIException;
050: import javax.jbi.management.LifeCycleMBean;
051:
052: public class CbServiceUnit {
053:
054: protected CbComponent component;
055: protected String name;
056: protected String rootPath;
057: protected String status = LifeCycleMBean.SHUTDOWN;
058: protected Map<String, CbEndpoint> endpoints = new HashMap<String, CbEndpoint>();
059: protected ServiceUnitDescriptor serviceUnitDescriptor;
060:
061: public CbServiceUnit() {
062: }
063:
064: public CbServiceUnit(CbComponent component) {
065: this .component = component;
066: }
067:
068: public void start() throws Exception {
069: // Activate endpoints
070: List<CbEndpoint> activated = new ArrayList<CbEndpoint>();
071: try {
072: for (Iterator iter = getEndpoints().iterator(); iter
073: .hasNext();) {
074: CbEndpoint endpoint = (CbEndpoint) iter.next();
075: activated.add(endpoint);
076: endpoint.activate();
077: }
078: this .status = LifeCycleMBean.STARTED;
079: } catch (Exception e) {
080: // Deactivate activated endpoints
081: for (CbEndpoint endpoint : activated) {
082: try {
083: endpoint.deactivate();
084: } catch (Exception e2) {
085: // do nothing
086: }
087: }
088: this .status = LifeCycleMBean.STOPPED;
089: throw e;
090: }
091: }
092:
093: public void stop() throws Exception {
094: this .status = LifeCycleMBean.STOPPED;
095: // Deactivate endpoints
096: Exception exception = null;
097: for (Iterator iter = getEndpoints().iterator(); iter.hasNext();) {
098: CbEndpoint endpoint = (CbEndpoint) iter.next();
099: try {
100: endpoint.deactivate();
101: } catch (Exception e) {
102: exception = e;
103: }
104: }
105: if (exception != null) {
106: throw exception;
107: }
108: }
109:
110: public void shutDown() throws JBIException {
111: this .status = LifeCycleMBean.SHUTDOWN;
112: }
113:
114: public String getCurrentState() {
115: return status;
116: }
117:
118: public String getName() {
119: return name;
120: }
121:
122: public void setName(String name) {
123: this .name = name;
124: }
125:
126: public String getRootPath() {
127: return rootPath;
128: }
129:
130: public void setRootPath(String rootPath) {
131: this .rootPath = rootPath;
132: }
133:
134: /**
135: * @return Returns the component.
136: */
137: public CbComponent getComponent() {
138: return component;
139: }
140:
141: /**
142: * @param component The component to set.
143: */
144: public void setComponent(CbComponent component) {
145: this .component = component;
146: }
147:
148: public Collection getEndpoints() {
149: return this .endpoints.values();
150: }
151:
152: public void addEndpoint(CbEndpoint endpoint) {
153: String key = EndpointKeyUtil.getKey(endpoint);
154: if (this .endpoints.put(key, endpoint) != null) {
155: throw new IllegalStateException(
156: "More than one endpoint found in the SU for key: "
157: + key);
158: }
159: }
160:
161: public CbEndpoint getEndpoint(String key) {
162: return this .endpoints.get(key);
163: }
164:
165: /**
166: * @return the serviceUnitDescriptor
167: */
168: public ServiceUnitDescriptor getServiceUnitDescriptor() {
169: return serviceUnitDescriptor;
170: }
171:
172: }
|