View Javadoc

1   package com.explosion.expfmodules.rdbmsconn;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.util.HashMap;
23  import java.util.prefs.Preferences;
24  
25  import com.explosion.utilities.preferences.Preference;
26  import com.explosion.utilities.preferences.groups.PreferenceGroup;
27  import com.explosion.utilities.preferences.groups.PreferenceGroupManager;
28  
29  /***
30   * @author Stephen Cowx
31   * Date created:@25-Jan-2003
32   */
33  public class ConnectionDescriptorManager  extends PreferenceGroupManager
34  {
35    
36    private DriverDescriptorManager driverManager;
37    
38    /***
39     * Contructor for this.
40     * @param driverManager
41     * @param referenceToStore
42     * @throws Exception
43     */
44    public ConnectionDescriptorManager(DriverDescriptorManager driverManager, Object referenceToStore) throws Exception
45    {
46        super(referenceToStore);
47        this.driverManager = driverManager;
48        loadUsing(referenceToStore);        
49    }
50    
51    /***
52     * 
53     */
54    public void loadUsing(Object referenceToStore) throws Exception
55    {
56      //@TODO come up with some factory based method of deciding which 
57        //type of store is being used and use the correct load mechanism.  
58        //or just tidy this mess up.  28/03/05
59        if (getReferenceToStore() instanceof Preferences)
60      {
61        Preferences root = (Preferences) getReferenceToStore();
62        //.userRoot().node(uri + "/" + RdbmsConnConstants.CONNECTION_DESCRIPTORS);
63        String[] names = root.childrenNames();
64        
65        for (int i=0;i<names.length;i++)
66        {
67          /* Add the persistent object to the store */
68          this.addGroup(names[i], createGroup(names[i]));
69        }
70      }
71    }
72    
73    /***
74     * Return the correct node for the group with the given identifier
75     * @param identifier
76     * @return
77     */
78    public Object getReferenceForGroup(String identifier)
79    {
80        if (getReferenceToStore() instanceof Preferences)
81        {
82          Preferences pref = (Preferences) getReferenceToStore();
83          return pref.node(identifier);
84        }
85        else
86        {
87            return new HashMap();
88        }
89        
90    }
91    
92    /***
93     * This method creates a new ConnectionPersistentObject
94     */
95    public PreferenceGroup createGroup(String identifier) throws Exception
96    {
97      Object correctReferenceForThisGroup = getReferenceForGroup(identifier);
98        
99      /* Set up the preference objects */
100     Preference url = new Preference(RdbmsConnConstants.CD_URL, Preference.PROPERTY_TYPE_TEXT, "", correctReferenceForThisGroup);
101     ConnectionDescriptorPreference driver = new ConnectionDescriptorPreference(RdbmsConnConstants.CD_DRIVER, Preference.PROPERTY_TYPE_STRING_CHOICE, "", correctReferenceForThisGroup, this.driverManager);
102     Preference user = new Preference(RdbmsConnConstants.CD_USER, Preference.PROPERTY_TYPE_TEXT, "", correctReferenceForThisGroup);
103     Preference pass = new Preference(RdbmsConnConstants.CD_PASS, Preference.PROPERTY_TYPE_ENCRYPTED, "", correctReferenceForThisGroup);
104       
105     /* Set up their descriptions */
106     url.setLongName("Connect string");
107     url.setDescription("The connection url may be different for each driver. Essentially it provides information as to the location of the database.");
108     user.setLongName("Username");
109     user.setDescription("The username to be used when connecting to the database.");
110     pass.setLongName("Password");
111     pass.setDescription("The password to be used when connecting to the database.");
112     driver.setLongName("Driver");  
113     driver.setDescription("The driver refers to the JDBC driver to be used for this connection.");
114    
115     /* Set up the possible values for the driver */
116     driver.setChoiceValues(driverManager.getIdentifiers());
117     
118     /* Create a persistent object out of them */  
119     PreferenceGroup persistentDescriptor = new PreferenceGroup( correctReferenceForThisGroup, identifier);
120     persistentDescriptor.addPreference(RdbmsConnConstants.CD_DRIVER, driver);
121     persistentDescriptor.addPreference(RdbmsConnConstants.CD_URL, url);
122     persistentDescriptor.addPreference(RdbmsConnConstants.CD_USER, user);
123     persistentDescriptor.addPreference(RdbmsConnConstants.CD_PASS, pass);
124     
125     return persistentDescriptor;
126   }
127 }
128