KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import net.sf.hibernate.HibernateException;
24 import net.sf.hibernate.ObjectNotFoundException;
25 import net.sf.hibernate.Session;
26
27 import org.aopalliance.intercept.ConstructorInvocation;
28 import org.aopalliance.intercept.MethodInvocation;
29 import org.objectweb.jac.core.AspectComponent;
30 import org.objectweb.jac.core.Interaction;
31 import org.objectweb.jac.core.NameRepository;
32 import org.objectweb.jac.core.Wrapper;
33 import org.objectweb.jac.util.Repository;
34
35 /**
36  * This wrapper delimits the begining of a persistent session.
37  *
38  * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
39  * @version 1.0
40  */

41 public class BeginPersistentSessionWrapper extends Wrapper {
42
43     /** The gateway instance to Hibernate. */
44     private HibernateHelper hh = HibernateHelper.get();
45     
46     public BeginPersistentSessionWrapper( AspectComponent ac ) {
47         super(ac);
48     }
49     
50     /**
51      * Wrapping method around pointcuts
52      * where a persistent session begins.
53      */

54     public Object JavaDoc invoke( MethodInvocation invocation ) {
55         Interaction interaction = (Interaction) invocation;
56         try {
57             return _begin(interaction);
58         } catch (HibernateException e) {
59             e.printStackTrace();
60             System.exit(1);
61         }
62         return null;
63     }
64     private Object JavaDoc _begin( Interaction interaction ) throws HibernateException {
65         
66         hh.openSessionAndBeginTx();
67         Session session = hh.getSession();
68             
69         /**
70          * Load all persistent objects.
71          * If they do not exist yet in the storage,
72          * create an entry for them (save).
73          */

74         HibernateAC hac = (HibernateAC) ac;
75         List JavaDoc pObjects = hac.getPersistentObjects();
76         for ( Iterator JavaDoc iter=pObjects.iterator() ; iter.hasNext() ; ) {
77             
78             String JavaDoc name = (String JavaDoc) iter.next();
79             Object JavaDoc object = names.getObject(name);
80             
81             try {
82                 session.load(object,name);
83             } catch (ObjectNotFoundException e) {
84                 session.save(object,name);
85             }
86         }
87                 
88         /**
89          * Proceed the interaction.
90          */

91         return proceed(interaction);
92     }
93     
94
95     /**
96      * The reference towards the JAC name repository.
97      */

98     private Repository names = NameRepository.get();
99
100     public Object JavaDoc construct(ConstructorInvocation invocation)
101         throws Throwable JavaDoc {
102         throw new Exception JavaDoc("This wrapper does not support constructor wrapping");
103     }
104 }
105
Popular Tags