From commits-return-4500-apmail-jackrabbit-commits-archive=jackrabbit.apache.org@jackrabbit.apache.org Wed Sep 05 21:22:07 2007 Return-Path: Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: (qmail 12160 invoked from network); 5 Sep 2007 21:22:06 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 5 Sep 2007 21:22:06 -0000 Received: (qmail 11199 invoked by uid 500); 5 Sep 2007 21:22:01 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 11173 invoked by uid 500); 5 Sep 2007 21:22:01 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 11164 invoked by uid 99); 5 Sep 2007 21:22:01 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Sep 2007 14:22:01 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Sep 2007 21:22:05 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 9CF521A9832; Wed, 5 Sep 2007 14:21:45 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r573064 - in /jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper: AnnotatedObjectMapper.java AnnotationDescriptorReader.java Date: Wed, 05 Sep 2007 21:21:45 -0000 To: commits@jackrabbit.apache.org From: clombart@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20070905212145.9CF521A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: clombart Date: Wed Sep 5 14:21:44 2007 New Revision: 573064 URL: http://svn.apache.org/viewvc?rev=573064&view=rev Log: Refactor Mapper & reader (issue 1113). Added: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotatedObjectMapper.java jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotationDescriptorReader.java Added: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotatedObjectMapper.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotatedObjectMapper.java?rev=573064&view=auto ============================================================================== --- jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotatedObjectMapper.java (added) +++ jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotatedObjectMapper.java Wed Sep 5 14:21:44 2007 @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.ocm.annotation.mapper; + +import java.util.List; + +import org.apache.jackrabbit.ocm.mapper.Mapper; +import org.apache.jackrabbit.ocm.mapper.impl.AbstractMapperImpl; + +/** + * An implementation of the OCM mapper that builds class descriptors based on + * the use of annotations on the underlying classes + * + * @author Philip Dodds + * + */ +public class AnnotatedObjectMapper extends AbstractMapperImpl implements Mapper { + + + public AnnotatedObjectMapper(List annotatedClassNames) + { + descriptorReader = new AnnotationDescriptorReader(annotatedClassNames); + this.buildMapper(); + } + +} Added: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotationDescriptorReader.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotationDescriptorReader.java?rev=573064&view=auto ============================================================================== --- jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotationDescriptorReader.java (added) +++ jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/annotation/src/main/java/org/apache/jackrabbit/ocm/annotation/mapper/AnnotationDescriptorReader.java Wed Sep 5 14:21:44 2007 @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.ocm.annotation.mapper; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.util.List; + +import org.apache.jackrabbit.ocm.annotation.Bean; +import org.apache.jackrabbit.ocm.annotation.Collection; +import org.apache.jackrabbit.ocm.annotation.Field; +import org.apache.jackrabbit.ocm.annotation.Node; +import org.apache.jackrabbit.ocm.mapper.DescriptorReader; +import org.apache.jackrabbit.ocm.mapper.model.BeanDescriptor; +import org.apache.jackrabbit.ocm.mapper.model.ClassDescriptor; +import org.apache.jackrabbit.ocm.mapper.model.CollectionDescriptor; +import org.apache.jackrabbit.ocm.mapper.model.FieldDescriptor; +import org.apache.jackrabbit.ocm.mapper.model.MappingDescriptor; +import org.apache.jackrabbit.ocm.reflection.ReflectionUtils; + +/** + * Helper class that reads the xml mapping file and load all class descriptors into memory (object graph) + * + * @author Lombart Christophe + * + */ +public class AnnotationDescriptorReader implements DescriptorReader +{ + List annotatedClassNames; + public AnnotationDescriptorReader(List annotatedClassNames) + { + this.annotatedClassNames = annotatedClassNames; + } + + + + public MappingDescriptor loadClassDescriptors() + { + MappingDescriptor mappingDescriptor = new MappingDescriptor(); + for (String className : annotatedClassNames) { + Class clazz = ReflectionUtils.forName(className); + ClassDescriptor classDescriptor = buildClassDescriptor(mappingDescriptor, clazz); + mappingDescriptor.addClassDescriptor(classDescriptor); + } + return mappingDescriptor; + + } + + private ClassDescriptor buildClassDescriptor(MappingDescriptor mappingDescriptor, Class clazz) + { + Node annotationNode = (Node) clazz.getAnnotation(Node.class); + ClassDescriptor descriptor = new ClassDescriptor(); + descriptor.setClassName(clazz.getName()); + descriptor.setJcrType(annotationNode.jcrType()); + descriptor.setJcrSuperTypes(annotationNode.jcrSuperTypes()); + descriptor.setJcrMixinTypes(annotationNode.jcrMixinTypes()); + descriptor.setExtend(annotationNode.extend()); + descriptor.setAbstract(annotationNode.isAbstract()); + descriptor.setInterface(clazz.isInterface()); + + addFieldDescriptors(descriptor, clazz); + addBeanDescriptors(descriptor, clazz); + addCollectionDescriptors(mappingDescriptor, descriptor, clazz); + return descriptor; + } + + private void addCollectionDescriptors(MappingDescriptor mappingDescriptor, ClassDescriptor descriptor,Class clazz) { + BeanInfo beanInfo; + try { + beanInfo = Introspector.getBeanInfo(clazz); + } catch (IntrospectionException e) { + throw new RuntimeException(e); + } + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + Collection jcrChildNode = propertyDescriptor.getReadMethod().getAnnotation(Collection.class); + if (jcrChildNode != null) { + Class targetClass = jcrChildNode.type(); + CollectionDescriptor collectionDescriptor = new CollectionDescriptor(); + ClassDescriptor classDescriptor = mappingDescriptor.getClassDescriptorByName(targetClass.getName()); + + if (classDescriptor == null) + throw new RuntimeException( + "Unable to reference class " + + targetClass.getName() + + " as a child node since it has not been registered, ordering perhaps?"); + + if (jcrChildNode.jcrName() != null && ! jcrChildNode.jcrName().equals("")) + { + collectionDescriptor.setJcrName(jcrChildNode.jcrName()); + } + else + { + collectionDescriptor.setJcrName(propertyDescriptor.getName()); + } + + Node annotationNode = (Node) targetClass.getAnnotation(Node.class); + collectionDescriptor.setDefaultPrimaryType(annotationNode.jcrType()); + collectionDescriptor.setJcrSameNameSiblings(jcrChildNode.sameNameSiblings()); + collectionDescriptor.setJcrAutoCreated(jcrChildNode.autoCreate()); + collectionDescriptor.setJcrProtected(jcrChildNode.protect()); + collectionDescriptor.setJcrOnParentVersion(jcrChildNode.onParentVersion()); + collectionDescriptor.setJcrMandatory(jcrChildNode.mandatory()); + collectionDescriptor.setAutoInsert(jcrChildNode.autoInsert()); + collectionDescriptor.setAutoRetrieve(jcrChildNode.autoRetrieve()); + collectionDescriptor.setAutoUpdate(jcrChildNode.autoUpdate()); + collectionDescriptor.setCollectionClassName(propertyDescriptor.getReadMethod().getReturnType().getName()); + collectionDescriptor.setElementClassName(targetClass.getName()); + collectionDescriptor.setCollectionConverter(jcrChildNode.converter().getName()); + collectionDescriptor.setFieldName(propertyDescriptor.getName()); + + collectionDescriptor.setJcrType(annotationNode.jcrType()); + collectionDescriptor.setJcrSameNameSiblings(jcrChildNode.sameNameSiblings()); + collectionDescriptor.setProxy(jcrChildNode.proxy()); + + descriptor.addCollectionDescriptor(collectionDescriptor); + } + } + + } + + + private void addBeanDescriptors(ClassDescriptor descriptor,Class clazz) { + BeanInfo beanInfo; + try { + beanInfo = Introspector.getBeanInfo(clazz); + } catch (IntrospectionException e) { + throw new RuntimeException(e); + } + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + Bean jcrChildNode = propertyDescriptor.getReadMethod().getAnnotation(Bean.class); + if (jcrChildNode != null) { + + BeanDescriptor beanDescriptor = new BeanDescriptor(); + beanDescriptor.setFieldName(propertyDescriptor.getName()); + if (jcrChildNode.jcrName() != null && ! jcrChildNode.jcrName().equals("")) + { + beanDescriptor.setJcrName(jcrChildNode.jcrName()); + } + else + { + beanDescriptor.setJcrName(propertyDescriptor.getName()); + } + + beanDescriptor.setProxy(jcrChildNode.proxy()); + beanDescriptor.setConverter(jcrChildNode.converter().getName()); + beanDescriptor.setAutoInsert(jcrChildNode.autoInsert()); + beanDescriptor.setAutoRetrieve(jcrChildNode.autoRetrieve()); + beanDescriptor.setAutoUpdate(jcrChildNode.autoUpdate()); + beanDescriptor.setJcrType(jcrChildNode.jcrType()); + beanDescriptor.setJcrAutoCreated(jcrChildNode.jcrAutoCreated()); + beanDescriptor.setJcrMandatory(jcrChildNode.jcrMandatory()); + beanDescriptor.setJcrOnParentVersion(jcrChildNode.jcrOnParentVersion()); + beanDescriptor.setJcrProtected(jcrChildNode.jcrProtected()); + beanDescriptor.setJcrSameNameSiblings(jcrChildNode.jcrSameNameSiblings()); + + descriptor.addBeanDescriptor(beanDescriptor); + } + } + + } + + private void addFieldDescriptors(ClassDescriptor descriptor, Class clazz) { + BeanInfo beanInfo; + try { + beanInfo = Introspector.getBeanInfo(clazz); + } catch (IntrospectionException e) { + throw new RuntimeException(e); + } + + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + Field jcrProperty = propertyDescriptor.getReadMethod().getAnnotation(Field.class); + if (jcrProperty != null) { + FieldDescriptor fieldDescriptor = new FieldDescriptor(); + fieldDescriptor.setFieldName(propertyDescriptor.getName()); + if ((jcrProperty.jcrName() != null) && (!jcrProperty.jcrName().equals(""))) + { + fieldDescriptor.setJcrName(jcrProperty.jcrName()); + } + else + { + fieldDescriptor.setJcrName(propertyDescriptor.getName()); + } + fieldDescriptor.setId(jcrProperty.id()); + fieldDescriptor.setPath(jcrProperty.path()); + fieldDescriptor.setUuid(jcrProperty.uuid()); + + // It is not possible to set a null value into a annotation attribute. + // If the converter == Object.class, it shoudl be considered as null + if (! jcrProperty.converter().equals(Object.class)) + { + fieldDescriptor.setConverter(jcrProperty.converter().getName()); + } + fieldDescriptor.setJcrDefaultValue(jcrProperty.jcrDefaultValue()); + fieldDescriptor.setJcrValueConstraints(jcrProperty.jcrDefaultValue()); + fieldDescriptor.setJcrType(jcrProperty.jcrType()); + + fieldDescriptor.setJcrAutoCreated(jcrProperty.jcrAutoCreated()); + fieldDescriptor.setJcrMandatory(jcrProperty.jcrMandatory()); + fieldDescriptor.setJcrOnParentVersion(jcrProperty.jcrOnParentVersion()); + fieldDescriptor.setJcrProtected(jcrProperty.jcrProtected()); + fieldDescriptor.setJcrMultiple(jcrProperty.jcrMultiple()); + + //fieldDescriptor.setJcrType(value) + descriptor.addFieldDescriptor(fieldDescriptor); + } + } + + } + + +}