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-2006 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.openide.util;
043:
044: import java.io.ByteArrayOutputStream;
045: import java.io.IOException;
046: import java.io.PrintStream;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import java.util.prefs.Preferences;
050: import org.netbeans.modules.openide.util.PreferencesProvider;
051:
052: /**
053: * Provides an implementation of the Preferences API which may be backed by
054: * a NetBeans-specific implementation.
055: * @see <a href="doc-files/preferences.html">Preferences API in NetBeans</a>
056: * @since org.openide.util 7.4
057: * @author Radek Matous
058: */
059: public final class NbPreferences {
060: private static PreferencesProvider PREFS_IMPL;
061:
062: private NbPreferences() {
063: }
064:
065: /**
066: * Returns user preference node . {@link Preferences#absolutePath} of such
067: * a node depends whether class provided as a parameter was loaded as a part of any module
068: * or not. If so, then absolute path corresponds to slashified code name base of module.
069: * If not, then absolute path corresponds to class's package.
070: *
071: * @param cls the class for which a user preference node is desired.
072: * @return the user preference node
073: */
074: public static Preferences forModule(Class cls) {
075: if (PREFS_IMPL == null) {
076: PREFS_IMPL = getPreferencesProvider();
077: }
078: return PREFS_IMPL.preferencesForModule(cls);
079: }
080:
081: /**
082: * Returns the root preference node.
083: *
084: * @return the root preference node.
085: */
086: public static Preferences root() {
087: if (PREFS_IMPL == null) {
088: PREFS_IMPL = getPreferencesProvider();
089: }
090: return PREFS_IMPL.preferencesRoot();
091: }
092:
093: private static PreferencesProvider getPreferencesProvider() {
094: PreferencesProvider retval = Lookup.getDefault().lookup(
095: PreferencesProvider.class);
096: if (retval == null) {
097: retval = new PreferencesProvider() {
098: public Preferences preferencesForModule(Class cls) {
099: return Preferences.userNodeForPackage(cls);
100: }
101:
102: public Preferences preferencesRoot() {
103: return Preferences.userRoot();
104: }
105: };
106: // Avoided warning in case it is set
107: //(e.g. from NbTestCase - org.netbeans.junit.internal.MemoryPreferencesFactory).
108: String prefsFactory = System
109: .getProperty("java.util.prefs.PreferencesFactory");//NOI18N
110: if (!"org.netbeans.junit.internal.MemoryPreferencesFactory"
111: .equals(prefsFactory)) {//NOI18N
112: Logger logger = Logger.getLogger(NbPreferences.class
113: .getName());
114: ByteArrayOutputStream bos = new ByteArrayOutputStream();
115: new Exception().printStackTrace(new PrintStream(bos));
116: logger.log(prefsFactory == null ? Level.WARNING
117: : Level.FINE,
118: "NetBeans implementation of Preferences not found: "
119: + bos.toString());//NOI18N
120: }
121: }
122: return retval;
123: }
124: }
|