roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From c..@apache.org
Subject svn commit: r447605 - in /incubator/roller/trunk/sandbox/jdobackend: src/org/apache/roller/business/jpa/ web/WEB-INF/classes/
Date Mon, 18 Sep 2006 22:36:59 GMT
Author: clr
Date: Mon Sep 18 15:36:58 2006
New Revision: 447605

URL: http://svn.apache.org/viewvc?view=rev&rev=447605
Log:
Datamapper JPA and metadata

Added:
    incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/
    incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAPersistenceStrategy.java
    incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAQueryImpl.java
    incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARemoveQueryImpl.java
    incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARollerImpl.java
    incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.jdo
    incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.orm

Added: incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAPersistenceStrategy.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAPersistenceStrategy.java?view=auto&rev=447605
==============================================================================
--- incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAPersistenceStrategy.java
(added)
+++ incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAPersistenceStrategy.java
Mon Sep 18 15:36:58 2006
@@ -0,0 +1,286 @@
+/*
+ * Copyright 2006 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.
+ */
+
+
+package org.apache.roller.business.jpa;
+
+import java.util.Collection;
+import java.util.Properties;
+import java.util.Iterator;
+import java.io.InputStream;
+import java.io.IOException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.RollerException;
+import org.apache.roller.business.datamapper.DatamapperPersistenceStrategy;
+import org.apache.roller.business.datamapper.DatamapperQuery;
+import org.apache.roller.business.datamapper.DatamapperRemoveQuery;
+import org.apache.roller.pojos.PersistentObject;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityManager;
+import javax.persistence.Persistence;
+
+/**
+ * JPAPersistenceStrategy is responsible for the lowest-level interaction with
+ * the JPA API.
+ */
+public class JPAPersistenceStrategy implements DatamapperPersistenceStrategy {
+
+    /**
+     * The thread local EntityManager.
+     */
+    private static final ThreadLocal threadLocalEntityManager = new ThreadLocal();
+
+    /**
+     * The EntityManagerFactory for this Roller instance.
+     */
+    private EntityManagerFactory emf = null;
+
+    /**
+     * The logger instance for this class.
+     */
+    private static Log logger = LogFactory.getFactory().getInstance(
+            JPAPersistenceStrategy.class);
+
+    /**
+     * Construct by finding JPA EntityManagerFactory.
+     * @throws org.apache.roller.RollerException on any error
+     */
+    public JPAPersistenceStrategy() throws RollerException {
+        Properties emfProps = loadPropertiesFromResourceName(
+                "JPAEMF.properties", getContextClassLoader());
+        this.emf =
+                Persistence.createEntityManagerFactory("RollerPU", emfProps);
+    }
+
+    /**
+     * Flush changes to the datastore, commit transaction, release em.
+     * @throws org.apache.roller.RollerException on any error
+     */
+    public void flush() throws RollerException {
+        EntityManager em = getEntityManager(false);
+        if (isTransactionActive(em)) {
+            em.getTransaction().commit();
+        }
+        em.close();
+    }
+
+    /**
+     * Release database session, rolls back any uncommitted changes.
+     */
+    public void release() {
+        EntityManager em = getEntityManager(false);
+        if (isTransactionActive(em)) {
+            em.getTransaction().rollback();
+        }
+        em.close();
+        setThreadLocalEntityManager(null);
+    }
+
+    /**
+     * Store object using an existing transaction.
+     * @param obj the object to persist
+     * @return the object persisted
+     * @throws org.apache.roller.RollerException on any error
+     */
+    public PersistentObject store(PersistentObject obj) throws RollerException {
+        EntityManager em = getEntityManager(true);
+        em.persist(obj);
+        return obj;
+    }
+
+    /**
+     * Remove object from persistence storage.
+     * @param clazz the class of object to remove
+     * @param id the id of the object to remove
+     * @throws RollerException on any error deleting object
+     */
+    public void remove(Class clazz, String id) throws RollerException {
+        EntityManager em = getEntityManager(true);
+        Object po = em.find(clazz, id);
+        em.remove(po);
+    }
+
+    /**
+     * Remove object from persistence storage.
+     * @param po the persistent object to remove
+     * @throws org.apache.roller.RollerException on any error
+     */
+    public void remove(PersistentObject po) throws RollerException {
+        EntityManager em = getEntityManager(true);
+        em.remove(po);
+    }
+
+    /**
+     * Remove object from persistence storage.
+     * @param pos the persistent objects to remove
+     * @throws org.apache.roller.RollerException on any error
+     */
+    public void removeAll(Collection pos) throws RollerException {
+        EntityManager em = getEntityManager(true);
+        for (Iterator iterator = pos.iterator(); iterator.hasNext();) {
+            Object obj = iterator.next();
+            em.remove(obj);
+        }
+    }
+
+    /**
+     * Remove objects from persistence storage.
+     * @param clazz the persistent from which to remove all objects
+     * @throws org.apache.roller.RollerException on any error
+     */
+    public void removeAll(Class clazz) throws RollerException {
+        //TODO: Think how this would be implemented
+        DatamapperRemoveQuery rq = newRemoveQuery(clazz, null);
+        rq.removeAll();
+    }
+
+    /**
+     * Retrieve object, no transaction needed.
+     * @param clazz the class of object to retrieve
+     * @param id the id of the object to retrieve
+     * @return the object retrieved
+     * @throws RollerException on any error retrieving object
+     */
+    public PersistentObject load(Class clazz, String id)
+            throws RollerException {
+        EntityManager em = getEntityManager(false);
+        return (PersistentObject) em.find(clazz, id);
+    }
+
+    /**
+     * Return true if a transaction is active on the current EntityManager.
+     * @param em the persistence manager
+     * @return true if the persistence manager is not null and has an active
+     *         transaction
+     */
+    private boolean isTransactionActive(EntityManager em) {
+        if (em == null) {
+            return false;
+        }
+        return em.getTransaction().isActive();
+    }
+
+    /**
+     * Get the EntityManager associated with the current thread of control.
+     * @param isTransactionRequired true if a transaction is begun if not
+     * already active
+     * @return the EntityManager
+     */
+    private EntityManager getEntityManager(boolean isTransactionRequired) {
+        EntityManager em = getThreadLocalEntityManager();
+        if (isTransactionRequired && !em.getTransaction().isActive()) {
+            em.getTransaction().begin();
+        }
+        return em;
+    }
+
+    /**
+     * Get the current ThreadLocal EntityManager
+     */
+    private EntityManager getThreadLocalEntityManager() {
+        EntityManager em = (EntityManager) threadLocalEntityManager.get();
+        if (em == null) {
+            em = emf.createEntityManager();
+            threadLocalEntityManager.set(em);
+        }
+        return em;
+    }
+
+    /**
+     * Set the current ThreadLocal EntityManager
+     */
+    private void setThreadLocalEntityManager(Object em) {
+        threadLocalEntityManager.set(em);
+    }
+
+    /**
+     * Create query.
+     * @param clazz the class of instances to find
+     * @param queryName the name of the query
+     * @throws org.apache.roller.RollerException on any error
+     */
+    public DatamapperQuery newQuery(Class clazz, String queryName)
+            throws RollerException {
+        EntityManager em = getEntityManager(false);
+        return new JPAQueryImpl(em, clazz, queryName);
+    }
+
+    /**
+     * Create query used for bulk remove operations.
+     * @param clazz the class of instances to remove
+     * @param queryName the name of the query
+     * @throws org.apache.roller.RollerException on any error
+     */
+    public DatamapperRemoveQuery newRemoveQuery(Class clazz, String queryName)
+            throws RollerException {
+        EntityManager em = getEntityManager(false);
+        return new JPARemoveQueryImpl(em, clazz, queryName);
+    }
+
+    /**
+     * Loads properties from given resourceName using given class loader
+     * @param resourceName The name of the resource containing properties
+     * @param cl Classloeder to be used to locate the resouce
+     * @return A properties object
+     * @throws RollerException
+     */
+    private static Properties loadPropertiesFromResourceName(
+            String resourceName, ClassLoader cl) throws RollerException {
+        Properties props = new Properties();
+        InputStream in = null;
+        in = cl.getResourceAsStream(resourceName);
+        if (in == null) {
+            //TODO: Check how i18n is done in roller
+            throw new RollerException(
+                    "Could not locate properties to load " + resourceName);
+        }
+        try {
+            props.load(in);
+        } catch (IOException ioe) {
+            throw new RollerException(
+                    "Could not load properties from " + resourceName);
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException ioe) {
+                }
+            }
+        }
+
+        return props;
+    }
+
+    /**
+     * Get the context class loader associated with the current thread. This is
+     * done in a doPrivileged block because it is a secure method.
+     * @return the current thread's context class loader.
+     */
+    private static ClassLoader getContextClassLoader() {
+        return (ClassLoader) AccessController.doPrivileged(
+                new PrivilegedAction() {
+                    public Object run() {
+                        return Thread.currentThread().getContextClassLoader();
+                    }
+                });
+    }
+}
+

