jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r573060 - in /jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper: ./ impl/ model/
Date Wed, 05 Sep 2007 21:15:57 GMT
Author: clombart
Date: Wed Sep  5 14:15:56 2007
New Revision: 573060

URL: http://svn.apache.org/viewvc?rev=573060&view=rev
Log:
Refactor Mapper & reader (issue 1113). 

Added:
    jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/DescriptorReader.java
    jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/AbstractMapperImpl.java
Modified:
    jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterDescriptorReader.java
    jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterMapperImpl.java
    jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/model/MappingDescriptor.java

Added: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/DescriptorReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/DescriptorReader.java?rev=573060&view=auto
==============================================================================
--- jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/DescriptorReader.java
(added)
+++ jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/DescriptorReader.java
Wed Sep  5 14:15:56 2007
@@ -0,0 +1,20 @@
+package org.apache.jackrabbit.ocm.mapper;
+
+import org.apache.jackrabbit.ocm.mapper.impl.AbstractMapperImpl;
+import org.apache.jackrabbit.ocm.mapper.model.MappingDescriptor;
+
+public interface DescriptorReader {
+
+	/**
+	 * Load all class descriptors found in an classdescriptor definition.
+	 * A classdescriptor definition can be a xml config file or annotations 
+	 * or another kind of resource which contain the classdescriptors 
+	 * 
+	 * DescriptorReader is an abstraction used to maximize reusability in {@link AbstractMapperImpl}
+	 * 
+	 * @return a {@link MappingDescriptor} wich will contains a collection of classdescriptors
+	 * 
+	 */
+	public abstract MappingDescriptor loadClassDescriptors();
+
+}
\ No newline at end of file

