You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by ep...@apache.org on 2004/10/07 17:28:43 UTC
cvs commit: jakarta-turbine-fulcrum/security/hibernate/src/test PersistenceHelperDefaultImpl.xml HibernateMapping.hbm hibernate.cfg.xml
epugh 2004/10/07 08:28:43
Modified: security/hibernate/src/test/org/apache/fulcrum/security/hibernate
PersistenceHelperTest.java
security/hibernate/src/java/org/apache/fulcrum/security/hibernate
PersistenceHelper.java
Added: security/hibernate/src/test/org/apache/fulcrum/security/hibernate
PersistenceHelperDefaultImplTest.java
security/hibernate/src/java/org/apache/fulcrum/security/hibernate
PersistenceHelperHibernateServiceImpl.java
PersistenceHelperDefaultImpl.java
security/hibernate/src/test PersistenceHelperDefaultImpl.xml
HibernateMapping.hbm hibernate.cfg.xml
Removed: security/hibernate/src/java/org/apache/fulcrum/security/hibernate
PersistenceHelperImpl.java
Log:
Start deprecating the HibernateService!
Revision Changes Path
1.5 +3 -1 jakarta-turbine-fulcrum/security/hibernate/src/test/org/apache/fulcrum/security/hibernate/PersistenceHelperTest.java
Index: PersistenceHelperTest.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/hibernate/src/test/org/apache/fulcrum/security/hibernate/PersistenceHelperTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- PersistenceHelperTest.java 7 Oct 2004 15:13:15 -0000 1.4
+++ PersistenceHelperTest.java 7 Oct 2004 15:28:43 -0000 1.5
@@ -74,4 +74,6 @@
assertSame(persistenceHelperFromRoleManager2.getHibernateService(),persistenceHelperFromRoleManager.getHibernateService());
assertSame(persistenceHelperFromRoleManager2.getHibernateService(),persistenceHelperFromGroupManager.getHibernateService());
}
+
+
}
1.1 jakarta-turbine-fulcrum/security/hibernate/src/test/org/apache/fulcrum/security/hibernate/PersistenceHelperDefaultImplTest.java
Index: PersistenceHelperDefaultImplTest.java
===================================================================
package org.apache.fulcrum.security.hibernate;
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import net.sf.hibernate.Session;
import org.apache.fulcrum.security.SecurityService;
import org.apache.fulcrum.testcontainer.BaseUnitTest;
/**
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
* @version $Id: PersistenceHelperDefaultImplTest.java,v 1.1 2004/10/07 15:28:43 epugh Exp $
*/
public class PersistenceHelperDefaultImplTest extends BaseUnitTest
{
/**
* Constructor for HibernatePermissionManagerTest.
* @param arg0
*/
public PersistenceHelperDefaultImplTest(String arg0)
{
super(arg0);
}
public void testPassingInExternalHibernateService() throws Exception
{
this.setRoleFileName(null);
this.setConfigurationFileName("src/test/PersistenceHelperDefaultImpl.xml");
SecurityService securityService = (SecurityService) lookup(SecurityService.ROLE);
HibernateGroupManagerImpl groupManager = (HibernateGroupManagerImpl)securityService.getGroupManager();
PersistenceHelper persistenceHelper = groupManager.getPersistenceHelper();
assertTrue(persistenceHelper instanceof PersistenceHelperDefaultImpl);
PersistenceHelperDefaultImpl persistenceHelperFromGroupManager = (PersistenceHelperDefaultImpl)persistenceHelper;
Session s = persistenceHelper.retrieveSession();
}
}
1.3 +1 -4 jakarta-turbine-fulcrum/security/hibernate/src/java/org/apache/fulcrum/security/hibernate/PersistenceHelper.java
Index: PersistenceHelper.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/security/hibernate/src/java/org/apache/fulcrum/security/hibernate/PersistenceHelper.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- PersistenceHelper.java 7 Jul 2004 17:29:37 -0000 1.2
+++ PersistenceHelper.java 7 Oct 2004 15:28:43 -0000 1.3
@@ -17,7 +17,6 @@
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
-import net.sf.hibernate.avalon.HibernateService;
import org.apache.fulcrum.security.entity.SecurityEntity;
import org.apache.fulcrum.security.util.DataBackendException;
@@ -38,8 +37,6 @@
public void addEntity(SecurityEntity entity) throws DataBackendException;
public Session retrieveSession() throws HibernateException;
- public void setHibernateService(HibernateService hibernateService);
- public HibernateService getHibernateService() throws HibernateException;
-
+
}
1.1 jakarta-turbine-fulcrum/security/hibernate/src/java/org/apache/fulcrum/security/hibernate/PersistenceHelperHibernateServiceImpl.java
Index: PersistenceHelperHibernateServiceImpl.java
===================================================================
package org.apache.fulcrum.security.hibernate;
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.avalon.HibernateService;
import org.apache.avalon.framework.component.Component;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fulcrum.security.entity.SecurityEntity;
import org.apache.fulcrum.security.spi.AbstractManager;
import org.apache.fulcrum.security.util.DataBackendException;
/**
*
* This base implementation persists to a database via Hibernate. it provides methods shared by all
* Hibernate SPI managers.
*
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
* @version $Id: PersistenceHelperHibernateServiceImpl.java,v 1.1 2004/10/07 15:28:43 epugh Exp $
*/
public class PersistenceHelperHibernateServiceImpl
extends AbstractManager
implements PersistenceHelper, Component
{
/** Logging */
private static Log log = LogFactory.getLog(PersistenceHelperHibernateServiceImpl.class);
protected HibernateService hibernateService;
private Session session;
protected Transaction transaction;
/**
* Deletes an entity object
*
* @param role The object to be removed
* @throws DataBackendException if there was an error accessing the data backend.
* @throws UnknownEntityException if the object does not exist.
*/
public void removeEntity(SecurityEntity entity) throws DataBackendException
{
try
{
session = retrieveSession();
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
}
catch (HibernateException he)
{
try
{
transaction.rollback();
}
catch (HibernateException hex)
{
}
throw new DataBackendException(
"Problem removing entity:" + he.getMessage(),
he);
}
}
/**
* Stores changes made to an object
*
* @param role The object to be saved
* @throws DataBackendException if there was an error accessing the data backend.
* @throws UnknownEntityException if the role does not exist.
*/
public void updateEntity(SecurityEntity entity) throws DataBackendException
{
try
{
session = retrieveSession();
transaction = session.beginTransaction();
session.update(entity);
transaction.commit();
}
catch (HibernateException he)
{
try
{
if (transaction != null)
{
transaction.rollback();
}
if (he
.getMessage()
.indexOf("Another object was associated with this id")
> -1)
{
session.close();
updateEntity(entity);
}
else
{
throw new DataBackendException(
"updateEntity(" + entity + ")",
he);
}
}
catch (HibernateException hex)
{
}
}
return;
}
/**
* adds an entity
*
* @param role The object to be saved
* @throws DataBackendException if there was an error accessing the data backend.
* @throws UnknownEntityException if the role does not exist.
*/
public void addEntity(SecurityEntity entity) throws DataBackendException
{
try
{
session = retrieveSession();
transaction = session.beginTransaction();
session.save(entity);
transaction.commit();
}
catch (HibernateException he)
{
try
{
transaction.rollback();
}
catch (HibernateException hex)
{
}
throw new DataBackendException("addEntity(s,name)", he);
}
return;
}
/**
* Returns a hibernate session that has been opened if it was null or not
* connected or not open.
* @return An Open hibernate session.
* @throws HibernateException
*/
public Session retrieveSession() throws HibernateException
{
if (session == null || (!session.isConnected() && !session.isOpen()))
{
session = getHibernateService().openSession();
}
return session;
}
/**
* In some environments (like ECM) the service ends up getting it's own
* copy of the HibernateService. In those environments, we might want to
* pass in a different HibernateService instead.
*
* @param hibernateService The hibernateService to set.
*/
public void setHibernateService(HibernateService hibernateService)
{
this.hibernateService = hibernateService;
}
/**
* Lazy loads the hibernateservice if it hasn't been requested yet.
* @return the hibernate service
*/
public HibernateService getHibernateService() throws HibernateException
{
if (hibernateService == null)
{
hibernateService =
(HibernateService) resolve(HibernateService.ROLE);
}
return hibernateService;
}
public void dispose(){
release(hibernateService);
super.dispose();
}
}
1.1 jakarta-turbine-fulcrum/security/hibernate/src/java/org/apache/fulcrum/security/hibernate/PersistenceHelperDefaultImpl.java
Index: PersistenceHelperDefaultImpl.java
===================================================================
package org.apache.fulcrum.security.hibernate;
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import org.apache.avalon.framework.component.Component;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fulcrum.security.entity.SecurityEntity;
import org.apache.fulcrum.security.spi.AbstractManager;
import org.apache.fulcrum.security.util.DataBackendException;
import org.apache.fulcrum.security.util.UnknownEntityException;
/**
*
* This persistenceHelper expects you to either pass in a SessionFactory to use,
* or it will create one from a hibernate.cfg.xml in the root of the classpath.
*
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
* @version $Id: PersistenceHelperDefaultImpl.java,v 1.1 2004/10/07 15:28:43 epugh Exp $
*/
public class PersistenceHelperDefaultImpl
extends AbstractManager
implements PersistenceHelper, Component
{
/** Logging */
private static Log log = LogFactory.getLog(PersistenceHelperDefaultImpl.class);
private SessionFactory sessionFactory;
private Session session;
protected Transaction transaction;
/**
* Deletes an entity object
*
* @param role The object to be removed
* @throws DataBackendException if there was an error accessing the data backend.
* @throws UnknownEntityException if the object does not exist.
*/
public void removeEntity(SecurityEntity entity) throws DataBackendException
{
try
{
session = retrieveSession();
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
}
catch (HibernateException he)
{
try
{
transaction.rollback();
}
catch (HibernateException hex)
{
}
throw new DataBackendException(
"Problem removing entity:" + he.getMessage(),
he);
}
}
/**
* Stores changes made to an object
*
* @param role The object to be saved
* @throws DataBackendException if there was an error accessing the data backend.
* @throws UnknownEntityException if the role does not exist.
*/
public void updateEntity(SecurityEntity entity) throws DataBackendException
{
try
{
session = retrieveSession();
transaction = session.beginTransaction();
session.update(entity);
transaction.commit();
}
catch (HibernateException he)
{
try
{
if (transaction != null)
{
transaction.rollback();
}
if (he
.getMessage()
.indexOf("Another object was associated with this id")
> -1)
{
session.close();
updateEntity(entity);
}
else
{
throw new DataBackendException(
"updateEntity(" + entity + ")",
he);
}
}
catch (HibernateException hex)
{
}
}
return;
}
/**
* adds an entity
*
* @param role The object to be saved
* @throws DataBackendException if there was an error accessing the data backend.
* @throws UnknownEntityException if the role does not exist.
*/
public void addEntity(SecurityEntity entity) throws DataBackendException
{
try
{
session = retrieveSession();
transaction = session.beginTransaction();
session.save(entity);
transaction.commit();
}
catch (HibernateException he)
{
try
{
transaction.rollback();
}
catch (HibernateException hex)
{
}
throw new DataBackendException("addEntity(s,name)", he);
}
return;
}
/**
* Allow an external session to be passed in.
* @param session
*/
public void setSession(Session session){
this.session = session;
}
/**
* Returns a hibernate session, or if is null, opens one.
* @return An Open hibernate session.
* @throws HibernateException
*/
public Session retrieveSession() throws HibernateException
{
if (session == null )
{
session = getSessionFactory().openSession();
}
return session;
}
/**
* In some environments we will load the session factory up
* and pass it in.
*
* @param hibernateService The hibernateService to set.
*/
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
/**
* Lazy loads the sessionFactory if it hasn't been requested yet.
* @return the hibernate service
*/
public SessionFactory getSessionFactory() throws HibernateException
{
if (sessionFactory == null)
{
sessionFactory = new Configuration().buildSessionFactory();
}
return sessionFactory;
}
}
1.1 jakarta-turbine-fulcrum/security/hibernate/src/test/PersistenceHelperDefaultImpl.xml
Index: PersistenceHelperDefaultImpl.xml
===================================================================
<my-system>
<component
role="org.apache.fulcrum.security.SecurityService"
class="org.apache.fulcrum.security.BaseSecurityService">
</component>
<component
role="org.apache.fulcrum.security.UserManager"
class="org.apache.fulcrum.security.hibernate.HibernateUserManagerImpl">
<className>org.apache.fulcrum.security.model.turbine.entity.TurbineUser</className>
</component>
<component
role="org.apache.fulcrum.security.GroupManager"
class="org.apache.fulcrum.security.hibernate.HibernateGroupManagerImpl">
<className>org.apache.fulcrum.security.model.turbine.entity.TurbineGroup</className>
</component>
<component
role="org.apache.fulcrum.security.RoleManager"
class="org.apache.fulcrum.security.hibernate.HibernateRoleManagerImpl">
<className>org.apache.fulcrum.security.model.turbine.entity.TurbineRole</className>
</component>
<component
role="org.apache.fulcrum.security.PermissionManager"
class="org.apache.fulcrum.security.hibernate.HibernatePermissionManagerImpl">
<className>org.apache.fulcrum.security.model.turbine.entity.TurbinePermission</className>
</component>
<component
role="org.apache.fulcrum.security.hibernate.PersistenceHelper"
class="org.apache.fulcrum.security.hibernate.PersistenceHelperDefaultImpl">
</component>
<component
role="org.apache.fulcrum.security.ModelManager"
class="org.apache.fulcrum.security.hibernate.turbine.HibernateModelManagerImpl">
</component>
<component
role="org.apache.fulcrum.security.authenticator.Authenticator"
class="org.apache.fulcrum.security.authenticator.TextMatchAuthenticator">
</component>
</my-system>
1.1 jakarta-turbine-fulcrum/security/hibernate/src/test/HibernateMapping.hbm
Index: HibernateMapping.hbm
===================================================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="foo.nothing"/>
1.1 jakarta-turbine-fulcrum/security/hibernate/src/test/hibernate.cfg.xml
Index: hibernate.cfg.xml
===================================================================
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping file="conf/test/HibernateMapping.hbm"/>
</session-factory>
</hibernate-configuration>
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org