001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ServiceManager.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.sequencing;
030:
031: import com.sun.jbi.engine.sequencing.servicelist.ServicelistBean;
032: import com.sun.jbi.engine.sequencing.util.StringTranslator;
033: import com.sun.jbi.engine.sequencing.util.UtilBase;
034:
035: import java.util.Iterator;
036: import java.util.Vector;
037: import java.util.logging.Logger;
038:
039: import javax.jbi.component.ComponentContext;
040: import javax.jbi.servicedesc.ServiceEndpoint;
041:
042: import javax.xml.namespace.QName;
043:
044: /**
045: * Manages all the stopping and starting of services.
046: *
047: * @author Sun Microsystems, Inc.
048: */
049: public class ServiceManager extends UtilBase implements
050: SequencingEngineResources {
051: /**
052: * Wait time for thread.
053: */
054: private static final long WAIT_TIME = 2000;
055:
056: /**
057: * Binding Channel.
058: */
059: private ComponentContext mContext;
060:
061: /**
062: * Deployment Registry.
063: */
064: private DeploymentRegistry mRegistry;
065:
066: /**
067: * Logger object.
068: */
069: private Logger mLog;
070:
071: /**
072: * Translator object for internationalization.
073: */
074: private StringTranslator mTranslator;
075:
076: /**
077: * List of active services.
078: */
079: private Vector mActiveServices;
080:
081: /**
082: * Creates a new ServiceManager object.
083: */
084: public ServiceManager() {
085: mRegistry = DeploymentRegistry.getInstance();
086: mActiveServices = new Vector();
087: mLog = SequencingEngineContext.getInstance().getLogger();
088: mTranslator = new StringTranslator();
089: }
090:
091: /**
092: * Sets the channel object.
093: *
094: * @param ctx engine channel object.
095: */
096: public void setContext(ComponentContext ctx) {
097: mContext = ctx;
098: }
099:
100: /**
101: * Returns the number of services actually running.
102: *
103: * @return int number of running services.
104: */
105: public int getRunningServicesCount() {
106: int count = 0;
107:
108: return count;
109: }
110:
111: /**
112: * Activates all the registered Services.
113: */
114: public void activateServices() {
115: /*Query Registry and get services
116: */
117: Iterator iter = mRegistry.listAllServices();
118:
119: if (iter == null) {
120: mLog.info(mTranslator.getString(SEQ_NO_SERVICES));
121:
122: return;
123: }
124:
125: while (iter.hasNext()) {
126: String ep = (String) iter.next();
127: ServicelistBean eb = mRegistry.findService(ep);
128:
129: if (eb == null) {
130: mLog.severe(mTranslator.getString(
131: SEQ_START_SERVICE_FAILED, ep));
132: setError(mTranslator.getString(
133: SEQ_START_SERVICE_FAILED, ep));
134:
135: continue;
136: }
137:
138: startDeployment(eb.getDeploymentId());
139: }
140:
141: mLog.fine(mTranslator.getString(SEQ_ACTIVATE_SERVICES_DONE));
142: }
143:
144: /**
145: * Starts a deployment.
146: *
147: * @param asid sub-assembly id
148: */
149: public void startDeployment(String asid) {
150: mLog.fine(mTranslator.getString(SEQ_START_DEPLOYMENT, asid));
151:
152: ServicelistBean eb = (ServicelistBean) mRegistry
153: .getService(asid);
154: clear();
155:
156: if (eb == null) {
157: mLog.severe(mTranslator.getString(
158: SEQ_START_DEPLOYMENT_FAILED_BEANNULL, asid));
159:
160: setError(mTranslator.getString(
161: SEQ_START_DEPLOYMENT_FAILED_BEANNULL, asid));
162:
163: return;
164: }
165:
166: try {
167: ServiceEndpoint ref = mContext.activateEndpoint(new QName(
168: eb.getNamespace(), eb.getServicename()), eb
169: .getEndpointName());
170: eb.setServiceReference(ref);
171: mLog.fine(mTranslator.getString(
172: SEQ_ACTIVATE_INBOUND_SUCCESS, eb.getServicename()));
173: } catch (javax.jbi.JBIException me) {
174: mLog.severe(mTranslator.getString(
175: SEQ_ACTIVATE_INBOUND_FAILED, eb.getServicename()));
176: mLog.severe(me.getMessage());
177: setError(mTranslator.getString(SEQ_ACTIVATE_INBOUND_FAILED,
178: eb.getServicename()));
179: setException(me);
180: }
181:
182: mLog.fine(mTranslator.getString(SEQ_START_DEPLOYMENT_SUCCESS,
183: asid));
184: }
185:
186: /**
187: * Stops all running services.
188: */
189: public void stopAllServices() {
190: if (mActiveServices == null) {
191: return;
192: }
193:
194: if (mActiveServices.isEmpty()) {
195: return;
196: }
197:
198: MessageRegistry reg = MessageRegistry.getInstance();
199:
200: if (!reg.isEmpty()) {
201: try {
202: Thread.sleep(WAIT_TIME);
203: } catch (Exception e) {
204: ;
205: }
206: }
207:
208: if (!reg.isEmpty()) {
209: mLog.info(mTranslator.getString(SEQ_STOP_ACTIVE_EXCHANGE));
210: } else {
211: mLog.info(mTranslator.getString(SEQ_STOP_EXCHANGES_DONE));
212: }
213: }
214:
215: /**
216: * Stops a deployment.
217: *
218: * @param asid sub-assembly id
219: */
220: public void stopDeployment(String asid) {
221: super .clear();
222:
223: Iterator iter = mActiveServices.iterator();
224: mLog.info(mTranslator.getString(SEQ_STOP_DEPLOYMENT, asid));
225:
226: ServicelistBean sb = mRegistry.getService(asid);
227:
228: if (sb == null) {
229: setError(mTranslator.getString(SEQ_STOP_DEPLOYMENT_FAILED));
230: }
231:
232: if (sb.isExecuting()) {
233: setError(mTranslator.getString(SEQ_DEPLOYMENT_EXECUTING,
234: asid));
235:
236: return;
237: }
238:
239: try {
240: mContext.deactivateEndpoint(sb.getServiceReference());
241: } catch (Exception e) {
242: setError("Cannot deactivate endpoint");
243: setException(e);
244: }
245:
246: mLog.info(mTranslator.getString(SEQ_STOP_DEPLOYMENT_SUCCESS,
247: asid));
248: }
249: }
|