001: /*
002: * <copyright>
003: *
004: * Copyright 2002-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.servicediscovery.service;
028:
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.Enumeration;
032: import java.util.Iterator;
033: import java.util.Vector;
034:
035: import org.cougaar.core.component.ServiceBroker;
036: import org.cougaar.core.component.ServiceProvider;
037: import org.cougaar.core.mts.MessageAddress;
038: import org.cougaar.core.plugin.ComponentPlugin;
039: import org.cougaar.core.service.LoggingService;
040: import org.cougaar.core.service.ThreadService;
041: import org.cougaar.core.service.community.Community;
042: import org.cougaar.servicediscovery.description.AdditionalQualificationRecord;
043: import org.cougaar.servicediscovery.description.BusinessCategory;
044: import org.cougaar.servicediscovery.description.ProviderDescription;
045: import org.cougaar.servicediscovery.description.ServiceCategory;
046: import org.cougaar.servicediscovery.description.ServiceClassification;
047: import org.cougaar.servicediscovery.description.ServiceProfile;
048: import org.cougaar.util.log.Logger;
049: import org.cougaar.util.log.Logging;
050: import org.cougaar.yp.YPFuture;
051: import org.cougaar.yp.YPProxy;
052: import org.cougaar.yp.YPService;
053: import org.cougaar.yp.YPStateMachine;
054: import org.uddi4j.datatype.Name;
055: import org.uddi4j.datatype.binding.BindingTemplates;
056: import org.uddi4j.datatype.binding.TModelInstanceDetails;
057: import org.uddi4j.datatype.business.BusinessEntity;
058: import org.uddi4j.datatype.service.BusinessService;
059: import org.uddi4j.datatype.service.BusinessServices;
060: import org.uddi4j.response.BusinessInfo;
061: import org.uddi4j.response.BusinessList;
062: import org.uddi4j.response.ServiceDetail;
063: import org.uddi4j.response.ServiceInfo;
064: import org.uddi4j.util.CategoryBag;
065: import org.uddi4j.util.FindQualifier;
066: import org.uddi4j.util.FindQualifiers;
067: import org.uddi4j.util.KeyedReference;
068:
069: /**
070: * This component provides access to the Registration Service
071: * which allows a service provider agent to register
072: * the services it provides in the registry.
073: *
074: * Registered Services are kept both locally and on the Blackboard.
075: * The redundant storage is to provide quick access by having the information stored
076: * locally within the Agent, but also have the ability to re-hydrate if the agent gets killed.
077: *
078: * @see RegistrationService
079: */
080: public final class UDDI4JRegistrationServiceComponent extends
081: ComponentPlugin {
082: // why is this a plugin?? it doesn't use the blackboard!
083:
084: private static Logger staticLogger = Logging
085: .getLogger(UDDI4JRegistrationServiceComponent.class);
086:
087: /** Cougaar service used for logging **/
088: private LoggingService ursclog;
089:
090: public void setLoggingService(LoggingService ls) {
091: ursclog = ((ls == null) ? LoggingService.NULL : ls);
092: }
093:
094: private YPService ypService;
095:
096: public void setYPService(YPService yp) {
097: ypService = yp;
098: }
099:
100: private ThreadService threads;
101:
102: public void setThreadService(ThreadService t) {
103: threads = t;
104: }
105:
106: protected String agentName = "anonymous";
107:
108: private RegistrationServiceProviderImpl mySP;
109:
110: public void load() {
111: super .load();
112:
113: // create and advertise our service
114: mySP = new RegistrationServiceProviderImpl();
115: getServiceBroker().addService(RegistrationService.class, mySP);
116:
117: agentName = agentIdentificationService.getName();
118: }
119:
120: public void unload() {
121: // revoke our service
122: if (mySP != null) {
123: getServiceBroker().revokeService(RegistrationService.class,
124: mySP);
125: mySP = null;
126: }
127: super .unload();
128: }
129:
130: /**
131: * Populate the Agent will all known ProviderDescriptions.
132: * The only reason ProviderDescriptions would exist is if the
133: * agent is re-hydrating.
134: */
135: protected void setupSubscriptions() {
136: }
137:
138: protected void execute() {
139: }
140:
141: private class RegistrationServiceProviderImpl implements
142: ServiceProvider {
143:
144: public Object getService(ServiceBroker sb, Object requestor,
145: Class serviceClass) {
146: if (serviceClass == RegistrationService.class) {
147: return new RegistrationServiceImpl();
148: } else {
149: return null;
150: }
151: }
152:
153: public void releaseService(ServiceBroker sb, Object requestor,
154: Class serviceClass, Object service) {
155: }
156: }
157:
158: //The following uddi registry parameters are set via System properties.
159: private static String username = null;
160: private static String password = null;
161:
162: static {
163: username = System.getProperty(
164: "org.cougaar.yp.juddi-users.username",
165: YPProxy.DEFAULT_UDDI_USERNAME);
166: password = System.getProperty(
167: "org.cougaar.yp.juddi-users.password",
168: YPProxy.DEFAULT_UDDI_PASSWORD);
169: }
170:
171: private class RegistrationServiceImpl extends UDDI4JUtility
172: implements RegistrationService {
173:
174: public RegistrationServiceImpl() {
175: super (UDDI4JRegistrationServiceComponent.this .ursclog,
176: UDDI4JRegistrationServiceComponent.this .ypService,
177: UDDI4JRegistrationServiceComponent.this .threads);
178: }
179:
180: private YPProxy makeProxy(Object ypContext) {
181: YPProxy proxy = null;
182:
183: if (ypContext == null) {
184: proxy = ypService.getYP();
185: } else if (ypContext instanceof String) {
186: // Treat this as the name of an Agent.
187: proxy = ypService.getYP((String) ypContext);
188: } else if (ypContext instanceof MessageAddress) {
189: proxy = ypService.getYP((MessageAddress) ypContext);
190: } else if (ypContext instanceof Community) {
191: proxy = ypService.getYP((Community) ypContext);
192: } else {
193: throw new IllegalArgumentException(
194: "Invalid datatype for ypContext - "
195: + ypContext.getClass()
196: + " - must be String, MessageAddress, or Community.");
197: }
198:
199: return proxy;
200: }
201:
202: //
203: // Implement the registrationservice api
204: //
205: public void addProviderDescription(Object ypContext,
206: ProviderDescription pd, Callback callback) {
207: addProviderDescription(ypContext, pd,
208: Collections.EMPTY_LIST, callback);
209: }
210:
211: ////// inner-classes follow
212: // Before each one is the single method (of RegistrationServiceImpl) that uses the inner class
213:
214: // This is the base for all the other inner classes
215: private class SMBase extends YPStateMachine {
216: protected final Object ypContext;
217: protected final Callback callback;
218:
219: public SMBase(Object ypContext, Callback callback) {
220: super (ypService, makeProxy(ypContext),
221: UDDI4JRegistrationServiceComponent.this .threads);
222: this .ypContext = ypContext;
223: this .callback = callback;
224: }
225:
226: protected void handleException(Exception e) {
227: staticLogger
228: .error(
229: "Caught unexpected Exception. StateMachine will exit",
230: e);
231: callback.handle(e);
232: }
233:
234: public void transit(State s0, State s1) {
235: if (staticLogger.isInfoEnabled()) {
236: staticLogger.info(this .toString()
237: + " StateMachine transit: " + s0 + " to "
238: + s1);
239: }
240: super .transit(s0, s1);
241: }
242:
243: protected void kick() {
244: if (staticLogger.isInfoEnabled()) {
245: staticLogger
246: .info(this .toString() + " kicked"/*, new Throwable()*/); // sigh
247: }
248: super .kick();
249: }
250:
251: protected Frame popFrame() {
252: Frame f = super .popFrame();
253: if (staticLogger.isInfoEnabled()) {
254: staticLogger
255: .info(this .toString()
256: + " StateMachine pop("
257: + stackSize() + ") backto "
258: + f.getReturnTag() /*, new Throwable()*/);
259: }
260: return f;
261: }
262:
263: protected void pushFrame(Frame f) {
264: if (staticLogger.isInfoEnabled()) {
265: staticLogger
266: .info(this .toString()
267: + " StateMachine push("
268: + stackSize() + ")");
269: }
270: super .pushFrame(f);
271: }
272:
273: public void set(State s) {
274: super .set(s);
275: if (staticLogger.isInfoEnabled()) {
276: staticLogger.info(this .toString()
277: + " StateMachine set to " + s);
278: }
279: }
280:
281: public boolean step() {
282: State s1 = getState();
283: boolean b = super .step();
284: State s2 = getState();
285: if (staticLogger.isInfoEnabled()) {
286: staticLogger.info(this .toString()
287: + " StateMachine stepped from " + s1
288: + " to " + s2 + " progress=" + b);
289: }
290: return b;
291: }
292:
293: public void go() {
294: super .go();
295: if (staticLogger.isInfoEnabled()) {
296: staticLogger.info(this .toString()
297: + " StateMachine stopped in state "
298: + getState());
299: }
300: }
301: } // end of SMBase
302:
303: /**
304: * Adds a new ProviderDescription object.
305: *
306: * @param pd ProviderDescription for this provider.
307: * @param callback Callback.invoke(Boolean) called with true on success
308: */
309: public void addProviderDescription(Object ypContext,
310: ProviderDescription pd,
311: Collection additionalServiceClassifications,
312: Callback callback) {
313: (new PublishSM(ypContext, pd,
314: additionalServiceClassifications, callback))
315: .start();
316: }
317:
318: private class PublishSM extends SMBase {
319: private ProviderDescription pd;
320: private Collection additionalServiceClassifications;
321:
322: public String toString() {
323: return (pd != null) ? (pd.getProviderName())
324: : "anonymous";
325: }
326:
327: public PublishSM(Object ypContext, ProviderDescription pd,
328: Collection additionalServiceClassifications,
329: Callback callback) {
330: super (ypContext, callback);
331: if (pd.getProviderName() == null) {
332: throw new IllegalArgumentException(
333: "Provider name is null, unable to register. ");
334: }
335: this .pd = pd;
336: this .additionalServiceClassifications = additionalServiceClassifications;
337: }
338:
339: BusinessEntity be;
340: CategoryBag bzBag = new CategoryBag();
341: Iterator iter;
342:
343: Vector entities = new Vector();
344: Vector services = new Vector();
345: Collection serviceDescriptions;
346: BusinessServices businessServices = new BusinessServices();
347:
348: protected void init() {
349: super .init();
350:
351: addLink("YPError", "handleYPError");
352: add(new SState("handleYPError") {
353: public void invoke() {
354: callback
355: .handle((Exception) getVar("YPErrorException"));
356: }
357: });
358:
359: addLink("START", "getToken");
360: add(new SState("getToken") {
361: public void invoke() {
362: call("getAuthToken", null, "gotToken");
363: }
364: });
365:
366: addLink("gotToken", "initBag");
367:
368: // initBag - fills up the new BusinessEntity with keyedrefs to the business categories
369: add(new SState("initBag") {
370: public void invoke() {
371: be = new BusinessEntity("", pd
372: .getProviderName());
373: setVar("be", be);
374: setVar("bzBag", bzBag);
375: iterate(pd.getBusinessCategories(),
376: "initBag.Loop", "initBagDone");
377: }
378: });
379:
380: add(new SState("initBag.Loop") {
381: public void invoke() {
382: final BusinessCategory bc = (BusinessCategory) getArgument();
383: Callback cb = new Callback() {
384: public void invoke(Object o) {
385: KeyedReference kr = (KeyedReference) o;
386: bzBag.getKeyedReferenceVector().add(kr);
387: callReturn(null);
388: kick();
389: }
390:
391: public void handle(Exception e) {
392: log.error(
393: "initBag.Loop.getKeyedReference("
394: + bc + ")", e);
395: //transit("ERROR");
396: callback.handle(e);
397: }
398: };
399: getKeyedReference(getYPProxy(), bc
400: .getCategorySchemeName(), bc
401: .getCategoryName(), bc
402: .getCategoryCode(), cb);
403: }
404: });
405: add(new SState("initBagDone") {
406: public void invoke() {
407: be.setCategoryBag(bzBag);
408: transit("initBS");
409: }
410: });
411:
412: // initBS - inits the be's Services, iterate over the SDs
413: add(new SState("initBS") {
414: public void invoke() {
415: serviceDescriptions = pd.getServiceProfiles();
416: iterate(serviceDescriptions, "ibsl",
417: "initBSDone");
418: }
419: });
420: // foreach service description, loop over service categories
421: add(new SState("ibsl") {
422: public void invoke() {
423: ServiceProfile sd = (ServiceProfile) getArgument();
424: setVar("sd", sd);
425: BusinessService bSvc = new BusinessService("");
426: bSvc.setDefaultName(new Name(sd
427: .getServiceProfileID()));
428: setVar("bSvc", bSvc);
429: Collection serviceCategories = sd
430: .getServiceCategories();
431: CategoryBag categoryBag = new CategoryBag();
432: setVar("categoryBag", categoryBag);
433: iterate(serviceCategories, "ibsl_sub1",
434: "ibsl_1b");
435: }
436: });
437: // foreach service category, get the SC ref and add to the bag
438: add(new SState("ibsl_sub1") {
439: public void invoke() {
440: final ServiceCategory sc = (ServiceCategory) getArgument();
441: setVar("sc", sc);
442: final CategoryBag categoryBag = (CategoryBag) getVar("categoryBag");
443: Callback cb = new Callback() {
444: public void invoke(Object o) {
445: KeyedReference kr = (KeyedReference) o;
446: categoryBag.getKeyedReferenceVector()
447: .add(kr);
448: transit("ibsl_sub1a");
449: kick();
450: }
451:
452: public void handle(Exception e) {
453: log.error(
454: "ibsl_sub1.getKeyedReference("
455: + sc + ")", e);
456: //transit("ERROR");
457: callback.handle(e);
458: }
459: };
460: getKeyedReference(getYPProxy(), sc
461: .getCategorySchemeName(), sc
462: .getCategoryName(), sc
463: .getCategoryCode(), cb);
464: }
465: });
466: // foreach Service category, loop over the additional qualifications
467: add(new SState("ibsl_sub1a") {
468: public void invoke() {
469: ServiceCategory sc = (ServiceCategory) getVar("sc");
470: iterate(sc.getAdditionalQualifications(),
471: "ibsl_sub2", "ibsl_sub1b");
472: }
473: });
474:
475: // foreach AQ, collect the keyed refs and return.
476: add(new SState("ibsl_sub2") {
477: public void invoke() {
478: AdditionalQualificationRecord aqr = (AdditionalQualificationRecord) getArgument();
479: final ServiceCategory sc = (ServiceCategory) getVar("sc");
480: final CategoryBag categoryBag = (CategoryBag) getVar("categoryBag");
481: Callback cb = new Callback() {
482: public void invoke(Object o) {
483: KeyedReference kr = (KeyedReference) o;
484: categoryBag.getKeyedReferenceVector()
485: .add(kr);
486: callReturn(null);
487: kick();
488: }
489:
490: public void handle(Exception e) {
491: log.error(
492: "ibsl_sub2.getKeyedReference("
493: + sc + ")", e);
494: //transit("ERROR");
495: callback.handle(e);
496: }
497: };
498: getKeyedReference(getYPProxy(), sc
499: .getCategorySchemeName(), aqr
500: .getQualificationName(), aqr
501: .getQualificationValue(), cb);
502: }
503: });
504:
505: // endpoint of loop started in ibsl_sub1a, returns up to loop started in ibsl which may
506: // progress to ibsl_1
507: add(new SState("ibsl_sub1b") {
508: public void invoke() {
509: callReturn(null);
510: }
511: });
512:
513: // ibsl_1b, iterate over additionalServiceClassifications
514: add(new SState("ibsl_1b") {
515: public void invoke() {
516: iterate(additionalServiceClassifications,
517: "ibsl_sub3", "ibsl_1c");
518: }
519: });
520:
521: // foreach ASQ, collect the keyed refs and return.
522: add(new SState("ibsl_sub3") {
523: public void invoke() {
524: final ServiceClassification sc = (ServiceClassification) getArgument();
525: final CategoryBag categoryBag = (CategoryBag) getVar("categoryBag");
526: Callback cb = new Callback() {
527: public void invoke(Object o) {
528: KeyedReference kr = (KeyedReference) o;
529: categoryBag.getKeyedReferenceVector()
530: .add(kr);
531: callReturn(null);
532: kick();
533: }
534:
535: public void handle(Exception e) {
536: log.error(
537: "ibsl_sub3.getKeyedReference("
538: + sc + ")", e);
539: //transit("ERROR");
540: callback.handle(e);
541: }
542: };
543: getKeyedReference(getYPProxy(), sc
544: .getClassificationSchemeName(), sc
545: .getClassificationName(), sc
546: .getClassificationCode(), cb);
547: }
548: });
549:
550: // ibsl_1c, collect results
551: add(new SState("ibsl_1c") {
552: public void invoke() {
553: final CategoryBag categoryBag = (CategoryBag) getVar("categoryBag");
554: final BusinessService bSvc = (BusinessService) getVar("bSvc");
555: bSvc.setCategoryBag(categoryBag);
556: bSvc.setBusinessKey("");
557:
558: final ServiceProfile sd = (ServiceProfile) getVar("sd");
559: if (sd.getTextDescription().trim().length() != 0) {
560: bSvc.setDefaultDescriptionString(sd
561: .getTextDescription());
562: }
563: Callback cb = new Callback() {
564: public void invoke(Object o) {
565: TModelInstanceDetails tModelInstanceDetails = (TModelInstanceDetails) o;
566: BindingTemplates bindings = createBindingTemplates(
567: sd.getServiceGroundingURI(),
568: tModelInstanceDetails);
569: bSvc.setBindingTemplates(bindings);
570: services.add(bSvc);
571: callReturn(null); // return back to initBS
572: kick();
573: }
574:
575: public void handle(Exception e) {
576: log.error(
577: "ibsl_1c.createTModelInstance",
578: e);
579: //transit("ERROR");
580: callback.handle(e);
581: }
582: };
583: createTModelInstance(getYPProxy(), sd
584: .getServiceGroundingBindingType(), pd
585: .getProviderName(), cb);
586: }
587: });
588:
589: add(new SState("initBSDone") {
590: public void invoke() {
591: businessServices
592: .setBusinessServiceVector(services);
593: transit("saveBusiness");
594: }
595: });
596:
597: addYPQ("saveBusiness", "saveBusinessDone", new YPQ() {
598: public YPFuture get(Frame f) {
599: be.setBusinessServices(businessServices);
600: entities.add(be);
601: return getYPProxy().save_business(
602: getAuthToken().getAuthInfoString(),
603: entities);
604: }
605:
606: public void set(Frame f, Object result) {
607: // ignore the result.
608: }
609:
610: public void handle(Frame f, Exception e) {
611: log.error("saveBusiness exception", e);
612: callback.handle(e);
613: }
614: });
615: addLink("saveBusinessDone", "finish");
616: add(new SState("finish") {
617: public void invoke() {
618: callback.invoke(Boolean.TRUE); // let complete while discarding token
619: call("discardAuthToken", null, "DONE");
620: }
621: });
622: }
623: } // end of PublishSM definition
624:
625: /**
626: * @param callback Callback.invoke(Boolean) true on success, Callback.handle(Exception) on failure
627: */
628: public void updateServiceDescription(Object ypContext,
629: String providerName, Collection serviceCategories,
630: Callback callback) {
631: (new UpdateSM(ypContext, providerName, serviceCategories,
632: callback)).start();
633: }
634:
635: private class UpdateSM extends SMBase {
636: final String providerName;
637: final Collection serviceCategories;
638:
639: private UpdateSM(Object ypContext, String providerName,
640: Collection serviceCategories, Callback callback) {
641: super (ypContext, callback);
642: this .providerName = providerName;
643: this .serviceCategories = serviceCategories;
644:
645: // do some pre-machine initialization
646: namePatterns.add(new Name(providerName));
647: }
648:
649: BusinessList businessList;
650: Iterator iter;
651: CategoryBag updateBag = new CategoryBag();
652: Vector namePatterns = new Vector();
653: Vector services = new Vector();
654: Vector serviceKeys = new Vector();
655:
656: protected void init() {
657: super .init();
658: addLink("START", "getToken");
659: add(new SState("getToken") {
660: public void invoke() {
661: call("getAuthToken", null, "gotToken");
662: }
663: });
664: addLink("gotToken", "findBusiness");
665:
666: addYPQ("findBusiness", "findBusinessDone", new YPQ() {
667: public YPFuture get(Frame f) {
668: FindQualifiers findQualifiers = new FindQualifiers();
669: Vector qualifier = new Vector();
670: qualifier.add(new FindQualifier(
671: "caseSensitiveMatch"));
672: findQualifiers
673: .setFindQualifierVector(qualifier);
674: return getYPProxy().find_business(namePatterns,
675: null, null, null, null, findQualifiers,
676: 5);
677: }
678:
679: public void set(Frame f, Object r) {
680: businessList = (BusinessList) r;
681: }
682:
683: public void handle(Frame f, Exception e) {
684: log
685: .error(
686: "updateServiceDescription.findBusiness",
687: e);
688: callback.handle(e);
689: transit("ERROR");
690: }
691: });
692: addLink("findBusinessDone", "ScanBusinesses");
693: add(new SState("ScanBusinesses") {
694: public void invoke() {
695: Iterator it = businessList.getBusinessInfos()
696: .getBusinessInfoVector().iterator();
697: if (!it.hasNext()) {
698: if (log.isDebugEnabled()) {
699: log
700: .debug("updateServiceDescription, cannot find registration for: "
701: + providerName);
702: }
703: callback.invoke(Boolean.TRUE);
704: transit("DONE");
705: return;
706: }
707: while (it.hasNext()) {
708: BusinessInfo businessInfo = (BusinessInfo) it
709: .next();
710: for (Iterator kit = businessInfo
711: .getServiceInfos()
712: .getServiceInfoVector().iterator(); kit
713: .hasNext();) {
714: serviceKeys.add(((ServiceInfo) kit
715: .next()).getServiceKey());
716: }
717: }
718: transit("SBDone");
719: }
720: });
721:
722: addLink("SBDone", "initBag");
723:
724: add(new SState("initBag") {
725: public void invoke() {
726: iter = serviceCategories.iterator();
727: transit("initBagLoop");
728: }
729: });
730: add(new SState("initBagLoop") {
731: public void invoke() {
732: if (iter.hasNext()) {
733: final ServiceClassification sc = (ServiceClassification) iter
734: .next();
735: Callback cb = new Callback() {
736: public void invoke(Object o) {
737: KeyedReference kr = (KeyedReference) o;
738: updateBag.getKeyedReferenceVector()
739: .add(kr);
740: transit("initBagLoop");
741: kick();
742: }
743:
744: public void handle(Exception e) {
745: log.error(
746: "initBagLoop.getKeyedReference("
747: + sc + ")", e);
748: //transit("ERROR");
749: callback.handle(e);
750: }
751: };
752: getKeyedReference(getYPProxy(), sc
753: .getClassificationSchemeName(), sc
754: .getClassificationName(), sc
755: .getClassificationCode(), cb);
756: } else {
757: transit("initBagDone");
758: }
759: }
760: });
761: addLink("initBagDone", "getServiceDetail");
762:
763: addYPQ("getServiceDetail", "getServiceDetailDone",
764: new YPQ() {
765: public YPFuture get(Frame f) {
766: return getYPProxy().get_serviceDetail(
767: serviceKeys);
768: }
769:
770: public void set(Frame f, Object r) {
771: ServiceDetail sd = (ServiceDetail) r;
772: Enumeration e = sd
773: .getBusinessServiceVector()
774: .elements();
775: while (e.hasMoreElements()) {
776: BusinessService bs = (BusinessService) e
777: .nextElement();
778: CategoryBag this Bag = bs
779: .getCategoryBag();
780: this Bag
781: .getKeyedReferenceVector()
782: .addAll(
783: updateBag
784: .getKeyedReferenceVector());
785: services.add(bs);
786: }
787: }
788:
789: public void handle(Frame f, Exception e) {
790: log.error("getServiceDetail", e);
791: callback.handle(e);
792: }
793: });
794:
795: addLink("getServiceDetailDone", "saveService");
796: addYPQ("saveService", "saveServiceDone", new YPQ() {
797: public YPFuture get(Frame f) {
798: return getYPProxy().save_service(
799: getAuthToken().getAuthInfoString(),
800: services);
801: }
802:
803: public void set(Frame f, Object r) {
804: // copacetic
805: }
806:
807: public void handle(Frame f, Exception e) {
808: log.error("saveService", e);
809: callback.handle(e);
810: }
811: });
812: addLink("saveServiceDone", "finish");
813: add(new SState("finish") {
814: public void invoke() {
815: callback.invoke(Boolean.TRUE); // let complete while discarding token
816: call("discardAuthToken", null, "DONE");
817: }
818: });
819:
820: } // end of init()
821: } // end of UpdateSM definition
822:
823: /**
824: * @param callback Callback.invoke(Boolean) true IFF success.
825: **/
826: public void deleteServiceDescription(Object ypContext,
827: String providerName, Collection serviceCategories,
828: Callback callback) {
829: (new DeleteSM(ypContext, providerName, serviceCategories,
830: callback)).start();
831: }
832:
833: private class DeleteSM extends SMBase {
834: final String providerName;
835: final Collection serviceCategories;
836:
837: ServiceInfo service = null;
838: Vector namePatterns = new Vector();
839: FindQualifiers findQualifiers = new FindQualifiers();
840: Vector qualifier = new Vector();
841: CategoryBag bag = new CategoryBag();
842: Iterator iter; // used by initBag
843: BusinessList businessList; // set by findBusiness
844: BusinessInfo businessInfo; // set by findBusinessDone
845: Vector serviceKeys; // used in dbLoop
846:
847: private DeleteSM(Object ypContext, String providerName,
848: Collection serviceCategories, Callback callback) {
849: super (ypContext, callback);
850: this .providerName = providerName;
851: this .serviceCategories = serviceCategories;
852:
853: namePatterns.add(new Name(providerName));
854: qualifier.add(new FindQualifier(
855: FindQualifier.serviceSubset));
856: findQualifiers.setFindQualifierVector(qualifier);
857: }
858:
859: protected void init() {
860: super .init();
861: addLink("START", "getToken");
862: add(new SState("getToken") {
863: public void invoke() {
864: call("getAuthToken", null, "gotToken");
865: }
866: });
867: addLink("gotToken", "initBag");
868:
869: add(new SState("initBag") {
870: public void invoke() {
871: iter = serviceCategories.iterator();
872: transit("initBagLoop");
873: }
874: });
875: add(new SState("initBagLoop") {
876: public void invoke() {
877: if (iter.hasNext()) {
878: final ServiceClassification sc = (ServiceClassification) iter
879: .next();
880: Callback cb = new Callback() {
881: public void invoke(Object o) {
882: KeyedReference kr = (KeyedReference) o;
883: bag.getKeyedReferenceVector().add(
884: kr);
885: transit("initBagLoop");
886: kick();
887: }
888:
889: public void handle(Exception e) {
890: log.error(
891: "initBagLoop.getKeyedReference("
892: + sc + ")", e);
893: //transit("ERROR");
894: callback.handle(e);
895: }
896: };
897: getKeyedReference(getYPProxy(), sc
898: .getClassificationSchemeName(), sc
899: .getClassificationName(), sc
900: .getClassificationCode(), cb);
901: } else {
902: transit("initBagDone");
903: }
904: }
905: });
906: addLink("initBagDone", "findBusiness");
907:
908: addYPQ("findBusiness", "findBusinessDone", new YPQ() {
909: public YPFuture get(Frame f) {
910: return getYPProxy().find_business(namePatterns,
911: null, null, bag, null, findQualifiers,
912: 1);
913: }
914:
915: public void set(Frame f, Object result) {
916: businessList = (BusinessList) result;
917: }
918:
919: public void handle(Frame f, Exception e) {
920: log.error("findBusiness", e);
921: callback.handle(e);
922: }
923: });
924:
925: add(new SState("findBusinessDone") {
926: public void invoke() {
927: if (businessList == null) {
928: callback.invoke(Boolean.TRUE); // easy - nothing to delete
929: transit(DONE);
930: }
931: transit("deleteBusiness");
932: }
933: });
934: add(new SState("deleteBusiness") {
935: public void invoke() {
936: iter = businessList.getBusinessInfos()
937: .getBusinessInfoVector().iterator();
938: transit("dbLoop");
939: }
940: });
941: add(new SState("dbLoop") {
942: public void invoke() {
943: if (iter.hasNext()) {
944: businessInfo = (BusinessInfo) iter.next();
945: if (businessInfo == null) {
946: if (log.isDebugEnabled()) {
947: log
948: .debug("deleteServiceDescription, cannot find registration for: "
949: + providerName);
950: }
951: } else {
952: transit("dbLoop1");
953: }
954: } else {
955: transit("dbDone");
956: }
957: }
958: });
959: addYPQ("dbLoop1", "dbLoop", new YPQ() {
960: public YPFuture get(Frame f) {
961: serviceKeys = new Vector();
962: for (Enumeration e = businessInfo
963: .getServiceInfos()
964: .getServiceInfoVector().elements(); e
965: .hasMoreElements();) {
966: serviceKeys.add(((ServiceInfo) e
967: .nextElement()).getServiceKey());
968: }
969: return getYPProxy().delete_service(
970: getAuthToken().getAuthInfoString(),
971: serviceKeys);
972: }
973:
974: public void set(Frame f, Object r) {
975: // dont care
976: }
977:
978: public void handle(Frame f, Exception e) {
979: log.error("deleteServiceDescription exception",
980: e);
981: transit("dbLoop"); // continue to next
982: }
983: });
984: add(new SState("dbDone") {
985: public void invoke() {
986: callback.invoke(Boolean.TRUE);
987: // let it complete while we're discarding the token
988: call("discardAuthToken", null, "DONE");
989: }
990: });
991: } // end of init()
992: } // end of DeleteSM defi
993: } // end of RegistrationServiceImpl def
994: }
|