01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.jetspeed.prefs.impl;
18:
19: import java.util.prefs.Preferences;
20: import java.util.prefs.PreferencesFactory;
21:
22: import org.apache.jetspeed.prefs.PreferencesException;
23: import org.apache.jetspeed.prefs.PreferencesProvider;
24:
25: /**
26: * <p>{@link java.util.prefs.PreferencesFactory} implementation to
27: * return {@link PreferencesImpl}.</p>
28: *
29: * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
30: */
31: public class PreferencesFactoryImpl implements PreferencesFactory {
32:
33: protected static PreferencesProvider prefsProvider;
34:
35: public PreferencesFactoryImpl() {
36: super ();
37: System.setProperty("java.util.prefs.PreferencesFactory",
38: getClass().getName());
39: }
40:
41: /**
42: * @see java.util.prefs.PreferencesFactory#systemRoot()
43: */
44: public Preferences systemRoot() {
45: return PreferencesImpl.systemRoot;
46: }
47:
48: /**
49: * @see java.util.prefs.PreferencesFactory#userRoot()
50: */
51: public Preferences userRoot() {
52: return PreferencesImpl.userRoot;
53: }
54:
55: /**
56: * <p>
57: * Initializes the factory.
58: * </p>
59: *
60: * @throws Exception
61: */
62: public void init() throws Exception {
63: try {
64: PreferencesImpl.setPreferencesProvider(prefsProvider);
65: PreferencesImpl.systemRoot = new PreferencesImpl(null, "",
66: PreferencesImpl.SYSTEM_NODE_TYPE);
67: PreferencesImpl.userRoot = new PreferencesImpl(null, "",
68: PreferencesImpl.USER_NODE_TYPE);
69: } catch (Throwable e) {
70: e.printStackTrace();
71: throw new PreferencesException(
72: "Failed to initialize prefs api. " + e.toString());
73: }
74: }
75:
76: /**
77: * @return The {@link PreferencesProvider}
78: */
79: public PreferencesProvider getPrefsProvider() {
80: return prefsProvider;
81: }
82:
83: /**
84: * <p>
85: * Set the preferences provider.
86: * </p>
87: *
88: * @param prefsProvider The {@link PreferencesProvider}
89: */
90: public void setPrefsProvider(PreferencesProvider prefsProvider) {
91: PreferencesFactoryImpl.prefsProvider = prefsProvider;
92: }
93: }
|