1 19 20 package com.sslexplorer.tunnels; 21 22 import java.io.File ; 23 import java.io.Serializable ; 24 import java.sql.ResultSet ; 25 import java.sql.Timestamp ; 26 import java.util.ArrayList ; 27 import java.util.Calendar ; 28 import java.util.List ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 import com.sslexplorer.boot.ContextHolder; 34 import com.sslexplorer.boot.HostService; 35 import com.sslexplorer.core.CoreEvent; 36 import com.sslexplorer.core.CoreListener; 37 import com.sslexplorer.core.CoreServlet; 38 import com.sslexplorer.extensions.store.ExtensionStore; 39 import com.sslexplorer.extensions.types.PluginDefinition; 40 import com.sslexplorer.jdbc.DBUpgrader; 41 import com.sslexplorer.jdbc.JDBCDatabaseEngine; 42 import com.sslexplorer.jdbc.JDBCPreparedStatement; 43 44 51 public class JDBCTunnelDatabase implements TunnelDatabase, CoreListener { 52 private static final Log log = LogFactory.getLog(JDBCTunnelDatabase.class); 53 54 private JDBCDatabaseEngine db; 55 56 59 public JDBCTunnelDatabase() { 60 } 61 62 67 public void open(CoreServlet controllingServlet) throws Exception { 68 throw new Exception ("Plugin databases need a PluginDefinition."); 69 } 70 71 76 public void close() throws Exception { 77 } 78 79 84 public void cleanup() throws Exception { 85 } 86 87 92 public void coreEvent(CoreEvent evt) { 93 } 108 109 117 public void updateTunnel(int id, String name, String description, int type, boolean autoStart, String transport, 118 String username, int sourcePort, HostService destination, String sourceInterface) throws Exception { 119 120 JDBCPreparedStatement ps2 = db.getStatement("updateTunnel.update"); 121 try { 122 ps2.setString(1, name); 123 ps2.setString(2, description); 124 ps2.setInt(3, type); 125 ps2.setInt(4, autoStart ? 1 : 0); 126 ps2.setString(5, transport); 127 ps2.setString(6, username == null ? "" : username); 128 ps2.setInt(7, sourcePort); 129 ps2.setInt(8, destination.getPort()); 130 ps2.setString(9, destination.getHost()); 131 ps2.setString(10, sourceInterface); 132 Calendar now = Calendar.getInstance(); 133 ps2.setString(11, db.formatTimestamp(now)); 134 ps2.setInt(12, id); 135 ps2.execute(); 136 } finally { 137 ps2.releasePreparedStatement(); 138 } 139 } 140 141 144 public Tunnel createTunnel(int realmID, String name, String description, int type, boolean autoStart, String transport, String username, 145 int sourcePort, HostService destination, String sourceInterface) throws Exception { 146 147 JDBCPreparedStatement ps2 = db.getStatement("createTunnel.insert"); 148 try { 149 ps2.setString(1, name); 150 ps2.setString(2, description); 151 ps2.setInt(3, type); 152 ps2.setInt(4, autoStart ? 1 : 0); 153 ps2.setString(5, transport); 154 ps2.setString(6, username == null ? "" : username); 155 ps2.setInt(7, sourcePort); 156 ps2.setInt(8, destination.getPort()); 157 ps2.setString(9, destination.getHost()); 158 ps2.setString(10, sourceInterface); 159 Calendar now = Calendar.getInstance(); 160 ps2.setString(11, db.formatTimestamp(now)); 161 ps2.setString(12, db.formatTimestamp(now)); 162 ps2.setInt(13, realmID); 163 ps2.execute(); 164 return new DefaultTunnel(realmID, name, description, db.getLastInsertId(ps2, "createTunnel.lastInsertId"), type, autoStart, 165 transport, username, sourcePort, destination, sourceInterface, now, now); 166 167 } finally { 168 ps2.releasePreparedStatement(); 169 } 170 } 171 172 177 public Tunnel removeTunnel(int id) throws Exception { 178 Tunnel t = getTunnel(id); 179 if (t == null) { 180 throw new Exception ("No tunnel with " + id); 181 } 182 JDBCPreparedStatement ps = db.getStatement("removeTunnel.deleteFavorite"); 183 try { 184 ps.setInt(1, TunnelPlugin.SSL_TUNNEL_RESOURCE_TYPE_ID); 185 ps.setString(2, String.valueOf(id)); 186 ps.execute(); 187 } finally { 188 ps.releasePreparedStatement(); 189 } 190 ps = db.getStatement("removeTunnel.deleteTunnel"); 191 try { 192 ps.setInt(1, id); 193 ps.execute(); 194 } finally { 195 ps.releasePreparedStatement(); 196 } 197 return t; 198 } 199 200 205 public List <Tunnel> getTunnels() throws Exception { 206 JDBCPreparedStatement ps = db.getStatement("getTunnels.select"); 207 List <Tunnel> v = new ArrayList <Tunnel>(); 208 try { 209 ResultSet rs = ps.executeQuery(); 210 try { 211 while (rs.next()) { 212 v.add(buildTunnel(rs)); 213 } 214 } finally { 215 rs.close(); 216 } 217 } finally { 218 ps.releasePreparedStatement(); 219 } 220 return v; 221 } 222 223 228 public Tunnel getTunnel(int id) throws Exception { 229 JDBCPreparedStatement ps = db.getStatement("getTunnel.select.id"); 230 ps.setInt(1, id); 231 try { 232 ResultSet rs = ps.executeQuery(); 233 try { 234 if (rs.next()) { 235 return buildTunnel(rs); 236 } 237 } finally { 238 rs.close(); 239 } 240 } finally { 241 ps.releasePreparedStatement(); 242 } 243 return null; 244 } 245 246 Tunnel buildTunnel(ResultSet rs) throws Exception { 247 Timestamp cd = rs.getTimestamp("date_created"); 248 Calendar c = Calendar.getInstance(); 249 c.setTimeInMillis(cd == null ? System.currentTimeMillis() : cd.getTime()); 250 Timestamp ad = rs.getTimestamp("date_amended"); 251 Calendar a = Calendar.getInstance(); 252 a.setTimeInMillis(ad == null ? System.currentTimeMillis() : ad.getTime()); 253 return new DefaultTunnel(rs.getInt("realm_id"), rs.getString("name"), rs.getString("description"), rs.getInt("tunnel_id"), rs.getInt("type"), rs 254 .getBoolean("auto_start"), rs.getString("transport"), rs.getString("username"), rs.getInt("source_port"), 255 new HostService(rs.getString("destination_host"), rs.getInt("destination_port")), rs 256 .getString("source_interface"), c, a); 257 } 258 259 263 class CacheList extends ArrayList implements Serializable { 264 private static final long serialVersionUID = 6613983448357872637L; 265 } 266 267 public void open(CoreServlet controllingServlet, PluginDefinition def) throws Exception { 268 String dbName = System.getProperty("sslexplorer.systemDatabase.jdbc.dbName", "explorer_configuration"); 269 controllingServlet.addDatabase(dbName, ContextHolder.getContext().getDBDirectory()); 270 String jdbcUser = System.getProperty("sslexplorer.jdbc.username", "sa"); 271 String jdbcPassword = System.getProperty("sslexplorer.jdbc.password", ""); 272 String vendorDB = System.getProperty("sslexplorer.jdbc.vendorClass", "com.sslexplorer.jdbc.hsqldb.HSQLDBDatabaseEngine"); 273 274 if (log.isInfoEnabled()) { 275 log.info("System database is being opened..."); 276 log.info("JDBC vendor class implementation is " + vendorDB); 277 } 278 279 db = (JDBCDatabaseEngine) Class.forName(vendorDB).newInstance(); 280 db.init("tunnelsDatabase", dbName, jdbcUser, jdbcPassword, null); 281 282 File upgradeDir = new File (def.getDescriptor().getApplicationBundle().getBaseDir(), "upgrade"); 283 DBUpgrader upgrader = new DBUpgrader(ExtensionStore.getInstance() 284 .getExtensionBundle(TunnelPlugin.BUNDLE_ID) 285 .getVersion(), db, ContextHolder.getContext() 286 .getDBDirectory(), upgradeDir); 287 upgrader.upgrade(); 288 289 CoreServlet.getServlet().addCoreListener(this); 290 } 291 292 295 public List <Tunnel> getTunnels(int realmID) throws Exception { 296 JDBCPreparedStatement ps = db.getStatement("getTunnels.realm.select"); 297 List <Tunnel> v = new ArrayList <Tunnel>(); 298 try { 299 ps.setInt(1, realmID); 300 ResultSet rs = ps.executeQuery(); 301 try { 302 while (rs.next()) { 303 v.add(buildTunnel(rs)); 304 } 305 } finally { 306 rs.close(); 307 } 308 } finally { 309 ps.releasePreparedStatement(); 310 } 311 return v; 312 } 313 314 317 public Tunnel getTunnel(String name, int realmID) throws Exception { 318 JDBCPreparedStatement ps = db.getStatement("getTunnel.select.name"); 319 ps.setString(1, name); 320 ps.setInt(2, realmID); 321 try { 322 ResultSet rs = ps.executeQuery(); 323 try { 324 if (rs.next()) { 325 return buildTunnel(rs); 326 } 327 } finally { 328 rs.close(); 329 } 330 } finally { 331 ps.releasePreparedStatement(); 332 } 333 return null; 334 } 335 } 336 | Popular Tags |