incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apope...@apache.org
Subject svn commit: r375681 - in /incubator/graffito/trunk/jcr/jcr-mapping/src: java/org/apache/portals/graffito/jcr/mapper/impl/ test/org/apache/portals/graffito/jcr/ test/org/apache/portals/graffito/jcr/mapper/
Date Tue, 07 Feb 2006 20:10:08 GMT
Author: apopescu
Date: Tue Feb  7 12:10:06 2006
New Revision: 375681

URL: http://svn.apache.org/viewcvs?rev=375681&view=rev
Log:
DigesterMapper refactored to allow more configurability (a la DI/IoC)

Modified:
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterMapperImpl.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/TestBase.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/DigesterMapperImplTest.java

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java?rev=375681&r1=375680&r2=375681&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java
Tue Feb  7 12:10:06 2006
@@ -31,10 +31,20 @@
  * Helper class that reads the xml mapping file and load all class descriptors into memory
(object graph)
  * 
  * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
- * 
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
 public class DigesterDescriptorReader
 {
+    private boolean validating = true;
+    
+    /**
+     * Set if the mapping should be validated.
+     * @param flag <tt>true</tt> if the mapping should be validated
+     */
+    public void setValidating(boolean flag) {
+        this.validating= flag;
+    }
+
 	/**
 	 * Load all class descriptors found in the xml mapping file.
 	 * 
@@ -42,12 +52,12 @@
 	 * @return a {@link MappingDescriptor}
 	 * 
 	 */
-	public static MappingDescriptor loadClassDescriptors(InputStream stream)
+	public MappingDescriptor loadClassDescriptors(InputStream stream)
 	{
 		try
 		{
 			Digester digester = new Digester();
-			digester.setValidating(true);
+			digester.setValidating(this.validating);
 
 			digester.addObjectCreate("graffito-jcr", MappingDescriptor.class);
 
@@ -79,7 +89,8 @@
             digester.addSetProperties("graffito-jcr/class-descriptor/field-descriptor", "jcrOnParentVersion",
"jcrOnParentVersion");
             digester.addSetProperties("graffito-jcr/class-descriptor/field-descriptor", "jcrProtected",
"jcrProtected");
             digester.addSetProperties("graffito-jcr/class-descriptor/field-descriptor", "jcrMultiple",
"jcrMultiple");
-			digester.addSetNext("graffito-jcr/class-descriptor/field-descriptor", "addFieldDescriptor");
+
+            digester.addSetNext("graffito-jcr/class-descriptor/field-descriptor", "addFieldDescriptor");
 
 			// --------------------------------------------------------------------------------
 			// Rules used for the bean-descriptor element
@@ -97,7 +108,8 @@
             digester.addSetProperties("graffito-jcr/class-descriptor/bean-descriptor", "jcrOnParentVersion",
"jcrOnParentVersion");
             digester.addSetProperties("graffito-jcr/class-descriptor/bean-descriptor", "jcrProtected",
"jcrProtected");
             digester.addSetProperties("graffito-jcr/class-descriptor/bean-descriptor", "jcrSameNameSiblings",
"jcrSameNameSiblings");
-			digester.addSetNext("graffito-jcr/class-descriptor/bean-descriptor", "addBeanDescriptor");
+			
+            digester.addSetNext("graffito-jcr/class-descriptor/bean-descriptor", "addBeanDescriptor");
 
 			// --------------------------------------------------------------------------------
 			// Rules used for the collection-descriptor element
@@ -118,8 +130,7 @@
             digester.addSetProperties("graffito-jcr/class-descriptor/collection-descriptor",
"jcrSameNameSiblings", "jcrSameNameSiblings");            
 			digester.addSetNext("graffito-jcr/class-descriptor/collection-descriptor", "addCollectionDescriptor");
 
-			MappingDescriptor mappingDescriptor = (MappingDescriptor) digester.parse(stream);
-			return mappingDescriptor;
+			return (MappingDescriptor) digester.parse(stream);
 		}
 		catch (Exception e)
 		{
@@ -134,7 +145,7 @@
 	 * @return a {@link MappingDescriptor}
 	 * 
 	 */	
-	public static MappingDescriptor loadClassDescriptors(String xmlFile)
+	public MappingDescriptor loadClassDescriptors(String xmlFile)
 	{
 		try
 		{

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterMapperImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterMapperImpl.java?rev=375681&r1=375680&r2=375681&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterMapperImpl.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterMapperImpl.java
Tue Feb  7 12:10:06 2006
@@ -1,104 +1,157 @@
-/*
- * 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.mapper.impl;
-
-import java.io.InputStream;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.portals.graffito.jcr.mapper.Mapper;
-import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
-import org.apache.portals.graffito.jcr.mapper.model.MappingDescriptor;
-
-/**
- *
- * Digester implementation for {@link org.apache.portals.graffito.jcr.mapper.Mapper}
- * 
- * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
- */
-public class DigesterMapperImpl implements Mapper
-{    
-    private MappingDescriptor mappingDescriptor;
-    private Log log = LogFactory.getLog(DigesterMapperImpl.class); 
-    
-    /**
-     * Constructor
-     *  
-     * @param xmlFile The xml mapping file to read
-     *
-     */
-    public DigesterMapperImpl(String xmlFile)
-    {
-        log.info("Read the xml mapping file : " +  xmlFile);
-        mappingDescriptor = DigesterDescriptorReader.loadClassDescriptors(xmlFile);
-    }
-    
-    /**
-     * Constructor 
-     * 
-     * @param files a set of xml mapping files to read
-     *
-     */
-    public DigesterMapperImpl(String[] files)
-    {
-    	log.info("Read the xml mapping file : " +  files[0]);
-        mappingDescriptor = DigesterDescriptorReader.loadClassDescriptors(files[0]);
-        for (int i=1; i<files.length;i++)
-        {
-        	log.info("Read the xml mapping file : " +  files[i]);
-            MappingDescriptor anotherMappingDescriptor = DigesterDescriptorReader.loadClassDescriptors(files[i]);
-            mappingDescriptor.getClassDescriptors().putAll(anotherMappingDescriptor.getClassDescriptors());
-        }
-    }
-    
-    /**
-     * Constructor
-     *  
-     * @param stream The xml mapping file to read
-     */
-    public DigesterMapperImpl(InputStream stream)
-    {
-        log.info("Read the input stream : " +  stream.toString());
-        mappingDescriptor = DigesterDescriptorReader.loadClassDescriptors(stream);
-    }
-
-    /**
-     * Constructor 
-     * 
-     * @param streams a set of mapping files to read
-     *
-     */
-    public DigesterMapperImpl(InputStream[] streams)
-    {
-    	log.info("Read the input stream : " +  streams[0].toString());
-        mappingDescriptor = DigesterDescriptorReader.loadClassDescriptors(streams[0]);
-        for (int i=1; i<streams.length;i++)
-        {
-        	log.info("Read the input stream : " +  streams[i].toString());
-            MappingDescriptor anotherMappingDescriptor = DigesterDescriptorReader.loadClassDescriptors(streams[i]);
-            mappingDescriptor.getClassDescriptors().putAll(anotherMappingDescriptor.getClassDescriptors());
-        }
-    }
-
-    /**
-     * 
-     * @see org.apache.portals.graffito.jcr.mapper.Mapper#getClassDescriptor(java.lang.Class)
-     */
-    public ClassDescriptor getClassDescriptor(Class clazz) 
-    {        
-        return mappingDescriptor.getClassDescriptor(clazz.getName());
-    }
-}
+/*
+ * 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.mapper.impl;
+
+
+
+import java.io.InputStream;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.portals.graffito.jcr.exception.InitMapperException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.MappingDescriptor;
+
+/**
+ *
+ * Digester implementation for {@link org.apache.portals.graffito.jcr.mapper.Mapper}
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
+ */
+public class DigesterMapperImpl implements Mapper {
+    private static final Log log = LogFactory.getLog(DigesterMapperImpl.class);
+
+    private MappingDescriptor mappingDescriptor;
+
+    private String[] mappingFiles;
+    private InputStream[] mappingStreams;
+    private DigesterDescriptorReader descriptorReader;
+
+    /**
+     * No-arg constructor.
+     */
+    public DigesterMapperImpl() {
+    }
+
+    /**
+     * Constructor
+     *
+     * @param xmlFile The xml mapping file to read
+     *
+     */
+    public DigesterMapperImpl(String xmlFile) {
+        this.mappingFiles = new String[] { xmlFile };
+    }
+
+    /**
+     * Constructor
+     *
+     * @param files a set of xml mapping files to read
+     *
+     */
+    public DigesterMapperImpl(String[] files) {
+        this.mappingFiles = files;
+    }
+
+    /**
+     * Constructor
+     *
+     * @param stream The xml mapping file to read
+     */
+    public DigesterMapperImpl(InputStream stream) {
+        this.mappingStreams = new InputStream[] { stream };
+    }
+
+    /**
+     * Constructor
+     *
+     * @param streams a set of mapping files to read
+     *
+     */
+    public DigesterMapperImpl(InputStream[] streams) {
+        this.mappingStreams = streams;
+    }
+
+    /**
+     * Set a mapping file.
+     * 
+     * @param file path to mapping file
+     */
+    public void setMappingFile(String file) {
+        setMappingFiles(new String[] { file });
+    }
+
+    /**
+     * 
+     * @param files
+     */
+    public void setMappingFiles(String[] files) {
+        this.mappingFiles = files;
+    }
+
+    public void setMappingStream(InputStream stream) {
+        setMappingStreams(new InputStream[] { stream });
+    }
+
+    public void setMappingStreams(InputStream[] streams) {
+        this.mappingStreams = streams;
+    }
+
+    public void setDescriptorReader(DigesterDescriptorReader reader) {
+        this.descriptorReader = reader;
+    }
+
+    public Mapper buildMapper() {
+        if (this.descriptorReader == null) {
+            this.descriptorReader = new DigesterDescriptorReader();
+        }
+        if (this.mappingFiles != null && this.mappingFiles.length > 0) {
+            log.info("Read the xml mapping file : " +  this.mappingFiles[0]);
+            this.mappingDescriptor = this.descriptorReader.loadClassDescriptors(this.mappingFiles[0]);
+
+            for (int i = 1; i < this.mappingFiles.length; i++) {
+                log.info("Read the xml mapping file : " +  this.mappingFiles[i]);
+                MappingDescriptor anotherMappingDescriptor = this.descriptorReader.loadClassDescriptors(this.mappingFiles[i]);
+                this.mappingDescriptor.getClassDescriptors().putAll(anotherMappingDescriptor.getClassDescriptors());
+            }
+        }
+        else if (this.mappingStreams != null && this.mappingStreams.length > 0)
{
+            log.info("Read the stream mapping file : " +  this.mappingStreams[0].toString());
+            this.mappingDescriptor = this.descriptorReader.loadClassDescriptors(this.mappingStreams[0]);
+
+            for (int i = 1; i < this.mappingStreams.length; i++) {
+                log.info("Read the stream mapping file : " +  this.mappingStreams[i].toString());
+                MappingDescriptor anotherMappingDescriptor = this.descriptorReader.loadClassDescriptors(this.mappingStreams[i]);
+                this.mappingDescriptor.getClassDescriptors().putAll(anotherMappingDescriptor.getClassDescriptors());
+            }
+        }
+        else {
+            throw new InitMapperException("No mappings were provided");
+        }
+        
+        return this;
+    }
+
+    /**
+     *
+     * @see org.apache.portals.graffito.jcr.mapper.Mapper#getClassDescriptor(java.lang.Class)
+     */
+    public ClassDescriptor getClassDescriptor(Class clazz) {
+        return mappingDescriptor.getClassDescriptor(clazz.getName());
+    }
+}

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/TestBase.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/TestBase.java?rev=375681&r1=375680&r2=375681&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/TestBase.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/TestBase.java
Tue Feb  7 12:10:06 2006
@@ -44,6 +44,7 @@
 import org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl;
 import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
 import org.apache.jackrabbit.core.nodetype.xml.NodeTypeReader;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
 import org.apache.portals.graffito.jcr.mapper.impl.DigesterMapperImpl;
 import org.apache.portals.graffito.jcr.persistence.PersistenceManager;
 import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterProvider;
@@ -83,7 +84,7 @@
 
 	private QueryManager queryManager;
 
-	DigesterMapperImpl mapper;
+	Mapper mapper;
 
 	private boolean isInit = false;
 
@@ -179,7 +180,7 @@
 		String[] files = { "./src/test-config/jcrmapping.xml", "./src/test-config/jcrmapping-atomic.xml"
};
 		session = RepositoryUtil.login(repository, "superuser", "superuser");
 		
-		mapper = new DigesterMapperImpl(files);
+		mapper = new DigesterMapperImpl(files).buildMapper();
         AtomicTypeConverterProvider converterProvider = new DefaultAtomicTypeConverterProvider();
         Map atomicTypeConverters = converterProvider.getAtomicTypeConverters();
 		queryManager = new QueryManagerImpl(mapper, atomicTypeConverters);

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/DigesterMapperImplTest.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/DigesterMapperImplTest.java?rev=375681&r1=375680&r2=375681&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/DigesterMapperImplTest.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/DigesterMapperImplTest.java
Tue Feb  7 12:10:06 2006
@@ -46,22 +46,6 @@
         super(testName);
     }
 
-    /**
-     * @see junit.framework.TestCase#setUp()
-     */
-    protected void setUp() throws Exception
-    {
-        super.setUp();
-    }
-
-    /**
-     * @see junit.framework.TestCase#tearDown()
-     */
-    public void tearDown() throws Exception
-    {
-        super.tearDown();
-    }
-
     public static Test suite()
     {
         // All methods starting with "test" will be executed in the test suite.
@@ -76,7 +60,8 @@
     {
         try
         {
-            DigesterMapperImpl mapper = new DigesterMapperImpl("./src/test-config/jcrmapping-testmappings.xml");
+            Mapper mapper = new DigesterMapperImpl("./src/test-config/jcrmapping-testmappings.xml")
+                .buildMapper();
             assertNotNull("Mapper is null", mapper);
             
             ClassDescriptor classDescriptor = mapper.getClassDescriptor(A.class);
@@ -116,7 +101,8 @@
     {
         try
         {
-            DigesterMapperImpl mapper = new DigesterMapperImpl("./src/test-config/jcrmapping.xml");
+            Mapper mapper = new DigesterMapperImpl("./src/test-config/jcrmapping.xml")
+                    .buildMapper();
             assertNotNull("Mapper is null", mapper);
             
             ClassDescriptor classDescriptor = mapper.getClassDescriptor(B.class);



Mime
View raw message