001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.yp;
028:
029: import java.util.Properties;
030:
031: import org.cougaar.core.service.ThreadService;
032: import org.cougaar.core.thread.Schedulable;
033: import org.cougaar.util.log.Logger;
034: import org.cougaar.util.log.Logging;
035:
036: /**
037: * Using OneShotMachine is similar to directly setting a callback on a YPFuture, except
038: * that the invocation of the callback is done in a pooled thread rather than
039: * in the thread of the YPService.
040: **/
041:
042: public class OneShotMachine {
043: private static final Logger log = Logging
044: .getLogger(YPStateMachine.class);
045:
046: private static String UDDI_USERID = "cougaar";
047: private static String UDDI_PASSWORD = "cougaarPass";
048:
049: static {
050: UDDI_USERID = System.getProperty(
051: "org.cougaar.yp.juddi-users.username",
052: YPProxy.DEFAULT_UDDI_USERNAME);
053: UDDI_PASSWORD = System.getProperty(
054: "org.cougaar.yp.juddi-users.password",
055: YPProxy.DEFAULT_UDDI_PASSWORD);
056: }
057:
058: private final Properties ypproperties = new Properties();
059:
060: private final YPService yps;
061:
062: protected final YPService getYPService() {
063: return yps;
064: }
065:
066: private final ThreadService threads;
067:
068: protected final ThreadService getThreadService() {
069: return threads;
070: }
071:
072: private final YPFuture fut;
073:
074: protected final YPFuture getYPFuture() {
075: return fut;
076: }
077:
078: private final YPFuture.ResponseCallback rcallback;
079:
080: protected final YPFuture.ResponseCallback getResponseCallback() {
081: return rcallback;
082: }
083:
084: public OneShotMachine(YPFuture fut,
085: YPFuture.ResponseCallback rcallback, YPService yps,
086: ThreadService threads) {
087: this .fut = fut;
088: this .rcallback = rcallback;
089: this .yps = yps;
090: this .threads = threads;
091: ypproperties.put("username", UDDI_USERID);
092: ypproperties.put("password", UDDI_PASSWORD);
093: if (log.isInfoEnabled()) {
094: log.info("OneShotMachine constructor=" + this );
095: }
096: }
097:
098: /** set a property for just this instance of YPStateMachine.
099: * Current properties are "username" and "password".
100: * @note that it is undefined to change existing values after initialization.
101: **/
102: public void setProperty(String name, String value) {
103: ypproperties.put(name, value);
104: }
105:
106: /** Sets the machine in motion. Will continue until DONE (or an error state) is achieved **/
107: public void start() {
108: submit();
109: }
110:
111: private YPFuture.Callback ypcallback = new YPFuture.Callback() {
112: public void ready(YPFuture r) {
113: if (r != fut) {
114: log.warn("In StateMachine YPCallback, expected " + fut
115: + " instead of " + r);
116: }
117: if (fut.isReady()) {
118: kick();
119: } else {
120: log
121: .error("YPCallback invoked without YPFuture.isReady()!");
122: }
123: }
124: };
125:
126: // utility for submitting ypqueries and waiting for response
127: protected void submit() {
128: if (log.isInfoEnabled()) {
129: log.info("OneShotMachine(" + this + ").submit("
130: + ypcallback + ")");
131: }
132: fut.setCallback(ypcallback);
133: yps.submit(fut);
134: }
135:
136: // only kicked when ready by the callback!
137: private void kick() {
138: Schedulable thread = getThreadService().getThread(this ,
139: new Runnable() {
140: public void run() {
141: if (log.isInfoEnabled()) {
142: log
143: .info("OneShotMachine("
144: + (OneShotMachine.this )
145: + ").run()");
146: }
147: try {
148: Object r = fut.get();
149: rcallback.invoke(r);
150: } catch (Exception e) {
151: rcallback.handle(e);
152: }
153: }
154: });
155: thread.start();
156: }
157: }
|