View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     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      
34      protected static PreferencesProvider prefsProvider;
35  
36      public PreferencesFactoryImpl()
37      {
38          super();        
39          System.setProperty("java.util.prefs.PreferencesFactory", getClass().getName());
40      }  
41  
42      /***
43       * @see java.util.prefs.PreferencesFactory#systemRoot()
44       */
45      public Preferences systemRoot()
46      {
47        return  PreferencesImpl.systemRoot;
48      }
49  
50      /***
51       * @see java.util.prefs.PreferencesFactory#userRoot()
52       */
53      public Preferences userRoot()
54      {
55          return  PreferencesImpl.userRoot;
56      }
57      
58      /***
59       * <p>
60       * Initializes the factory.
61       * </p>
62       * 
63       * @throws Exception
64       */
65      public void init() throws Exception
66      {        
67          try
68          {           
69                PreferencesImpl.setPreferencesProvider(prefsProvider);
70                PreferencesImpl.systemRoot = new PreferencesImpl(null, "", PreferencesImpl.SYSTEM_NODE_TYPE);
71                PreferencesImpl.userRoot =  new PreferencesImpl(null, "", PreferencesImpl.USER_NODE_TYPE);            
72          }
73          catch(Throwable e)
74          {
75  	    e.printStackTrace();
76              throw new PreferencesException("Failed to initialize prefs api.  "+e.toString());
77          }
78      }
79      
80      /***
81       * @return The {@link PreferencesProvider}
82       */
83      public PreferencesProvider getPrefsProvider()
84      {
85          return prefsProvider;
86      }
87      
88      /***
89       * <p>
90       * Set the preferences provider.
91       * </p>
92       * 
93       * @param prefsProvider The {@link PreferencesProvider}
94       */
95      public void setPrefsProvider(PreferencesProvider prefsProvider)
96      {
97          PreferencesFactoryImpl.prefsProvider = prefsProvider;
98      }
99  }