001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.jumpimpl.module.preferences;
026:
027: import com.sun.jump.module.preferences.JUMPPreferencesModule;
028: import com.sun.jump.module.contentstore.JUMPStoreHandle;
029:
030: public class SystemPreferencesModuleImpl extends PreferencesStore
031: implements JUMPPreferencesModule {
032:
033: /**
034: * Name of directory where preferences will be stored
035: */
036: private static String REPOSITORY_SYSTEM_PREFERENCES_DIRNAME = "./preferences/System";
037:
038: /*
039: * Name of the file that will store the preferences
040: */
041: private static String SYSTEM_PREFERENCES_FILE_NAME = "./preferences/System/systemPreferences.props";
042:
043: /*
044: * Name of the file that will store default preferences
045: */
046: private static String DEFAULT_SYSTEM_PREFERENCES_FILE_NAME = "./preferences/System/defaultSystemPreferences.props";
047:
048: //
049: // A single instance of system-wide preferences
050: // and the defaults
051: //
052: private PersistentPreferences systemPreferences;
053: private PersistentPreferences defaultSystemPreferences;
054: private JUMPStoreHandle storeHandle = null;
055:
056: public SystemPreferencesModuleImpl() {
057:
058: // Get a handle to content store
059: storeHandle = openStore(true);
060:
061: try {
062: // create preferences root dir if it doesn't already exist.
063: if (storeHandle
064: .getNode(REPOSITORY_PREFERENCES_ROOT_DIRNAME) == null) {
065: storeHandle
066: .createNode(REPOSITORY_PREFERENCES_ROOT_DIRNAME);
067: }
068:
069: if (storeHandle
070: .getNode(REPOSITORY_SYSTEM_PREFERENCES_DIRNAME) == null) {
071: storeHandle
072: .createNode(REPOSITORY_SYSTEM_PREFERENCES_DIRNAME);
073: }
074:
075: // Default SystemPreferences
076: defaultSystemPreferences = new PersistentPreferences(
077: DEFAULT_SYSTEM_PREFERENCES_FILE_NAME, storeHandle);
078:
079: // SystemPreferences
080: systemPreferences = new PersistentPreferences(
081: defaultSystemPreferences,
082: SYSTEM_PREFERENCES_FILE_NAME, storeHandle);
083:
084: } catch (Exception ex) {
085: ex.printStackTrace();
086: }
087:
088: closeStore(storeHandle);
089: }
090:
091: /**
092: * @param prefName the name of the preference to look up
093: * @return value of the preference
094: */
095: public String getPreference(String prefName) {
096: return systemPreferences.get(prefName);
097: }
098:
099: /**
100: * @param prefName the name of the preference to set
101: * @param prefValue the value to set the preference to
102: * @return value of the preference
103: */
104: public String setPreference(String prefName, String prefValue) {
105: systemPreferences.set(prefName, prefValue);
106: return prefValue;
107: }
108:
109: /**
110: * @param prefName the name of the preference to delete
111: */
112: public void deletePreference(String prefName) {
113: systemPreferences.delete(prefName);
114: }
115:
116: /**
117: * @return a list of names of preferences.
118: */
119: public String[] getPreferenceNames() {
120: return systemPreferences.getNames();
121: }
122:
123: /**
124: * Force persistence of the namespace.
125: */
126: public void save() {
127: storeHandle = openStore(true);
128: systemPreferences.save(storeHandle);
129: closeStore(storeHandle);
130: }
131:
132: }
|