You are viewing a plain text version of this content. The canonical link for it is here.
Posted to graffito-commits@incubator.apache.org by cl...@apache.org on 2005/12/05 20:36:34 UTC
svn commit: r354166 [3/3] - in
/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr:
exception/ mapper/ mapper/impl/ mapper/model/ persistence/
persistence/atomictypeconverter/ persistence/atomictypeconverter/impl/
persist...
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,497 @@
+/*
+ * Copyright 2000-2005 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.portals.graffito.jcr.persistence.objectconverter.impl;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.nodetype.PropertyDefinition;
+import javax.jcr.version.VersionException;
+
+import org.apache.commons.beanutils.ConstructorUtils;
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
+import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
+import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollectionUtil;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.DefaultCollectionConverterImpl;
+import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
+import org.apache.portals.graffito.jcr.repository.RepositoryUtil;
+
+/**
+ * Default implementation for {@link ObjectConverterImpl}
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe </a>
+ *
+ */
+public class ObjectConverterImpl implements ObjectConverter
+{
+ private Mapper mapper;
+
+ private Map atomicTypeConverters;
+
+ /**
+ * Constructor
+ *
+ * @param mapper The mapper to used
+ * @param atomicTypeConverters The atomic type converters to used
+ *
+ */
+ public ObjectConverterImpl(Mapper mapper, Map atomicTypeConverters)
+ {
+ this.mapper = mapper;
+ this.atomicTypeConverters = atomicTypeConverters;
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter#insert(javax.jcr.Session, java.lang.Object)
+ */
+ public void insert(Session session, Object object)
+ {
+ String path = this.getPath(session, object);
+ try
+ {
+ String parentPath = RepositoryUtil.getParentPath(path);
+ String nodeName = RepositoryUtil.getNodeName(path);
+ Node parentNode = (Node) session.getItem(parentPath);
+ this.insert(session, parentNode, nodeName, object);
+
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to insert the object at " + path, e);
+ }
+
+ }
+
+ /**
+ *
+ * @see org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter#insert(javax.jcr.Session, javax.jcr.Node, java.lang.String, java.lang.Object)
+ */
+ public void insert(Session session, Node parentNode, String nodeName, Object object)
+ {
+ try
+ {
+ ClassDescriptor classDescriptor = mapper.getClassDescriptor(object.getClass());
+ if (classDescriptor == null)
+ {
+ throw new PersistenceException("Class of type: " + object.getClass().getName() + " is not JCR persistable. Maybe element 'class-descriptor' for this type in mapping file is missing");
+ }
+
+ String jcrNodeType = classDescriptor.getJcrNodeType();
+ if (jcrNodeType == null || jcrNodeType.equals(""))
+ {
+ throw new PersistenceException("Undefined node type for " + parentNode);
+ }
+
+ Node objectNode = null;
+ objectNode = parentNode.addNode(nodeName, jcrNodeType);
+
+ storeSimpleFields(object, classDescriptor, objectNode);
+ insertBeanFields(session, object, classDescriptor, objectNode);
+ insertCollectionFields(session, object, classDescriptor, objectNode);
+
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to insert the object at " + parentNode, e);
+ }
+
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter#update(javax.jcr.Session, java.lang.Object)
+ */
+ public void update(Session session, Object object)
+ {
+ String path = this.getPath(session, object);
+ try
+ {
+ String parentPath = RepositoryUtil.getParentPath(path);
+ String nodeName = RepositoryUtil.getNodeName(path);
+ Node parentNode = (Node) session.getItem(parentPath);
+ this.update(session, parentNode, nodeName, object);
+
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to update the object at " + path, e);
+ }
+ }
+
+ /**
+ *
+ * @see org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter#update(javax.jcr.Session, javax.jcr.Node, java.lang.String, java.lang.Object)
+ */
+ public void update(Session session, Node parentNode, String nodeName, Object object)
+ {
+ try
+ {
+ ClassDescriptor classDescriptor = mapper.getClassDescriptor(object.getClass());
+ if (classDescriptor == null)
+ {
+ throw new PersistenceException("Class of type: " + object.getClass().getName() + " is not JCR persistable. Maybe element 'class-descriptor' for this type in mapping file is missing");
+ }
+
+ String jcrNodeType = classDescriptor.getJcrNodeType();
+ if (jcrNodeType == null || jcrNodeType.equals(""))
+ {
+ throw new PersistenceException("Undefined node type for " + parentNode);
+ }
+
+ Node objectNode = null;
+ objectNode = parentNode.getNode(nodeName);
+
+ storeSimpleFields(object, classDescriptor, objectNode);
+ updateBeanFields(session, object, classDescriptor, objectNode);
+ updateCollectionFields(session, object, classDescriptor, objectNode);
+
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to update the node : " + parentNode, e);
+ }
+
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter#getObject(javax.jcr.Session, java.lang.Class, java.lang.String)
+ */
+ public Object getObject(Session session, Class clazz, String path)
+ {
+ try
+ {
+
+ if (!session.itemExists(path))
+ {
+ return null;
+ }
+
+ ClassDescriptor classDescriptor = mapper.getClassDescriptor(clazz);
+ if (classDescriptor == null)
+ {
+ throw new PersistenceException("Class of type: " + clazz.getName() + " is not JCR persistable. Maybe element 'class-descriptor' for this type in mapping file is missing");
+ }
+
+ Node node = (Node) session.getItem(path);
+ Object object = clazz.newInstance();
+
+ retrieveSimpleFields(classDescriptor, node, object);
+ retrieveBeanFields(session, path, classDescriptor, object);
+ retrieveCollectionFields(session, classDescriptor, node, object);
+
+ return object;
+
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to get the object at " + path, e);
+ }
+ }
+
+
+ public String getPath(Session session, Object object)
+ {
+ try
+ {
+ ClassDescriptor classDescriptor = mapper.getClassDescriptor(object.getClass());
+ if (classDescriptor == null)
+ {
+ throw new PersistenceException("Class of type: " + object.getClass().getName() + " is not JCR persistable. Maybe element 'class-descriptor' for this type in mapping file is missing");
+ }
+
+ final FieldDescriptor pathFieldDescriptor = classDescriptor.getPathFieldDescriptor();
+ if (pathFieldDescriptor == null)
+ {
+ throw new PersistenceException("Class of type: " + object.getClass().getName() + " has no path mapping. Maybe attribute path=\"true\" for a field element of this class in jcrmapping.xml is missing?");
+ }
+ String pathField = pathFieldDescriptor.getFieldName();
+ return (String) PropertyUtils.getNestedProperty(object, pathField);
+
+
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to get the path", e);
+ }
+
+ }
+
+ /**
+ * Retrieve simple fields (atomic fields)
+ */
+ private void retrieveSimpleFields(ClassDescriptor classDescriptor, Node node, Object object) throws PathNotFoundException, RepositoryException, ValueFormatException, IllegalAccessException,
+ InvocationTargetException, NoSuchMethodException
+ {
+ Iterator fieldDescriptorIterator = classDescriptor.getFieldDescriptors().iterator();
+
+ while (fieldDescriptorIterator.hasNext())
+ {
+ FieldDescriptor fieldDescriptor = (FieldDescriptor) fieldDescriptorIterator.next();
+
+ String fieldName = fieldDescriptor.getFieldName();
+ String propertyName = fieldDescriptor.getJcrName();
+
+ if (fieldDescriptor.isPath())
+ {
+ PropertyUtils.setNestedProperty(object, fieldName, node.getPath());
+ }
+ else
+ {
+ AtomicTypeConverter converter = (AtomicTypeConverter) atomicTypeConverters
+ .get(PropertyUtils.getPropertyType(object, fieldName));
+ if (node.hasProperty(propertyName))
+ {
+ Object fieldValue = converter.getObject(node.getProperty(propertyName).getValue());
+ PropertyUtils.setNestedProperty(object, fieldName, fieldValue);
+ }
+ }
+ }
+ }
+
+ /**
+ * Retrieve bean fields
+ */
+ private void retrieveBeanFields(Session session, String path, ClassDescriptor classDescriptor, Object object) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
+ {
+ Iterator beanDescriptorIterator = classDescriptor.getBeanDescriptors().iterator();
+ while (beanDescriptorIterator.hasNext())
+ {
+ BeanDescriptor beanDescriptor = (BeanDescriptor) beanDescriptorIterator.next();
+ String beanName = beanDescriptor.getFieldName();
+ Class beanClass = PropertyUtils.getPropertyDescriptor(object, beanName).getPropertyType();
+ Object bean = this.getObject(session, beanClass, path + "/" + beanDescriptor.getJcrName());
+ PropertyUtils.setNestedProperty(object, beanName, bean);
+ }
+ }
+
+ /**
+ * Retrieve Collection fields
+ */
+ private void retrieveCollectionFields(Session session, ClassDescriptor classDescriptor, Node node, Object object) throws PathNotFoundException, RepositoryException, JcrMappingException,
+ ClassNotFoundException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException
+ {
+ Iterator collectionDescriptorIterator = classDescriptor.getCollectionDescriptors().iterator();
+ while (collectionDescriptorIterator.hasNext())
+ {
+ CollectionDescriptor collectionDescriptor = (CollectionDescriptor) collectionDescriptorIterator.next();
+ CollectionConverter collectionConverter = this.getCollectionConverter(collectionDescriptor);
+ Class collectionFieldClass = PropertyUtils.getPropertyDescriptor(object, collectionDescriptor.getFieldName()).getPropertyType();
+ ManageableCollection collection = collectionConverter.getCollection(session, node, collectionDescriptor, collectionFieldClass);
+ PropertyUtils.setNestedProperty(object, collectionDescriptor.getFieldName(), collection);
+ }
+ }
+
+ /**
+ * Insert Bean fields
+ */
+ private void insertBeanFields(Session session, Object object, ClassDescriptor classDescriptor, Node objectNode)
+ {
+ try
+ {
+ Iterator beanDescriptorIterator = classDescriptor.getBeanDescriptors().iterator();
+ while (beanDescriptorIterator.hasNext())
+ {
+ BeanDescriptor beanDescriptor = (BeanDescriptor) beanDescriptorIterator.next();
+ String jcrName = beanDescriptor.getJcrName();
+ Object bean = PropertyUtils.getNestedProperty(object, beanDescriptor.getFieldName());
+ if (bean != null)
+ {
+ this.insert(session, objectNode, jcrName, bean);
+ }
+
+ }
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to insert the bean fields", e);
+ }
+
+ }
+
+ /**
+ * Update Bean fields
+ */
+ private void updateBeanFields(Session session, Object object, ClassDescriptor classDescriptor, Node objectNode)
+ {
+ try
+ {
+ Iterator beanDescriptorIterator = classDescriptor.getBeanDescriptors().iterator();
+ while (beanDescriptorIterator.hasNext())
+ {
+ BeanDescriptor beanDescriptor = (BeanDescriptor) beanDescriptorIterator.next();
+ String jcrName = beanDescriptor.getJcrName();
+ Object bean = PropertyUtils.getNestedProperty(object, beanDescriptor.getFieldName());
+
+ // if the bean is null, remove existing node
+ if ((bean == null))
+ {
+ if (objectNode.hasNode(jcrName))
+ {
+ objectNode.getNode(jcrName).remove();
+ }
+ }
+ else
+ {
+ this.update(session, objectNode, jcrName, bean);
+ }
+
+ }
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to update the bean fields", e);
+ }
+
+ }
+
+ private void insertCollectionFields(Session session, Object object, ClassDescriptor classDescriptor, Node objectNode)
+ {
+ try
+ {
+ Iterator collectionDescriptorIterator = classDescriptor.getCollectionDescriptors().iterator();
+ while (collectionDescriptorIterator.hasNext())
+ {
+ CollectionDescriptor collectionDescriptor = (CollectionDescriptor) collectionDescriptorIterator.next();
+ CollectionConverter collectionConverter = this.getCollectionConverter(collectionDescriptor);
+ Object collection = PropertyUtils.getNestedProperty(object, collectionDescriptor.getFieldName());
+ ManageableCollection manageableCollection = ManageableCollectionUtil.getManageableCollection(collection);
+ collectionConverter.insertCollection(session, objectNode, collectionDescriptor, manageableCollection);
+ }
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to insert the collection fields", e);
+ }
+
+ }
+
+ private void updateCollectionFields(Session session, Object object, ClassDescriptor classDescriptor, Node objectNode)
+ {
+ try
+ {
+ Iterator collectionDescriptorIterator = classDescriptor.getCollectionDescriptors().iterator();
+ while (collectionDescriptorIterator.hasNext())
+ {
+ CollectionDescriptor collectionDescriptor = (CollectionDescriptor) collectionDescriptorIterator.next();
+ CollectionConverter collectionConverter = this.getCollectionConverter(collectionDescriptor);
+ Object collection = PropertyUtils.getNestedProperty(object, collectionDescriptor.getFieldName());
+ ManageableCollection manageableCollection = ManageableCollectionUtil.getManageableCollection(collection);
+ collectionConverter.updateCollection(session, objectNode, collectionDescriptor, manageableCollection);
+ }
+ }
+ catch (Exception e)
+ {
+ throw new PersistenceException("Impossible to store the bean fields", e);
+ }
+
+ }
+
+ private void storeSimpleFields(Object object, ClassDescriptor classDescriptor, Node objectNode)
+ throws PathNotFoundException, ValueFormatException, VersionException, LockException, ConstraintViolationException,
+ RepositoryException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
+ {
+
+
+ Iterator fieldDescriptorIterator = classDescriptor.getFieldDescriptors().iterator();
+ while (fieldDescriptorIterator.hasNext())
+ {
+
+ FieldDescriptor fieldDescriptor = (FieldDescriptor) fieldDescriptorIterator.next();
+
+ //Of course, Path field is not updated as property
+ if (fieldDescriptor.isPath())
+ {
+ continue;
+ }
+
+ String fieldName = fieldDescriptor.getFieldName();
+ String jcrName = fieldDescriptor.getJcrName();
+
+ // Check if the node property is "autocreated"
+ boolean autoCreated = false;
+
+ if (objectNode.hasProperty(jcrName))
+ {
+ autoCreated = objectNode.getProperty(jcrName).getDefinition().isAutoCreated();
+ }
+
+ // All auto created JCR properties are ignored
+ if (!autoCreated)
+ {
+ Object fieldValue = PropertyUtils.getNestedProperty(object, fieldName);
+
+ AtomicTypeConverter converter = (AtomicTypeConverter) atomicTypeConverters.get(PropertyUtils.getPropertyType(object, fieldName));
+ Value value = converter.getValue(fieldValue);
+ // Check if mandatory property are not null
+ this.checkMandatoryProperty(objectNode, fieldDescriptor, value);
+
+ objectNode.setProperty(jcrName, value);
+ }
+
+ }
+
+ }
+
+ private CollectionConverter getCollectionConverter(CollectionDescriptor collectionDescriptor)
+ throws ClassNotFoundException, NoSuchMethodException, InstantiationException, InvocationTargetException, IllegalAccessException
+ {
+
+ String className = collectionDescriptor.getCollectionConverterClassName();
+ if (className == null)
+ {
+ return new DefaultCollectionConverterImpl(this.atomicTypeConverters, this, this.mapper);
+ }
+ else
+ {
+ Class converterClass = Class.forName(className);
+ Object[] param =
+ { this.atomicTypeConverters, this, this.mapper };
+ return (CollectionConverter) ConstructorUtils.invokeConstructor(converterClass, param);
+ }
+
+ }
+
+ private void checkMandatoryProperty(Node objectNode, FieldDescriptor fieldDescriptor, Value value) throws RepositoryException
+ {
+ PropertyDefinition[] propertyDefinitions = objectNode.getPrimaryNodeType().getDeclaredPropertyDefinitions();
+ for (int i = 0; i < propertyDefinitions.length; i++)
+ {
+ PropertyDefinition definition = propertyDefinitions[i];
+ if (definition.getName().equals(fieldDescriptor.getJcrName()) && definition.isMandatory() && definition.isAutoCreated() == false && value == null)
+ {
+ throw new PersistenceException("Class of type:" + fieldDescriptor.getClassDescriptor().getClassName() + " has property: " + fieldDescriptor.getFieldName()
+ + " declared as JCR property: " + fieldDescriptor.getJcrName() + " This property is mandatory but property in bean has value null");
+ }
+ }
+ }
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Filter.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Filter.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Filter.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Filter.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,89 @@
+/*
+ * 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.portals.graffito.jcr.query;
+
+import org.apache.portals.graffito.jcr.exception.IncorrectAtomicTypeException;
+
+
+/**
+ *
+ * Graffito JCR Filter interface.
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
+ *
+ */
+public interface Filter
+{
+
+
+ /**
+ * Set the filter scope. The scope is an Node path specifying where to search in the content tree.
+ * For example,
+ * /mynode/mysecondnode/', the search engine will search on child objects in the /mynode/mysecondnode
+ * /mynode/mysecondnode//', the search engine will search on desncendant objects in the /mynode/mysecondnode (the complete subnode tree)
+ *
+ * @param scope The filter scope
+ *
+ */
+ public void setScope(String scope);
+
+
+ /**
+ * Get the filter scope.
+ *
+ * @return The filter scope
+ */
+ public String getScope();
+
+
+ /**
+ * Search content based on a fullTextSearch.
+ * Depending on the full text search engine, you can also filter on properties.
+ *
+ * @param scope either a a jcr node or propserty. If a node is used, all properties of this node are searche (following the internal index
+ * @param fullTextSearch The full text search string
+ */
+ public void addContains(String scope, String fullTextSearch) throws IncorrectAtomicTypeException;
+
+ public void addBetween(String arg0, Object arg1, Object arg2) throws IncorrectAtomicTypeException;
+
+ public void addEqualTo(String arg0, Object arg1) throws IncorrectAtomicTypeException;
+
+ public void addGreaterOrEqualThan(String arg0, Object arg1) throws IncorrectAtomicTypeException;
+
+ public void addGreaterThan(String arg0, Object arg1) throws IncorrectAtomicTypeException;
+
+ public void addLessOrEqualThan(String arg0, Object arg1) throws IncorrectAtomicTypeException;
+
+ public void addLessThan(String arg0, Object arg1) throws IncorrectAtomicTypeException;
+
+ public void addLike(String arg0, Object arg1) throws IncorrectAtomicTypeException;
+
+ public void addNotEqualTo(String arg0, Object arg1) throws IncorrectAtomicTypeException;
+
+ public void addNotNull(String arg0) throws IncorrectAtomicTypeException;
+
+ public void addIsNull(String arg0) throws IncorrectAtomicTypeException;
+
+ public void addOrFilter(Filter arg0) throws IncorrectAtomicTypeException;
+
+ public void addJCRExpression(String jcrExpression) throws IncorrectAtomicTypeException;
+
+ public Class getFilterClass();
+
+
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,54 @@
+/*
+ * 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.portals.graffito.jcr.query;
+
+/**
+ * Graffito JCR Query interface
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
+ *
+ */
+public interface Query
+{
+
+ /**
+ * Set the filter to use with this query
+ * @param filter The filter to use
+ */
+ public void setFilter(Filter filter);
+
+ /**
+ *
+ * @return The filter used for this query
+ */
+ public Filter getFilter();
+
+ /**
+ * Order the object found (ascending)
+ * @param fieldNameAttribute the name of the field used to sort the search result
+ */
+ public void addOrderByAscending(String fieldNameAttribute);
+
+ /**
+ * Order the object found (descending)
+ * @param fieldNameAttribute the name of the field used to sort the search result
+ */
+
+ public void addOrderByDescending(String fieldNameAttribute);
+
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,53 @@
+/*
+ * 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.portals.graffito.jcr.query;
+
+
+
+/**
+ * The query manager is used to instantiate query objects and execute query based on the object model.
+ * Internally, this service used the JCR QueryManager
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
+ *
+ */
+public interface QueryManager
+{
+ /**
+ * Create a new empty filter
+ * @param classQuery The class used to search
+ * @return a new instantiated filter object
+ */
+ public Filter createFilter(Class classQuery);
+
+ /**
+ * Create a new empty query
+ * @param filter the filter used by the query
+ * @return a new instantiated query object
+ */
+ public Query createQuery(Filter filter);
+
+ /**
+ * Build a JCR search expression from a Query
+ * @param query the query matching to the JCR search expression
+ * @return a JCR XPATH search expression
+ */
+ public String buildJCRExpression(Query query);
+
+
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,285 @@
+/*
+ * 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.portals.graffito.jcr.query.impl;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
+import org.apache.portals.graffito.jcr.query.Filter;
+
+/**
+ * {@link org.apache.portals.graffito.jcr.query.Filter}
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
+ *
+ */
+public class FilterImpl implements Filter
+{
+
+ private Class claszz;
+ private String scope = "";
+ private ArrayList jcrExpressions = new ArrayList();
+
+
+ private ClassDescriptor classDescriptor;
+ private Map atomicTypeConverters;
+
+ private String orJcrExpression;
+
+ private final static Log log = LogFactory.getLog(FilterImpl.class);
+
+ /**
+ * Construtor
+ *
+ * @param classDescriptor
+ * @param atomicTypeConverters
+ * @param clazz
+ */
+ public FilterImpl(ClassDescriptor classDescriptor, Map atomicTypeConverters, Class clazz)
+ {
+ this.claszz = clazz;
+ this.atomicTypeConverters = atomicTypeConverters;
+ this.classDescriptor = classDescriptor;
+ }
+
+
+ /**
+ *
+ * @see org.apache.portals.graffito.jcr.query.Filter#getFilterClass()
+ */
+ public Class getFilterClass()
+ {
+ return claszz;
+ }
+
+
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#setScope(java.lang.String)
+ */
+ public void setScope(String scope)
+ {
+ this.scope = scope;
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#getScope()
+ */
+ public String getScope()
+ {
+
+ return this.scope;
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addContains(java.lang.String)
+ */
+ public void addContains(String scope, String fullTextSearch)
+ {
+ String jcrExpression = null;
+ if (scope.equals("."))
+ {
+ jcrExpression = "jcr:contains(., '" + fullTextSearch + "')";
+ }
+ else
+ {
+ jcrExpression = "jcr:contains(@" + this.getJcrFieldName(scope) + ", '" + fullTextSearch + "')";
+ }
+
+ jcrExpressions.add(jcrExpression);
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addBetween(java.lang.String, java.lang.Object, java.lang.Object)
+ */
+ public void addBetween(String fieldAttributeName, Object value1, Object value2)
+ {
+ String jcrExpression = "( @" + this.getJcrFieldName(fieldAttributeName) + " >= " + this.getStringValue(value1) +
+ " and @" + this.getJcrFieldName(fieldAttributeName) + " <= " + this.getStringValue(value2) + ")";
+ jcrExpressions.add(jcrExpression) ;
+
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addEqualTo(java.lang.String, java.lang.Object)
+ */
+ public void addEqualTo(String fieldAttributeName, Object value)
+ {
+ String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " = " + this.getStringValue(value);
+ jcrExpressions.add(jcrExpression);
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addGreaterOrEqualThan(java.lang.String, java.lang.Object)
+ */
+ public void addGreaterOrEqualThan(String fieldAttributeName, Object value)
+ {
+
+ String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " >= " + this.getStringValue(value);
+ jcrExpressions.add(jcrExpression);
+
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addGreaterThan(java.lang.String, java.lang.Object)
+ */
+ public void addGreaterThan(String fieldAttributeName, Object value)
+ {
+ String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " > " + this.getStringValue(value);
+ jcrExpressions.add(jcrExpression);
+
+ }
+
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addLessOrEqualThan(java.lang.String, java.lang.Object)
+ */
+ public void addLessOrEqualThan(String fieldAttributeName, Object value)
+ {
+ String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " <= " + this.getStringValue(value);
+ jcrExpressions.add(jcrExpression);
+
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addLessOrEqualThan(java.lang.String, java.lang.Object)
+ */
+ public void addLessThan(String fieldAttributeName, Object value)
+ {
+ String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " < " + this.getStringValue(value);
+ jcrExpressions.add(jcrExpression);
+
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addLike(java.lang.Object, java.lang.Object)
+ */
+ public void addLike(String fieldAttributeName, Object value)
+ {
+ String jcrExpression = "jcr:like(" + "@" + this.getJcrFieldName(fieldAttributeName) + ", '" + value + "')";
+ jcrExpressions.add(jcrExpression);
+ }
+
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addNotEqualTo(java.lang.String, java.lang.Object)
+ */
+ public void addNotEqualTo(String fieldAttributeName, Object value)
+ {
+ String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " != " + this.getStringValue(value);
+ jcrExpressions.add(jcrExpression);
+ }
+
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addNotNull(java.lang.String)
+ */
+ public void addNotNull(String fieldAttributeName)
+ {
+ String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName);
+ jcrExpressions.add(jcrExpression);
+
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addIsNull(java.lang.String)
+ */
+ public void addIsNull(String fieldAttributeName)
+ {
+ String jcrExpression = "not(@" + this.getJcrFieldName(fieldAttributeName)+")";
+ jcrExpressions.add(jcrExpression);
+
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Filter#addOrFilter(org.apache.portals.graffito.jcr.query.Filter)
+ */
+ public void addOrFilter(Filter filter)
+ {
+
+ orJcrExpression = ((FilterImpl)filter).getJcrExpression();
+
+ }
+
+ public void addJCRExpression(String jcrExpression)
+ {
+ jcrExpressions.add(jcrExpression);
+ }
+
+
+ private String getJcrFieldName(String fieldAttribute)
+ {
+ String jcrFieldName = classDescriptor.getJcrName(fieldAttribute);
+ if (jcrFieldName == null)
+ {
+ log.error("Impossible to find the jcrFieldName for the attribute :" + fieldAttribute);
+ }
+ return jcrFieldName;
+
+ }
+
+ private String getStringValue (Object value)
+ {
+ AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(value.getClass());
+ return atomicTypeConverter.getStringValue(value);
+ }
+
+ public String getJcrExpression()
+ {
+ if (orJcrExpression == null || orJcrExpression.equals(""))
+ {
+ return buildJcrExpression();
+ }
+ else
+ {
+ return "(" + buildJcrExpression() + ") or (" + this.orJcrExpression + ")";
+ }
+
+ }
+
+ private String buildJcrExpression()
+ {
+ int count = 1;
+ String jcrExp = "";
+
+
+ Iterator criteriaIterator = jcrExpressions.iterator();
+ while (criteriaIterator.hasNext())
+ {
+ if (count > 1)
+ {
+ jcrExp += " and ";
+ }
+ jcrExp += (String) criteriaIterator.next();
+ count++;
+
+ }
+
+
+ return jcrExp;
+
+ }
+
+
+
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,117 @@
+/*
+ * 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.portals.graffito.jcr.query.impl;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.query.Filter;
+import org.apache.portals.graffito.jcr.query.Query;
+
+/**
+ * Default Query implementation
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
+ *
+ */
+public class QueryImpl implements Query
+{
+
+ private Filter filter;
+
+ ClassDescriptor classDescriptor;
+
+ private ArrayList orderByExpressions = new ArrayList();
+
+ /**
+ * Constructor
+ *
+ * @param filter
+ * @param mapper
+ */
+ public QueryImpl(Filter filter, Mapper mapper)
+ {
+ this.filter = filter;
+ classDescriptor = mapper.getClassDescriptor(filter.getFilterClass());
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Query#setFilter(org.apache.portals.graffito.jcr.query.Filter)
+ */
+ public void setFilter(Filter filter)
+ {
+ this.filter = filter;
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.query.Query#getFilter()
+ */
+ public Filter getFilter()
+ {
+ return this.filter;
+ }
+
+ public void addOrderByDescending(String fieldNameAttribute)
+ {
+ orderByExpressions.add("@" + this.getJcrFieldName(fieldNameAttribute) + " descending");
+ }
+
+ /**
+ *
+ * @see org.apache.portals.graffito.jcr.query.Query#addOrderByAscending(java.lang.String)
+ */
+ public void addOrderByAscending(String fieldNameAttribute)
+ {
+ orderByExpressions.add("@" + this.getJcrFieldName(fieldNameAttribute) + " ascending");
+ }
+
+ public String getOrderByExpression()
+ {
+
+ if (orderByExpressions.size() == 0)
+ {
+ return "";
+ }
+
+ String orderByExpression = "order by ";
+ Iterator iterator = orderByExpressions.iterator();
+ int count=1;
+ while (iterator.hasNext())
+ {
+ if (count > 1)
+ {
+ orderByExpression += " , ";
+ }
+ orderByExpression+= (String) iterator.next();
+ count++;
+ }
+
+ return orderByExpression;
+ }
+
+
+ private String getJcrFieldName(String fieldAttribute)
+ {
+
+ return classDescriptor.getJcrName(fieldAttribute);
+
+ }
+
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java Mon Dec 5 12:35:57 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.portals.graffito.jcr.query.impl;
+
+import java.util.Map;
+
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.query.Filter;
+import org.apache.portals.graffito.jcr.query.Query;
+import org.apache.portals.graffito.jcr.query.QueryManager;
+
+public class QueryManagerImpl implements QueryManager
+{
+
+ private Mapper mapper;
+ private Map atomicTypeConverters;
+ public QueryManagerImpl(Mapper mapper, Map atomicTypeConverters)
+ {
+ this.mapper = mapper;
+ this.atomicTypeConverters = atomicTypeConverters;
+ }
+
+ public Filter createFilter(Class classQuery)
+ {
+
+ return new FilterImpl(mapper.getClassDescriptor(classQuery), atomicTypeConverters, classQuery);
+ }
+
+ public Query createQuery(Filter filter)
+ {
+
+ return new QueryImpl(filter, mapper);
+ }
+
+ public String buildJCRExpression(Query query)
+ {
+
+ Filter filter = query.getFilter();
+
+ String jcrExp = "";
+
+ // Add scope
+ if ((filter.getScope() != null && ( ! filter.getScope().equals(""))))
+ {
+ jcrExp += "/jcr:root" + filter.getScope() + "element(*, ";
+ }
+ else
+ {
+ jcrExp += "//element(*, ";
+ }
+
+ // Add node type
+ jcrExp += this.getNodeType(filter) + ") ";
+
+ // Add filter criteria
+ String filterExp = ((FilterImpl)filter).getJcrExpression();
+ if ((filterExp != null) && ( ! filterExp.equals("")))
+ {
+ jcrExp += "[" + filterExp + "]";
+ }
+
+ // Add order by
+ jcrExp += ((QueryImpl)query).getOrderByExpression();
+
+ return jcrExp;
+
+ }
+
+ private String getNodeType(Filter filter)
+ {
+ ClassDescriptor classDescriptor = mapper.getClassDescriptor(filter.getFilterClass());
+ return classDescriptor.getJcrNodeType();
+
+ }
+
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/repository/RepositoryUtil.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/repository/RepositoryUtil.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/repository/RepositoryUtil.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/repository/RepositoryUtil.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,300 @@
+/*
+ * Copyright 2000-2005 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.portals.graffito.jcr.repository;
+
+import java.util.Hashtable;
+
+import javax.jcr.Repository;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.core.jndi.RegistryHelper;
+import org.apache.jackrabbit.util.ISO9075;
+import org.apache.jackrabbit.util.Text;
+import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.exception.RepositoryException;
+
+/**
+* Utility class for managing JCR repositories.
+*
+* @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe </a>
+* @version $Id: Exp $
+*/
+public class RepositoryUtil
+{
+
+ /** Graffito namespace prefix constant.
+ */
+ private static final String GRAFFITO_NAMESPACE_PREFIX = "graffito";
+
+ /** Graffito namespace constant.
+ */
+ private static final String GRAFFITO_NAMESPACE = "http://incubator.apache.org/graffito";
+ /** Item path separator */
+ public static final String PATH_SEPARATOR = "/";
+
+ private final static Log log = LogFactory.getLog(RepositoryUtil.class);
+ /**
+ * Register a new repository
+ *
+ * @param repositoryName The repository unique name
+ * @param configFile The JCR config file
+ * @param homeDir The directory containing the complete repository settings (workspace, node types, ...)
+ *
+ * @throws RepositoryException when it is not possible to register the repository
+ */
+ public static void registerRepository(String repositoryName, String configFile, String homeDir) throws RepositoryException
+ {
+ try
+ {
+ Hashtable env = new Hashtable();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
+ env.put(Context.PROVIDER_URL, "localhost");
+ InitialContext ctx = new InitialContext(env);
+
+ RegistryHelper.registerRepository(ctx, repositoryName, configFile, homeDir, true);
+
+ }
+ catch (Exception e)
+ {
+ throw new RepositoryException("Impossible to register the respository : " +
+ repositoryName + " - config file : " + configFile, e);
+ }
+
+ }
+
+
+ /**
+ * Unregister a repository
+ *
+ * @param repositoryName The repository unique name
+ *
+ * @throws RepositoryException when it is not possible to unregister the repository
+ */
+ public static void unRegisterRepository(String repositoryName) throws RepositoryException
+ {
+ try
+ {
+
+
+ Hashtable env = new Hashtable();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
+ env.put(Context.PROVIDER_URL, "localhost");
+ InitialContext ctx = new InitialContext(env);
+
+ RegistryHelper.unregisterRepository(ctx, repositoryName);
+
+ }
+ catch (Exception e)
+ {
+ throw new RepositoryException("Impossible to unregister the respository : " +
+ repositoryName , e);
+ }
+
+ }
+
+ /**
+ * Get a repository
+ *
+ * @param repositoryName The repository name
+ * @return a JCR repository reference
+ *
+ * @throws RepositoryException when it is not possible to get the repository.
+ * Before calling this method, the repository has to be registered (@see RepositoryUtil#registerRepository(String, String, String)
+ */
+ public static Repository getRepository(String repositoryName) throws RepositoryException
+ {
+ try
+ {
+ Hashtable env = new Hashtable();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
+ env.put(Context.PROVIDER_URL, "localhost");
+ InitialContext ctx = new InitialContext(env);
+
+ Repository repository = (Repository) ctx.lookup(repositoryName);
+ return repository;
+ }
+ catch (Exception e)
+ {
+ throw new RepositoryException("Impossible to get the repository : " + repositoryName, e);
+ }
+ }
+
+ /**
+ * Connect to a JCR repository
+ *
+ * @param repository The JCR repository
+ * @param user The user name
+ * @param password The password
+ * @return a valid JCR session
+ *
+ * @throws RepositoryException when it is not possible to connect to the JCR repository
+ */
+ public static Session login(Repository repository, String user, String password) throws RepositoryException
+ {
+ try
+ {
+ Session session = repository.login(new SimpleCredentials(user, password.toCharArray()), null);
+ setupSession(session);
+
+ return session;
+
+ }
+ catch (Exception e)
+ {
+ throw new RepositoryException("Impossible to login ", e);
+ }
+ }
+
+ /**
+ * Check if a path is valid
+ *
+ * @param path The path to validate
+ * @return true if the path is valid, else false
+ */
+ public static boolean isValidPath(String path)
+ {
+ if ((path == null) ||
+ (path.equals(PATH_SEPARATOR)) ||
+ (path.endsWith(PATH_SEPARATOR)) ||
+ (! path.startsWith(PATH_SEPARATOR)) ||
+ (path.equals("")))
+ {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Get the parent path
+ * @param path The path from wich the parent path has to be returned
+ * @return The parent path
+ *
+ * @throws PersistenceException when the path is invalid
+ */
+ public static String getParentPath(String path) throws PersistenceException
+ {
+ String parentPath = "";
+
+ if (!isValidPath(path))
+ {
+ throw new PersistenceException("Invalid path : " + path);
+ }
+
+ String[] pathElements = path.split(PATH_SEPARATOR);
+
+ // Firts path element should be = empty string because a uri always start with '/'
+ // So, if len=2, means it is a root folder like '/foo'.
+ // In this case the uri has not parent folder => return "/"
+ if (pathElements.length == 2)
+ {
+ return PATH_SEPARATOR;
+ }
+
+ for(int i=0; i < pathElements.length -1; i++)
+ {
+ if (! pathElements[i].equals(""))
+ {
+ parentPath += PATH_SEPARATOR + pathElements[i];
+ }
+ }
+ return parentPath;
+ }
+
+ /**
+ * Get the node name
+ * @param path The path from which the node name has to be retrieved
+ * @return The node name
+ *
+ * @throws PersistenceException when the path is invalid
+ */
+ public static String getNodeName(String path) throws PersistenceException
+ {
+
+ String[] pathElements = path.split(PATH_SEPARATOR);
+
+ if (! isValidPath(path))
+ {
+ throw new PersistenceException("Invalid path : " + path);
+ }
+ return pathElements[pathElements.length-1];
+ }
+
+ /**
+ * Setup the session.
+ * Until now, we check only if the Graffito namespace prefix exist in the repository
+ *
+ */
+ private static void setupSession(Session session) throws RepositoryException
+ {
+ try
+ {
+ String[] jcrNamespaces = session.getWorkspace().getNamespaceRegistry().getPrefixes();
+ boolean createGraffitoNamespace = true;
+ for (int i = 0; i < jcrNamespaces.length; i++)
+ {
+ if (jcrNamespaces[i].equals(GRAFFITO_NAMESPACE_PREFIX))
+ {
+ createGraffitoNamespace = false;
+ log.debug("Graffito namespace exists.");
+ }
+ }
+
+ if (createGraffitoNamespace)
+ {
+ session.getWorkspace().getNamespaceRegistry().registerNamespace(GRAFFITO_NAMESPACE_PREFIX, GRAFFITO_NAMESPACE);
+ log.info("Successfully created graffito namespace.");
+ }
+
+ if (session.getRootNode() != null)
+ {
+ log.info("Jcr repository setup successfull.");
+ }
+
+
+ }
+ catch (Exception e)
+ {
+ log.error("Error while setting up the jcr repository.", e);
+ throw new RepositoryException(e.getMessage());
+ }
+ }
+
+ /**
+ * Encode a path
+ * @TODO : drop Jackrabbit dependency
+ *
+ * @param path the path to encode
+ * @return the encoded path
+ *
+ */
+ public static String encodePath(String path)
+ {
+ String[] pathElements = Text.explode(path, '/');
+ for (int i=0;i<pathElements.length;i++)
+ {
+ pathElements[i] = ISO9075.encode(pathElements[i]);
+ }
+ return "/" + Text.implode(pathElements, "/");
+
+ }
+
+
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/security/SimpleAccessManager.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/security/SimpleAccessManager.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/security/SimpleAccessManager.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/security/SimpleAccessManager.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,164 @@
+/*
+ * 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.portals.graffito.jcr.security;
+
+import org.apache.jackrabbit.core.HierarchyManager;
+import org.apache.jackrabbit.core.ItemId;
+import org.apache.jackrabbit.core.security.AMContext;
+import org.apache.jackrabbit.core.security.AccessManager;
+import org.apache.jackrabbit.core.security.AnonymousPrincipal;
+import org.apache.jackrabbit.core.security.SystemPrincipal;
+import org.apache.log4j.Logger;
+
+import javax.jcr.AccessDeniedException;
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.NoSuchWorkspaceException;
+import javax.jcr.RepositoryException;
+import javax.security.auth.Subject;
+
+/**
+ * <code>SimpleAccessManager</code> ...
+ */
+public class SimpleAccessManager implements AccessManager
+{
+
+ private static Logger log = Logger.getLogger(SimpleAccessManager.class);
+
+ /**
+ * Subject whose access rights this AccessManager should reflect
+ */
+ protected Subject subject;
+
+ /**
+ * hierarchy manager used for ACL-based access control model
+ */
+ protected HierarchyManager hierMgr;
+
+ private boolean initialized;
+
+ protected boolean system;
+
+ protected boolean anonymous;
+
+ /**
+ * Empty constructor
+ */
+ public SimpleAccessManager()
+ {
+ initialized = false;
+ anonymous = false;
+ system = false;
+ }
+
+ //--------------------------------------------------------< AccessManager >
+ /**
+ * {@inheritDoc}
+ */
+ public void init(AMContext context) throws AccessDeniedException, Exception
+ {
+ if (initialized)
+ {
+ throw new IllegalStateException("already initialized");
+ }
+
+ subject = context.getSubject();
+ hierMgr = context.getHierarchyManager();
+ anonymous = !subject.getPrincipals(AnonymousPrincipal.class).isEmpty();
+ system = !subject.getPrincipals(SystemPrincipal.class).isEmpty();
+
+ // @todo check permission to access given workspace based on principals
+ initialized = true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public synchronized void close() throws Exception
+ {
+ if (!initialized)
+ {
+ throw new IllegalStateException("not initialized");
+ }
+
+ initialized = false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void checkPermission(ItemId id, int permissions) throws AccessDeniedException, ItemNotFoundException, RepositoryException
+ {
+ if (!initialized)
+ {
+ throw new IllegalStateException("not initialized");
+ }
+
+ if (system)
+ {
+ // system has always all permissions
+ return;
+ }
+ else if (anonymous)
+ {
+ // anonymous is always denied WRITE & REMOVE premissions
+ if ((permissions & WRITE) == WRITE || (permissions & REMOVE) == REMOVE)
+ {
+ throw new AccessDeniedException();
+ }
+ }
+ // @todo check permission based on principals
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isGranted(ItemId id, int permissions) throws ItemNotFoundException, RepositoryException
+ {
+ if (!initialized)
+ {
+ throw new IllegalStateException("not initialized");
+ }
+
+ if (system)
+ {
+ // system has always all permissions
+ return true;
+ }
+ else if (anonymous)
+ {
+ // anonymous is always denied WRITE & REMOVE premissions
+ if ((permissions & WRITE) == WRITE || (permissions & REMOVE) == REMOVE)
+ {
+ return false;
+ }
+ }
+
+ // @todo check permission based on principals
+ return true;
+
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean canAccess(String workspaceName) throws NoSuchWorkspaceException, RepositoryException
+ {
+ // @todo check permission to access given workspace based on principals
+ return true;
+ }
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/security/SimpleLoginModule.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/security/SimpleLoginModule.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/security/SimpleLoginModule.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/security/SimpleLoginModule.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,220 @@
+/*
+ * 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.portals.graffito.jcr.security;
+
+import org.apache.jackrabbit.core.security.AnonymousPrincipal;
+import org.apache.jackrabbit.core.security.CredentialsCallback;
+import org.apache.jackrabbit.core.security.SecurityConstants;
+import org.apache.jackrabbit.core.security.UserPrincipal;
+import org.apache.log4j.Logger;
+
+import javax.jcr.Credentials;
+import javax.jcr.SimpleCredentials;
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.FailedLoginException;
+import javax.security.auth.login.LoginException;
+import javax.security.auth.spi.LoginModule;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A <code>SimpleLoginModule</code> ...
+ */
+public class SimpleLoginModule implements LoginModule
+{
+
+ private static Logger log = Logger.getLogger(SimpleLoginModule.class);
+
+ /**
+ * Name of the anonymous user id option in the LoginModule configuration
+ */
+ private static final String OPT_ANONYMOUS = "anonymousId";
+
+ /**
+ * The default user id for anonymous login
+ */
+ private static final String DEFAULT_ANONYMOUS_ID = "anonymous";
+
+ // initial state
+ private Subject subject;
+
+ private CallbackHandler callbackHandler;
+
+ private Map sharedState;
+
+ private Map options;
+
+ // configurable options
+ //private boolean someOpt = false;
+
+ // local authentication state:
+ // the principals, i.e. the authenticated identities
+ private final Set principals = new HashSet();
+
+ /**
+ * Id of an anonymous user login
+ */
+ private String anonymousUserId = DEFAULT_ANONYMOUS_ID;
+
+ /**
+ * Constructor
+ */
+ public SimpleLoginModule()
+ {
+ }
+
+ //----------------------------------------------------------< LoginModule >
+ /**
+ * {@inheritDoc}
+ */
+ public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options)
+ {
+ this.subject = subject;
+ this.callbackHandler = callbackHandler;
+ this.sharedState = sharedState;
+ this.options = options;
+
+ // initialize any configured options
+ //someOpt = "true".equalsIgnoreCase((String)options.get("someOpt"));
+ String userId = (String) options.get(OPT_ANONYMOUS);
+ if (userId != null)
+ {
+ anonymousUserId = userId;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean login() throws LoginException
+ {
+ // prompt for a user name and password
+ if (callbackHandler == null)
+ {
+ throw new LoginException("no CallbackHandler available");
+ }
+
+ Callback[] callbacks = new Callback[]
+ { new CredentialsCallback() };
+
+ boolean authenticated = false;
+ principals.clear();
+ try
+ {
+ callbackHandler.handle(callbacks);
+ // credentials
+ CredentialsCallback ccb = (CredentialsCallback) callbacks[0];
+ Credentials creds = ccb.getCredentials();
+ if (creds != null)
+ {
+ if (creds instanceof SimpleCredentials)
+ {
+ SimpleCredentials sc = (SimpleCredentials) creds;
+ // authenticate
+
+ Object attr = sc.getAttribute(SecurityConstants.IMPERSONATOR_ATTRIBUTE);
+ if (attr != null && attr instanceof Subject)
+ {
+ Subject impersonator = (Subject) attr;
+ // @todo check privileges to 'impersonate' the user represented by the supplied credentials
+ }
+ else
+ {
+ // @todo implement simple username/password authentication
+ }
+
+ if (anonymousUserId.equals(sc.getUserID()))
+ {
+ principals.add(new AnonymousPrincipal());
+ }
+ else
+ {
+ // else assume the user we authenticated is the UserPrincipal
+ principals.add(new UserPrincipal(sc.getUserID()));
+ }
+ authenticated = true;
+ }
+ }
+ }
+ catch (java.io.IOException ioe)
+ {
+ throw new LoginException(ioe.toString());
+ }
+ catch (UnsupportedCallbackException uce)
+ {
+ throw new LoginException(uce.getCallback().toString() + " not available");
+ }
+
+ if (authenticated)
+ {
+ return !principals.isEmpty();
+ }
+ else
+ {
+ // authentication failed: clean out state
+ principals.clear();
+ throw new FailedLoginException();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean commit() throws LoginException
+ {
+ if (principals.isEmpty())
+ {
+ return false;
+ }
+ else
+ {
+ // add a principals (authenticated identities) to the Subject
+ subject.getPrincipals().addAll(principals);
+ return true;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean abort() throws LoginException
+ {
+ if (principals.isEmpty())
+ {
+ return false;
+ }
+ else
+ {
+ logout();
+ }
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean logout() throws LoginException
+ {
+ subject.getPrincipals().removeAll(principals);
+ principals.clear();
+ return true;
+ }
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/transaction/jackrabbit/UserTransactionImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/transaction/jackrabbit/UserTransactionImpl.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/transaction/jackrabbit/UserTransactionImpl.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/transaction/jackrabbit/UserTransactionImpl.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,213 @@
+/*
+ * 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.portals.graffito.jcr.transaction.jackrabbit;
+
+import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
+import javax.transaction.xa.XAException;
+import javax.transaction.UserTransaction;
+import javax.transaction.Status;
+import javax.transaction.NotSupportedException;
+import javax.transaction.SystemException;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.RollbackException;
+import javax.jcr.Session;
+
+import org.apache.jackrabbit.core.XASession;
+
+/**
+ * Internal {@link javax.transaction.UserTransaction} implementation.
+ */
+public class UserTransactionImpl implements UserTransaction {
+
+ /**
+ * Global transaction id counter
+ */
+ private static byte counter = 0;
+
+ /**
+ * XAResource
+ */
+ private final XAResource xares;
+
+ /**
+ * Xid
+ */
+ private Xid xid;
+
+ /**
+ * Status
+ */
+ private int status = Status.STATUS_NO_TRANSACTION;
+
+ /**
+ * Create a new instance of this class. Takes a session as parameter.
+ * @param session session. If session is not of type
+ * {@link XASession}, an <code>IllegalArgumentException</code>
+ * is thrown
+ */
+ public UserTransactionImpl(Session session) {
+ if (session instanceof XASession) {
+ xares = ((XASession) session).getXAResource();
+ } else {
+ throw new IllegalArgumentException("Session not of type XASession");
+ }
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#begin
+ */
+ public void begin() throws NotSupportedException, SystemException {
+ if (status != Status.STATUS_NO_TRANSACTION) {
+ throw new IllegalStateException("Transaction already active");
+ }
+
+ try {
+ xid = new XidImpl(counter++);
+ xares.start(xid, XAResource.TMNOFLAGS);
+ status = Status.STATUS_ACTIVE;
+
+ } catch (XAException e) {
+
+ throw new SystemException("Unable to begin transaction: " +
+ "XA_ERR=" + e.errorCode);
+ }
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#commit
+ */
+ public void commit() throws HeuristicMixedException,
+ HeuristicRollbackException, IllegalStateException,
+ RollbackException, SecurityException, SystemException {
+
+ if (status != Status.STATUS_ACTIVE) {
+ throw new IllegalStateException("Transaction not active");
+ }
+
+ try {
+ xares.end(xid, XAResource.TMSUCCESS);
+
+ status = Status.STATUS_PREPARING;
+ xares.prepare(xid);
+ status = Status.STATUS_PREPARED;
+
+ status = Status.STATUS_COMMITTING;
+ xares.commit(xid, false);
+ status = Status.STATUS_COMMITTED;
+
+ } catch (XAException e) {
+
+ if (e.errorCode >= XAException.XA_RBBASE &&
+ e.errorCode <= XAException.XA_RBEND) {
+ throw new RollbackException();
+ } else {
+ throw new SystemException("Unable to commit transaction: " +
+ "XA_ERR=" + e.errorCode);
+ }
+ }
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#getStatus
+ */
+ public int getStatus() throws SystemException {
+ return status;
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#rollback
+ */
+ public void rollback() throws IllegalStateException, SecurityException,
+ SystemException {
+
+ if (status != Status.STATUS_ACTIVE &&
+ status != Status.STATUS_MARKED_ROLLBACK) {
+
+ throw new IllegalStateException("Transaction not active");
+ }
+
+ try {
+ xares.end(xid, XAResource.TMFAIL);
+
+ status = Status.STATUS_ROLLING_BACK;
+ xares.rollback(xid);
+ status = Status.STATUS_ROLLEDBACK;
+
+ } catch (XAException e) {
+
+ throw new SystemException("Unable to rollback transaction: " +
+ "XA_ERR=" + e.errorCode);
+ }
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#setRollbackOnly()
+ */
+ public void setRollbackOnly() throws IllegalStateException, SystemException {
+ if (status != Status.STATUS_ACTIVE) {
+ throw new IllegalStateException("Transaction not active");
+ }
+ status = Status.STATUS_MARKED_ROLLBACK;
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#setTransactionTimeout
+ */
+ public void setTransactionTimeout(int seconds) throws SystemException {}
+
+
+ /**
+ * Internal {@link Xid} implementation.
+ */
+ class XidImpl implements Xid {
+
+ /** Global transaction id */
+ private final byte[] globalTxId;
+
+ /**
+ * Create a new instance of this class. Takes a global
+ * transaction number as parameter
+ * @param globalTxNumber global transaction number
+ */
+ public XidImpl(byte globalTxNumber) {
+ this.globalTxId = new byte[] { globalTxNumber };
+ }
+
+ /**
+ * @see javax.transaction.xa.Xid#getFormatId()
+ */
+ public int getFormatId() {
+ return 0;
+ }
+
+ /**
+ * @see javax.transaction.xa.Xid#getBranchQualifier()
+ */
+ public byte[] getBranchQualifier() {
+ return new byte[0];
+ }
+
+ /**
+ * @see javax.transaction.xa.Xid#getGlobalTransactionId()
+ */
+ public byte[] getGlobalTransactionId() {
+ return globalTxId;
+ }
+ }
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/version/Version.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/version/Version.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/version/Version.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/version/Version.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2000-2005 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.portals.graffito.jcr.version;
+
+import java.util.Calendar;
+
+import javax.jcr.RepositoryException;
+
+import org.apache.portals.graffito.jcr.exception.VersionException;
+
+/**
+ *
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe </a>
+ *
+ */
+public class Version
+{
+
+ private javax.jcr.version.Version version;
+
+ public Version(javax.jcr.version.Version version)
+ {
+ this.version = version;
+ }
+
+ public Calendar getCreated()
+ {
+ try
+ {
+ return version.getCreated();
+ }
+ catch (RepositoryException e)
+ {
+
+ throw new VersionException("Error while retrieving the version creation date", e);
+ }
+ }
+
+ public String getUuid()
+ {
+ try
+ {
+ return version.getUUID();
+ }
+ catch (RepositoryException e)
+ {
+
+ throw new VersionException("Error while retrieving the version UUID", e);
+ }
+ }
+
+ public String getPath()
+ {
+ try
+ {
+ return version.getPath();
+ }
+ catch (RepositoryException e)
+ {
+
+ throw new VersionException("Error while retrieving the version path", e);
+ }
+ }
+
+ public String getName()
+ {
+ try
+ {
+ return version.getName();
+
+ }
+ catch (RepositoryException e)
+ {
+
+ throw new VersionException("Error while retrieving the version path", e);
+ }
+ }
+
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/version/VersionIterator.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/version/VersionIterator.java?rev=354166&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/version/VersionIterator.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/version/VersionIterator.java Mon Dec 5 12:35:57 2005
@@ -0,0 +1,88 @@
+/*
+ * 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.portals.graffito.jcr.version;
+
+import java.util.Iterator;
+
+import javax.jcr.version.Version;
+
+
+/**
+ * VersionIterator is a wrapper class for JCR VersionIterator
+ *
+ * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
+ *
+ */
+public class VersionIterator implements Iterator
+{
+
+ private javax.jcr.version.VersionIterator versionIterator;
+
+ public VersionIterator(javax.jcr.version.VersionIterator versionIterator)
+ {
+ this.versionIterator = versionIterator;
+ }
+
+ /**
+ *
+ * @see java.util.Iterator#hasNext()
+ */
+ public boolean hasNext()
+ {
+ return versionIterator.hasNext();
+ }
+
+ /**
+ *
+ * @see java.util.Iterator#next()
+ */
+ public Object next()
+ {
+
+ try
+ {
+ Version version = versionIterator.nextVersion();
+ return new org.apache.portals.graffito.jcr.version.Version(version);
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+
+ }
+
+ /**
+ *
+ * @return the versionIterator size
+ */
+ public long getSize()
+ {
+ return versionIterator.getSize();
+ }
+
+ /**
+ *
+ * @see java.util.Iterator#remove()
+ */
+ public void remove()
+ {
+ versionIterator.remove();
+
+ }
+
+}