001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.cache.impl;
043:
044: import java.io.BufferedInputStream;
045: import java.io.BufferedOutputStream;
046: import java.io.File;
047: import java.io.FileInputStream;
048: import java.io.FileOutputStream;
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.io.ObjectInputStream;
052: import java.io.ObjectOutputStream;
053: import java.io.OutputStream;
054: import java.io.Serializable;
055: import java.util.HashMap;
056: import java.util.Iterator;
057: import java.util.Map;
058:
059: /**
060: * base class for cache indexers
061: * @author Vladimir Voskresensky
062: */
063: abstract class AbstractCacheIndex implements Serializable {
064: private static final long serialVersionUID = -7790789617759717718L;
065:
066: private Object indexLock = new Object();
067: private Map<CharSequence, Object> index;
068:
069: protected AbstractCacheIndex() {
070: index = new HashMap();
071: }
072:
073: ////////////////////////////////////////////////////////////////////////////
074: // index content support
075:
076: /** returns string key for caching object */
077: protected abstract CharSequence getIndexKey(Object obj2cache);
078:
079: /** creates value object from cacheName */
080: protected abstract Object createValue(CharSequence cacheName,
081: Object obj2cache);
082:
083: /** get base cache name for the object */
084: protected abstract CharSequence getBaseCacheName(Object obj2cache);
085:
086: /** checks if cacheName the same as defined in current value */
087: protected abstract boolean isEqual(Object value,
088: CharSequence checkCacheName);
089:
090: /** gets relative cache file for object in this indexer */
091: protected Object get(Object obj) {
092: CharSequence key = getIndexKey(obj);
093: assert (key != null);
094: synchronized (indexLock) {
095: assert (index != null);
096: return index.get(key);
097: }
098: }
099:
100: /** put object in indexer and returns relative cache file */
101: protected Object put(Object obj) {
102: CharSequence key = getIndexKey(obj);
103: CharSequence baseValue = getBaseCacheName(obj);
104: Object value = createUniqValueImpl(baseValue, obj);
105: assert (key != null && key.length() > 0);
106: assert (value != null);
107: synchronized (indexLock) {
108: assert (index != null);
109: index.put(key, value);
110: }
111: return value;
112: }
113:
114: private Object createUniqValueImpl(CharSequence baseValue,
115: Object obj2cache) {
116: assert (baseValue != null);
117: assert (baseValue.length() > 0);
118: int postfix = 0;
119: CharSequence value = baseValue;
120: while (true) {
121: boolean found = false;
122: synchronized (indexLock) {
123: for (Iterator it = index.values().iterator(); it
124: .hasNext()
125: && !found;) {
126: Object elem = it.next();
127: if (isEqual(elem, value)) {
128: found = true;
129: }
130: }
131: }
132: if (!found) {
133: return createValue(value, obj2cache);
134: } else {
135: value = baseValue.toString() + postfix++;
136: }
137: }
138: }
139:
140: ////////////////////////////////////////////////////////////////////////////
141: // save/load support
142:
143: public boolean load(File file) {
144: ObjectInputStream ois = null;
145: try {
146: InputStream in = null;
147: try {
148: in = new FileInputStream(file);
149: in = new BufferedInputStream(in);
150: ois = new ObjectInputStream(in);
151: } finally {
152: if (in != null && ois == null) {
153: in.close();
154: }
155: }
156: if (ois != null) {
157: loadData(ois);
158: return true;
159: }
160: } catch (IOException io) {
161: // null cache
162: } catch (ClassNotFoundException e) {
163: // null cache
164: } finally {
165: if (ois != null) {
166: try {
167: ois.close();
168: } catch (IOException e) {
169: // ignore
170: }
171: }
172: }
173: return false;
174: }
175:
176: protected void loadData(ObjectInputStream ois) throws IOException,
177: ClassNotFoundException {
178: // version 1:
179: // - index map
180:
181: int version = ois.readInt();
182: if (version >= 1) {
183: Map index = (Map) ois.readObject();
184: synchronized (indexLock) {
185: this .index = index;
186: }
187: }
188: assert (index != null);
189: }
190:
191: public boolean save(File file) {
192: ObjectOutputStream oos = null;
193: try {
194: OutputStream os = null;
195: try {
196: os = new FileOutputStream(file);
197: os = new BufferedOutputStream(os);
198: oos = new ObjectOutputStream(os);
199: } finally {
200: if (os != null && oos == null) {
201: os.close();
202: }
203: }
204: if (oos != null) {
205: saveData(oos);
206: return true;
207: }
208: } catch (IOException io) {
209: // null cache
210: } finally {
211: if (oos != null) {
212: try {
213: oos.close();
214: } catch (IOException e) {
215: // ignore
216: }
217: }
218: }
219: return false;
220: }
221:
222: protected void saveData(ObjectOutputStream oos) throws IOException {
223: // version
224: int version = 1;
225: oos.writeInt(version);
226:
227: // version 1:
228: // - index map
229: synchronized (indexLock) {
230: oos.writeObject(index);
231: }
232: }
233:
234: public String toString() {
235: synchronized (indexLock) {
236: return index.toString();
237: }
238: }
239: }
|