cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmen...@apache.org
Subject svn commit: r470645 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src: main/java/org/apache/cayenne/tools/ test/java/org/apache/cayenne/tools/
Date Fri, 03 Nov 2006 04:01:54 GMT
Author: kmenard
Date: Thu Nov  2 20:01:53 2006
New Revision: 470645

URL: http://svn.apache.org/viewvc?view=rev&rev=470645
Log:
Refactoring to cut down on code duplication between cgen ant task and cgen maven2 mojo.  Also,
isolated the ant dependencies to only the ant task.  This is in furtherance of the development
of the cgen mojo (CAY-591).

Added:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntTaskLogger.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGeneratorUtil.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/ILog.java
Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGenerator.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/tools/NamePatternMatcherTst.java

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java?view=diff&rev=470645&r1=470644&r2=470645
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntDataPortDelegate.java
Thu Nov  2 20:01:53 2006
@@ -59,7 +59,7 @@
         this.parentTask = parentTask;
 
         this.namePatternMatcher = new NamePatternMatcher(
-                parentTask,
+                new AntTaskLogger(parentTask),
                 includeEntitiesPattern,
                 excludeEntitiesPattern);
 

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntTaskLogger.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntTaskLogger.java?view=auto&rev=470645
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntTaskLogger.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/AntTaskLogger.java
Thu Nov  2 20:01:53 2006
@@ -0,0 +1,39 @@
+/*****************************************************************
+ *   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.cayenne.tools;
+
+import org.apache.tools.ant.Task;
+
+public class AntTaskLogger implements ILog {
+
+    private Task parentTask;
+    
+    public AntTaskLogger(Task parentTask) {
+        this.parentTask = parentTask;
+    }
+    
+    public void log(String msg) {
+        parentTask.log(msg);
+    }
+
+    public void log(String msg, int msgLevel) {
+        parentTask.log(msg, msgLevel);
+    }
+}

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGenerator.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGenerator.java?view=diff&rev=470645&r1=470644&r2=470645
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGenerator.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGenerator.java
Thu Nov  2 20:01:53 2006
@@ -20,24 +20,13 @@
 package org.apache.cayenne.tools;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
 
 import org.apache.cayenne.gen.AntClassGenerator;
 import org.apache.cayenne.gen.ClassGenerator;
 import org.apache.cayenne.gen.DefaultClassGenerator;
-import org.apache.cayenne.map.DataMap;
-import org.apache.cayenne.map.EntityResolver;
-import org.apache.cayenne.map.MapLoader;
-import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.util.Util;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
 import org.apache.tools.ant.types.Path;
-import org.xml.sax.InputSource;
 
 import foundrylogic.vpp.VPPConfig;
 
@@ -45,7 +34,7 @@
  * Ant task to perform class generation from data map. This class is an Ant adapter to
  * DefaultClassGenerator class.
  * 
- * @author Andrus Adamchik
+ * @author Andrus Adamchik, Kevin Menard
  */
 public class CayenneGenerator extends Task {
 
@@ -57,8 +46,11 @@
     protected File additionalMaps[];
     protected DefaultClassGenerator generator;
 
+    protected CayenneGeneratorUtil generatorUtil;
+    
     public CayenneGenerator() {
         generator = createGenerator();
+        generatorUtil = new CayenneGeneratorUtil();
     }
 
     /**
@@ -75,97 +67,27 @@
      */
     public void execute() throws BuildException {
         validateAttributes();
-
-        try {
-            processMap();
-        }
-        catch (Throwable th) {
-            th = Util.unwindException(th);
-
-            String thMessage = th.getLocalizedMessage();
-            String message = "Error generating classes: ";
-            message += (!Util.isEmptyString(thMessage)) ? thMessage : th
-                    .getClass()
-                    .getName();
-
-            super.log(message);
-            throw new BuildException(message, th);
-        }
-    }
-
-    protected void processMap() throws Exception {
-
-        DataMap dataMap = loadDataMap();
-        DataMap additionalDataMaps[] = loadAdditionalDataMaps();
-
-        // Create MappingNamespace for maps.
-        EntityResolver entityResolver = new EntityResolver(Collections.singleton(dataMap));
-        dataMap.setNamespace(entityResolver);
-        for (int i = 0; i < additionalDataMaps.length; i++) {
-            entityResolver.addDataMap(additionalDataMaps[i]);
-            additionalDataMaps[i].setNamespace(entityResolver);
-        }
-
-        Collection allEntities = dataMap.getObjEntities();
-        List filteredEntities = new ArrayList(allEntities.size());
-
-        // filter client entities
-        if (generator.isClient()) {
-            if (dataMap.isClientSupported()) {
-                Iterator it = allEntities.iterator();
-                while (it.hasNext()) {
-                    ObjEntity entity = (ObjEntity) it.next();
-                    if (entity.isClientAllowed()) {
-                        filteredEntities.add(entity);
-                    }
-                }
-            }
-        }
-        else {
-            filteredEntities.addAll(allEntities);
-        }
-
-        // filter names according to the specified pattern
-        NamePatternMatcher namePatternMatcher = new NamePatternMatcher(
-                this,
-                includeEntitiesPattern,
-                excludeEntitiesPattern);
-        namePatternMatcher.filter(filteredEntities);
-
+        
+        // Take care of setting up VPP for the generator.
         if (false == ClassGenerator.VERSION_1_1.equals(generator.getVersionString())) {
             initializeVppConfig();
             generator.setVppConfig(vppConfig);
         }
+        
+        // Initialize the util generator state.
+        generatorUtil.setAdditionalMaps(additionalMaps);
+        generatorUtil.setExcludeEntitiesPattern(excludeEntitiesPattern);
+        generatorUtil.setGenerator(generator);
+        generatorUtil.setIncludeEntitiesPattern(includeEntitiesPattern);
+        generatorUtil.setLogger(new AntTaskLogger(this));
+        generatorUtil.setMap(map);
 
-        generator.setTimestamp(map.lastModified());
-        generator.setDataMap(dataMap);
-        generator.setObjEntities(filteredEntities);
-        generator.validateAttributes();
-        generator.execute();
-    }
-
-    /** Loads and returns a DataMap by File. */
-    protected DataMap loadDataMap(File mapName) throws Exception {
-        InputSource in = new InputSource(mapName.getCanonicalPath());
-        return new MapLoader().loadDataMap(in);
-    }
-
-    /** Loads and returns DataMap based on <code>map</code> attribute. */
-    protected DataMap loadDataMap() throws Exception {
-        return loadDataMap(map);
-    }
-
-    /** Loads and returns DataMap based on <code>map</code> attribute. */
-    protected DataMap[] loadAdditionalDataMaps() throws Exception {
-        if (null == additionalMaps) {
-            return new DataMap[0];
+        try {
+            generatorUtil.execute();
         }
-
-        DataMap dataMaps[] = new DataMap[additionalMaps.length];
-        for (int i = 0; i < additionalMaps.length; i++) {
-            dataMaps[i] = loadDataMap(additionalMaps[i]);
+        catch (Exception e) {
+            throw new BuildException(e);
         }
-        return dataMaps;
     }
 
     /**
@@ -318,6 +240,10 @@
     public void setMode(String mode) {
         generator.setMode(mode);
     }
+    
+    public void setVppConfig(VPPConfig vppConfig) {
+        this.vppConfig = vppConfig;
+    }
 
     /**
      * Provides a <code>VPPConfig</code> object to configure. (Written with
@@ -329,7 +255,7 @@
         this.vppConfig = new VPPConfig();
         return this.vppConfig;
     }
-
+    
     /**
      * If no VppConfig element specified, use the default one.
      * 

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGeneratorUtil.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGeneratorUtil.java?view=auto&rev=470645
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGeneratorUtil.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/CayenneGeneratorUtil.java
Thu Nov  2 20:01:53 2006
@@ -0,0 +1,167 @@
+/*****************************************************************
+ *   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.cayenne.tools;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.cayenne.gen.DefaultClassGenerator;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.map.MapLoader;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.util.Util;
+import org.xml.sax.InputSource;
+
+/**
+ * Utility class to perform class generation from data map. This class is used by
+ * ant and Maven plugins.
+ * 
+ * @author Andrus Adamchik, Kevin Menard
+ * @since 3.0
+ */
+class CayenneGeneratorUtil {
+
+    protected ILog logger;
+    
+    protected File map;
+    protected File additionalMaps[];
+    protected DefaultClassGenerator generator;
+    
+    protected String includeEntitiesPattern;
+    protected String excludeEntitiesPattern;
+    
+    /** Loads and returns a DataMap by File. */
+    public DataMap loadDataMap(File mapName) throws Exception {
+        InputSource in = new InputSource(mapName.getCanonicalPath());
+        return new MapLoader().loadDataMap(in);
+    }
+
+    /** Loads and returns DataMap based on <code>map</code> attribute. */
+    public DataMap loadDataMap() throws Exception {
+        return loadDataMap(map);
+    }
+    
+    /** Loads and returns DataMap based on <code>map</code> attribute. */
+    protected DataMap[] loadAdditionalDataMaps() throws Exception {
+        if (null == additionalMaps) {
+            return new DataMap[0];
+        }
+
+        DataMap dataMaps[] = new DataMap[additionalMaps.length];
+        for (int i = 0; i < additionalMaps.length; i++) {
+            dataMaps[i] = loadDataMap(additionalMaps[i]);
+        }
+        return dataMaps;
+    }
+    
+    public void processMap() throws Exception {
+
+        DataMap dataMap = loadDataMap();
+        DataMap additionalDataMaps[] = loadAdditionalDataMaps();
+
+        // Create MappingNamespace for maps.
+        EntityResolver entityResolver = new EntityResolver(Collections.singleton(dataMap));
+        dataMap.setNamespace(entityResolver);
+        for (int i = 0; i < additionalDataMaps.length; i++) {
+            entityResolver.addDataMap(additionalDataMaps[i]);
+            additionalDataMaps[i].setNamespace(entityResolver);
+        }
+
+        Collection allEntities = dataMap.getObjEntities();
+        List filteredEntities = new ArrayList(allEntities.size());
+
+        // filter client entities
+        if (generator.isClient()) {
+            if (dataMap.isClientSupported()) {
+                Iterator it = allEntities.iterator();
+                while (it.hasNext()) {
+                    ObjEntity entity = (ObjEntity) it.next();
+                    if (entity.isClientAllowed()) {
+                        filteredEntities.add(entity);
+                    }
+                }
+            }
+        }
+        else {
+            filteredEntities.addAll(allEntities);
+        }
+
+        // filter names according to the specified pattern
+        NamePatternMatcher namePatternMatcher = new NamePatternMatcher(
+                logger,
+                includeEntitiesPattern,
+                excludeEntitiesPattern);
+        namePatternMatcher.filter(filteredEntities);
+
+        generator.setTimestamp(map.lastModified());
+        generator.setDataMap(dataMap);
+        generator.setObjEntities(filteredEntities);
+        generator.validateAttributes();
+        generator.execute();
+    }
+    
+    public void execute() throws Exception
+    {
+        try {
+            processMap();
+        }
+        catch (Throwable th) {
+            th = Util.unwindException(th);
+
+            String thMessage = th.getLocalizedMessage();
+            String message = "Error generating classes: ";
+            message += (!Util.isEmptyString(thMessage)) ? thMessage : th
+                    .getClass()
+                    .getName();
+
+            logger.log(message);
+            throw new Exception(message, th);
+        }
+    }
+
+    public void setAdditionalMaps(File[] additionalMaps) {
+        this.additionalMaps = additionalMaps;
+    }
+    
+    public void setExcludeEntitiesPattern(String excludeEntitiesPattern) {
+        this.excludeEntitiesPattern = excludeEntitiesPattern;
+    }
+    
+    public void setGenerator(DefaultClassGenerator generator) {
+        this.generator = generator;
+    }
+    
+    public void setIncludeEntitiesPattern(String includeEntitiesPattern) {
+        this.includeEntitiesPattern = includeEntitiesPattern;
+    }
+    
+    public void setLogger(ILog logger) {
+        this.logger = logger;
+    }
+    
+    public void setMap(File map) {
+        this.map = map;
+    }
+}

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/ILog.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/ILog.java?view=auto&rev=470645
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/ILog.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/ILog.java
Thu Nov  2 20:01:53 2006
@@ -0,0 +1,31 @@
+/*****************************************************************
+ *   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.cayenne.tools;
+
+/**
+ * Generic logging interface used by some of the tool classes.
+ * 
+ * @author Kevin Menard
+ * @since 3.0
+ */
+interface ILog {
+    void log(String msg);
+    void log(String msg, int msgLevel);
+}

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java?view=diff&rev=470645&r1=470644&r2=470645
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java
Thu Nov  2 20:01:53 2006
@@ -27,7 +27,6 @@
 import java.util.regex.PatternSyntaxException;
 
 import org.apache.tools.ant.Project;
-import org.apache.tools.ant.Task;
 import org.apache.cayenne.util.CayenneMapEntry;
 
 /**
@@ -38,14 +37,14 @@
  */
 public class NamePatternMatcher {
 
-    protected Task parentTask;
+    protected ILog logger;
 
     protected Pattern[] itemIncludeFilters;
     protected Pattern[] itemExcludeFilters;
 
-    public NamePatternMatcher(Task parentTask, String includePattern,
+    public NamePatternMatcher(ILog parentTask, String includePattern,
             String excludePattern) {
-        this.parentTask = parentTask;
+        this.logger = parentTask;
         this.itemIncludeFilters = createPatterns(includePattern);
         this.itemExcludeFilters = createPatterns(excludePattern);
     }
@@ -71,8 +70,8 @@
             }
             catch (PatternSyntaxException e) {
 
-                if (parentTask != null) {
-                    parentTask.log("Ignoring invalid pattern ["
+                if (logger != null) {
+                    logger.log("Ignoring invalid pattern ["
                             + patternStrings[i]
                             + "], reason: "
                             + e.getMessage(), Project.MSG_WARN);

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/tools/NamePatternMatcherTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/tools/NamePatternMatcherTst.java?view=diff&rev=470645&r1=470644&r2=470645
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/tools/NamePatternMatcherTst.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/tools/NamePatternMatcherTst.java
Thu Nov  2 20:01:53 2006
@@ -71,7 +71,7 @@
         String includePattern = "billing_*,user?";
         String excludePattern = null;
         NamePatternMatcher namePatternMatcher = new NamePatternMatcher(
-                parentTask,
+                new AntTaskLogger(parentTask),
                 includePattern,
                 excludePattern);
 
@@ -98,7 +98,7 @@
         String includePattern = "Organization,SecGroup,SecIndividual";
         String excludePattern = null;
         NamePatternMatcher namePatternMatcher = new NamePatternMatcher(
-                parentTask,
+                new AntTaskLogger(parentTask),
                 includePattern,
                 excludePattern);
 



Mime
View raw message