1 22 package fr.dyade.aaa.util; 23 24 import java.io.File ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.IOException ; 28 import java.io.FileNotFoundException ; 29 30 import java.util.Vector ; 31 import java.util.Properties ; 32 33 import java.sql.Connection ; 34 import java.sql.DriverManager ; 35 import java.sql.Statement ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 40 final class DBRepository implements Repository { 41 String driver = "org.apache.derby.jdbc.EmbeddedDriver"; 42 String connurl = "jdbc:derby:"; 43 46 File dir = null; 47 48 private int nbsaved = 0; 49 50 55 public int getNbSavedObjects() { 56 return nbsaved; 57 } 58 59 private int nbdeleted = 0; 60 61 66 public int getNbDeletedObjects() { 67 return nbdeleted; 68 } 69 70 private int baddeleted = 0; 71 72 77 public int getNbBadDeletedObjects() { 78 return baddeleted; 79 } 80 81 private int nbloaded = 0; 82 83 88 public int getNbLoadedObjects() { 89 return nbloaded; 90 } 91 92 Connection conn = null; 93 94 DBRepository() {} 95 96 PreparedStatement insertStmt = null; 97 PreparedStatement updateStmt = null; 98 PreparedStatement deleteStmt = null; 99 100 104 public void init(File dir) throws IOException { 105 this.dir = dir; 106 107 try { 108 Class.forName(driver).newInstance(); 109 Properties props = new Properties (); 111 props.put("user", "user1"); 112 props.put("password", "user1"); 113 114 conn = DriverManager.getConnection(connurl + new File (dir, "JoramDB").getPath() + ";create=true", props); 115 conn.setAutoCommit(false); 116 } catch (IllegalAccessException exc) { 117 throw new IOException (exc.getMessage()); 118 } catch (ClassNotFoundException exc) { 119 throw new IOException (exc.getMessage()); 120 } catch (InstantiationException exc) { 121 throw new IOException (exc.getMessage()); 122 } catch (SQLException sqle) { 123 throw new IOException (sqle.getMessage()); 124 } 125 126 try { 127 Statement s = conn.createStatement(); 129 s.execute("CREATE TABLE JoramDB (name VARCHAR(256), content LONG VARCHAR FOR BIT DATA, PRIMARY KEY(name))"); 132 s.close(); 133 conn.commit(); 134 } catch (SQLException sqle) { 135 } 136 137 try { 138 insertStmt = conn.prepareStatement("INSERT INTO JoramDB VALUES (?, ?)"); 139 updateStmt = conn.prepareStatement("UPDATE JoramDB SET content=? WHERE name=?"); 140 deleteStmt = conn.prepareStatement("DELETE FROM JoramDB WHERE name=?"); 141 } catch (SQLException sqle) { 142 throw new IOException (sqle.getMessage()); 143 } 144 } 145 146 151 public String [] list(String prefix) throws IOException { 152 try { 153 Statement s = conn.createStatement(); 155 ResultSet rs = s.executeQuery("SELECT name FROM JoramDB WHERE name LIKE '" + prefix + "%'"); 156 157 Vector v = new Vector (); 158 while (rs.next()) { 159 v.add(rs.getString(1)); 160 } 161 rs.close(); 162 s.close(); 163 164 String [] result = new String [v.size()]; 165 result = (String []) v.toArray(result); 166 167 return result; 168 } catch (SQLException sqle) { 169 throw new IOException (sqle.getMessage()); 170 } 171 } 172 173 176 public void save(String dirName, String name, byte[] content) throws IOException { 177 String fname = null; 178 if (dirName == null) { 179 fname = name; 180 } else { 181 fname = new StringBuffer (dirName).append('/').append(name).toString(); 182 } 183 184 try { 185 insertStmt.setString(1, fname); 186 insertStmt.setBytes(2, content); 187 insertStmt.executeUpdate(); 188 } catch (SQLException e) { 189 try { 190 updateStmt.setBytes(1, content); 191 updateStmt.setString(2, fname); 192 updateStmt.executeUpdate(); 193 } catch (SQLException sqle) { 194 throw new IOException (sqle.getMessage()); 195 } 196 } 197 198 nbsaved += 1; 199 } 200 201 206 public Object loadobj(String dirName, String name) throws IOException , ClassNotFoundException { 207 byte[] content = load(dirName, name); 208 209 ByteArrayInputStream bis = new ByteArrayInputStream (content); 210 ObjectInputStream ois = new ObjectInputStream (bis); 211 try { 212 Object obj = ois.readObject(); 213 return obj; 214 } finally { 215 ois.close(); 216 bis.close(); 217 } 218 } 219 220 225 public byte[] load(String dirName, String name) throws IOException { 226 String fname = null; 227 if (dirName == null) { 228 fname = name; 229 } else { 230 fname = new StringBuffer (dirName).append('/').append(name).toString(); 231 } 232 233 try { 234 Statement s = conn.createStatement(); 236 ResultSet rs = s.executeQuery("SELECT content FROM JoramDB WHERE name='" + fname + "'"); 238 239 if (!rs.next()) { 240 throw new FileNotFoundException ("Cannot find object " + fname); 241 } 242 243 byte[] content = rs.getBytes(1); 244 245 rs.close(); 246 s.close(); 247 248 nbloaded += 1; 249 return content; 250 } catch (SQLException sqle) { 251 throw new IOException (sqle.getMessage()); 252 } 253 } 254 255 258 public void delete(String dirName, String name) throws IOException { 259 String fname = null; 260 if (dirName == null) { 261 fname = name; 262 } else { 263 fname = new StringBuffer (dirName).append('/').append(name).toString(); 264 } 265 266 int nb = 0; 267 try { 268 Statement s = conn.createStatement(); 270 nb = s.executeUpdate("DELETE FROM JoramDB WHERE name='" + fname + "'"); 272 } catch (SQLException sqle) { 273 throw new IOException (sqle.getMessage()); 274 } 275 276 if (nb != 1) baddeleted += 1; 277 nbdeleted += 1; 278 } 279 280 283 public void commit() throws IOException { 284 try { 285 conn.commit(); 286 } catch (SQLException sqle) { 287 throw new IOException (sqle.getMessage()); 288 } 289 } 290 291 294 public void close() throws IOException { 295 try { 296 conn.close(); 297 } catch (SQLException sqle) { 298 throw new IOException (sqle.getMessage()); 299 } 300 } 301 } 302 | Popular Tags |