1 17 18 package org.objectweb.jac.aspects.distrans.persistence; 19 20 import org.objectweb.jac.aspects.distrans.JOTMHelper; 21 import org.objectweb.jac.core.AspectComponent; 22 23 import java.sql.SQLException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import javax.transaction.TransactionManager ; 28 29 import org.enhydra.jdbc.standard.StandardXADataSource; 30 31 45 public class PersistenceAC extends AspectComponent { 46 47 public PersistenceAC() {} 48 49 50 private Map sources = new HashMap (); 51 52 62 public void defineDataSource( 63 String sourceName, 64 String driver, String url, String user, String password ) { 65 66 StandardXADataSource xads = new StandardXADataSource(); 67 try { 68 xads.setDriverName(driver); 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 System.exit(1); 72 } 73 xads.setUrl(url); 74 xads.setUser(user); 75 xads.setPassword(password); 76 TransactionManager tm = JOTMHelper.get().getTransactionManager(); 77 xads.setTransactionManager(tm); 78 79 sources.put( sourceName, xads ); 80 } 81 82 83 87 private PersistenceItf storage; 88 89 public void setStorageType( String classname ) { 90 try { 91 _setStorageType(classname); 92 return; 93 } catch (ClassNotFoundException e) { 94 e.printStackTrace(); 95 } catch (InstantiationException e) { 96 e.printStackTrace(); 97 } catch (IllegalAccessException e) { 98 e.printStackTrace(); 99 } 100 System.exit(1); 101 } 102 103 private void _setStorageType( String classname ) 104 throws ClassNotFoundException , InstantiationException , IllegalAccessException { 105 106 Class storageClass = Class.forName(classname); 107 if (!PersistenceItf.class.isAssignableFrom(storageClass)) 108 throw new RuntimeException (classname+" must implement jac.aspects.distrans.persistence.PersistenceItf"); 109 110 storage = (PersistenceItf) storageClass.newInstance(); 111 } 112 113 114 121 public void initStorageIfNeeded( String className, String sourceName ) { 122 123 if ( storage == null ) 124 throw new RuntimeException ("setStorageType() must be called first"); 125 126 StandardXADataSource ds = (StandardXADataSource) sources.get(sourceName); 127 if ( ds == null ) 128 throw new RuntimeException ("Unknown data source "+sourceName); 129 130 storage.initStorageIfNeeded(className,ds); 131 } 132 133 140 public void initStorage( String className, String sourceName ) { 141 142 if ( storage == null ) 143 throw new RuntimeException ("setStorageType() must be called first"); 144 145 StandardXADataSource ds = (StandardXADataSource) sources.get(sourceName); 146 if ( ds == null ) 147 throw new RuntimeException ("Unknown data source "+sourceName); 148 149 storage.initStorage(className,ds); 150 } 151 152 165 public void registerPersistentRessource( 166 String objectNameExpression, String sourceName ) { 167 168 if ( storage == null ) 169 throw new RuntimeException ("setStorageType() must be called first"); 170 171 StandardXADataSource ds = (StandardXADataSource) sources.get(sourceName); 172 if ( ds == null ) 173 throw new RuntimeException ("Unknown data source "+sourceName); 174 175 180 WriteWrapper pw = new WriteWrapper(this,storage,ds); 181 pointcut( 182 objectNameExpression, "ALL", "MODIFIERS", 183 pw, null 184 ); 185 186 192 ReadWrapper rw = new ReadWrapper(this,storage,ds); 193 pointcut( 194 objectNameExpression, "ALL", "MODIFIERS || ACCESSORS", 195 rw, null 196 ); 197 } 198 199 } 200 | Popular Tags |