001 /* 002 Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.hibernate; 019 020 import java.util.ArrayList; 021 import java.util.List; 022 023 import net.sf.hibernate.HibernateException; 024 import net.sf.hibernate.MappingException; 025 026 import org.objectweb.jac.core.AspectComponent; 027 import org.objectweb.jac.core.rtti.ClassItem; 028 import org.objectweb.jac.core.rtti.ClassRepository; 029 030 /** 031 * Persistence AC relying on Hibernate. 032 * 033 * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr> 034 * @version 1.0 035 */ 036 public class HibernateAC extends AspectComponent { 037 public HibernateAC() {} 038 039 /** The gateway instance to Hibernate. */ 040 private HibernateHelper hh = HibernateHelper.get(); 041 042 /** 043 * Declare a new persistent class. 044 * 045 * @param className the persistant class name 046 */ 047 public void registerPersistentClass( String className ) { 048 049 ClassItem cli = cr.getClass(className); 050 Class cl = cli.getActualClass(); 051 052 try { 053 hh.addClass(cl); 054 } catch (MappingException e) { 055 e.printStackTrace(); 056 System.exit(1); 057 } 058 } 059 060 private ClassRepository cr = ClassRepository.get(); 061 062 063 /** 064 * Create tables to hold data for persistent classes. 065 */ 066 public void initStorage() { 067 try { 068 hh.schemaExport(); 069 } catch (HibernateException e) { 070 e.printStackTrace(); 071 System.exit(1); 072 } 073 } 074 075 076 /** The list of object names declare to be persistent. */ 077 private List persistentObjects = new ArrayList(); 078 List getPersistentObjects() { return persistentObjects; } 079 080 /** 081 * All objects matching the objectNameExpression 082 * are made persistent with Hibernate. 083 * 084 * Even if the objectNameExpression can be any regular expression, 085 * it is assumed to designate instances storable in existing 086 * storages (eventually call initStorage before). 087 * 088 * @param objectNameExpression the object name expression 089 */ 090 public void registerPersistentObject( String objectNameExpression ) { 091 persistentObjects.add(objectNameExpression); 092 } 093 094 095 /** 096 * Delimit a persistent session with Hibernate. 097 * The session will begin before the method designated by the pointcut 098 * designated by the 3 first parameter, and will end after the pointcut 099 * 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 sessionid, 111 String beginCNE, String beginONE, String beginMNE, 112 String endCNE, String endONE, String 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 }