Added: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/AbstractMapperImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/AbstractMapperImpl.java?rev=573060&view=auto
==============================================================================
--- jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/AbstractMapperImpl.java
(added)
+++ jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/AbstractMapperImpl.java
Wed Sep  5 14:15:56 2007
@@ -0,0 +1,190 @@
+/*
+ * 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.mapper.impl;
+
+
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.ocm.exception.IncorrectPersistentClassException;
+import org.apache.jackrabbit.ocm.exception.InitMapperException;
+import org.apache.jackrabbit.ocm.exception.JcrMappingException;
+import org.apache.jackrabbit.ocm.mapper.DescriptorReader;
+import org.apache.jackrabbit.ocm.mapper.Mapper;
+import org.apache.jackrabbit.ocm.mapper.model.ClassDescriptor;
+import org.apache.jackrabbit.ocm.mapper.model.MappingDescriptor;
+
+/**
+ *
+ * Abstract class for {@link org.apache.jackrabbit.ocm.mapper.Mapper}
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
+ * 
+ */
+public abstract class AbstractMapperImpl implements Mapper {
+    protected static final Log log = LogFactory.getLog(AbstractMapperImpl.class);
+
+    protected DescriptorReader descriptorReader;
+    protected MappingDescriptor mappingDescriptor;
+    protected Collection rootClassDescriptors = new ArrayList(); // contains the class descriptor
which have not ancestors 
+
+    public void buildMapper() 
+    {
+    	mappingDescriptor = descriptorReader.loadClassDescriptors();
+    	mappingDescriptor.setMapper(this);
+    	
+        if (null != this.mappingDescriptor) 
+        {
+            List errors = new ArrayList();
+            errors =  solveReferences(errors);            
+            errors = validateDescriptors(errors, rootClassDescriptors);
+
+            if (!errors.isEmpty()) 
+            {
+                throw new InitMapperException("Mapping files contain errors."
+                        + getErrorMessage(errors));
+            }
+        }
+        else 
+        {
+            throw new InitMapperException("No mappings were provided");
+        }
+                
+    }
+
+
+    protected List solveReferences(List errors) {
+        for(Iterator it = this.mappingDescriptor.getClassDescriptorsByClassName().entrySet().iterator();
it.hasNext(); ) {
+            Map.Entry entry = (Map.Entry) it.next();
+            ClassDescriptor cd = (ClassDescriptor) entry.getValue();
+
+            if (null != cd.getExtend() && !"".equals(cd.getExtend())) 
+            {
+                ClassDescriptor superClassDescriptor = this.mappingDescriptor.getClassDescriptorByName(cd.getExtend());
+
+                if (null == superClassDescriptor) 
+                {
+                    errors.add("Cannot find mapping for class "
+                            + cd.getExtend()
+                            + " referenced as extends from "
+                            + cd.getClassName());
+                }
+                else 
+                {
+            	       log.debug("Class " +cd.getClassName() +  " extends " + cd.getExtend());
+                    cd.setSuperClassDescriptor(superClassDescriptor);
+                }
+            }
+            else
+            {
+                   rootClassDescriptors.add(cd);
+            }
+            
+            Collection interfaces = cd.getImplements();
+            if (interfaces.size() > 0) 
+            {	
+            	      for (Iterator iterator = interfaces.iterator(); iterator.hasNext();)
+            	      {
+            	    	          String interfaceName= (String) iterator.next();
+                          ClassDescriptor interfaceClassDescriptor = this.mappingDescriptor.getClassDescriptorByName(interfaceName);
+
+                          if (null == interfaceClassDescriptor) 
+                          {
+                              errors.add("Cannot find mapping for interface "
+                                      + interfaceName
+                                      + " referenced as implements from "
+                                      + cd.getClassName());
+                          }
+                          else 
+                          {
+                      	       log.debug("Class " +cd.getClassName() +  " implements " + interfaceName);
+                              //cd.setSuperClassDescriptor(interfaceClassDescriptor);
+                      	      interfaceClassDescriptor.addDescendantClassDescriptor(cd); 
+                          }
+            	    	      
+            	      }
+            }
+            
+        }
+
+        return errors;
+    }
+    
+    /**
+     * Validate all class descriptors.
+     * This method validates the toplevel ancestors and after the descendants. 
+     * Otherwise, we can have invalid settings in the class descriptors
+     * @param errors all errors found during the validation process
+     * @param classDescriptors the ancestor classdescriptors
+     * @return
+     */
+    protected List  validateDescriptors(List errors, Collection classDescriptors ) {
+        for(Iterator it = classDescriptors.iterator(); it.hasNext(); ) {
+            ClassDescriptor classDescriptor = (ClassDescriptor) it.next();
+            try {
+                classDescriptor.afterPropertiesSet();
+                if (classDescriptor.hasDescendants()) {
+                    errors = validateDescriptors(errors, classDescriptor.getDescendantClassDescriptors());
+                }
+            }
+            catch(JcrMappingException jme) {
+                log.warn("Mapping of class " + classDescriptor.getClassName() + " is invalid",
jme);
+                errors.add(jme.getMessage());
+            }
+        }
+        return errors;
+    }    
+    
+    protected String getErrorMessage(List errors) {
+        final String lineSep = System.getProperty("line.separator");
+        StringBuffer buf = new StringBuffer();
+        for(Iterator it = errors.iterator(); it.hasNext();) {
+            buf.append(lineSep).append(it.next());
+        }
+
+        return buf.toString();
+    }    
+    
+    /**
+    *
+    * @see org.apache.jackrabbit.ocm.mapper.Mapper#getClassDescriptorByClass(java.lang.Class)
+    */
+   public ClassDescriptor getClassDescriptorByClass(Class clazz) {
+	   ClassDescriptor descriptor = mappingDescriptor.getClassDescriptorByName(clazz.getName());
+	   if (descriptor==null) {
+			throw new IncorrectPersistentClassException("Class of type: " + clazz.getName() + " has
no descriptor.");
+	   }
+       return descriptor ; 
+   }
+   
+   /**
+   * @see org.apache.jackrabbit.ocm.mapper.Mapper#getClassDescriptorByNodeType(String)
+   */
+  public ClassDescriptor getClassDescriptorByNodeType(String jcrNodeType) {
+	  ClassDescriptor descriptor = mappingDescriptor.getClassDescriptorByNodeType(jcrNodeType);
+	   if (descriptor==null) {
+			throw new IncorrectPersistentClassException("Node type: " + jcrNodeType + " has no descriptor.");
+	   }
+      return descriptor ;      
+  }   
+}

Modified: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterDescriptorReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterDescriptorReader.java?rev=573060&r1=573059&r2=573060&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterDescriptorReader.java
(original)
+++ jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterDescriptorReader.java
Wed Sep  5 14:15:56 2007
@@ -20,9 +20,13 @@
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
 
 import org.apache.commons.digester.Digester;
 import org.apache.jackrabbit.ocm.exception.InitMapperException;
+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;
@@ -36,11 +40,57 @@
  * @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
+public class DigesterDescriptorReader implements DescriptorReader
 {
     private boolean validating = true;
     private URL dtdResolver;
     
+    private Collection configFileStreams = new ArrayList();
+    
+    public DigesterDescriptorReader(InputStream stream)
+    {
+        configFileStreams.add(stream);	
+    }
+    
+    public DigesterDescriptorReader(InputStream[] streams)
+    {
+        for (int i = 0; i < streams.length; i++) 
+        {
+        	configFileStreams.add(streams[i]);	
+		}
+    	
+    }
+    
+    public DigesterDescriptorReader(String xmlFile)
+    {
+    	try
+		{
+    		configFileStreams.add(new FileInputStream(xmlFile));
+		}		
+		catch (FileNotFoundException e)
+		{
+			throw new InitMapperException("Mapping file not found : " + xmlFile,e);
+		}
+    } 
+    
+    public DigesterDescriptorReader(String[] xmlFiles)
+    {
+   	
+    	for (int i = 0; i < xmlFiles.length; i++) 
+    	{
+        	try
+    		{
+        		configFileStreams.add(new FileInputStream(xmlFiles[i]));
+    		}
+    		
+    		catch (FileNotFoundException e)
+    		{
+    			throw new InitMapperException("Mapping file not found : " + xmlFiles[i],e);
+    		}    				
+		}
+    } 
+    
+    
     /**
      * Set if the mapping should be validated.
      * @param flag <tt>true</tt> if the mapping should be validated
@@ -56,86 +106,78 @@
 	/**
 	 * Load all class descriptors found in the xml mapping file.
 	 * 
-	 * @param stream the xml mapping file reference
 	 * @return a {@link MappingDescriptor}
 	 * 
 	 */
-	public MappingDescriptor loadClassDescriptors(InputStream stream)
+	public MappingDescriptor loadClassDescriptors()
 	{
 		try
 		{
-			Digester digester = new Digester();
-			digester.setValidating(this.validating);
-			if (null != this.dtdResolver) {
-                digester.register("-//The Apache Software Foundation//DTD Repository//EN",
-                                  this.dtdResolver.toString());
-            }
-	        digester.setClassLoader(getClass().getClassLoader());
-			
-	        MappingDescriptor mappingDescriptor = new MappingDescriptor();
-	        digester.push(mappingDescriptor);
-			
-
-			// --------------------------------------------------------------------------------
-			// Rules used for the class-descriptor element
-			// --------------------------------------------------------------------------------	 
                      
-			digester.addObjectCreate("jackrabbit-ocm/class-descriptor", ClassDescriptor.class);
-			digester.addSetProperties("jackrabbit-ocm/class-descriptor");
-			digester.addSetNext("jackrabbit-ocm/class-descriptor", "addClassDescriptor");		
-
-			// --------------------------------------------------------------------------------
-			// Rules used for the implement-descriptor element
-			// --------------------------------------------------------------------------------
-			digester.addObjectCreate("jackrabbit-ocm/class-descriptor/implement-descriptor", ImplementDescriptor.class);
-			digester.addSetProperties("jackrabbit-ocm/class-descriptor/implement-descriptor");
-            digester.addSetNext("jackrabbit-ocm/class-descriptor/implement-descriptor", "addImplementDescriptor");
-			
-			// --------------------------------------------------------------------------------
-			// Rules used for the field-descriptor element
-			// --------------------------------------------------------------------------------
-			digester.addObjectCreate("jackrabbit-ocm/class-descriptor/field-descriptor", FieldDescriptor.class);
-			digester.addSetProperties("jackrabbit-ocm/class-descriptor/field-descriptor");
-            digester.addSetNext("jackrabbit-ocm/class-descriptor/field-descriptor", "addFieldDescriptor");
-
-			// --------------------------------------------------------------------------------
-			// Rules used for the bean-descriptor element
-			// --------------------------------------------------------------------------------
-			digester.addObjectCreate("jackrabbit-ocm/class-descriptor/bean-descriptor", BeanDescriptor.class);
-			digester.addSetProperties("jackrabbit-ocm/class-descriptor/bean-descriptor");
-            digester.addSetNext("jackrabbit-ocm/class-descriptor/bean-descriptor", "addBeanDescriptor");
-
-			// --------------------------------------------------------------------------------
-			// Rules used for the collection-descriptor element
-			// --------------------------------------------------------------------------------
-			digester.addObjectCreate("jackrabbit-ocm/class-descriptor/collection-descriptor", CollectionDescriptor.class);
-			digester.addSetProperties("jackrabbit-ocm/class-descriptor/collection-descriptor");
-            digester.addSetNext("jackrabbit-ocm/class-descriptor/collection-descriptor",
"addCollectionDescriptor");			
-
-            return (MappingDescriptor) digester.parse(stream);
+			MappingDescriptor mappingDescriptor = new MappingDescriptor();
+			for (Iterator iter = configFileStreams.iterator(); iter.hasNext();) 
+			{
+				InputStream xmlMappingDescriptorFile = (InputStream) iter.next();
+				Digester digester = new Digester();
+				digester.setValidating(this.validating);
+				if (null != this.dtdResolver) 
+				{
+	                digester.register("-//The Apache Software Foundation//DTD Repository//EN",
+	                                  this.dtdResolver.toString());
+	            }
+		        digester.setClassLoader(getClass().getClassLoader());
+				
+		        MappingDescriptor currentMappingDescriptor = new MappingDescriptor();
+		        digester.push(currentMappingDescriptor);
+				
+
+				// --------------------------------------------------------------------------------
+				// Rules used for the class-descriptor element
+				// --------------------------------------------------------------------------------	
                       
+				digester.addObjectCreate("jackrabbit-ocm/class-descriptor", ClassDescriptor.class);
+				digester.addSetProperties("jackrabbit-ocm/class-descriptor");
+				digester.addSetNext("jackrabbit-ocm/class-descriptor", "addClassDescriptor");		
+
+				// --------------------------------------------------------------------------------
+				// Rules used for the implement-descriptor element
+				// --------------------------------------------------------------------------------
+				digester.addObjectCreate("jackrabbit-ocm/class-descriptor/implement-descriptor", ImplementDescriptor.class);
+				digester.addSetProperties("jackrabbit-ocm/class-descriptor/implement-descriptor");
+	            digester.addSetNext("jackrabbit-ocm/class-descriptor/implement-descriptor",
"addImplementDescriptor");
+				
+				// --------------------------------------------------------------------------------
+				// Rules used for the field-descriptor element
+				// --------------------------------------------------------------------------------
+				digester.addObjectCreate("jackrabbit-ocm/class-descriptor/field-descriptor", FieldDescriptor.class);
+				digester.addSetProperties("jackrabbit-ocm/class-descriptor/field-descriptor");
+	            digester.addSetNext("jackrabbit-ocm/class-descriptor/field-descriptor", "addFieldDescriptor");
+
+				// --------------------------------------------------------------------------------
+				// Rules used for the bean-descriptor element
+				// --------------------------------------------------------------------------------
+				digester.addObjectCreate("jackrabbit-ocm/class-descriptor/bean-descriptor", BeanDescriptor.class);
+				digester.addSetProperties("jackrabbit-ocm/class-descriptor/bean-descriptor");
+	            digester.addSetNext("jackrabbit-ocm/class-descriptor/bean-descriptor", "addBeanDescriptor");
+
+				// --------------------------------------------------------------------------------
+				// Rules used for the collection-descriptor element
+				// --------------------------------------------------------------------------------
+				digester.addObjectCreate("jackrabbit-ocm/class-descriptor/collection-descriptor", CollectionDescriptor.class);
+				digester.addSetProperties("jackrabbit-ocm/class-descriptor/collection-descriptor");
+	            digester.addSetNext("jackrabbit-ocm/class-descriptor/collection-descriptor",
"addCollectionDescriptor");			
+
+	            currentMappingDescriptor = (MappingDescriptor) digester.parse(xmlMappingDescriptorFile);
+				
+				mappingDescriptor.getClassDescriptorsByClassName().putAll(currentMappingDescriptor.getClassDescriptorsByClassName());
+				mappingDescriptor.getClassDescriptorsByNodeType().putAll(currentMappingDescriptor.getClassDescriptorsByNodeType());
+                
+			}
+			return mappingDescriptor;
 		}
 		catch (Exception e)
 		{
-			throw new InitMapperException("Impossible to read the xml mapping file", e);
+			throw new InitMapperException("Impossible to read the xml mapping descriptor file(s)",
e);
 		}
 	}
 
-	/**
-	 * Load all class descriptors found in the xml mapping file.
-	 * 
-	 * @param xmlFile the xml mapping file reference
-	 * @return a {@link MappingDescriptor}
-	 * 
-	 */	
-	public MappingDescriptor loadClassDescriptors(String xmlFile)
-	{
-		try
-		{
-			return loadClassDescriptors(new FileInputStream(xmlFile));
-		}
-		
-		catch (FileNotFoundException e)
-		{
-			throw new InitMapperException("Mapping file not found : " + xmlFile,e);
-		}
-	}
+
 }

Modified: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterMapperImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterMapperImpl.java?rev=573060&r1=573059&r2=573060&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterMapperImpl.java
(original)
+++ jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/impl/DigesterMapperImpl.java
Wed Sep  5 14:15:56 2007
@@ -20,35 +20,21 @@
 
 import java.io.InputStream;
 import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.jackrabbit.ocm.exception.IncorrectPersistentClassException;
+
 import org.apache.jackrabbit.ocm.exception.InitMapperException;
-import org.apache.jackrabbit.ocm.exception.JcrMappingException;
 import org.apache.jackrabbit.ocm.mapper.Mapper;
-import org.apache.jackrabbit.ocm.mapper.model.ClassDescriptor;
 import org.apache.jackrabbit.ocm.mapper.model.MappingDescriptor;
 
 /**
  *
  * Digester implementation for {@link org.apache.jackrabbit.ocm.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 Collection rootClassDescriptors = new ArrayList(); // contains the class descriptor
which have not ancestors 
-
-    private String[] mappingFiles;
-    private InputStream[] mappingStreams;
-    private DigesterDescriptorReader descriptorReader;
+public class DigesterMapperImpl extends AbstractMapperImpl implements Mapper {
+    
 
     /**
      * No-arg constructor.
@@ -63,7 +49,7 @@
      *
      */
     public DigesterMapperImpl(String xmlFile) {
-        this.mappingFiles = new String[] { xmlFile };
+        descriptorReader = new DigesterDescriptorReader(xmlFile);
         this.buildMapper();
     }
 
@@ -74,7 +60,7 @@
      *
      */
     public DigesterMapperImpl(String[] files) {
-        this.mappingFiles = files;
+        descriptorReader = new DigesterDescriptorReader(files);
         this.buildMapper();
     }
 
@@ -84,7 +70,7 @@
      * @param stream The xml mapping file to read
      */
     public DigesterMapperImpl(InputStream stream) {
-        this.mappingStreams = new InputStream[] { stream };
+        descriptorReader = new DigesterDescriptorReader(stream);
         this.buildMapper();
     }
 
@@ -95,199 +81,8 @@
      *
      */
     public DigesterMapperImpl(InputStream[] streams) {
-        this.mappingStreams = streams;
-        this.buildMapper();
-    }
-
-    /**
-     * Set a mapping file.
-     * 
-     * @param file path to mapping file
-     */
-    public void setMappingFile(String file) {
-        setMappingFiles(new String[] { file });
+        descriptorReader = new DigesterDescriptorReader(streams);
         this.buildMapper();
     }
 
-    /**
-     * 
-     * @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;
-    }
-
-    private 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]);
-            this.mappingDescriptor.setMapper(this);
-
-            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.getClassDescriptorsByClassName().putAll(anotherMappingDescriptor.getClassDescriptorsByClassName());
-                this.mappingDescriptor.getClassDescriptorsByNodeType().putAll(anotherMappingDescriptor.getClassDescriptorsByNodeType());
-                
-            }
-        }
-        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]);
-            this.mappingDescriptor.setMapper(this);
-
-            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.getClassDescriptorsByClassName().putAll(anotherMappingDescriptor.getClassDescriptorsByClassName());
-                this.mappingDescriptor.getClassDescriptorsByNodeType().putAll(anotherMappingDescriptor.getClassDescriptorsByNodeType());
-            }
-        }
-        if (null != this.mappingDescriptor) {
-            List errors = new ArrayList();
-            errors =  solveReferences(errors);            
-            errors = validateDescriptors(errors, rootClassDescriptors);
-
-            if (!errors.isEmpty()) {
-                throw new InitMapperException("Mapping files contain errors."
-                        + getErrorMessage(errors));
-            }
-        }
-        else {
-            throw new InitMapperException("No mappings were provided");
-        }
-        
-        return this;
-    }
-
-    private List solveReferences(List errors) {
-        for(Iterator it = this.mappingDescriptor.getClassDescriptorsByClassName().entrySet().iterator();
it.hasNext(); ) {
-            Map.Entry entry = (Map.Entry) it.next();
-            ClassDescriptor cd = (ClassDescriptor) entry.getValue();
-
-            if (null != cd.getExtend() && !"".equals(cd.getExtend())) 
-            {
-                ClassDescriptor superClassDescriptor = this.mappingDescriptor.getClassDescriptorByName(cd.getExtend());
-
-                if (null == superClassDescriptor) 
-                {
-                    errors.add("Cannot find mapping for class "
-                            + cd.getExtend()
-                            + " referenced as extends from "
-                            + cd.getClassName());
-                }
-                else 
-                {
-            	       log.debug("Class " +cd.getClassName() +  " extends " + cd.getExtend());
-                    cd.setSuperClassDescriptor(superClassDescriptor);
-                }
-            }
-            else
-            {
-                   rootClassDescriptors.add(cd);
-            }
-            
-            Collection interfaces = cd.getImplements();
-            if (interfaces.size() > 0) 
-            {	
-            	      for (Iterator iterator = interfaces.iterator(); iterator.hasNext();)
-            	      {
-            	    	          String interfaceName= (String) iterator.next();
-                          ClassDescriptor interfaceClassDescriptor = this.mappingDescriptor.getClassDescriptorByName(interfaceName);
-
-                          if (null == interfaceClassDescriptor) 
-                          {
-                              errors.add("Cannot find mapping for interface "
-                                      + interfaceName
-                                      + " referenced as implements from "
-                                      + cd.getClassName());
-                          }
-                          else 
-                          {
-                      	       log.debug("Class " +cd.getClassName() +  " implements " + interfaceName);
-                              //cd.setSuperClassDescriptor(interfaceClassDescriptor);
-                      	      interfaceClassDescriptor.addDescendantClassDescriptor(cd); 
-                          }
-            	    	      
-            	      }
-            }
-            
-        }
-
-        return errors;
-    }
-    
-    /**
-     * Validate all class descriptors.
-     * This method validates the toplevel ancestors and after the descendants. 
-     * Otherwise, we can have invalid settings in the class descriptors
-     * @param errors all errors found during the validation process
-     * @param classDescriptors the ancestor classdescriptors
-     * @return
-     */
-    private List  validateDescriptors(List errors, Collection classDescriptors ) {
-        for(Iterator it = classDescriptors.iterator(); it.hasNext(); ) {
-            ClassDescriptor classDescriptor = (ClassDescriptor) it.next();
-            try {
-                classDescriptor.afterPropertiesSet();
-                if (classDescriptor.hasDescendants()) {
-                    errors = validateDescriptors(errors, classDescriptor.getDescendantClassDescriptors());
-                }
-            }
-            catch(JcrMappingException jme) {
-                log.warn("Mapping of class " + classDescriptor.getClassName() + " is invalid",
jme);
-                errors.add(jme.getMessage());
-            }
-        }
-        return errors;
-    }    
-    
-    private String getErrorMessage(List errors) {
-        final String lineSep = System.getProperty("line.separator");
-        StringBuffer buf = new StringBuffer();
-        for(Iterator it = errors.iterator(); it.hasNext();) {
-            buf.append(lineSep).append(it.next());
-        }
-
-        return buf.toString();
-    }    
-    
-    /**
-    *
-    * @see org.apache.jackrabbit.ocm.mapper.Mapper#getClassDescriptorByClass(java.lang.Class)
-    */
-   public ClassDescriptor getClassDescriptorByClass(Class clazz) {
-	   ClassDescriptor descriptor = mappingDescriptor.getClassDescriptorByName(clazz.getName());
-	   if (descriptor==null) {
-			throw new IncorrectPersistentClassException("Class of type: " + clazz.getName() + " has
no descriptor.");
-	   }
-       return descriptor ; 
-   }
-   
-   /**
-   * @see org.apache.jackrabbit.ocm.mapper.Mapper#getClassDescriptorByNodeType(String)
-   */
-  public ClassDescriptor getClassDescriptorByNodeType(String jcrNodeType) {
-	  ClassDescriptor descriptor = mappingDescriptor.getClassDescriptorByNodeType(jcrNodeType);
-	   if (descriptor==null) {
-			throw new IncorrectPersistentClassException("Node type: " + jcrNodeType + " has no descriptor.");
-	   }
-      return descriptor ;      
-  }
-   
 }

Modified: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/model/MappingDescriptor.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/model/MappingDescriptor.java?rev=573060&r1=573059&r2=573060&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/model/MappingDescriptor.java
(original)
+++ jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/mapper/model/MappingDescriptor.java
Wed Sep  5 14:15:56 2007
@@ -17,18 +17,19 @@
 package org.apache.jackrabbit.ocm.mapper.model;
 
 
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.jackrabbit.ocm.manager.ManagerConstant;
 import org.apache.jackrabbit.ocm.mapper.Mapper;
-import org.apache.jackrabbit.ocm.mapper.impl.DigesterMapperImpl;
+
 
 /**
- * This class match to the complete xml mapping files.
- * it contains mainly a collection of {@link ClassDescriptor}
+ * MappingDescriptor is an helper class used to access to the {@link ClassDescriptor}
  *
  * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
  * @version $Id: Exp $
@@ -47,6 +48,7 @@
         this.packageName = pckgName;
     }
     
+    
     /**
      * Add a new ClassDescriptor
      *
@@ -87,6 +89,11 @@
         return (ClassDescriptor) classDescriptorsByNodeType.get(nodeType);	
     }
 
+    public Collection getAllClassDescriptors()
+    {
+    	return classDescriptorsByClassName.values();
+    }
+    
     /**
      * Get all class descriptors by class name
      * @return all class descriptors found



Mime
View raw message