Added: incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAQueryImpl.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAQueryImpl.java?view=auto&rev=447605
==============================================================================
--- incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAQueryImpl.java
(added)
+++ incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPAQueryImpl.java
Mon Sep 18 15:36:58 2006
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2006 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.
+ */
+
+
+package org.apache.roller.business.jpa;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.roller.business.datamapper.DatamapperQuery;
+
+/**
+ *
+ */
+public class JPAQueryImpl implements DatamapperQuery {
+
+    /** */
+    protected EntityManager em;
+
+    /** */
+    protected Query q;
+
+    private boolean singleResult = false;
+
+    /**
+     * Creates a new instance of JPAQueryImpl
+     */
+    public JPAQueryImpl(EntityManager em, Class clazz, String queryName) {
+        this.em = em;
+        q = em.createNamedQuery(queryName);
+    }
+
+    public Object execute() {
+        return executeQuery();
+    }
+
+    public Object execute(Object param) {
+        q.setParameter(1, param);
+        return executeQuery();
+    }
+
+    public Object execute(Object[] params) {
+        for (int i = 0; i < params.length ; i++) {
+            q.setParameter(i + 1, params[i]);
+        }
+        return executeQuery();
+    }
+
+    public DatamapperQuery setUnique() {
+        singleResult = true;
+        return this;
+    }
+
+    public DatamapperQuery setTypes(Object[] types) {
+        //TODO: Craig, a more natural fit is to pass types also with execute
+        return this;
+    }
+
+    public DatamapperQuery setRange(long fromIncl, long toExcl) {
+        //TODO: JPA takes these as int :(.
+        q.setFirstResult((int) fromIncl);
+        q.setMaxResults((int) toExcl);
+        return this;
+    }
+
+    /**
+     * Helper that calls q.getSingleResult vs. q.getResultList depending on
+     * value of singleResult.
+     */
+    private Object executeQuery() {
+        return singleResult ? q.getSingleResult() : q.getResultList();
+    }
+
+
+}

