KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > JDBCApplicationShortcutDatabase


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.applications;
21
22 import java.io.File JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.Timestamp JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import com.sslexplorer.boot.ContextHolder;
36 import com.sslexplorer.core.CoreEvent;
37 import com.sslexplorer.core.CoreListener;
38 import com.sslexplorer.core.CoreServlet;
39 import com.sslexplorer.extensions.store.ExtensionStore;
40 import com.sslexplorer.extensions.types.PluginDefinition;
41 import com.sslexplorer.jdbc.DBUpgrader;
42 import com.sslexplorer.jdbc.JDBCDatabaseEngine;
43 import com.sslexplorer.jdbc.JDBCPreparedStatement;
44
45 /**
46  * Implementation of a {@link com.sslexplorer.security.SystemDatabase} that uses
47  * a JDBC compliant database to store SSL-Explorer's basic configuration and
48  * resources.
49  *
50  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
51  */

52 public class JDBCApplicationShortcutDatabase implements ApplicationShortcutDatabase, CoreListener {
53     private static final Log log = LogFactory.getLog(JDBCApplicationShortcutDatabase.class);
54
55     private JDBCDatabaseEngine db;
56
57     /**
58      * Constructor
59      */

60     public JDBCApplicationShortcutDatabase() {
61     }
62
63     /*
64      * (non-Javadoc)
65      *
66      * @see com.sslexplorer.core.Database#open(com.sslexplorer.core.CoreServlet)
67      */

68     public void open(CoreServlet controllingServlet) throws Exception JavaDoc {
69         throw new Exception JavaDoc("Plugin databases need a PluginDefinition.");
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see com.sslexplorer.boot.Database#close()
76      */

77     public void close() throws Exception JavaDoc {
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see com.sslexplorer.boot.Database#cleanup()
84      */

85     public void cleanup() throws Exception JavaDoc {
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see com.sslexplorer.core.CoreListener#coreEvent(com.sslexplorer.core.CoreEvent)
92      */

93     public void coreEvent(CoreEvent evt) {
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see com.sslexplorer.extensions.ApplicationShortcutDatabase#createApplicationShortcut(java.lang.String,
100      * java.lang.String, java.lang.String, java.util.Map)
101      */

102     public int createApplicationShortcut(String JavaDoc application, String JavaDoc name, String JavaDoc description, Map JavaDoc settings, int realmID) throws Exception JavaDoc {
103         JDBCPreparedStatement ps = db.getStatement("createApplicationShortcut.insert");
104         try {
105             ps.setString(1, application);
106             ps.setString(2, name);
107             ps.setString(3, description);
108             Calendar JavaDoc now = Calendar.getInstance();
109             ps.setString(4, db.formatTimestamp(now));
110             ps.setString(5, db.formatTimestamp(now));
111             ps.setInt(6, realmID);
112             ps.execute();
113             int id = db.getLastInsertId(ps, "createApplicationShortcut.lastInsertId");
114             JDBCPreparedStatement ps3 = db.getStatement("createApplicationShortcut.insertParameters");
115             try {
116                 for (Iterator JavaDoc it = settings.keySet().iterator(); it.hasNext();) {
117                     String JavaDoc parameter = (String JavaDoc) it.next();
118                     String JavaDoc value = (String JavaDoc) settings.get(parameter);
119                     ps3.setInt(1, id);
120                     ps3.setString(2, parameter);
121                     ps3.setString(3, value);
122                     ps3.execute();
123                     ps3.reset();
124                 }
125             } finally {
126                 ps3.releasePreparedStatement();
127             }
128             return id;
129         } finally {
130             ps.releasePreparedStatement();
131         }
132     }
133
134     /*
135      * (non-Javadoc)
136      *
137      * @see com.sslexplorer.extensions.ApplicationShortcutDatabase#updateApplicationShortcut(int,
138      * java.lang.String, java.lang.String, java.util.Map)
139      */

140     public void updateApplicationShortcut(int id, String JavaDoc name, String JavaDoc description, Map JavaDoc settings) throws Exception JavaDoc {
141
142         JDBCPreparedStatement ps = db.getStatement("updateApplicationShortcut.update");
143
144         // Update the actual shortcut
145
try {
146             ps.setString(1, name);
147             ps.setString(2, description);
148             Calendar JavaDoc now = Calendar.getInstance();
149             ps.setString(3, db.formatTimestamp(now));
150             ps.setInt(4, id);
151             ps.execute();
152         } finally {
153             ps.releasePreparedStatement();
154         }
155
156         // Delete the current parameters
157
ps = db.getStatement("updateApplicationShortcut.deleteParameters");
158         try {
159             ps.setInt(1, id);
160             ps.execute();
161         } finally {
162             ps.releasePreparedStatement();
163         }
164
165         // Insert the parameters again
166
ps = db.getStatement("createApplicationShortcut.insertParameters");
167         try {
168             for (Iterator JavaDoc it = settings.keySet().iterator(); it.hasNext();) {
169                 String JavaDoc parameter = (String JavaDoc) it.next();
170                 String JavaDoc value = (String JavaDoc) settings.get(parameter);
171                 ps.setInt(1, id);
172                 ps.setString(2, parameter);
173                 ps.setString(3, value);
174                 ps.execute();
175                 ps.reset();
176
177             }
178         } finally {
179             ps.releasePreparedStatement();
180         }
181
182     }
183
184     /*
185      * (non-Javadoc)
186      *
187      * @see com.sslexplorer.extensions.ApplicationShortcutDatabase#removeApplicationShortcuts(java.lang.String)
188      */

189     public void removeApplicationShortcuts(String JavaDoc applicationId) throws Exception JavaDoc {
190         JDBCPreparedStatement ps = db.getStatement("removeApplicationShortcuts.select");
191         ps.setString(1, applicationId);
192         try {
193             ResultSet JavaDoc rs = ps.executeQuery();
194             try {
195                 while (rs.next()) {
196                     JDBCPreparedStatement ps2 = db.getStatement("removeApplicationShortcuts.delete.favorites");
197                     ps2.setInt(1, ApplicationsPlugin.APPLICATION_SHORTCUT_RESOURCE_TYPE_ID);
198                     ps2.setInt(2, rs.getInt("resource_id"));
199                     try {
200                         ps2.execute();
201                     } finally {
202                         ps2.releasePreparedStatement();
203                     }
204                     ps2 = db.getStatement("removeApplicationShortcuts.delete.shortcutParameters");
205                     ps2.setString(1, String.valueOf(rs.getInt("resource_id")));
206                     try {
207                         ps2.execute();
208                     } finally {
209                         ps2.releasePreparedStatement();
210                     }
211                 }
212             } finally {
213                 rs.close();
214             }
215         } finally {
216             ps.releasePreparedStatement();
217         }
218         ps = db.getStatement("removeApplicationShortcuts.delete.shortcuts");
219         ps.setString(1, applicationId);
220         try {
221             ps.execute();
222         } finally {
223             ps.releasePreparedStatement();
224         }
225
226     }
227
228     /*
229      * (non-Javadoc)
230      *
231      * @see com.sslexplorer.extensions.ApplicationShortcutDatabase#getShortcuts()
232      */

233     public List JavaDoc<ApplicationShortcut> getShortcuts() throws Exception JavaDoc {
234
235         JDBCPreparedStatement ps = db.getStatement("getShortcuts.selectAll");
236         Vector JavaDoc<ApplicationShortcut> v = new Vector JavaDoc<ApplicationShortcut>();
237         try {
238             ResultSet JavaDoc rs = ps.executeQuery();
239             try {
240                 while (rs.next()) {
241                     v.add(buildShortcut(rs));
242                 }
243             } finally {
244                 rs.close();
245             }
246         } finally {
247             ps.releasePreparedStatement();
248         }
249
250         return v;
251     }
252
253     /*
254      * (non-Javadoc)
255      *
256      * @see com.sslexplorer.extensions.ApplicationShortcutDatabase#getShortcut(int)
257      */

258     public ApplicationShortcut getShortcut(int shortcutId) throws Exception JavaDoc {
259         JDBCPreparedStatement ps = db.getStatement("getShortcut.select");
260         ps.setInt(1, shortcutId);
261         try {
262             ResultSet JavaDoc rs = ps.executeQuery();
263             try {
264                 if (rs.next()) {
265                     return buildShortcut(rs);
266                 } else {
267                     return null;
268                 }
269             } finally {
270                 rs.close();
271             }
272         } finally {
273             ps.releasePreparedStatement();
274         }
275     }
276
277     /*
278      * (non-Javadoc)
279      *
280      * @see com.sslexplorer.extensions.ApplicationShortcutDatabase#deleteShortcut(int)
281      */

282     public ApplicationShortcut deleteShortcut(int shortcutId) throws Exception JavaDoc {
283
284         ApplicationShortcut sc = getShortcut(shortcutId);
285         if (sc == null) {
286             throw new Exception JavaDoc("Application shortcut " + shortcutId + " does not exist.");
287         }
288
289         JDBCPreparedStatement ps = db.getStatement("deleteShortcuts.delete.favorite");
290
291         // Delete a favorite
292
try {
293             ps.setString(1, String.valueOf(shortcutId));
294             ps.setInt(2, ApplicationsPlugin.APPLICATION_SHORTCUT_RESOURCE_TYPE_ID);
295             ps.execute();
296             ps.reset();
297         } finally {
298             ps.releasePreparedStatement();
299         }
300
301         // Delete a shortcut
302
ps = db.getStatement("deleteShortcuts.delete.shortcut");
303         try {
304             ps.setInt(1, shortcutId);
305             ps.execute();
306             ps.reset();
307         } finally {
308             ps.releasePreparedStatement();
309         }
310
311         // Delete all parameters for a shortcut
312
ps = db.getStatement("deleteShortcuts.delete.shortcutParameters");
313         try {
314             ps.setInt(1, shortcutId);
315             ps.execute();
316             ps.reset();
317         } finally {
318             ps.releasePreparedStatement();
319         }
320
321         return sc;
322     }
323
324     /**
325      * @param rs
326      * @return ApplicationShortcut
327      * @throws Exception
328      */

329     ApplicationShortcut buildShortcut(ResultSet JavaDoc rs) throws Exception JavaDoc {
330         Timestamp JavaDoc cd = rs.getTimestamp("date_created");
331         Calendar JavaDoc c = Calendar.getInstance();
332         c.setTimeInMillis(cd == null ? System.currentTimeMillis() : cd.getTime());
333         Timestamp JavaDoc ad = rs.getTimestamp("date_amended");
334         Calendar JavaDoc a = Calendar.getInstance();
335         a.setTimeInMillis(ad == null ? System.currentTimeMillis() : ad.getTime());
336         int id = rs.getInt("resource_id");
337         int realmID = rs.getInt("realm_id");
338         String JavaDoc name = rs.getString("name");
339         String JavaDoc description = rs.getString("description");
340         String JavaDoc application = rs.getString("application");
341         JDBCPreparedStatement ps2 = db.getStatement("buildShortcut.select.parameters");
342         ps2.setInt(1, id);
343         HashMap JavaDoc<String JavaDoc,String JavaDoc> settings = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
344         try {
345             ResultSet JavaDoc rs2 = ps2.executeQuery();
346             try {
347                 while (rs2.next()) {
348                     settings.put(rs2.getString("parameter"), rs2.getString("value"));
349                 }
350
351             } finally {
352                 rs2.close();
353             }
354         } finally {
355             ps2.releasePreparedStatement();
356         }
357         return new DefaultApplicationShortcut(realmID, id, name, description, c, a, application, settings);
358     }
359
360     /* (non-Javadoc)
361      * @see com.sslexplorer.plugin.PluginDatabase#open(com.sslexplorer.core.CoreServlet, com.sslexplorer.plugin.PluginDefinition)
362      */

363     public void open(CoreServlet controllingServlet, PluginDefinition def) throws Exception JavaDoc {
364         String JavaDoc dbName = System.getProperty("sslexplorer.applicationShortcutsDatabase.jdbc.dbName", "explorer_configuration");
365         controllingServlet.addDatabase(dbName, ContextHolder.getContext().getDBDirectory());
366         String JavaDoc jdbcUser = System.getProperty("sslexplorer.jdbc.username", "sa");
367         String JavaDoc jdbcPassword = System.getProperty("sslexplorer.jdbc.password", "");
368         String JavaDoc vendorDB = System.getProperty("sslexplorer.jdbc.vendorClass", "com.sslexplorer.jdbc.hsqldb.HSQLDBDatabaseEngine");
369
370         if (log.isInfoEnabled()) {
371             log.info("System database is being opened...");
372             log.info("JDBC vendor class implementation is " + vendorDB);
373         }
374
375         db = (JDBCDatabaseEngine) Class.forName(vendorDB).newInstance();
376         db.init("applicationShortcutsDatabase", dbName, jdbcUser, jdbcPassword, null);
377
378         File JavaDoc upgradeDir = new File JavaDoc(def.getDescriptor().getApplicationBundle().getBaseDir(), "upgrade");
379         DBUpgrader upgrader = new DBUpgrader(ExtensionStore.getInstance()
380             .getExtensionBundle(ApplicationsPlugin.BUNDLE_ID)
381             .getVersion(), db, ContextHolder.getContext()
382                         .getDBDirectory(), upgradeDir);
383         upgrader.upgrade();
384
385         CoreServlet.getServlet().addCoreListener(this);
386     }
387
388     /* (non-Javadoc)
389      * @see com.sslexplorer.applications.ApplicationShortcutDatabase#getShortcuts(int)
390      */

391     public List JavaDoc<ApplicationShortcut> getShortcuts(int realmID) throws Exception JavaDoc {
392
393         JDBCPreparedStatement ps = db.getStatement("getShortcuts.realm.selectAll");
394         Vector JavaDoc<ApplicationShortcut> v = new Vector JavaDoc<ApplicationShortcut>();
395         try {
396             ps.setInt(1, realmID);
397             ResultSet JavaDoc rs = ps.executeQuery();
398             try {
399                 while (rs.next()) {
400                     v.add(buildShortcut(rs));
401                 }
402             } finally {
403                 rs.close();
404             }
405         } finally {
406             ps.releasePreparedStatement();
407         }
408
409         return v;
410     }
411
412     /* (non-Javadoc)
413      * @see com.sslexplorer.applications.ApplicationShortcutDatabase#getShortcut(java.lang.String, int)
414      */

415     public ApplicationShortcut getShortcut(String JavaDoc name, int realmID) throws Exception JavaDoc {
416         JDBCPreparedStatement ps = db.getStatement("getShortcutByName.select");
417         ps.setString(1, name);
418         ps.setInt(2, realmID);
419         try {
420             ResultSet JavaDoc rs = ps.executeQuery();
421             try {
422                 if (rs.next()) {
423                     return buildShortcut(rs);
424                 } else {
425                     return null;
426                 }
427             } finally {
428                 rs.close();
429             }
430         } finally {
431             ps.releasePreparedStatement();
432         }
433     }
434 }
435
Popular Tags