KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > hibernate > HibernateAC


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.hibernate;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import net.sf.hibernate.HibernateException;
24 import net.sf.hibernate.MappingException;
25
26 import org.objectweb.jac.core.AspectComponent;
27 import org.objectweb.jac.core.rtti.ClassItem;
28 import org.objectweb.jac.core.rtti.ClassRepository;
29
30 /**
31  * Persistence AC relying on Hibernate.
32  *
33  * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
34  * @version 1.0
35  */

36 public class HibernateAC extends AspectComponent {
37     public HibernateAC() {}
38
39     /** The gateway instance to Hibernate. */
40     private HibernateHelper hh = HibernateHelper.get();
41         
42     /**
43      * Declare a new persistent class.
44      *
45      * @param className the persistant class name
46      */

47     public void registerPersistentClass( String JavaDoc className ) {
48         
49         ClassItem cli = cr.getClass(className);
50         Class JavaDoc cl = cli.getActualClass();
51         
52         try {
53             hh.addClass(cl);
54         } catch (MappingException e) {
55             e.printStackTrace();
56             System.exit(1);
57         }
58     }
59     
60     private ClassRepository cr = ClassRepository.get();
61     
62     
63     /**
64      * Create tables to hold data for persistent classes.
65      */

66     public void initStorage() {
67         try {
68             hh.schemaExport();
69         } catch (HibernateException e) {
70             e.printStackTrace();
71             System.exit(1);
72         }
73     }
74
75     
76     /** The list of object names declare to be persistent. */
77     private List JavaDoc persistentObjects = new ArrayList JavaDoc();
78     List JavaDoc getPersistentObjects() { return persistentObjects; }
79     
80     /**
81      * All objects matching the objectNameExpression
82      * are made persistent with Hibernate.
83      *
84      * Even if the objectNameExpression can be any regular expression,
85      * it is assumed to designate instances storable in existing
86      * storages (eventually call initStorage before).
87      *
88      * @param objectNameExpression the object name expression
89      */

90     public void registerPersistentObject( String JavaDoc objectNameExpression ) {
91         persistentObjects.add(objectNameExpression);
92     }
93
94
95     /**
96      * Delimit a persistent session with Hibernate.
97      * The session will begin before the method designated by the pointcut
98      * designated by the 3 first parameter, and will end after the pointcut
99      * designated by the 3 last ones.
100      *
101      * @param sessionid the session identifier
102      * @param beginCNE begin class name expression
103      * @param beginONE begin object name expression
104      * @param beginMNE begin method name expression
105      * @param endCNE end class name expression
106      * @param endONE end object name expression
107      * @param endMNE end method name expression
108      */

109     public void delimitPersistentSession(
110         String JavaDoc sessionid,
111         String JavaDoc beginCNE, String JavaDoc beginONE, String JavaDoc beginMNE,
112         String JavaDoc endCNE, String JavaDoc endONE, String JavaDoc endMNE ) {
113             
114         BeginPersistentSessionWrapper beginwrapper =
115             new BeginPersistentSessionWrapper(this);
116         
117         EndPersistentSessionWrapper endwrapper =
118             new EndPersistentSessionWrapper(this);
119             
120         pointcut( beginONE, beginCNE, beginMNE, beginwrapper, null );
121         pointcut( endONE, endCNE, endMNE, endwrapper, null );
122     }
123     
124 }
125
Popular Tags