KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > distrans > persistence > PersistenceAC


1 /*
2   Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

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 JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.transaction.TransactionManager JavaDoc;
28
29 import org.enhydra.jdbc.standard.StandardXADataSource;
30
31 /**
32  * Transaction-enabled persistence storage.
33  * An instance this AC is mandatory with DisTransAC.
34  * This class delegates most of the work (apart from data sources
35  * registering) to a technical implementation of the persistence API
36  * PersistenceItf.
37  * Current implementations of this API: SimpleDbPersistence.
38  *
39  * Relies on jac.aspects.distrans.JOTMHelper
40  * to retrieve the JOTM instance used by JAC.
41  *
42  * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
43  * @version 1.0
44  */

45 public class PersistenceAC extends AspectComponent {
46     
47     public PersistenceAC() {}
48
49     /** A map storing data sources. */
50     private Map JavaDoc sources = new HashMap JavaDoc();
51     
52     /**
53      * Define a data source name that will be later on used by the remaining
54      * configuration methods of this AC.
55      *
56      * @param sourceName the data source name
57      * @param driver the JDBC driver name (eg org.postgresql.Driver)
58      * @param url the JDBC URL (eg jdbc:postgresql://localhost/test)
59      * @param user the login to use
60      * @param password the password to use
61      */

62     public void defineDataSource(
63         String JavaDoc sourceName,
64         String JavaDoc driver, String JavaDoc url, String JavaDoc user, String JavaDoc password ) {
65             
66         StandardXADataSource xads = new StandardXADataSource();
67         try {
68             xads.setDriverName(driver);
69         } catch (SQLException JavaDoc e) {
70             e.printStackTrace();
71             System.exit(1);
72         }
73         xads.setUrl(url);
74         xads.setUser(user);
75         xads.setPassword(password);
76         TransactionManager JavaDoc tm = JOTMHelper.get().getTransactionManager();
77         xads.setTransactionManager(tm);
78         
79         sources.put( sourceName, xads );
80     }
81     
82     
83     /**
84      * The instance implementing the technical API for persistence.
85      * SimpleDbPersistence implements it.
86      */

87     private PersistenceItf storage;
88     
89     public void setStorageType( String JavaDoc classname ) {
90         try {
91             _setStorageType(classname);
92             return;
93         } catch (ClassNotFoundException JavaDoc e) {
94             e.printStackTrace();
95         } catch (InstantiationException JavaDoc e) {
96             e.printStackTrace();
97         } catch (IllegalAccessException JavaDoc e) {
98             e.printStackTrace();
99         }
100         System.exit(1);
101     }
102     
103     private void _setStorageType( String JavaDoc classname )
104         throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc {
105
106         Class JavaDoc storageClass = Class.forName(classname);
107         if (!PersistenceItf.class.isAssignableFrom(storageClass))
108             throw new RuntimeException JavaDoc(classname+" must implement jac.aspects.distrans.persistence.PersistenceItf");
109         
110         storage = (PersistenceItf) storageClass.newInstance();
111     }
112     
113     
114     /**
115      * Initialize the persistence storage.
116      * If the storage already exists, do not reinitialize it.
117      *
118      * @param className the class name for which we want to create a storage
119      * @param sourceName the data source name
120      */

121     public void initStorageIfNeeded( String JavaDoc className, String JavaDoc sourceName ) {
122
123         if ( storage == null )
124             throw new RuntimeException JavaDoc("setStorageType() must be called first");
125         
126         StandardXADataSource ds = (StandardXADataSource) sources.get(sourceName);
127         if ( ds == null )
128             throw new RuntimeException JavaDoc("Unknown data source "+sourceName);
129
130         storage.initStorageIfNeeded(className,ds);
131     }
132     
133     /**
134      * Initialize the persistence storage.
135      * If the storage already exists, reinitialize it.
136      *
137      * @param className the class name for which we want to create a storage
138      * @param sourceName the data source name
139      */

140     public void initStorage( String JavaDoc className, String JavaDoc sourceName ) {
141
142         if ( storage == null )
143             throw new RuntimeException JavaDoc("setStorageType() must be called first");
144         
145         StandardXADataSource ds = (StandardXADataSource) sources.get(sourceName);
146         if ( ds == null )
147             throw new RuntimeException JavaDoc("Unknown data source "+sourceName);
148     
149         storage.initStorage(className,ds);
150     }
151
152     /**
153      * All objects matching the objectNameExpression
154      * are made persistent to a SQL database represented by the data source.
155      * These objects are ressources that will potentially be used
156      * later on in transactions.
157      *
158      * Even if the objectNameExpression can be any regular expression,
159      * it is assumed to designate instances storable in existing
160      * storages (eventually call initStorageIfNeeded before).
161      *
162      * @param objectNameExpression the object name expression
163      * @param sourceName the source name
164      */

165     public void registerPersistentRessource(
166         String JavaDoc objectNameExpression, String JavaDoc sourceName ) {
167                
168         if ( storage == null )
169             throw new RuntimeException JavaDoc("setStorageType() must be called first");
170         
171         StandardXADataSource ds = (StandardXADataSource) sources.get(sourceName);
172         if ( ds == null )
173             throw new RuntimeException JavaDoc("Unknown data source "+sourceName);
174         
175         /**
176          * Wrap methods that perform write operations.
177          * Modifier methods in all the classes
178          * for all the objects matching objectNameExpression.
179          */

180         WriteWrapper pw = new WriteWrapper(this,storage,ds);
181         pointcut(
182             objectNameExpression, "ALL", "MODIFIERS",
183             pw, null
184         );
185         
186         /**
187          * Wrap methods that perform read operations.
188          * Accessor and modifier methods in all the classes
189          * for all the objects matching objectNameExpression.
190          * Note: modifiers read fields before modifying them.
191          */

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