View Javadoc

1   package com.explosion.expfmodules.dbstore;
2   /* =============================================================================
3    *       
4    *     Copyright 2004 Stephen Cowx
5    *
6    *     Licensed under the Apache License, Version 2.0 (the "License");
7    *     you may not use this file except in compliance with the License.
8    *     You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *     Unless required by applicable law or agreed to in writing, software
13   *     distributed under the License is distributed on an "AS IS" BASIS,
14   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *     See the License for the specific language governing permissions and
16   *     limitations under the License.
17   * 
18   * =============================================================================
19   */
20  
21  import java.io.File;
22  import java.sql.Connection;
23  import java.util.HashMap;
24  import java.util.Hashtable;
25  import java.util.Map;
26  import java.util.Properties;
27  import java.util.Vector;
28  import java.util.prefs.Preferences;
29  import javax.swing.JPanel;
30  import org.apache.log4j.LogManager;
31  import org.apache.log4j.Logger;
32  import com.explosion.expf.ExpActionListener;
33  import com.explosion.expf.ExpConstants;
34  import com.explosion.expf.ExpModuleManager;
35  import com.explosion.expf.preferences.SystemPreferences;
36  import com.explosion.expfmodules.rdbmsconn.ConnectionDescriptorManager;
37  import com.explosion.expfmodules.rdbmsconn.RdbmsConnConstants;
38  import com.explosion.expfmodules.rdbmsconn.RdbmsConnModuleManager;
39  import com.explosion.expfmodules.rdbmsconn.connect.ConnectionManager;
40  import com.explosion.utilities.exception.ExceptionManagerFactory;
41  import com.explosion.utilities.preferences.Preference;
42  import com.explosion.utilities.preferences.groups.PreferenceGroup;
43  /***
44   * Author: Stephen Cowx
45   * Date: Dec 9, 2002
46   * Time: 11:48:47 PM
47   * 
48   * This class provides the basis for an embedded database to be used 
49   * within an application, it is a pluggable component
50    */
51  public class DbStoreModuleManager implements ExpModuleManager
52  {
53    private static Logger log = LogManager.getLogger(DbStoreModuleManager.class);
54    
55    private Vector preferences = new Vector();
56    private Hashtable preferenceHash = new Hashtable();
57    private static DbStoreModuleManager instance = null;
58    private ExpActionListener globalActionListener = null;
59      
60    public DbStoreModuleManager()
61    {
62    	instance = this;
63    }
64    
65    public void initialiseGui() throws Exception
66    {
67      
68    }
69    
70    public String getVersion()
71    {
72      return "1.0";
73    }
74  
75    public String getName()
76    {
77      return "Internal persistent store";
78    }
79  
80    public String getDescription()
81    {
82      return "A persistent store for application data as well as an embedded database for use by application processes";
83    }
84  
85    public void applyPreferences()
86    {
87    }
88  
89    public void initialiseCore(Properties properties)
90    {
91      try
92      {
93        Preferences prefs = Preferences.userRoot().node(getPreferenceRoot());
94        
95        /* Find the best directory possible to place the internal store */
96        String fs = System.getProperty("file.separator");
97        String userHome = System.getProperty("user.home");
98        String appPrefix =  (String) SystemPreferences.getPreference(ExpConstants.EXPF_APP_PREFIX).getValue();
99        String dir = userHome + fs + "." + appPrefix.substring(1,appPrefix.length()) + fs + "store";
100       
101       File directory =  new File(dir);
102       if (!directory.exists())
103       {
104           if (! directory.mkdirs())
105           {
106              log.error("Unable to create directory for internal application store");
107              directory = new File(System.getProperty("java.io.tempDir"));
108           }
109       }
110       log.debug("Created directory "+directory.getAbsolutePath()+" for internal application store");
111       Preference internalStoreDirectory = new Preference(DbStoreConstants.INTERNAL_STORE_DIRECTORY, Preference.PROPERTY_TYPE_DIRECTORY, directory, prefs);
112       internalStoreDirectory.setLongName("Internal store directory");
113       internalStoreDirectory.setDescription("This is the directory in which the internal store database of the application is kept.");
114       internalStoreDirectory.setEditable(false);
115       internalStoreDirectory.setVisible(false);
116       
117       preferences.addElement(internalStoreDirectory);
118       preferenceHash.put(DbStoreConstants.INTERNAL_STORE_DIRECTORY, internalStoreDirectory);
119       
120       Preference created = new Preference(DbStoreConstants.INTERNAL_STORE_CREATED, Preference.PROPERTY_TYPE_BOOLEAN, new Boolean(false), prefs);
121       created.setLongName("Store created");
122       created.setDescription("Indicates whether the internal store has been created or not.");
123       preferences.addElement(created);
124       preferenceHash.put(DbStoreConstants.INTERNAL_STORE_CREATED, created);
125       
126       /* If the store has never been created */
127       if (!((Boolean)created.getValue()).booleanValue())
128       {
129           log.debug("Creating database tables");
130           Connection conn = getConnection();
131           ConstructTables ct = new ConstructTables(conn,created.getClass().getClassLoader().getResourceAsStream(DbStoreConstants.DBSTORE_TABLES_FILENAME));
132           ct.process();
133           conn.close();
134           log.debug("Done");
135           created.setValue(new Boolean(true));
136       }
137     }
138     catch (Exception e)
139     {
140       com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while initialising dbstore properties.");
141     }
142   }
143 
144   public Vector getPreferences()
145   {
146     return preferences;
147   }
148 
149   /***
150    * Returns a connectio to this database
151    * @return
152    */
153   public Connection getConnection() 
154   {
155         try
156         {
157             /* Get the values for the connection from the wizard values */
158             String user = "sa";
159             String pass = "";
160             
161             /* Use a manager to create a ConnectionDescriptor preference group */
162             Map store = new HashMap();
163             ConnectionDescriptorManager manager = new ConnectionDescriptorManager(RdbmsConnModuleManager.instance().getDriverDescriptorManager(), store);
164             PreferenceGroup descriptor = manager.createGroup(DbStoreConstants.DBSTORE_CONNECTION_NAME);
165             descriptor.setPreferenceValue(RdbmsConnConstants.CD_DRIVER,"HyperSonic SQL JDBC Driver (1.7.1)");
166             descriptor.setPreferenceValue(RdbmsConnConstants.CD_USER,user);
167             descriptor.setPreferenceValue(RdbmsConnConstants.CD_PASS,pass);
168              
169             /* This is a file based store */
170             File directory = (File) getPreference(DbStoreConstants.INTERNAL_STORE_DIRECTORY).getValue();
171             String location = directory.getAbsolutePath() + System.getProperty("file.separator") + "store";
172             descriptor.setPreferenceValue(RdbmsConnConstants.CD_URL,"jdbc:hsqldb:" + location);
173             
174             int connectionKey = ConnectionManager.getInstance().connect(descriptor, RdbmsConnModuleManager.instance().getDriverDescriptorManager());
175             
176             return ConnectionManager.getInstance().getConnection(connectionKey);
177         } 
178         catch (Exception e)
179         {
180            ExceptionManagerFactory.getExceptionManager().manageException(e,"Caught exception while connecting to internal db store");
181         }
182         return null;
183   }
184   
185 
186   public Preference getPreference(String preferenceName)
187   {
188   	return (Preference) preferenceHash.get(preferenceName);
189   }
190   
191   public JPanel getPreferencesEditor()
192   {
193     return null;
194   }
195   
196   public ExpActionListener getGlobalListener()
197   {
198   	return globalActionListener;
199   }
200   
201   public static DbStoreModuleManager instance()
202   {
203     return instance;
204   }
205  
206   public String getPreferenceRoot()
207   {
208     return (String) SystemPreferences.getPreference(ExpConstants.EXPF_APP_PREFIX).getValue()+"/dbstore";
209   }
210   
211 }