001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.core.agent.service.uid;
028:
029: import org.cougaar.core.mts.MessageAddress;
030: import org.cougaar.core.persist.PersistenceState;
031: import org.cougaar.core.service.UIDService;
032: import org.cougaar.core.util.UID;
033: import org.cougaar.core.util.UniqueObject;
034:
035: /**
036: * The UIDService implementation.
037: */
038: final class UIDServiceImpl implements UIDService {
039: private MessageAddress cid;
040: private String prefix;
041: private long count = System.currentTimeMillis();
042:
043: public UIDServiceImpl(MessageAddress cid) {
044: this .cid = cid;
045: prefix = cid.getAddress();
046: }
047:
048: /**
049: * MessageAddress of the agent.
050: * <p>
051: * This is primarily for backwards compatibility; components
052: * should get their agent's address through the
053: * {@link org.cougaar.core.service.AgentIdentificationService},
054: * which the common component base classes provide as a
055: * "getAgentIdentifier()" method.
056: */
057: public MessageAddress getMessageAddress() {
058: return cid;
059: }
060:
061: private synchronized long nextID() {
062: return ++count;
063: }
064:
065: /** Take the next Unique ID. */
066: public UID nextUID() {
067: return new UID(prefix, nextID());
068: }
069:
070: /**
071: * Assign the next UID to a unique object.
072: * <p>
073: * This is equivalent to <code>o.setUID(nextUID())</code>.
074: */
075: public UID registerUniqueObject(UniqueObject o) {
076: UID uid = nextUID();
077: o.setUID(uid);
078: return uid;
079: }
080:
081: // persistence backwards compatibility
082: //
083: // This is no longer used, since we assume that a rehydrated
084: // agent's current time is greater than its crashed instance's
085: // counter. If we hand out UIDs faster than one a millisecond
086: // then this could cause problems!
087: public synchronized PersistenceState getPersistenceState() {
088: return new UIDServerPersistenceState(count);
089: }
090:
091: public synchronized void setPersistenceState(PersistenceState state) {
092: if (state instanceof UIDServerPersistenceState) {
093: long persistedCount = ((UIDServerPersistenceState) state).count;
094: if (persistedCount > count)
095: count = persistedCount;
096: } else {
097: throw new IllegalArgumentException(state.toString());
098: }
099: }
100:
101: private static class UIDServerPersistenceState implements
102: PersistenceState {
103: public long count;
104:
105: public UIDServerPersistenceState(long count) {
106: this.count = count;
107: }
108: }
109: }
|