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.persist;
028:
029: import org.cougaar.bootstrap.SystemProperties;
030: import org.cougaar.core.adaptivity.OMCRange;
031: import org.cougaar.core.adaptivity.OMCRangeList;
032: import org.cougaar.util.log.Logger;
033:
034: /**
035: * Adapter to simplify writing {@link PersistencePlugin} implementations.
036: * Implements several methods in the PersistencePlugin API that often
037: * need not be specialized in individual implementaions.
038: */
039: public abstract class PersistencePluginAdapter implements
040: PersistenceNames {
041: protected static final String[] emptyStringArray = new String[0];
042: protected static final OMCRangeList emptyOMCRangeList = new OMCRangeList(
043: new OMCRange[0]);
044: protected static String[] controlNames = { PERSISTENCE_ARCHIVE_COUNT_NAME };
045: protected PersistencePluginSupport pps;
046:
047: protected String name;
048: protected String[] params;
049: protected int archiveCount; // The number of archives to keep
050: protected int consolidationPeriod;
051: protected long persistenceInterval;
052: protected boolean writable = true;
053:
054: protected void init(PersistencePluginSupport pps, String name,
055: String[] params) {
056: this .pps = pps;
057: this .name = name;
058: this .params = params;
059: archiveCount = SystemProperties
060: .getInt(
061: PERSISTENCE_ARCHIVE_COUNT_PROP,
062: (SystemProperties
063: .getBoolean("org.cougaar.core.persistence.archivingDisabled") ? 0
064: : Integer.MAX_VALUE));
065: Logger logger = pps.getLogger();
066: for (int i = 0; i < params.length; i++) {
067: String param = params[i];
068: try {
069: if (param.startsWith(PERSISTENCE_ARCHIVE_COUNT_PREFIX)) {
070: archiveCount = Integer.parseInt(param
071: .substring(PERSISTENCE_ARCHIVE_COUNT_PREFIX
072: .length()));
073: if (logger.isDebugEnabled())
074: logger.debug("archiveCount=" + archiveCount);
075: continue;
076: }
077: if (param.startsWith(PERSISTENCE_INTERVAL_PREFIX)) {
078: persistenceInterval = Long.parseLong(param
079: .substring(PERSISTENCE_INTERVAL_PREFIX
080: .length()));
081: if (logger.isDebugEnabled())
082: logger.debug("persistenceInterval="
083: + persistenceInterval);
084: continue;
085: }
086: if (param
087: .startsWith(PERSISTENCE_CONSOLIDATION_PERIOD_PREFIX)) {
088: consolidationPeriod = Integer
089: .parseInt(param
090: .substring(PERSISTENCE_CONSOLIDATION_PERIOD_PREFIX
091: .length()));
092: if (logger.isDebugEnabled())
093: logger.debug("consolidationPeriod="
094: + consolidationPeriod);
095: continue;
096: }
097: if (param.startsWith(PERSISTENCE_DISABLE_WRITE_PREFIX)) {
098: writable = !"true".equals(param
099: .substring(PERSISTENCE_DISABLE_WRITE_PREFIX
100: .length()));
101: if (logger.isDebugEnabled())
102: logger.debug("writable=" + writable);
103: continue;
104: }
105: handleParameter(param);
106: } catch (Exception e) {
107: pps.getLogger().error("Parse error " + param);
108: }
109: }
110: }
111:
112: protected abstract void handleParameter(String param);
113:
114: protected String parseParamValue(String param, String key) {
115: if (param.startsWith(key)) {
116: return param.substring(key.length());
117: }
118: return null;
119: }
120:
121: public String getName() {
122: return name;
123: }
124:
125: public boolean isWritable() {
126: return writable;
127: }
128:
129: public void setWritable(boolean newWritable) {
130: writable = newWritable;
131: }
132:
133: public long getPersistenceInterval() {
134: return persistenceInterval;
135: }
136:
137: public void setPersistenceInterval(long newInterval) {
138: persistenceInterval = newInterval;
139: }
140:
141: public int getConsolidationPeriod() {
142: return consolidationPeriod;
143: }
144:
145: public void setConsolidationPeriod(int newPeriod) {
146: consolidationPeriod = newPeriod;
147: }
148:
149: public int getParamCount() {
150: return params.length;
151: }
152:
153: public String getParam(int i) {
154: return params[i];
155: }
156:
157: public String[] getControlNames() {
158: return controlNames;
159: }
160:
161: public OMCRangeList getControlValues(String controlName) {
162: if (PERSISTENCE_ARCHIVE_COUNT_NAME.equals(controlName)) {
163: return new OMCRangeList(new Integer(0), new Integer(
164: Integer.MAX_VALUE));
165: }
166: return emptyOMCRangeList; // Should never be called
167: }
168:
169: public void setControl(String controlName, Comparable newValue) {
170: if (PERSISTENCE_ARCHIVE_COUNT_NAME.equals(controlName)) {
171: archiveCount = ((Integer) newValue).intValue();
172: }
173: }
174:
175: public java.sql.Connection getDatabaseConnection(Object locker) {
176: throw new UnsupportedOperationException(
177: "FilePersistence.getDatabaseConnection not supported");
178: }
179:
180: public void releaseDatabaseConnection(Object locker) {
181: throw new UnsupportedOperationException(
182: "FilePersistence.releaseDatabaseConnection not supported");
183: }
184:
185: public boolean checkOwnership() throws PersistenceException {
186: return true;
187: }
188:
189: public void lockOwnership() throws PersistenceException {
190: }
191:
192: public void unlockOwnership() throws PersistenceException {
193: }
194: }
|