1 19 20 package com.sslexplorer.applications; 21 22 import java.io.File ; 23 import java.sql.ResultSet ; 24 import java.sql.Timestamp ; 25 import java.util.Calendar ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Vector ; 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 52 public class JDBCApplicationShortcutDatabase implements ApplicationShortcutDatabase, CoreListener { 53 private static final Log log = LogFactory.getLog(JDBCApplicationShortcutDatabase.class); 54 55 private JDBCDatabaseEngine db; 56 57 60 public JDBCApplicationShortcutDatabase() { 61 } 62 63 68 public void open(CoreServlet controllingServlet) throws Exception { 69 throw new Exception ("Plugin databases need a PluginDefinition."); 70 } 71 72 77 public void close() throws Exception { 78 } 79 80 85 public void cleanup() throws Exception { 86 } 87 88 93 public void coreEvent(CoreEvent evt) { 94 } 95 96 102 public int createApplicationShortcut(String application, String name, String description, Map settings, int realmID) throws Exception { 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 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 it = settings.keySet().iterator(); it.hasNext();) { 117 String parameter = (String ) it.next(); 118 String value = (String ) 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 140 public void updateApplicationShortcut(int id, String name, String description, Map settings) throws Exception { 141 142 JDBCPreparedStatement ps = db.getStatement("updateApplicationShortcut.update"); 143 144 try { 146 ps.setString(1, name); 147 ps.setString(2, description); 148 Calendar 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 ps = db.getStatement("updateApplicationShortcut.deleteParameters"); 158 try { 159 ps.setInt(1, id); 160 ps.execute(); 161 } finally { 162 ps.releasePreparedStatement(); 163 } 164 165 ps = db.getStatement("createApplicationShortcut.insertParameters"); 167 try { 168 for (Iterator it = settings.keySet().iterator(); it.hasNext();) { 169 String parameter = (String ) it.next(); 170 String value = (String ) 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 189 public void removeApplicationShortcuts(String applicationId) throws Exception { 190 JDBCPreparedStatement ps = db.getStatement("removeApplicationShortcuts.select"); 191 ps.setString(1, applicationId); 192 try { 193 ResultSet 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 233 public List <ApplicationShortcut> getShortcuts() throws Exception { 234 235 JDBCPreparedStatement ps = db.getStatement("getShortcuts.selectAll"); 236 Vector <ApplicationShortcut> v = new Vector <ApplicationShortcut>(); 237 try { 238 ResultSet 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 258 public ApplicationShortcut getShortcut(int shortcutId) throws Exception { 259 JDBCPreparedStatement ps = db.getStatement("getShortcut.select"); 260 ps.setInt(1, shortcutId); 261 try { 262 ResultSet 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 282 public ApplicationShortcut deleteShortcut(int shortcutId) throws Exception { 283 284 ApplicationShortcut sc = getShortcut(shortcutId); 285 if (sc == null) { 286 throw new Exception ("Application shortcut " + shortcutId + " does not exist."); 287 } 288 289 JDBCPreparedStatement ps = db.getStatement("deleteShortcuts.delete.favorite"); 290 291 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 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 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 329 ApplicationShortcut buildShortcut(ResultSet rs) throws Exception { 330 Timestamp cd = rs.getTimestamp("date_created"); 331 Calendar c = Calendar.getInstance(); 332 c.setTimeInMillis(cd == null ? System.currentTimeMillis() : cd.getTime()); 333 Timestamp ad = rs.getTimestamp("date_amended"); 334 Calendar 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 name = rs.getString("name"); 339 String description = rs.getString("description"); 340 String application = rs.getString("application"); 341 JDBCPreparedStatement ps2 = db.getStatement("buildShortcut.select.parameters"); 342 ps2.setInt(1, id); 343 HashMap <String ,String > settings = new HashMap <String ,String >(); 344 try { 345 ResultSet 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 363 public void open(CoreServlet controllingServlet, PluginDefinition def) throws Exception { 364 String dbName = System.getProperty("sslexplorer.applicationShortcutsDatabase.jdbc.dbName", "explorer_configuration"); 365 controllingServlet.addDatabase(dbName, ContextHolder.getContext().getDBDirectory()); 366 String jdbcUser = System.getProperty("sslexplorer.jdbc.username", "sa"); 367 String jdbcPassword = System.getProperty("sslexplorer.jdbc.password", ""); 368 String 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 upgradeDir = new File (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 391 public List <ApplicationShortcut> getShortcuts(int realmID) throws Exception { 392 393 JDBCPreparedStatement ps = db.getStatement("getShortcuts.realm.selectAll"); 394 Vector <ApplicationShortcut> v = new Vector <ApplicationShortcut>(); 395 try { 396 ps.setInt(1, realmID); 397 ResultSet 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 415 public ApplicationShortcut getShortcut(String name, int realmID) throws Exception { 416 JDBCPreparedStatement ps = db.getStatement("getShortcutByName.select"); 417 ps.setString(1, name); 418 ps.setInt(2, realmID); 419 try { 420 ResultSet 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 |