You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by st...@apache.org on 2005/02/14 16:35:41 UTC
svn commit: r153797 [6/6] - in incubator/jackrabbit/trunk: ./
applications/test/ contrib/orm-persistence/
contrib/orm-persistence/applications/
contrib/orm-persistence/applications/test/
contrib/orm-persistence/applications/test/hibernate/
contrib/orm-persistence/applications/test/ojb/
contrib/orm-persistence/applications/test/repository/
contrib/orm-persistence/applications/test/repository/nodetypes/
contrib/orm-persistence/applications/test/workspaces/
contrib/orm-persistence/applications/test/workspaces/default/
contrib/orm-persistence/docs/ contrib/orm-persistence/src/
contrib/orm-persistence/src/java/ contrib/orm-persistence/src/java/org/
contrib/orm-persistence/src/java/org/apache/
contrib/orm-persistence/src/java/org/apache/jackrabbit/
contrib/orm-persistence/src/java/org/apache/jackrabbit/core/
contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/
contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/
contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/hibernate/
contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/
contrib/orm-persistence/src/test/ contrib/orm-persistence/src/test/org/
contrib/orm-persistence/src/test/org/apache/
contrib/orm-persistence/src/test/org/apache/jackrabbit/
contrib/orm-persistence/src/test/org/apache/jackrabbit/test/
contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/
contrib/orm-persistence/target/ src/java/org/apache/jackrabbit/core/
Added: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/hibernate/HibernatePersistenceManager.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/hibernate/HibernatePersistenceManager.java?view=auto&rev=153797
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/hibernate/HibernatePersistenceManager.java (added)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/hibernate/HibernatePersistenceManager.java Mon Feb 14 07:35:27 2005
@@ -0,0 +1,703 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * 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.jackrabbit.core.state.orm.hibernate;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import javax.jcr.PropertyType;
+
+import org.apache.jackrabbit.core.BLOBFileValue;
+import org.apache.jackrabbit.core.InternalValue;
+import org.apache.jackrabbit.core.NodeId;
+import org.apache.jackrabbit.core.PropertyId;
+import org.apache.jackrabbit.core.QName;
+import org.apache.jackrabbit.core.state.AbstractPersistenceManager;
+import org.apache.jackrabbit.core.state.ItemState;
+import org.apache.jackrabbit.core.state.ItemStateException;
+import org.apache.jackrabbit.core.state.NoSuchItemStateException;
+import org.apache.jackrabbit.core.state.NodeReferences;
+import org.apache.jackrabbit.core.state.NodeReferencesId;
+import org.apache.jackrabbit.core.state.NodeState;
+import org.apache.jackrabbit.core.state.PMContext;
+import org.apache.jackrabbit.core.state.PropertyState;
+import org.apache.jackrabbit.core.state.orm.ORMBlobValue;
+import org.apache.jackrabbit.core.state.orm.ORMNodeReference;
+import org.apache.jackrabbit.core.state.orm.ORMPropertyState;
+import org.apache.log4j.Logger;
+import net.sf.hibernate.Hibernate;
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.ObjectNotFoundException;
+import net.sf.hibernate.Session;
+import net.sf.hibernate.SessionFactory;
+import net.sf.hibernate.Transaction;
+import net.sf.hibernate.cfg.Configuration;
+import net.sf.hibernate.type.Type;
+
+/**
+ * Hibernate implementation of a Jackrabbit persistence manager.
+ */
+public class HibernatePersistenceManager extends AbstractPersistenceManager {
+
+ private static Logger log = Logger.getLogger(HibernatePersistenceManager.class);
+
+ private SessionFactory sessionFactory;
+
+ public HibernatePersistenceManager() {
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#init
+ */
+ public void init(PMContext context) throws Exception {
+ try {
+ // Create the SessionFactory
+ sessionFactory = new Configuration().configure().
+ buildSessionFactory();
+ } catch (Throwable ex) {
+ log.error("Initial SessionFactory creation failed.", ex);
+ throw new ExceptionInInitializerError(ex);
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#close
+ */
+ public void close() throws Exception {
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#load(NodeId)
+ */
+ public NodeState load(NodeId nodeId) throws NoSuchItemStateException,
+ ItemStateException {
+ log.debug("Request for " + nodeId.getUUID());
+
+ NodeState state = null;
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ List nodeList = session.find(
+ "from org.apache.jackrabbit.core.state.orm.hibernate.HibernateNodeState as node WHERE " +
+ "node.uuid = ?",
+ new Object[] {nodeId.getUUID()},
+ new Type[] {Hibernate.STRING});
+
+ tx.commit();
+ if (nodeList.size() != 1) {
+ throw new NoSuchItemStateException("Couldn't find unique node " +
+ nodeId.getUUID() + ", found " +
+ nodeList.size() +
+ " results instead");
+ }
+ HibernateNodeState result = (HibernateNodeState) nodeList.get(0);
+ state = createNew(nodeId);
+ result.toPersistentNodeState(state);
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException("Error loading " + nodeId.getUUID(),
+ he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ return state;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#load(PropertyId)
+ */
+ public PropertyState load(PropertyId propId) throws
+ NoSuchItemStateException, ItemStateException {
+
+ PropertyState state = null;
+ Session session = null;
+ try {
+ session = sessionFactory.openSession();
+ Transaction tx = session.beginTransaction();
+ ORMPropertyState propState = null;
+ try {
+
+ List propertyList = session.find(
+ "from org.apache.jackrabbit.core.state.orm.ORMPropertyState as prop WHERE " +
+ "prop.parentUUID = ? and prop.name = ?",
+ new Object[] {propId.getParentUUID(),
+ propId.getName().toString()},
+ new Type[] {Hibernate.STRING, Hibernate.STRING});
+
+ tx.commit();
+ if (propertyList.size() != 1) {
+ throw new NoSuchItemStateException(
+ "Couldn't find unique property " + propId + ", found " +
+ propertyList.size() + " results instead");
+ }
+ propState = (ORMPropertyState) propertyList.get(0);
+ state = createNew(propId);
+ propState.toPersistentPropertyState(state);
+ if (propState.getType().intValue() == PropertyType.BINARY) {
+ // we must now load the binary values.
+ ArrayList internalValueList = new ArrayList();
+ List blobValueList = session.find(
+ "from org.apache.jackrabbit.core.state.orm.ORMBlobValue as bv WHERE " +
+ "bv.parentUUID = ? and bv.propertyName = ?",
+ new Object[] {propId.getParentUUID(),
+ propId.getName().toString()},
+ new Type[] {Hibernate.STRING, Hibernate.STRING});
+
+ Iterator resultIter = blobValueList.iterator();
+ while (resultIter.hasNext()) {
+ ORMBlobValue ormBlobValue = (ORMBlobValue) resultIter.
+ next();
+ ByteArrayInputStream in = new ByteArrayInputStream(
+ ormBlobValue.getBlobValue());
+ try {
+ BLOBFileValue blobValue = new BLOBFileValue(in);
+ internalValueList.add(blobValue);
+ } catch (Throwable t) {
+ throw new ItemStateException(
+ "Error while trying to load blob value", t);
+ }
+ }
+ state.setValues( (InternalValue[]) internalValueList.
+ toArray(new
+ InternalValue[internalValueList.
+ size()]));
+ }
+ } catch (ObjectNotFoundException onfe) {
+ throw new NoSuchItemStateException("Couldn't find " + propId,
+ onfe);
+ }
+ } catch (HibernateException he) {
+ throw new ItemStateException("Error loading " + propId, he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ return state;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#load(NodeReferencesId)
+ */
+ public NodeReferences load(NodeReferencesId targetId) throws
+ NoSuchItemStateException, ItemStateException {
+ log.debug("Loading node references for targetId=" +
+ targetId.toString());
+ NodeReferences refs = null;
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ Iterator nodeRefIter = session.iterate("from org.apache.jackrabbit.core.state.orm.ORMNodeReference as nf where nf.targetId='" +
+ targetId.toString() +
+ "'");
+ refs = new NodeReferences(targetId);
+ while (nodeRefIter.hasNext()) {
+ ORMNodeReference curNodeReference = (ORMNodeReference)
+ nodeRefIter.
+ next();
+ refs.addReference(new PropertyId(curNodeReference.
+ getPropertyParentUUID(),
+ QName.
+ valueOf(curNodeReference.
+ getPropertyName())));
+ }
+ tx.commit();
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ log.error("Error while loading node reference for targetId=" +
+ targetId.toString(), he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ return refs;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(NodeId)
+ */
+ public boolean exists(NodeId id) throws ItemStateException {
+ HibernateNodeState result = null;
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ List nodeList = session.find(
+ "from org.apache.jackrabbit.core.state.orm.hibernate.HibernateNodeState as node WHERE " +
+ "node.uuid = ?",
+ new Object[] {id.toString()},
+ new Type[] {Hibernate.STRING});
+
+ tx.commit();
+ if (nodeList.size() < 1) {
+ return false;
+ } else {
+ if (nodeList.size() > 1) {
+ log.warn("Node " + id +
+ " exists more than once in database !");
+ }
+ return true;
+ }
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException("Error loading " + id, he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(PropertyId)
+ */
+ public boolean exists(PropertyId id) throws ItemStateException {
+ boolean result = false;
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ ORMPropertyState propState = null;
+ PropertyId propId = (PropertyId) id;
+ List propertyList = session.find(
+ "from org.apache.jackrabbit.core.state.orm.ORMPropertyState as prop WHERE " +
+ "prop.parentUUID = ? and prop.name = ?",
+ new Object[] {propId.getParentUUID(),
+ propId.getName().toString()},
+ new Type[] {Hibernate.STRING, Hibernate.STRING});
+
+ tx.commit();
+ if (propertyList.size() < 1) {
+ return false;
+ } else {
+ if (propertyList.size() > 1) {
+ log.warn("Property " + id +
+ " exists more than once in database !");
+ }
+ return true;
+ }
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException("Error loading " + id, he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(NodeReferencesId)
+ */
+ public boolean exists(NodeReferencesId targetId) throws ItemStateException {
+ boolean result = false;
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ Iterator nodeRefIter = session.iterate("from org.apache.jackrabbit.core.state.orm.ORMNodeReference as nf where nf.targetId='" +
+ targetId.toString() +
+ "'");
+ NodeReferences refs = new NodeReferences(targetId);
+ if (nodeRefIter.hasNext()) {
+ result = true;
+ }
+ tx.commit();
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException(
+ "Error while testing reference existence for targetId=" +
+ targetId, he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(NodeState)
+ */
+ public void store(NodeState state) throws ItemStateException {
+ log.debug("Request to store " + state.getId());
+ boolean isUpdate = true;
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ HibernateNodeState nodeState = new HibernateNodeState(state);
+ if (state.getStatus() == ItemState.STATUS_NEW) {
+ session.save(nodeState);
+ } else {
+ session.update(nodeState);
+ }
+ tx.commit();
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException("Error saving " + state.getId(), he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(PropertyState)
+ */
+ public void store(PropertyState state) throws ItemStateException {
+ log.debug("Request to store " + state.getId());
+ boolean isUpdate = true;
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ ORMPropertyState propState = new ORMPropertyState(state);
+
+ InternalValue[] values = state.getValues();
+ if (values != null) {
+ for (int i = 0; i < values.length; i++) {
+
+ // first we delete any existing blob values (this is faster
+ // than trying to load and update each value seperately)
+ session.delete("from org.apache.jackrabbit.core.state.orm.ORMBlobValue as bv where bv.parentUUID=? AND bv.propertyName=?",
+ new Object[] {state.getParentUUID(),
+ state.getName().toString()},
+ new Type[] {Hibernate.STRING,
+ Hibernate.STRING});
+
+ InternalValue val = values[i];
+ if (val != null) {
+ if (state.getType() == PropertyType.BINARY) {
+ ORMBlobValue ormBlobValue = null;
+ ormBlobValue = new ORMBlobValue();
+ ormBlobValue.setParentUUID(state.getParentUUID());
+ ormBlobValue.setPropertyName(state.getName().
+ toString());
+ ormBlobValue.setIndex(new Integer(i));
+ BLOBFileValue blobVal = (BLOBFileValue) val.
+ internalValue();
+ propState.setValues("");
+ ByteArrayOutputStream out = new
+ ByteArrayOutputStream();
+ try {
+ blobVal.spool(out);
+ } catch (Throwable t) {
+ tx.rollback();
+ session.close();
+ throw new ItemStateException(t.getMessage(), t);
+ }
+ ormBlobValue.setSize(new Long(blobVal.getLength()));
+ ormBlobValue.setBlobValue(out.toByteArray());
+ session.save(ormBlobValue);
+ }
+ }
+ }
+ }
+
+ if (state.getStatus() == ItemState.STATUS_NEW) {
+ session.save(propState);
+ } else {
+ session.update(propState);
+ }
+ tx.commit();
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException("Error saving " + state.getId(), he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(NodeReferences)
+ */
+ public void store(NodeReferences refs) throws ItemStateException {
+ Iterator nodeRefPropIdIter = refs.getReferences().iterator();
+ log.debug("Request to store node references for targetId=" +
+ refs.getTargetId());
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ int i = 0;
+ while (nodeRefPropIdIter.hasNext()) {
+ PropertyId curPropertyId = (PropertyId) nodeRefPropIdIter.next();
+ ORMNodeReference curNodeReference = new ORMNodeReference(refs.
+ getTargetId().toString(), curPropertyId.getParentUUID(),
+ curPropertyId.getName().toString());
+ session.save(curNodeReference);
+ i++;
+ if (i % 20 == 0) {
+ session.flush();
+ session.clear();
+ }
+ }
+ tx.commit();
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException(
+ "Error storing node references for targetId=" +
+ refs.getTargetId(), he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(NodeState)
+ */
+ public void destroy(NodeState state) throws ItemStateException {
+ log.debug("Deleting node " + state.getUUID());
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ HibernateNodeState nodeState = null;
+ try {
+ List nodeList = session.find(
+ "from org.apache.jackrabbit.core.state.orm.hibernate.HibernateNodeState as node WHERE " +
+ "node.uuid = ?",
+ new Object[] {state.getId().toString()},
+ new Type[] {Hibernate.STRING});
+
+ if (nodeList.size() != 1) {
+ } else {
+ nodeState = (HibernateNodeState) nodeList.get(0);
+ session.delete(nodeState);
+ }
+ } catch (ObjectNotFoundException onfe) {
+ }
+ tx.commit();
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException("Error deleting " + state.getId(), he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(PropertyState)
+ */
+ public void destroy(PropertyState state) throws ItemStateException {
+ log.debug("Deleting property " + state.getId());
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ ORMPropertyState propState = null;
+ try {
+ List propertyList = session.find(
+ "from org.apache.jackrabbit.core.state.orm.ORMPropertyState as prop WHERE " +
+ "prop.itemId = ?",
+ new Object[] {state.getId().toString()},
+ new Type[] {Hibernate.STRING});
+
+ if (propertyList.size() != 1) {
+ } else {
+ propState = (ORMPropertyState) propertyList.get(0);
+ session.delete(propState);
+ if (state.getType() == PropertyType.BINARY) {
+ session.delete("from org.apache.jackrabbit.core.state.orm.ORMBlobValue as bv where bv.parentUUID=? AND bv.propertyName=?",
+ new Object[] {state.getParentUUID(),
+ state.getName().toString()},
+ new Type[] {Hibernate.STRING,
+ Hibernate.STRING});
+ }
+ }
+ } catch (ObjectNotFoundException onfe) {
+ }
+ tx.commit();
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException("Error deleting " + state.getId(), he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(NodeReferences)
+ */
+ public void destroy(NodeReferences refs) throws ItemStateException {
+ log.debug("Deleting node refences for targetId=" +
+ refs.getTargetId().toString());
+ Session session = null;
+ Transaction tx = null;
+ try {
+ session = sessionFactory.openSession();
+ tx = session.beginTransaction();
+ session.delete("from org.apache.jackrabbit.core.state.orm.ORMNodeReference as nf where nf.targetId='" +
+ refs.getTargetId().toString() +
+ "'");
+ refs.clearAllReferences();
+ tx.commit();
+ } catch (HibernateException he) {
+ try {
+ if (tx != null)
+ tx.rollback();
+ } catch (HibernateException he2) {
+ log.error("Error while rolling back transaction", he2);
+ }
+ throw new ItemStateException(
+ "Error deleting node references for targetId=" +
+ refs.getTargetId().toString(), he);
+ } finally {
+ if (session != null) {
+ try {
+ session.close();
+ } catch (HibernateException he) {
+ throw new ItemStateException(
+ "Error while closing hibernate session", he);
+ }
+ }
+ }
+ }
+
+}
Propchange: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/hibernate/HibernatePersistenceManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBNodeState.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBNodeState.java?view=auto&rev=153797
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBNodeState.java (added)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBNodeState.java Mon Feb 14 07:35:27 2005
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * 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.jackrabbit.core.state.orm.ojb;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.jackrabbit.core.ItemId;
+import org.apache.jackrabbit.core.state.NodeState;
+import org.apache.jackrabbit.core.state.orm.ORMNodeState;
+import org.apache.ojb.broker.util.collections.RemovalAwareList;
+
+/**
+ * <p> OJB specific node state. In order to properly track list
+ * modifications, we use an OJB specific list implementation.</p>
+ */
+public class OJBNodeState extends ORMNodeState implements Serializable {
+
+ private List awareChildNodeEntries = new RemovalAwareList();
+ private List awarePropertyEntries = new RemovalAwareList();
+ private List awareMixinTypeNames = new RemovalAwareList();
+ private List awareParentUUIDs = new RemovalAwareList();
+
+ public OJBNodeState() {
+ }
+ public OJBNodeState(ItemId id) {
+ super(id);
+ }
+ public OJBNodeState(NodeState state) {
+ super();
+ fromPersistentNodeState(state);
+ }
+ public Collection getChildNodeEntries() {
+ return awareChildNodeEntries;
+ }
+ public void setChildNodeEntries(Collection childNodeEntries) {
+ this.awareChildNodeEntries.clear();
+ this.awareChildNodeEntries.addAll(childNodeEntries);
+ }
+
+ public List getAwareChildNodeEntries() {
+ return awareChildNodeEntries;
+ }
+
+ public void setAwareChildNodeEntries(List awareChildNodeEntries) {
+ this.awareChildNodeEntries = awareChildNodeEntries;
+ }
+
+ public Collection getPropertyEntries() {
+ return awarePropertyEntries;
+ }
+
+ public Collection getMixinTypeNames() {
+ return awareMixinTypeNames;
+ }
+
+ public Collection getParentUUIDs() {
+ return awareParentUUIDs;
+ }
+
+ public void setPropertyEntries(Collection propertyEntries) {
+ this.awarePropertyEntries.clear();
+ this.awarePropertyEntries.addAll(propertyEntries);
+ }
+
+ public void setMixinTypeNames(Collection mixinTypeNames) {
+ this.awareMixinTypeNames.clear();
+ this.awareMixinTypeNames.addAll(mixinTypeNames);
+ }
+
+ public void setParentUUIDs(Collection parentUUIDs) {
+ this.awareParentUUIDs.clear();
+ this.awareParentUUIDs.addAll(parentUUIDs);
+ }
+
+}
Propchange: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBNodeState.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java?view=auto&rev=153797
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java (added)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java Mon Feb 14 07:35:27 2005
@@ -0,0 +1,354 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * 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.jackrabbit.core.state.orm.ojb;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import javax.jcr.PropertyType;
+
+import org.apache.jackrabbit.core.BLOBFileValue;
+import org.apache.jackrabbit.core.InternalValue;
+import org.apache.jackrabbit.core.NodeId;
+import org.apache.jackrabbit.core.PropertyId;
+import org.apache.jackrabbit.core.QName;
+import org.apache.jackrabbit.core.state.AbstractPersistenceManager;
+import org.apache.jackrabbit.core.state.ItemStateException;
+import org.apache.jackrabbit.core.state.NoSuchItemStateException;
+import org.apache.jackrabbit.core.state.NodeReferences;
+import org.apache.jackrabbit.core.state.NodeReferencesId;
+import org.apache.jackrabbit.core.state.NodeState;
+import org.apache.jackrabbit.core.state.PMContext;
+import org.apache.jackrabbit.core.state.PropertyState;
+import org.apache.jackrabbit.core.state.orm.ORMBlobValue;
+import org.apache.jackrabbit.core.state.orm.ORMNodeReference;
+import org.apache.jackrabbit.core.state.orm.ORMPropertyState;
+import org.apache.log4j.Logger;
+import org.apache.ojb.broker.PersistenceBroker;
+import org.apache.ojb.broker.PersistenceBrokerFactory;
+import org.apache.ojb.broker.query.Criteria;
+import org.apache.ojb.broker.query.QueryByCriteria;
+import org.apache.ojb.broker.query.QueryByIdentity;
+
+/**
+ * OJB implementation of a Jackrabbit persistence manager.
+ */
+public class OJBPersistenceManager extends AbstractPersistenceManager {
+
+ private static Logger log = Logger.getLogger(OJBPersistenceManager.class);
+
+ private PersistenceBroker broker = null;
+ private boolean initialized = false;
+
+ public OJBPersistenceManager() {
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#init
+ */
+ public void init(PMContext context) throws Exception {
+ if (initialized) {
+ throw new IllegalStateException("already initialized");
+ }
+ broker = PersistenceBrokerFactory.defaultPersistenceBroker();
+ initialized = true;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#close
+ */
+ public void close() throws Exception {
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#load(NodeId)
+ */
+ public NodeState load(NodeId nodeId) throws NoSuchItemStateException,
+ ItemStateException {
+ log.debug("Request for " + nodeId.getUUID());
+ OJBNodeState nodeState = new OJBNodeState();
+ nodeState.setUuid(nodeId.getUUID());
+ QueryByIdentity query = new QueryByIdentity(nodeState);
+ OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
+ if (result == null) {
+ throw new NoSuchItemStateException(nodeId.getUUID());
+ }
+ NodeState state = createNew(nodeId);
+ result.toPersistentNodeState(state);
+ return state;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#load(PropertyId)
+ */
+ public PropertyState load(PropertyId propId) throws
+ NoSuchItemStateException, ItemStateException {
+ log.debug("Request for property " + propId);
+ ORMPropertyState propState = new ORMPropertyState(propId);
+ QueryByIdentity query = new QueryByIdentity(propState);
+ PropertyState state = createNew(propId);
+ ORMPropertyState result = (ORMPropertyState) broker.getObjectByQuery(
+ query);
+ if (result == null) {
+ throw new NoSuchItemStateException("Couldn't find property " +
+ propId);
+ }
+ result.toPersistentPropertyState(state);
+ if (result.getType().intValue() == PropertyType.BINARY) {
+ // we must now load the binary values.
+ ArrayList internalValueList = new ArrayList();
+ Criteria criteria = new Criteria();
+ criteria.addEqualTo("parentUUID", state.getParentUUID());
+ criteria.addEqualTo("propertyName", state.getName().toString());
+ QueryByCriteria blobQuery = new QueryByCriteria(ORMBlobValue.class,
+ criteria);
+ Iterator resultIter = broker.getCollectionByQuery(blobQuery).
+ iterator();
+ while (resultIter.hasNext()) {
+ ORMBlobValue ormBlobValue = (ORMBlobValue) resultIter.next();
+ ByteArrayInputStream in = new ByteArrayInputStream(ormBlobValue.
+ getBlobValue());
+ try {
+ BLOBFileValue blobValue = new BLOBFileValue(in);
+ internalValueList.add(blobValue);
+ } catch (Throwable t) {
+ throw new ItemStateException(
+ "Error while trying to load blob value", t);
+ }
+ }
+ state.setValues( (InternalValue[]) internalValueList.toArray(new
+ InternalValue[internalValueList.size()]));
+ }
+ return state;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#load(NodeReferencesId)
+ */
+ public NodeReferences load(NodeReferencesId targetId) throws
+ NoSuchItemStateException, ItemStateException {
+ ORMNodeReference nodeRef = new ORMNodeReference();
+ nodeRef.setTargetId(targetId.toString());
+ QueryByCriteria query = new QueryByCriteria(nodeRef);
+ Iterator resultIter = broker.getCollectionByQuery(query).iterator();
+ NodeReferences refs = new NodeReferences(targetId);
+ while (resultIter.hasNext()) {
+ ORMNodeReference curNodeReference = (ORMNodeReference) resultIter.
+ next();
+ refs.addReference(new PropertyId(curNodeReference.
+ getPropertyParentUUID(),
+ QName.valueOf(curNodeReference.
+ getPropertyName())));
+ }
+ return refs;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(NodeId)
+ */
+ public boolean exists(NodeId id) throws ItemStateException {
+ OJBNodeState nodeState = new OJBNodeState(id);
+ QueryByIdentity query = new QueryByIdentity(nodeState);
+ OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
+ if (result == null) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(PropertyId)
+ */
+ public boolean exists(PropertyId id) throws ItemStateException {
+ ORMPropertyState propState = new ORMPropertyState(id);
+ // QueryByCriteria query = new QueryByCriteria(propState);
+ QueryByIdentity query = new QueryByIdentity(propState);
+ ORMPropertyState result = (ORMPropertyState) broker.
+ getObjectByQuery(query);
+ if (result == null) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.PersistenceManager#exists(NodeReferencesId)
+ */
+ public boolean exists(NodeReferencesId targetId)
+ throws ItemStateException {
+ ORMNodeReference nodeRef = new ORMNodeReference();
+ nodeRef.setTargetId(targetId.toString());
+ QueryByCriteria query = new QueryByCriteria(nodeRef);
+ Iterator resultIter = broker.getCollectionByQuery(query).iterator();
+ NodeReferences refs = new NodeReferences(targetId);
+ if (resultIter.hasNext()) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(NodeState)
+ */
+ public void store(NodeState state) throws ItemStateException {
+ log.debug("Request to store node " + state.getId());
+ broker.beginTransaction();
+ OJBNodeState nodeState = new OJBNodeState(state.getId());
+ QueryByIdentity query = new QueryByIdentity(nodeState);
+ OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
+ if (result == null) {
+ result = new OJBNodeState();
+ }
+ result.fromPersistentNodeState(state);
+ broker.store(result);
+ broker.commitTransaction();
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(PropertyState)
+ */
+ public void store(PropertyState state) throws ItemStateException {
+ log.debug("Request to store property " + state.getId());
+ broker.beginTransaction();
+ ORMPropertyState propState = new ORMPropertyState(state.getId());
+ QueryByIdentity query = new QueryByIdentity(propState);
+ ORMPropertyState result = (ORMPropertyState) broker.getObjectByQuery(
+ query);
+ if (result == null) {
+ result = new ORMPropertyState();
+ }
+ result.fromPersistentPropertyState(state);
+
+ InternalValue[] values = state.getValues();
+ if (values != null) {
+ for (int i = 0; i < values.length; i++) {
+ InternalValue val = values[i];
+ if (val != null) {
+ if (state.getType() == PropertyType.BINARY) {
+ Criteria criteria = new Criteria();
+ criteria.addEqualTo("parentUUID", state.getParentUUID());
+ criteria.addEqualTo("propertyName",
+ state.getName().toString());
+ criteria.addEqualTo("index", new Integer(i));
+ QueryByCriteria blobQuery = new QueryByCriteria(
+ ORMBlobValue.class, criteria);
+ Iterator resultIter = broker.getCollectionByQuery(
+ blobQuery).iterator();
+ ORMBlobValue ormBlobValue = null;
+ if (resultIter.hasNext()) {
+ ormBlobValue = (ORMBlobValue) resultIter.next();
+ } else {
+ ormBlobValue = new ORMBlobValue();
+ ormBlobValue.setParentUUID(state.getParentUUID());
+ ormBlobValue.setPropertyName(state.getName().
+ toString());
+ ormBlobValue.setIndex(new Integer(i));
+ }
+ BLOBFileValue blobVal = (BLOBFileValue) val.
+ internalValue();
+ result.setValues("");
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try {
+ blobVal.spool(out);
+ } catch (Throwable t) {
+ broker.abortTransaction();
+ throw new ItemStateException(t.getMessage(), t);
+ }
+ ormBlobValue.setSize(new Long(blobVal.getLength()));
+ ormBlobValue.setBlobValue(out.toByteArray());
+ broker.store(ormBlobValue);
+ }
+ }
+ }
+ }
+ broker.store(result);
+ broker.commitTransaction();
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#store(NodeReferences)
+ */
+ public void store(NodeReferences refs) throws ItemStateException {
+ Iterator nodeRefPropIdIter = refs.getReferences().iterator();
+ broker.beginTransaction();
+ while (nodeRefPropIdIter.hasNext()) {
+ PropertyId curPropertyId = (PropertyId) nodeRefPropIdIter.next();
+ ORMNodeReference curNodeReference = new ORMNodeReference(refs.
+ getTargetId().toString(), curPropertyId.getParentUUID(),
+ curPropertyId.getName().toString());
+ broker.store(curNodeReference);
+ }
+ broker.commitTransaction();
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(NodeState)
+ */
+ public void destroy(NodeState state) throws ItemStateException {
+ log.debug("Deleting node " + state.getId());
+ broker.beginTransaction();
+ OJBNodeState nodeState = new OJBNodeState(state.getId());
+ QueryByIdentity query = new QueryByIdentity(nodeState);
+ OJBNodeState result = (OJBNodeState) broker.getObjectByQuery(query);
+ if (result == null) {
+ result = new OJBNodeState();
+ }
+ broker.delete(result);
+ broker.commitTransaction();
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(PropertyState)
+ */
+ public void destroy(PropertyState state) throws
+ ItemStateException {
+ ORMPropertyState propState = new ORMPropertyState(state);
+ broker.beginTransaction();
+ broker.delete(propState);
+ if (state.getType() == PropertyType.BINARY) {
+ Criteria criteria = new Criteria();
+ criteria.addEqualTo("parentUUID", state.getParentUUID());
+ criteria.addEqualTo("propertyName", state.getName().toString());
+ QueryByCriteria blobQuery = new QueryByCriteria(ORMBlobValue.class,
+ criteria);
+ broker.deleteByQuery(blobQuery);
+ }
+ broker.commitTransaction();
+ }
+
+ /**
+ * @see org.apache.jackrabbit.core.state.AbstractPersistenceManager#destroy(NodeReferences)
+ */
+ public void destroy(NodeReferences refs) throws ItemStateException {
+ broker.beginTransaction();
+ ORMNodeReference nodeRef = new ORMNodeReference();
+ nodeRef.setTargetId(refs.getTargetId().toString());
+ QueryByCriteria query = new QueryByCriteria(nodeRef);
+ Iterator resultIter = broker.getCollectionByQuery(query).iterator();
+ while (resultIter.hasNext()) {
+ ORMNodeReference curNodeReference = (ORMNodeReference) resultIter.
+ next();
+ broker.delete(curNodeReference);
+ }
+ broker.commitTransaction();
+ }
+
+
+}
Propchange: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/OJBPersistenceManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/ValuesToStringFieldConversion.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/ValuesToStringFieldConversion.java?view=auto&rev=153797
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/ValuesToStringFieldConversion.java (added)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/ValuesToStringFieldConversion.java Mon Feb 14 07:35:27 2005
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * 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.jackrabbit.core.state.orm.ojb;
+
+import java.util.ArrayList;
+import java.util.StringTokenizer;
+
+import org.apache.jackrabbit.core.InternalValue;
+import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
+import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
+
+/**
+ * <p> Helper class to convert multi-valued properties into an encoded
+ * string stored in a single database column.</p>
+ */
+public class ValuesToStringFieldConversion
+ implements FieldConversion {
+
+ private int type;
+
+ public ValuesToStringFieldConversion() {
+ }
+
+ public ValuesToStringFieldConversion(int type) {
+ this.type = type;
+ }
+
+ public Object javaToSql(Object object) throws ConversionException {
+ InternalValue[] values = (InternalValue[]) object;
+ StringBuffer buffer = new StringBuffer();
+ for (int i=0; i < values.length; i++) {
+ buffer.append(values[i].toString());
+ if (i < values.length - 1) {
+ buffer.append(",");
+ }
+ }
+ return buffer.toString();
+ }
+
+ public Object sqlToJava(Object object) throws ConversionException {
+ ArrayList valueList = new ArrayList();
+ StringTokenizer tokenizer = new StringTokenizer((String) object, ",");
+ while (tokenizer.hasMoreTokens()) {
+ InternalValue curValue = InternalValue.valueOf(tokenizer.nextToken(), type);
+ valueList.add(curValue);
+ }
+ return (InternalValue[]) valueList.toArray(new InternalValue[valueList.size()]);
+ }
+}
Propchange: incubator/jackrabbit/trunk/contrib/orm-persistence/src/java/org/apache/jackrabbit/core/state/orm/ojb/ValuesToStringFieldConversion.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/BlobTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/BlobTest.java?view=auto&rev=153797
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/BlobTest.java (added)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/BlobTest.java Mon Feb 14 07:35:27 2005
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * 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.jackrabbit.test.orm;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import java.io.InputStream;
+import javax.jcr.StringValue;
+import java.io.ByteArrayInputStream;
+import java.io.BufferedInputStream;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+
+/**
+ * BLOB insertion/retrieval test case.
+ */
+public class BlobTest extends AbstractJCRTest {
+
+ private final static int BLOB_SIZE = 1*1024*1024;
+ byte[] blobContent = new byte[BLOB_SIZE];
+
+ public BlobTest() {
+ }
+
+ public void testBlob() throws Exception {
+ Node rn = superuser.getRootNode();
+
+ NodeIterator nodeIter = rn.getNodes();
+ while (nodeIter.hasNext()) {
+ Node curNode = nodeIter.nextNode();
+ }
+
+ log.println("Creating BLOB data of " + BLOB_SIZE + " bytes...");
+ for (int i=0; i < BLOB_SIZE; i++) {
+ blobContent[i] = (byte) (i % 256);
+ }
+ log.println("Adding BLOB node...");
+ if (!rn.hasNode("blobnode")) {
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(blobContent);
+ Node n = rn.addNode("blobnode", "nt:unstructured");
+ n.setProperty("testprop", new StringValue("Hello, World."));
+ n.setProperty("blobTest", inputStream);
+ superuser.save();
+ }
+ log.println("Verifying BLOB node...");
+ InputStream readInputStream = rn.getProperty("blobnode/blobTest").
+ getStream();
+ int ch = -1;
+ int i=0;
+ BufferedInputStream buf = new BufferedInputStream(readInputStream);
+ while ((ch = buf.read()) != -1) {
+ assertEquals((byte) ch, blobContent[i]);
+ i++;
+ }
+ log.println("Removing BLOB node...");
+ rn.getNode("blobnode").remove();
+ superuser.save();
+ }
+
+}
Propchange: incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/BlobTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/TestAll.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/TestAll.java?view=auto&rev=153797
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/TestAll.java (added)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/TestAll.java Mon Feb 14 07:35:27 2005
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * 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.jackrabbit.test.orm;
+
+import junit.framework.TestCase;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Test suite that includes all testcases for the package
+ * <code>javax.jcr</code>.
+ */
+public class TestAll extends TestCase {
+
+ /**
+ * Returns a <code>Test</code> suite that executes all tests inside this
+ * package.
+ *
+ * @return a <code>Test</code> suite that executes all tests inside this
+ * package.
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite("Jackrabbit ORM persistence tests");
+
+ suite.addTestSuite(BlobTest.class);
+ suite.addTestSuite(XMLImportSpeedTest.class);
+
+ return suite;
+ }
+}
Propchange: incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/TestAll.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/XMLImportSpeedTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/XMLImportSpeedTest.java?view=auto&rev=153797
==============================================================================
--- incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/XMLImportSpeedTest.java (added)
+++ incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/XMLImportSpeedTest.java Mon Feb 14 07:35:27 2005
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * 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.jackrabbit.test.orm;
+
+import java.io.FileInputStream;
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.dom4j.Document;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+
+/**
+ * Test suite for XML import speed benchmarking.
+ */
+public class XMLImportSpeedTest extends AbstractJCRTest {
+
+ private static final int BLOB_NODE_COUNT = 2;
+ private static final int XML_IMPORT_NODE_COUNT = 1;
+
+ private final String XML_IMPORT_FILENAME = "applications/test/import.xml";
+
+ private int totalNodeCount = 0;
+ private int totalElementCount = 0;
+ private int totalAttributeCount = 0;
+
+ public void treeWalk(Document document) {
+ treeWalk(document.getRootElement());
+ }
+
+ public void treeWalk(Element element) {
+ totalElementCount++;
+ totalAttributeCount += element.attributeCount();
+ for (int i = 0, size = element.nodeCount(); i < size; i++) {
+ totalNodeCount++;
+ org.dom4j.Node node = element.node(i);
+ if (node instanceof Element) {
+ treeWalk( (Element) node);
+ } else {
+ // do something....
+ }
+ }
+ }
+
+ public void testXMLImportSpeed() throws Exception {
+ Node rn = superuser.getRootNode();
+
+ if (!rn.hasNode("importxml0")) {
+ log.println("importing xml");
+
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(new FileInputStream(
+ XML_IMPORT_FILENAME));
+ treeWalk(document);
+ log.println("XML file " + XML_IMPORT_FILENAME + " has " +
+ totalElementCount + " elements, " + totalNodeCount +
+ " nodes and " + totalAttributeCount +
+ " attributes");
+
+ log.println("Now performing XML import " +
+ XML_IMPORT_NODE_COUNT +
+ " time(s)...");
+ long xmlImportTestStart = System.currentTimeMillis();
+ for (int i = 0; i < XML_IMPORT_NODE_COUNT; i++) {
+ Node nl = rn.addNode("importxml" + i, "nt:unstructured");
+ superuser.importXML("/importxml" + i,
+ new FileInputStream(
+ XML_IMPORT_FILENAME));
+ log.println("Saving...");
+ superuser.save();
+ }
+ long xmlImportTestTime = System.currentTimeMillis() -
+ xmlImportTestStart;
+ log.println("Imported XML " + XML_IMPORT_NODE_COUNT +
+ " time(s) in " +
+ xmlImportTestTime + "ms average=" +
+ xmlImportTestTime / XML_IMPORT_NODE_COUNT +
+ "ms/node");
+
+ log.println("Removing imported node(s)...");
+ for (int i = 0; i < XML_IMPORT_NODE_COUNT; i++) {
+ Node curXMLImportNode = rn.getNode("importxml" + i);
+ curXMLImportNode.remove();
+ }
+ superuser.save();
+ } else {
+ log.println(
+ "XML import has already been run previously, not reimporting on same nodes");
+ }
+
+ // dump(rn);
+
+ }
+
+ public void dump(Node n) throws RepositoryException {
+ log.println(n.getPath());
+ PropertyIterator pit = n.getProperties();
+ while (pit.hasNext()) {
+ Property p = pit.nextProperty();
+ if (!p.getDefinition().isMultiple()) {
+ Value curValue = p.getValue();
+ if (curValue.getType() == PropertyType.BINARY) {
+ log.println(p.getPath() + "=BINARY[" + p.getLength() + "]");
+ } else {
+ log.println(p.getPath() + "=" + p.getString());
+ }
+ } else {
+ log.println("Multi-value for " + p.getPath());
+ }
+ }
+ NodeIterator nit = n.getNodes();
+ while (nit.hasNext()) {
+ Node cn = nit.nextNode();
+ dump(cn);
+ }
+ }
+
+}
Propchange: incubator/jackrabbit/trunk/contrib/orm-persistence/src/test/org/apache/jackrabbit/test/orm/XMLImportSpeedTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/jackrabbit/trunk/project.xml
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/project.xml?view=diff&r1=153796&r2=153797
==============================================================================
--- incubator/jackrabbit/trunk/project.xml (original)
+++ incubator/jackrabbit/trunk/project.xml Mon Feb 14 07:35:27 2005
@@ -226,6 +226,15 @@
</roles>
<timezone>+1</timezone>
</contributor>
+ <contributor>
+ <name>Serge Huber</name>
+ <email/>
+ <organization/>
+ <roles>
+ <role>Java Developer</role>
+ </roles>
+ <timezone>+1</timezone>
+ </contributor>
</contributors>
<licenses>
<license>
Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Constants.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Constants.java?view=diff&r1=153796&r2=153797
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Constants.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Constants.java Mon Feb 14 07:35:27 2005
@@ -185,6 +185,4 @@
/** nt:frozenNode */
public static final QName NT_FROZENNODE = new QName(NS_NT_URI, "frozenNode");
-
-
}