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