001: /*
002: * <copyright>
003: * Copyright 1997-2001 BBNT Solutions, LLC
004: * under sponsorship of the Defense Advanced Research Projects Agency (DARPA).
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the Cougaar Open Source License as published by
008: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
009: *
010: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
011: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
012: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
013: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
014: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
015: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
016: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
017: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
018: * PERFORMANCE OF THE COUGAAR SOFTWARE.
019: * </copyright>
020: */
021: package org.cougaar.core.persist;
022:
023: import java.io.PrintStream;
024: import java.util.Collection;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Set;
029: import org.cougaar.bootstrap.SystemProperties;
030: import org.cougaar.core.agent.RegisterContext;
031: import org.cougaar.core.agent.service.uid.UIDServiceComponent;
032: import org.cougaar.core.blackboard.EnvelopeTuple;
033: import org.cougaar.core.blackboard.PersistenceEnvelope;
034: import org.cougaar.core.component.BindingSite;
035: import org.cougaar.core.component.BindingUtility;
036: import org.cougaar.core.component.Component;
037: import org.cougaar.core.component.ServiceBroker;
038: import org.cougaar.core.component.ServiceBrokerSupport;
039: import org.cougaar.core.component.ServiceProvider;
040: import org.cougaar.core.component.ServiceRevokedListener;
041: import org.cougaar.core.logging.LoggingServiceProvider;
042: import org.cougaar.core.mts.MessageAddress;
043: import org.cougaar.core.service.AgentIdentificationService;
044: import org.cougaar.core.service.LoggingService;
045: import org.cougaar.util.UnaryPredicate;
046:
047: /*
048: import org.cougaar.planning.ldm.asset.Asset;
049: import org.cougaar.planning.ldm.asset.PropertyGroup;
050: import org.cougaar.planning.ldm.plan.*;
051: import org.cougaar.glm.ldm.asset.*;
052: */
053:
054: /**
055: * Test class to rehydrate an agent snapshot and print the contents.
056: * <p>
057: * Usage is:<pre>
058: * java \
059: * -Xbootclasspath/p:$COUGAAR_INSTALL_PATH/lib/javaiopatch.jar \
060: * -Djava.class.path=$COUGAAR_INSTALL_PATH/lib/bootstrap.jar \
061: * -Dorg.cougaar.install.path=$COUGAAR_INSTALL_PATH \
062: * -Dorg.cougaar.core.persistence.enable=true \
063: * org.cougaar.bootstrap.Bootstrapper \
064: * org.cougaar.core.persist.RehydrateTest \
065: * <i>THE_AGENT_NAME</i>
066: * </pre>
067: * Optionally enable persistence detail logging by setting:<pre>
068: * -Dorg.cougaar.core.logging.log4j.category.org.cougaar.core=DETAIL#org.cougaar.util.log.log4j.DetailPriority
069: * </pre>
070: *
071: * @property org.cougaar.core.persist.rehydrateTest.agent
072: * Default RehydrateTest agent name if not specified on the
073: * command line
074: */
075: public class RehydrateTest {
076:
077: private static final String DEFAULT_AGENT_PROP = "org.cougaar.core.persist.rehydrateTest.agent";
078:
079: private static final String DEFAULT_AGENT = SystemProperties
080: .getProperty(DEFAULT_AGENT_PROP);
081:
082: public static void main(String[] args) throws Exception {
083: (new RehydrateTest(args)).run();
084: }
085:
086: private final String[] args;
087: private ServiceBroker sb;
088:
089: public RehydrateTest(String[] args) {
090: this .args = args;
091: }
092:
093: public void run() throws Exception {
094: String name = getAgent();
095: System.out.println("Test rehydrate " + name);
096:
097: // create component model and minimal services
098: createServiceBroker();
099: loadAgentIdentificationService(name);
100: loadLoggingService();
101: loadUIDService();
102: loadContext();
103: loadPersistence();
104:
105: // rehydrate
106: rehydrateAgent();
107:
108: // get blackboard contents
109: System.out.println("# rehydrating blackboard for " + name);
110: PersistenceEnvelope rehydrationEnvelope = rehydrateBlackboard();
111:
112: // print objects matching our filter
113: UnaryPredicate pred = createPredicate();
114: showContents(rehydrationEnvelope, pred);
115: }
116:
117: protected String getAgent() {
118: if (args != null && args.length > 0) {
119: return args[0];
120: }
121: if (DEFAULT_AGENT != null) {
122: return DEFAULT_AGENT;
123: }
124: throw new RuntimeException("Specify an agent name");
125: }
126:
127: protected UnaryPredicate createPredicate() {
128: UnaryPredicate pred = new UnaryPredicate() {
129: public boolean execute(Object o) {
130: return true;
131: }
132: };
133: /*
134: final Verb GLS = Verb.getVerb("GetLogSupport");
135: UnaryPredicate pred = new UnaryPredicate() {
136: public boolean execute(Object o) {
137: if (o instanceof Task) {
138: Task t = (Task) o;
139: Verb v = t.getVerb();
140: return (v != null && v.equals(GLS));
141: } else if (o instanceof AssetTransfer) {
142: return true;
143: } else if (o instanceof Organization) {
144: return true;
145: }
146: return false;
147: }
148: };
149: */
150: return pred;
151: }
152:
153: /**
154: * Construct the service broker with minimal services.
155: */
156: private void createServiceBroker() {
157: this .sb = new ServiceBrokerSupport() {
158: };
159: }
160:
161: private void loadAgentIdentificationService(String name) {
162: sb.addService(AgentIdentificationService.class,
163: new AgentIdentificationServiceProvider(name));
164: }
165:
166: private void loadLoggingService() {
167: sb.addService(LoggingService.class,
168: new LoggingServiceProvider());
169: }
170:
171: private void loadUIDService() {
172: UIDServiceComponent uidC = new UIDServiceComponent();
173: load(uidC);
174: }
175:
176: private void loadContext() {
177: RegisterContext rc = new RegisterContext();
178: load(rc);
179: }
180:
181: //DataProtectionService.class
182: private void loadPersistence() {
183: PersistenceServiceComponent psc = new PersistenceServiceComponent();
184: load(psc);
185: }
186:
187: private void load(Component c) {
188: BindingSite bs = new BindingSite() {
189: public ServiceBroker getServiceBroker() {
190: return sb;
191: }
192:
193: public void requestStop() {
194: }
195: };
196: BindingUtility.activate(c, bs, sb);
197: }
198:
199: private void rehydrateAgent() {
200: rehydrateList("ignoreme");
201: }
202:
203: private List rehydrateList(String clname) {
204: // mobile state
205: PersistenceObject persistenceObject = null;
206: // identity
207: final PersistenceIdentity persistenceIdentity = new PersistenceIdentity(
208: clname);
209: PersistenceClient persistenceClient = new PersistenceClient() {
210: public PersistenceIdentity getPersistenceIdentity() {
211: return persistenceIdentity;
212: }
213:
214: public List getPersistenceData() {
215: throw new UnsupportedOperationException();
216: }
217: };
218: // rehydrate list
219: PersistenceServiceForAgent persistenceService = (PersistenceServiceForAgent) sb
220: .getService(persistenceClient,
221: PersistenceServiceForAgent.class, null);
222: persistenceService.rehydrate(persistenceObject);
223: RehydrationData rd = persistenceService.getRehydrationData();
224: List rehydrationList = (rd == null ? null : rd.getObjects());
225: return rehydrationList;
226: }
227:
228: private PersistenceEnvelope rehydrateBlackboard()
229: throws PersistenceException {
230: Object state = null;
231: Persistence persistence = BlackboardPersistence.find(sb);
232: PersistenceEnvelope rehydrationEnvelope = new PersistenceEnvelope();
233: // ignore the returned "RehydrateResult", we only want the envelope
234: persistence.rehydrate(rehydrationEnvelope, state);
235: return rehydrationEnvelope;
236: }
237:
238: private void showContents(PersistenceEnvelope rehydrationEnvelope,
239: UnaryPredicate pred) {
240: int size = rehydrationEnvelope.size();
241: System.out.println("size: " + size);
242: Set addedSet = new HashSet();
243: Set removedSet = new HashSet();
244: Iterator iter = rehydrationEnvelope.getAllTuples();
245: while (iter.hasNext()) {
246: EnvelopeTuple et = (EnvelopeTuple) iter.next();
247: Object obj = et.getObject();
248: if (et.isAdd()) {
249: if (pred.execute(obj))
250: addedSet.add(obj);
251: } else if (et.isBulk()) {
252: Collection c = (Collection) obj;
253: for (Iterator x = c.iterator(); x.hasNext();) {
254: Object o2 = x.next();
255: if (pred.execute(o2))
256: addedSet.add(o2);
257: }
258: } else if (et.isRemove()) {
259: if (pred.execute(obj))
260: removedSet.add(obj);
261: }
262: }
263: System.out.println("addedSet[" + addedSet.size() + "]:");
264: for (Iterator i2 = addedSet.iterator(); i2.hasNext();) {
265: Object obj = i2.next();
266: System.out.println(" " + obj);
267: }
268: System.out.println("removedSet[" + removedSet.size() + "]:");
269: for (Iterator i2 = removedSet.iterator(); i2.hasNext();) {
270: Object obj = i2.next();
271: System.out.println(" " + obj);
272: }
273: }
274:
275: private static class AgentIdentificationServiceProvider implements
276: ServiceProvider {
277: private final AgentIdentificationService AIS;
278:
279: public AgentIdentificationServiceProvider(final String name) {
280: final MessageAddress addr = MessageAddress
281: .getMessageAddress(name);
282: this .AIS = new AgentIdentificationService() {
283: public MessageAddress getMessageAddress() {
284: return addr;
285: }
286:
287: public String getName() {
288: return name;
289: }
290: };
291: }
292:
293: public Object getService(ServiceBroker sb, Object requestor,
294: Class serviceClass) {
295: if (AgentIdentificationService.class
296: .isAssignableFrom(serviceClass)) {
297: return AIS;
298: } else {
299: return null;
300: }
301: }
302:
303: public void releaseService(ServiceBroker sb, Object requestor,
304: Class serviceClass, Object service) {
305: }
306: }
307: }
|