Added: incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARemoveQueryImpl.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARemoveQueryImpl.java?view=auto&rev=447605
==============================================================================
--- incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARemoveQueryImpl.java
(added)
+++ incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARemoveQueryImpl.java
Mon Sep 18 15:36:58 2006
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2006 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.
+ */
+
+
+package org.apache.roller.business.jpa;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.roller.business.datamapper.DatamapperRemoveQuery;
+
+/**
+ *
+ */
+public class JPARemoveQueryImpl implements DatamapperRemoveQuery {
+
+    /** */
+    protected EntityManager em;
+
+    /** */
+    protected Query q;
+
+    /**
+     * Creates a new instance of JPAQueryImpl
+     */
+    public JPARemoveQueryImpl(EntityManager em, Class clazz, String queryName) {
+        this.em = em;
+        q = em.createNamedQuery(queryName);
+    }
+
+    public void removeAll() {
+        q.executeUpdate();
+    }
+
+    public void removeAll(Object param) {
+        q.setParameter(1, param);
+        q.executeUpdate();
+    }
+
+    public void removeAll(Object[] params) {
+        for (int i = 0; i < params.length ; i++) {
+            q.setParameter(i + 1, params[i]);
+        }
+        q.executeUpdate();
+    }
+
+    public DatamapperRemoveQuery setUnique() {
+        // TODO - Craig, what does setUnique mean on a RemoveQuery?
+        //q.setUnique(true);
+        return this;
+    }
+
+    public DatamapperRemoveQuery setTypes(Object[] types) {
+        return this;
+    }
+
+}
\ No newline at end of file

Added: incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARollerImpl.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARollerImpl.java?view=auto&rev=447605
==============================================================================
--- incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARollerImpl.java
(added)
+++ incubator/roller/trunk/sandbox/jdobackend/src/org/apache/roller/business/jpa/JPARollerImpl.java
Mon Sep 18 15:36:58 2006
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2006 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.
+ */
+
+package org.apache.roller.business.jpa;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.RollerException;
+import org.apache.roller.business.datamapper.DatamapperRollerImpl;
+import org.apache.roller.business.jpa.JPAPersistenceStrategy;
+import org.apache.roller.model.Roller;
+
+
+/**
+ * Implements Roller, the entry point interface for the Roller business tier
+ * APIs. JDO specific implementation.
+ */
+public class JPARollerImpl extends DatamapperRollerImpl {
+    
+    /**
+     * The logger associated with this class.
+     */
+    private static Log logger = LogFactory.getLog(JPARollerImpl.class);
+    
+    
+    /**
+     * Single constructor.
+     * @throws org.apache.roller.RollerException on any error
+     */
+    protected JPARollerImpl() throws RollerException {
+        // set strategy used by Datamapper
+        strategy = new JPAPersistenceStrategy();
+    }
+
+    
+    /**
+     * Construct and return the singleton instance of the class.
+     * @throws org.apache.roller.RollerException on any error
+     * @return the singleton
+     */
+    public static Roller instantiate() throws RollerException {
+        logger.debug("Instantiating JPARollerImpl");
+        return new JPARollerImpl();
+    }
+    
+}
\ No newline at end of file

