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.repository.impl;
043:
044: import java.util.HashMap;
045: import java.util.Map;
046: import java.util.Set;
047: import org.netbeans.modules.cnd.repository.api.Repository;
048: import org.netbeans.modules.cnd.repository.spi.Key;
049: import org.netbeans.modules.cnd.repository.spi.Persistent;
050: import org.netbeans.modules.cnd.repository.spi.RepositoryListener;
051: import org.netbeans.modules.cnd.repository.testbench.Stats;
052: import org.netbeans.modules.cnd.repository.util.RepositoryListenersManager;
053:
054: /**
055: *
056: * @author Vladimir Voskresensky
057: */
058: public final class DelegateRepository implements Repository {
059:
060: private final Repository delegate;
061:
062: private Map<String, Object> locks = new HashMap<String, Object>();
063: private String mainLock = new String("DelegateRepository main lock"); // NOI18N
064:
065: public DelegateRepository() {
066: if (Stats.validateKeys) {
067: Stats.log("Testing keys using KeyValidatorRepository."); // NOI18N
068: delegate = new KeyValidatorRepository();
069: } else if (Stats.useHardRefRepository) {
070: Stats.log("Using HashMapRepository."); // NOI18N
071: delegate = new HashMapRepository();
072: } else {
073: Stats.log("by default using HybridRepository."); // NOI18N
074: delegate = new HybridRepository();
075: }
076: }
077:
078: public void hang(Key key, Persistent obj) {
079: delegate.hang(key, obj);
080: }
081:
082: public void put(Key key, Persistent obj) {
083: delegate.put(key, obj);
084: }
085:
086: public Persistent get(Key key) {
087: Persistent result = delegate.get(key);
088: if (result == null && Stats.useNullWorkaround) {
089: String keyClassName = key.getClass().getName();
090: // repository is often asked for projects when theis persistence just does not exist
091: if (!keyClassName.endsWith(".ProjectKey")
092: && !keyClassName
093: .endsWith(".OffsetableDeclarationKey")) { // NOI18N
094: System.err.printf(
095: "NULL returned for key %s on attempt 1\n", key);
096: result = delegate.get(key);
097: System.err.printf(
098: "%s value returned for key %s on attempt 2\n",
099: (result == null) ? "NULL" : "NON-NULL", key);
100: }
101: }
102: return result;
103: }
104:
105: public Persistent tryGet(Key key) {
106: return delegate.tryGet(key);
107: }
108:
109: public void remove(Key key) {
110: delegate.remove(key);
111: }
112:
113: public void debugClear() {
114: delegate.debugClear();
115: }
116:
117: public void shutdown() {
118: delegate.shutdown();
119: }
120:
121: private Object getLock(String unitName) {
122: synchronized (mainLock) {
123: Object lock = locks.get(unitName);
124: if (lock == null) {
125: lock = new String(unitName); // NOI18N
126: locks.put(unitName, lock);
127: }
128: return lock;
129: }
130: }
131:
132: public void openUnit(String unitName) {
133: synchronized (getLock(unitName)) {
134: delegate.openUnit(unitName);
135: }
136: }
137:
138: public void closeUnit(String unitName, boolean cleanRepository,
139: Set<String> requiredUnits) {
140: synchronized (getLock(unitName)) {
141: delegate
142: .closeUnit(unitName, cleanRepository, requiredUnits);
143: }
144: }
145:
146: public void removeUnit(String unitName) {
147: synchronized (getLock(unitName)) {
148: delegate.removeUnit(unitName);
149: }
150: }
151:
152: public void cleanCaches() {
153: delegate.cleanCaches();
154: }
155:
156: public void registerRepositoryListener(RepositoryListener aListener) {
157: RepositoryListenersManager.getInstance().registerListener(
158: aListener);
159: }
160:
161: public void unregisterRepositoryListener(
162: RepositoryListener aListener) {
163: RepositoryListenersManager.getInstance().unregisterListener(
164: aListener);
165: }
166:
167: public void startup(int persistMechanismVersion) {
168: delegate.startup(persistMechanismVersion);
169: }
170:
171: }
|