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.repository;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.List;
047: import java.util.Set;
048: import org.netbeans.modules.cnd.api.model.CsmIdentifiable;
049: import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
050: import org.netbeans.modules.cnd.api.model.CsmUID;
051: import org.netbeans.modules.cnd.api.project.NativeProject;
052: import org.netbeans.modules.cnd.apt.debug.DebugUtils;
053: import org.netbeans.modules.cnd.modelimpl.csm.core.ProjectBase;
054: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
055: import org.netbeans.modules.cnd.repository.api.Repository;
056: import org.netbeans.modules.cnd.repository.api.RepositoryAccessor;
057: import org.netbeans.modules.cnd.repository.spi.Key;
058: import org.netbeans.modules.cnd.repository.spi.Persistent;
059: import org.netbeans.modules.cnd.repository.spi.RepositoryListener;
060:
061: /**
062: *
063: * @author Vladimir Voskresensky
064: */
065: public class RepositoryUtils {
066:
067: private static final boolean TRACE_REPOSITORY_ACCESS = DebugUtils
068: .getBoolean("cnd.modelimpl.trace.repository", false);
069:
070: /**
071: * the version of the persistency mechanism
072: */
073: private static int CURRENT_VERSION_OF_PERSISTENCY = 24;
074:
075: /** Creates a new instance of RepositoryUtils */
076: private RepositoryUtils() {
077: }
078:
079: ////////////////////////////////////////////////////////////////////////////
080: // repository access wrappers
081: private static volatile int counter = 0;
082:
083: public static <T extends CsmIdentifiable> T get(CsmUID<T> uid) {
084: Key key = UIDtoKey(uid);
085: Persistent out = get(key);
086: assert out == null || (out instanceof CsmIdentifiable);
087: return (T) out;
088: }
089:
090: public static Persistent tryGet(Key key) {
091: assert key != null;
092: Persistent out = RepositoryAccessor.getRepository().tryGet(key);
093: if (TRACE_REPOSITORY_ACCESS) {
094: System.err.printf("%d:trying key %s got %s", nextIndex(),
095: key, out);
096: }
097: return out;
098: }
099:
100: public static Persistent get(Key key) {
101: assert key != null;
102: long time = 0;
103: int index = 0;
104: if (TRACE_REPOSITORY_ACCESS) {
105: index = nextIndex();
106: time = System.currentTimeMillis();
107: System.err.println(index + ":getting key " + key);
108: }
109: Persistent out = RepositoryAccessor.getRepository().get(key);
110: if (TRACE_REPOSITORY_ACCESS) {
111: time = System.currentTimeMillis() - time;
112: System.err.println(index + ":got in " + time
113: + "ms the key " + key);
114: }
115: return out;
116: }
117:
118: private static synchronized int nextIndex() {
119: return counter++;
120: }
121:
122: public static void remove(CsmUID uid) {
123: Key key = UIDtoKey(uid);
124: if (key != null) {
125: long time = 0;
126: int index = 0;
127: if (TRACE_REPOSITORY_ACCESS) {
128: index = nextIndex();
129: time = System.currentTimeMillis();
130: System.err.println(index + ":removing key " + key);
131: }
132: if (!TraceFlags.SAFE_REPOSITORY_ACCESS) {
133: RepositoryAccessor.getRepository().remove(key);
134: }
135: if (TRACE_REPOSITORY_ACCESS) {
136: time = System.currentTimeMillis() - time;
137: System.err.println(index + ":removed in " + time
138: + "ms the key " + key);
139: }
140: }
141: }
142:
143: public static void remove(Collection<? extends CsmUID> uids) {
144: if (uids != null) {
145: for (CsmUID uid : uids) {
146: remove(uid);
147: }
148: }
149: }
150:
151: public static CsmUID put(CsmIdentifiable csmObj) {
152: CsmUID uid = null;
153: if (csmObj != null) {
154: uid = csmObj.getUID();
155: assert uid != null;
156: Key key = UIDtoKey(uid);
157: put(key, (Persistent) csmObj);
158: }
159: return uid;
160: }
161:
162: public static void put(Key key, Persistent obj) {
163: if (key != null) {
164: long time = 0;
165: int index = 0;
166: if (TRACE_REPOSITORY_ACCESS) {
167: index = nextIndex();
168: time = System.currentTimeMillis();
169: System.err.println(index + ":putting key " + key);
170: }
171: RepositoryAccessor.getRepository().put(key, obj);
172: if (TRACE_REPOSITORY_ACCESS) {
173: time = System.currentTimeMillis() - time;
174: System.err.println(index + ":put in " + time
175: + "ms the key " + key);
176: }
177: }
178: }
179:
180: public static void hang(CsmIdentifiable csmObj) {
181: CsmUID uid = null;
182: if (csmObj != null) {
183: uid = csmObj.getUID();
184: assert uid != null;
185: Key key = UIDtoKey(uid);
186: hang(key, (Persistent) csmObj);
187: }
188: }
189:
190: public static void hang(Key key, Persistent obj) {
191: if (key != null) {
192: long time = 0;
193: int index = 0;
194: if (TRACE_REPOSITORY_ACCESS) {
195: index = nextIndex();
196: time = System.currentTimeMillis();
197: System.err.println(index + ":hanging key " + key);
198: }
199: RepositoryAccessor.getRepository().hang(key, obj);
200: if (TRACE_REPOSITORY_ACCESS) {
201: time = System.currentTimeMillis() - time;
202: System.err.println(index + ":hung in " + time
203: + "ms the key " + key);
204: }
205: }
206: }
207:
208: public static <T extends CsmOffsetableDeclaration> List<CsmUID<T>> put(
209: List<T> decls) {
210: assert decls != null;
211: List<CsmUID<T>> uids = new ArrayList<CsmUID<T>>(decls.size());
212: for (T decl : decls) {
213: CsmUID<T> uid = put(decl);
214: uids.add(uid);
215: }
216: return uids;
217: }
218:
219: ////////////////////////////////////////////////////////////////////////////
220: //
221: public static Key UIDtoKey(CsmUID uid) {
222: if (uid instanceof KeyHolder) {
223: return ((KeyHolder) uid).getKey();
224: } else {
225: return null;
226: }
227: }
228:
229: public static CharSequence getUnitName(CsmUID uid) {
230: Key key = UIDtoKey(uid);
231: assert key != null;
232: CharSequence unitName = key.getUnit();
233:
234: return unitName;
235: }
236:
237: public static void startup() {
238: Repository repository = RepositoryAccessor.getRepository();
239: repository.startup(CURRENT_VERSION_OF_PERSISTENCY);
240: repository.unregisterRepositoryListener(RepositoryListenerImpl
241: .instance());
242: repository.registerRepositoryListener(RepositoryListenerImpl
243: .instance());
244: }
245:
246: public static void shutdown() {
247: // we intentionally do not unregister listener here since it will be automatically
248: // unregistered as soon as shutdown (which is async.) finishes
249: RepositoryAccessor.getRepository().shutdown();
250: }
251:
252: public static void cleanCashes() {
253: RepositoryAccessor.getRepository().cleanCaches();
254: }
255:
256: public static void closeUnit(CsmUID uid, Set<String> requiredUnits,
257: boolean cleanRepository) {
258: closeUnit(UIDtoKey(uid), requiredUnits, cleanRepository);
259: }
260:
261: public static void closeUnit(String unitName,
262: Set<String> requiredUnits) {
263: closeUnit(unitName, requiredUnits,
264: !TraceFlags.PERSISTENT_REPOSITORY);
265: }
266:
267: public static void closeUnit(String unitName,
268: Set<String> requiredUnits, boolean cleanRepository) {
269: RepositoryListenerImpl.instance().onExplicitClose(unitName);
270: RepositoryAccessor.getRepository().closeUnit(unitName,
271: cleanRepository, requiredUnits);
272: }
273:
274: public static void closeUnit(Key key, Set<String> requiredUnits) {
275: closeUnit(key, requiredUnits, !TraceFlags.PERSISTENT_REPOSITORY);
276: }
277:
278: public static void closeUnit(Key key, Set<String> requiredUnits,
279: boolean cleanRepository) {
280: assert key != null;
281: RepositoryAccessor.getRepository().closeUnit(
282: key.getUnit().toString(), cleanRepository,
283: requiredUnits);
284: }
285:
286: public static void onProjectDeleted(NativeProject nativeProject) {
287: Key key = KeyUtilities.createProjectKey(nativeProject);
288: RepositoryAccessor.getRepository().removeUnit(
289: key.getUnit().toString());
290: }
291:
292: public static void openUnit(ProjectBase project) {
293: CsmUID uid = project.getUID();
294: assert uid != null;
295: Key key = UIDtoKey(uid);
296: openUnit(key);
297: }
298:
299: public static void openUnit(Key key) {
300: openUnit(key.getUnit().toString());
301: }
302:
303: private static void openUnit(String unitName) {
304: // TODO explicit open should be called here:
305: RepositoryListenerImpl.instance().onExplicitOpen(unitName);
306: RepositoryAccessor.getRepository().openUnit(unitName);
307: }
308:
309: public static void unregisterRepositoryListener(
310: RepositoryListener listener) {
311: RepositoryAccessor.getRepository()
312: .unregisterRepositoryListener(listener);
313: }
314:
315: static int getUnitId(String unitName) {
316: return RepositoryAccessor.getTranslator().getUnitId(unitName);
317: }
318:
319: static String getUnitName(int unitIndex) {
320: return RepositoryAccessor.getTranslator()
321: .getUnitName(unitIndex);
322: }
323:
324: static int getFileIdByName(int unitId, String fileName) {
325: return RepositoryAccessor.getTranslator().getFileIdByName(
326: unitId, fileName);
327: }
328:
329: static String getFileNameByIdSafe(int unitId, int fileId) {
330: return RepositoryAccessor.getTranslator().getFileNameByIdSafe(
331: unitId, fileId);
332: }
333:
334: static String getFileNameById(int unitId, int fileId) {
335: return RepositoryAccessor.getTranslator().getFileNameById(
336: unitId, fileId);
337: }
338:
339: }
|