Added: incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.jdo
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.jdo?view=auto&rev=447605
==============================================================================
--- incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.jdo (added)
+++ incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.jdo Mon Sep 18 15:36:58
2006
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+This file names all persistence capable classes in Roller.
+-->
+<jdo xmlns="http://java.sun.com/xml/ns/jdo/jdo"
+     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+     xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdo 
+	http://java.sun.com/xml/ns/jdo/jdo_2_0.xsd">
+    <package name="org.apache.roller.pojos">
+        <class name="PermissionsData">
+         	<field name="id" primary-key="true" />
+         	<field name="website" persistence-modifier="none"/>
+         </class>
+        <class name="UserData">
+        	<field name="id" primary-key="true" />
+        	<field name="roles">
+        		<collection element-type="RoleData"/>
+        	</field>
+        	<field name="permissions">
+        		<collection element-type="PermissionsData"/>
+        	</field>
+        </class>	
+        <class name="RoleData">
+        	<field name="id" primary-key="true" />
+       	</class>
+
+<!--
+        <class name="AutoPingData"/>
+        <class name="BookmarkData"/>
+        <class name="CommentData"/>
+        <class name="EntryAttributeData"/>
+        <class name="FolderData"/>
+        <class name="ObjectAuditData"/>
+        <class name="PingCategoryRestrictionData"/>
+        <class name="PingQueueEntryData"/>
+        <class name="PingTargetData"/>
+        <class name="PlanetConfigData"/>
+        <class name="PlanetEntryData"/>
+        <class name="PlanetGroupData"/>
+        <class name="PlanetSubscriptionData"/>
+        <class name="RefererData"/>
+        <class name="RollerConfigData"/>
+        <class name="RollerPropertyData"/>
+        <class name="WeblogCategoryData"/>
+        <class name="WeblogEntryData"/>
+        <class name="WebsiteData"/>
+        <class name="WebsiteDisplayData"/>
+-->        
+<!-- testing
+ -->
+    </package>
+</jdo>

Added: incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.orm
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.orm?view=auto&rev=447605
==============================================================================
--- incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.orm (added)
+++ incubator/roller/trunk/sandbox/jdobackend/web/WEB-INF/classes/package.orm Mon Sep 18 15:36:58
2006
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<orm xmlns="http://java.sun.com/xml/ns/jdo/orm"
+     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+     xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/orm 
+    http://java.sun.com/xml/ns/jdo/orm_2_0.xsd">
+<!--
+This file contains the schema information when an implementation
+has application identity.
+
+Inheritance mapping: 
+Separate table for each class in the inheritance hierarchy.
+Each table contains columns for the declared fields.
+Inheritance strategy: new-table for all classes.
+-->
+    <package name="org.apache.roller.pojos">
+
+    
+    private Set roles = new HashSet();
+    private List permissions = new ArrayList();
+
+        <class name="UserData" table="rolleruser">
+        <field name="id" column="id"/>
+        <field name="userName" column="username"/>
+        <field name="password" column="passphrase"/>
+        <field name="fullName" column="fullname"/>
+        <field name="emailAddress" column="emailaddress"/>
+        <field name="dateCreated" column="datecreated"/>
+        <field name="locale" column="locale"/>
+        <field name="timeZone" column="timeZone"/>
+        <field name="enabled" column="isenabled"/>
+            <field name="roles" mapped-by="user"/>
+            <field name="permissions" mapped-by="user"/>
+        </class>
+   
+        <class name="RoleData" table="userrole">
+        <field name="id" column="id"/>
+        <field name="userName" column="username"/>
+        <field name="role" column="rolename"/>
+        <field name="user" column="userid"/>
+        </class>
+        
+        <class name="PermissionsData" table="rolleruser">
+        <field name="id" column="id"/>
+        <field name="pending" column="pending"/>
+        <field name="permissionMask" column="permission_mask"/>
+        <field name="user" column="user_id"/>
+        </class>
+
+
+    </package>
+</orm>



Mime
View raw message