1 package org.columba.core.association; 2 3 import java.io.File ; 4 import java.sql.Connection ; 5 import java.sql.DriverManager ; 6 import java.sql.SQLException ; 7 import java.sql.Statement ; 8 import java.util.Collection ; 9 import java.util.HashMap ; 10 import java.util.LinkedList ; 11 import java.util.List ; 12 import java.util.Vector ; 13 import java.util.logging.ConsoleHandler ; 14 import java.util.logging.Handler ; 15 import java.util.logging.Level ; 16 import java.util.logging.Logger ; 17 18 import javax.persistence.EntityManager; 19 import javax.persistence.EntityManagerFactory; 20 import javax.persistence.EntityTransaction; 21 import javax.persistence.Persistence; 22 import javax.persistence.PersistenceException; 23 import javax.persistence.Query; 24 25 import org.columba.core.association.api.IAssociation; 26 import org.columba.core.association.api.IAssociationStore; 27 import org.columba.core.config.DefaultConfigDirectory; 28 29 public class AssociationStore implements IAssociationStore, Runnable { 30 31 final static String ENTITY_MANAGER = "associations"; 32 33 34 private static final Logger LOG = Logger 35 .getLogger("org.columba.core.association.AssociationStore"); 36 37 EntityManagerFactory factory; 38 39 EntityManager manager; 40 41 Connection conn; 42 43 static private AssociationStore instance; 44 45 49 private AssociationStore() { 50 factory = null; 51 manager = null; 52 conn = null; 53 } 54 55 public void addAssociation(String serviceId, String metaDataId, 56 String itemId) { 57 58 if (!isReady()) 60 init(); 61 62 if (!isReady()) 64 return; 65 66 EntityTransaction tx = manager.getTransaction(); 68 tx.begin(); 69 try { 70 IAssociation association = new Association(itemId, serviceId, 71 metaDataId); 72 manager.persist(association); 73 tx.commit(); 74 } catch (Exception ex) { 75 if (tx.isActive()) { 76 tx.rollback(); 77 LOG 78 .severe("AddAssociation: Exception while persisting new association! Will Rollback! " 79 + serviceId + " " + metaDataId + " " + itemId); 80 } else 81 LOG 82 .log( 83 Level.SEVERE, 84 "Got an Exception, but no Transaction active, so no Rollback!", 85 ex); 86 } 87 } 88 89 @SuppressWarnings ("unchecked") 90 public Collection <IAssociation> getAllAssociations(String itemId) { 91 92 if (!isReady()) 94 init(); 95 96 if (!isReady()) 98 return new Vector <IAssociation>(); 99 100 EntityTransaction tx = manager.getTransaction(); 102 103 while (tx.isActive()) 106 ; 107 108 tx.begin(); 109 Query query = manager 110 .createQuery("select a from org.columba.core.association.Association a where a.itemId = '" 111 + itemId + "'"); 112 Collection <IAssociation> results = (Collection <IAssociation>) query 113 .getResultList(); 114 tx.commit(); 119 return results; 120 } 121 122 @SuppressWarnings ("unchecked") 123 public Collection <String > getAssociatedItems(String serviceId, 124 String metaDataId) { 125 126 if (!isReady()) 128 init(); 129 130 if (!isReady()) 132 return new Vector <String >(); 133 134 EntityTransaction tx = manager.getTransaction(); 136 tx.begin(); 137 Query query = manager 138 .createQuery("select a from org.columba.core.association.Association a where a.serviceId = '" 139 + serviceId 140 + "' and a.metaDataId = '" 141 + metaDataId 142 + "'"); 143 Collection <IAssociation> results = (Collection <IAssociation>) query 144 .getResultList(); 145 Collection <String > itemCollection = new LinkedList <String >(); 146 for (IAssociation a : results) { 147 itemCollection.add(a.getItemId()); 148 } 149 tx.commit(); 150 return itemCollection; 151 } 152 153 156 public void init() { 157 158 Handler [] handlers = Logger.getLogger("").getHandlers(); 160 ConsoleHandler consolehandler = null; 161 Level level = null; 162 for (int index = 0; index < handlers.length; index++) { 163 if (handlers[index] instanceof ConsoleHandler ) { 165 level = handlers[index].getLevel(); 166 handlers[index].setLevel(Level.OFF); 167 consolehandler = (ConsoleHandler ) handlers[index]; 168 break; 169 } 170 } 171 172 174 String connectionString = "jdbc:hsqldb:file:" 175 + DefaultConfigDirectory.getInstance().getCurrentPath().getAbsolutePath() 176 + File.separator + "associations"; 177 178 try { 180 181 if (conn == null) { 183 Class.forName("org.hsqldb.jdbcDriver").newInstance(); 184 conn = DriverManager.getConnection(connectionString, "sa", ""); 185 } 186 } catch (InstantiationException e) { 187 LOG.severe("AssociationStore: Could not start the HSQLDB! " + e.getClass().getName() 188 + ": " + e.getMessage()); 189 } catch (IllegalAccessException e) { 190 LOG.severe("AssociationStore: Could not start the HSQLDB! " + e.getClass().getName() 191 + ": " + e.getMessage()); 192 } catch (ClassNotFoundException e) { 193 LOG.severe("AssociationStore: Could not start the HSQLDB! " + e.getClass().getName() 194 + ": " + e.getMessage()); 195 } catch (SQLException e) { 196 LOG.severe("AssociationStore: Could not start the HSQLDB! " + e.getClass().getName() 197 + ": " + e.getMessage()); 198 } 199 200 try { 201 202 HashMap <String , String > map = new HashMap <String , String >(); 206 map.put("hibernate.connection.url", connectionString); 207 map.put("exclude-unlisted-classes", "true"); 211 if (factory == null) 213 factory = Persistence.createEntityManagerFactory( 214 ENTITY_MANAGER, map); 215 216 if (manager == null) 217 manager = factory.createEntityManager(map); 218 219 } catch (PersistenceException pEx) { 220 LOG.severe("AssociationStore: Could not start the Entity manager! " 221 + pEx.getMessage()); 222 } 223 224 if (consolehandler != null) 226 consolehandler.setLevel(level); 227 228 } 229 230 public boolean isReady() { 231 return ((factory != null) && (manager != null) && (conn != null)); 232 } 233 234 @SuppressWarnings ("unchecked") 235 public void removeAssociation(String serviceId, String metaDataId, 236 String itemId) { 237 238 if (!isReady()) 240 init(); 241 242 if (!isReady()) 244 return; 245 246 EntityTransaction tx = manager.getTransaction(); 247 tx.begin(); 248 try { 249 Query query = manager 250 .createQuery("select a from org.columba.core.association.Association a where a.itemId = '" 251 + itemId 252 + "' and a.serviceId = '" 253 + serviceId 254 + "'" + " and a.metaDataId = '" + metaDataId + "'"); 255 if (query.getResultList().size() > 1) { 256 LOG 259 .info("RemoveAssociation: Got more than one association, that is strange! We try to remove all!"); 260 for (Object row : query.getResultList()) 261 manager.remove(row); 262 tx.commit(); 263 } else if (query.getResultList().size() == 1) { 264 manager.remove(query.getSingleResult()); 265 tx.commit(); 266 } else { 267 } 270 } catch (Exception ex) { 271 if (tx.isActive()) { 272 tx.rollback(); 273 LOG 274 .severe("RemoveAssociation: Exception while removing association! Will Rollback! " 275 + serviceId + " " + metaDataId + " " + itemId); 276 } else { 277 ex.printStackTrace(); 278 LOG 279 .log( 280 Level.SEVERE, 281 "RemoveAssociation: Got an Exception, but no Transaction active, so no Rollback!", 282 ex); 283 } 284 } 285 } 286 287 @SuppressWarnings ("unchecked") 288 public void removeItem(String itemId) { 289 290 if (!isReady()) 292 init(); 293 294 if (!isReady()) 296 return; 297 298 EntityTransaction tx = manager.getTransaction(); 299 tx.begin(); 300 try { 301 Query query = manager 302 .createQuery("select a from org.columba.core.association.Association a where a.itemId = '" 303 + itemId + "'"); 304 List <Association> results = (List <Association>) query 305 .getResultList(); 306 for (Association a : results) { 307 manager.remove(a); 308 } 309 tx.commit(); 310 } catch (Exception ex) { 311 LOG.severe("RemoveItem: could not remove item" + ex.getMessage()); 312 ex.printStackTrace(); 313 tx.rollback(); 314 } 315 } 316 317 public void shutdown() { 318 319 try { 321 if (conn != null) { 323 Statement stmt = conn.createStatement(); 324 stmt.execute("SHUTDOWN"); 325 } 326 } catch (SQLException e) { 327 LOG.severe("AssociationStore: Could not shutdwon the database! " 328 + e.getClass().getName() + ": " + e.getMessage()); 329 } 330 331 if (manager != null) 333 manager.close(); 334 335 if (factory != null) 336 factory.close(); 337 } 338 339 public static AssociationStore getInstance() { 340 if (instance == null) { 341 synchronized (AssociationStore.class) { 342 if (instance == null) 343 instance = new AssociationStore(); 344 } 345 } 346 return instance; 347 } 348 349 public void run() { 350 shutdown(); 351 } 352 } 353 | Popular Tags |