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.xslt.util;
030:
031: import com.sun.jbi.engine.xslt.TEResources;
032: import com.sun.jbi.engine.xslt.TransformationEngineContext;
033:
034: import java.util.Collection;
035: import java.util.Hashtable;
036: import java.util.Iterator;
037: import java.util.Set;
038: import java.util.Vector;
039: import java.util.logging.Logger;
040:
041: import javax.jbi.JBIException;
042: import javax.jbi.component.ComponentContext;
043: import javax.jbi.messaging.MessagingException;
044: import javax.jbi.servicedesc.ServiceEndpoint;
045:
046: import javax.xml.namespace.QName;
047:
048: /**
049: * This class manages the service requests and dispatches the requests to be
050: * executed to work manager.
051: *
052: * @author Sun Microsystems, Inc.
053: */
054: public final class ServiceManager implements TEResources {
055: /**
056: * Handle to the Work Manager instance.
057: */
058: private static ServiceManager sServiceManager = null;
059:
060: /**
061: * Handle to the EngineChannel instance.
062: */
063: /**
064: * Handle to bucket of service instance.
065: */
066: private static Hashtable sServiceBucket = null;
067:
068: /**
069: * Handle to bucket of all deployed instances.
070: */
071: private static Hashtable sDeployBucket = null;
072:
073: /**
074: * Internal handle to javax.jbi.component.ComponentContext
075: */
076: private ComponentContext mContext = null;
077:
078: /**
079: * Internal handle to the logger instance.
080: */
081: private Logger mLogger;
082:
083: /**
084: *
085: */
086: private StringTranslator mTranslator = new StringTranslator(
087: "com.sun.jbi.engine.xslt", this .getClass().getClassLoader());
088:
089: /**
090: * Creates a new instance of ServiceManager.
091: */
092: private ServiceManager() {
093: init();
094: }
095:
096: /**
097: * Returns a handle to the Service Manager instance.
098: *
099: * @return a service manager instance
100: */
101: public static ServiceManager getInstance() {
102: if (sServiceManager == null) {
103: sServiceManager = new ServiceManager();
104: }
105:
106: return sServiceManager;
107: }
108:
109: /**
110: * DOCUMENT ME!
111: *
112: * @param context NOT YET DOCUMENTED
113: */
114: public void setComponentContext(
115: javax.jbi.component.ComponentContext context) {
116: mContext = context;
117: if (mLogger == null) {
118: mLogger = TransformationEngineContext.getInstance()
119: .getLogger("");
120: }
121: }
122:
123: /**
124: * Gets the status of the asa id.
125: *
126: * @param asId ASA Id
127: *
128: * @return true if deployed , false otherwise.
129: */
130: public boolean getDeploymentStatus(String asId) {
131: Set keyset = sDeployBucket.keySet();
132: Iterator iter = keyset.iterator();
133:
134: while (iter.hasNext()) {
135: String ser = (String) iter.next();
136:
137: if (ser.trim().equals(asId)) {
138: return true;
139: }
140: }
141:
142: return false;
143: }
144:
145: /**
146: * DOCUMENT ME!
147: *
148: * @param svcname NOT YET DOCUMENTED
149: *
150: * @return NOT YET DOCUMENTED
151: */
152: public Service getService(String svcname) {
153: Service service = (Service) sServiceBucket.get(svcname);
154:
155: return service;
156: }
157:
158: /**
159: * DOCUMENT ME!
160: *
161: * @param bean NOT YET DOCUMENTED
162: */
163: public void createService(ConfigBean bean) {
164: // Overides existing service with same service name
165: String serviceName = null;
166:
167: // Overides existing service with same service name
168: String namespace = null;
169:
170: // Overides existing service with same service name
171: String endpoint = null;
172:
173: Vector vec = (Vector) bean
174: .getValue(ConfigData.SERVICENAME_LOCALPART);
175:
176: if (vec != null) {
177: serviceName = (String) (vec.elementAt(0));
178: }
179:
180: //Changed to use namespace-uri element from service.xml instead of
181: //targetNamespace
182: Vector vec2 = (Vector) bean
183: .getValue(ConfigData.SERVICE_NAMESPACE);
184:
185: if (vec2 != null) {
186: mLogger.finer("There are " + vec2.size()
187: + " namespace attributes set for this service.");
188: namespace = (String) (vec2.elementAt(0));
189: if (namespace != null) {
190: mLogger.finer("namespace in createService is "
191: + namespace);
192: } else {
193: mLogger.finer("namespace in createService is NULL");
194: }
195: }
196:
197: Vector vec3 = (Vector) bean.getValue(ConfigData.ENDPOINT);
198:
199: if (vec3 != null) {
200: endpoint = (String) (vec3.elementAt(0));
201: }
202:
203: if (serviceName != null) {
204: serviceName = serviceName.trim();
205:
206: QName que = new QName(namespace, serviceName);
207: Service newService = new Service(bean);
208:
209: if (newService != null) {
210: newService.setName(que.toString());
211: newService.setEndpoint(endpoint);
212: sServiceBucket
213: .put(que.toString(), (Service) newService);
214: mLogger.finer(mTranslator.getString(
215: TEResources.SERVICE_CREATED, que.toString(),
216: endpoint));
217: mLogger.finer("Got delimiter" + newService.getType());
218: //mLogger.finer("Activated service " + que.toString() + " endpoint " + endpoint);
219: } else {
220: mLogger
221: .severe(mTranslator
222: .getString(TEResources.ERR_CASE_SERVIVCE_COULD_NOT_BE_CREATED)
223: + que.toString());
224: }
225: } else {
226: mLogger.severe(mTranslator
227: .getString(TEResources.SERVICE_NULL));
228: }
229: }
230:
231: /**
232: * DOCUMENT ME!
233: *
234: * @param serviceUnitID NOT YET DOCUMENTED
235: * @param beans NOT YET DOCUMENTED
236: */
237: public void createServices(String serviceUnitID, ConfigBean[] beans) {
238: int length = beans.length;
239:
240: for (int i = 0; i < length; i++) {
241: ConfigBean bean = (ConfigBean) beans[i];
242: createService(bean);
243: }
244:
245: sDeployBucket.put(serviceUnitID, beans);
246: }
247:
248: /**
249: * DOCUMENT ME!
250: *
251: * @param bean NOT YET DOCUMENTED
252: */
253: public void destroyService(ConfigBean bean) {
254: String svcname = null;
255: String namespace = null;
256:
257: Vector vec = (Vector) bean
258: .getValue(ConfigData.SERVICENAME_LOCALPART);
259:
260: if (vec != null) {
261: svcname = (String) (vec.elementAt(0));
262: }
263:
264: Vector vec2 = (Vector) bean
265: .getValue(ConfigData.SERVICE_NAMESPACE);
266:
267: if (vec2 != null) {
268: namespace = (String) (vec2.elementAt(0));
269: }
270:
271: if (svcname != null) {
272: svcname = svcname.trim();
273: namespace = namespace.trim();
274:
275: QName que = new QName(namespace, svcname);
276:
277: //stopService(bean);
278: mLogger.finer(mTranslator.getString(
279: TEResources.SERVICE_DESTROYED, que.toString()));
280: sServiceBucket.remove(svcname);
281: } else {
282: mLogger
283: .severe(mTranslator
284: .getString(TEResources.SERVICENAME_NULL_CANT_DESTROY));
285: }
286: }
287:
288: /**
289: * DOCUMENT ME!
290: *
291: * @param serviceUnitID NOT YET DOCUMENTED
292: */
293: public void destroyServices(String serviceUnitID) {
294: ConfigBean[] beans = (ConfigBean[]) sDeployBucket
295: .get(serviceUnitID);
296:
297: if (beans == null) {
298: return;
299: }
300:
301: int length = beans.length;
302:
303: for (int i = 0; i < length; i++) {
304: destroyService((ConfigBean) beans[i]);
305: }
306:
307: sDeployBucket.remove(serviceUnitID);
308: }
309:
310: /**
311: *
312: */
313: public void startAllServices() {
314: mLogger.info(mTranslator
315: .getString(TEResources.START_ALL_SERVICE));
316:
317: Collection list = (Collection) sServiceBucket.values();
318:
319: if (list == null) {
320: return;
321: }
322:
323: Iterator all = list.iterator();
324:
325: while (all.hasNext()) {
326: Service service = (Service) all.next();
327:
328: if (service != null) {
329: try {
330: QName name = QName.valueOf(service.getName());
331: mLogger.finer("QName activated is =" + name);
332:
333: ServiceEndpoint ref = mContext.activateEndpoint(
334: name, service.getEndpoint());
335: mLogger.info("Service activated is ="
336: + ref.getServiceName() + " endpoint "
337: + ref.getEndpointName());
338: service.setReference(ref);
339: mLogger
340: .finer(mTranslator
341: .getString(TEResources.TE_SERVICE)
342: + name
343: + ":"
344: + service.getEndpoint()
345: + mTranslator
346: .getString(TEResources.IS_ACTIVATED));
347: service.start();
348: } catch (MessagingException ex) {
349: mLogger
350: .severe(mTranslator
351: .getString(TEResources.SERVICE_COULD_NOT_BE_ACTIVATED)
352: + service.getName());
353: } catch (JBIException jbie) {
354: mLogger
355: .severe(mTranslator
356: .getString(TEResources.SERVICE_COULD_NOT_BE_ACTIVATED)
357: + service.getName());
358: }
359: } else {
360: mLogger.severe(mTranslator
361: .getString(TEResources.RETRIVED_SERVICE_NULL));
362: }
363: }
364:
365: mLogger.info(mTranslator
366: .getString(TEResources.STARTALLSERVICE_END));
367: }
368:
369: /**
370: * DOCUMENT ME!
371: *
372: * @param bean NOT YET DOCUMENTED
373: */
374: public void startService(ConfigBean bean) {
375: String svcname = null;
376: String namespace = null;
377:
378: Vector vec = (Vector) bean
379: .getValue(ConfigData.SERVICENAME_LOCALPART);
380:
381: if (vec != null) {
382: svcname = (String) (vec.elementAt(0));
383: }
384:
385: //Changed to use Servie Namespace (namespace-uri element in service.xml)
386: Vector vec2 = (Vector) bean
387: .getValue(ConfigData.SERVICE_NAMESPACE);
388:
389: if (vec2 != null) {
390: namespace = (String) (vec2.elementAt(0));
391: }
392:
393: if (svcname != null) {
394: svcname = svcname.trim();
395: namespace = namespace.trim();
396:
397: QName que = new QName(namespace, svcname);
398: Service service = getService(que.toString());
399:
400: if (service != null) {
401: try {
402: QName name = QName.valueOf(service.getName());
403: mLogger.finer("QName activated is =" + name);
404:
405: ServiceEndpoint ref = mContext.activateEndpoint(
406: name, service.getEndpoint());
407: mLogger.info("Service activated is ="
408: + ref.getServiceName() + ":"
409: + service.getEndpoint());
410: service.setReference(ref);
411: mLogger
412: .finer(mTranslator
413: .getString(TEResources.TE_SERVICE)
414: + name
415: + mTranslator
416: .getString(TEResources.IS_ACTIVATED));
417: service.start();
418: } catch (MessagingException ex) {
419: mLogger
420: .severe(mTranslator
421: .getString(TEResources.SERVICE_COULD_NOT_BE_ACTIVATED)
422: + svcname);
423: } catch (JBIException jbie) {
424: mLogger
425: .severe(mTranslator
426: .getString(TEResources.SERVICE_COULD_NOT_BE_ACTIVATED)
427: + service.getName());
428: }
429: } else {
430: mLogger
431: .severe(mTranslator
432: .getString(TEResources.SERVICE_RETREVAL_FAILED));
433: }
434: } else {
435: mLogger.severe(mTranslator
436: .getString(TEResources.SERVICENAME_NULL));
437: }
438: }
439:
440: /**
441: * DOCUMENT ME!
442: *
443: * @param serviceUnitID service unit ID
444: */
445: public void startServices(String serviceUnitID) {
446: //Get all the deployed services for this serviceUnitID
447: ConfigBean[] beans = (ConfigBean[]) sDeployBucket
448: .get(serviceUnitID);
449:
450: if (beans == null) {
451: return;
452: }
453:
454: //Start them
455: int length = beans.length;
456:
457: for (int i = 0; i < length; i++) {
458: ConfigBean bean = (ConfigBean) beans[i];
459: startService(bean);
460: }
461: }
462:
463: /**
464: *
465: */
466: public void stopAllServices() {
467: Collection list = (Collection) sServiceBucket.values();
468:
469: if (list == null) {
470: return;
471: }
472:
473: Iterator all = list.iterator();
474:
475: while (all.hasNext()) {
476: Service service = (Service) all.next();
477: String svcname = (String) service.getName();
478:
479: if (svcname != null) {
480: svcname = svcname.trim();
481:
482: ServiceEndpoint ref = service.getReference();
483:
484: if (ref != null) {
485: try {
486: mContext.deactivateEndpoint(ref);
487: } catch (Exception ex) {
488: mLogger
489: .severe(mTranslator
490: .getString(TEResources.FAILED_TO_DEACTIVATE));
491: ex.printStackTrace();
492: }
493: } else {
494: continue;
495: }
496:
497: service.stop();
498: mLogger
499: .finer(mTranslator
500: .getString(TEResources.SERVICE_IS_BEING_STOPPED)
501: + svcname);
502: } else {
503: mLogger
504: .severe(mTranslator
505: .getString(TEResources.SERVICE_RETREVAL_FAILED_STOP_FAILED));
506: }
507: }
508:
509: mLogger.info(mTranslator
510: .getString(TEResources.ALL_SERVICES_STOPPED));
511: }
512:
513: /**
514: * Stops the Work Manager. The method changes the state of the instance to
515: * "STOP". The Work Manager thread keeps checking the state of the
516: * instance and it starts the clean up when its state is "STOP".
517: *
518: * @param bean ConfigBean object
519: */
520: public void stopService(ConfigBean bean) {
521: String svcname = null;
522: String namespace = null;
523:
524: //Changed the service.xsd to represent service-name in QName format.
525: Vector vec = (Vector) bean
526: .getValue(ConfigData.SERVICENAME_LOCALPART);
527:
528: if (vec != null) {
529: svcname = (String) (vec.elementAt(0));
530: }
531:
532: Vector vec2 = (Vector) bean
533: .getValue(ConfigData.SERVICE_NAMESPACE);
534:
535: if (vec2 != null) {
536: namespace = (String) (vec2.elementAt(0));
537: }
538:
539: if (svcname != null) {
540: svcname = svcname.trim();
541: namespace = namespace.trim();
542:
543: QName que = new QName(namespace, svcname);
544: Service service = getService(que.toString());
545:
546: if (service != null) {
547: ServiceEndpoint ref = service.getReference();
548: if (ref != null) {
549:
550: try {
551: mContext.deactivateEndpoint(ref);
552: } catch (Exception ex) {
553: mLogger
554: .severe(mTranslator
555: .getString(TEResources.FAILED_TO_DEACTIVATE));
556: ex.printStackTrace();
557: }
558:
559: service.stop();
560: mLogger
561: .info(mTranslator
562: .getString(TEResources.SERVICE_IS)
563: + service.getName()
564: + mTranslator
565: .getString(TEResources.BEING_STOPPED));
566: } else {
567: mLogger
568: .severe("ServiceManager. NULL ServiceEndpoint. Check...");
569: }
570: } else {
571: mLogger
572: .severe(mTranslator
573: .getString(TEResources.SERVICE_RETREVAL_FAILED_STOP_FAILED));
574: }
575: } else {
576: mLogger.severe(mTranslator
577: .getString(TEResources.SERVICENAME_NULL));
578: }
579: }
580:
581: /**
582: * DOCUMENT ME!
583: *
584: * @param serviceUnitID NOT YET DOCUMENTED
585: */
586: public void stopServices(String serviceUnitID) {
587: //Get All Deployed Services for this serviceUnitID and stop them
588: ConfigBean[] beans = (ConfigBean[]) sDeployBucket
589: .get(serviceUnitID);
590:
591: if (beans == null) {
592: return;
593: }
594:
595: int length = beans.length;
596:
597: for (int i = 0; i < length; i++) {
598: ConfigBean bean = (ConfigBean) beans[i];
599: stopService(bean);
600: }
601: }
602:
603: /**
604: * Initializes the Logger.
605: */
606: private void init() {
607: sDeployBucket = new Hashtable();
608: sServiceBucket = new Hashtable();
609: }
610: }
|