You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/01/29 18:37:37 UTC

svn commit: r738935 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/main/java/org/apache/camel/util/ components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/ components/camel-bindy/src/main/jav...

Author: davsclaus
Date: Thu Jan 29 17:37:36 2009
New Revision: 738935

URL: http://svn.apache.org/viewvc?rev=738935&view=rev
Log:
CAMEL-398: camel-bindy now using ResolverUtil for discovering annotation classes by package searching.

Added:
    camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java   (with props)
Removed:
    camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/ClassHelper.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java
    camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvMarshallTest.java
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java
    camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/complex/twoclassesandonelink/Client.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java?rev=738935&r1=738934&r2=738935&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java Thu Jan 29 17:37:36 2009
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file;
 
 import java.io.InputStream;
+import java.io.File;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
@@ -114,7 +115,7 @@
         InputStream payload = exchange.getIn().getBody(InputStream.class);
         try {
             // build directory
-            int lastPathIndex = fileName.lastIndexOf('/');
+            int lastPathIndex = fileName.lastIndexOf(File.separator);
             if (lastPathIndex != -1) {
                 String directory = fileName.substring(0, lastPathIndex);
                 if (!operations.buildDirectory(directory, false)) {
@@ -171,8 +172,7 @@
             // If the path isn't empty, we need to add a trailing / if it isn't already there
             String baseDir = "";
             if (endpointFile.length() > 0) {
-                // TODO windows or unix slashes. Maybe we should replace all \ to /
-                baseDir = endpointFile + (endpointFile.endsWith("/") ? "" : "/");
+                baseDir = endpointFile + (endpointFile.endsWith(File.separator) ? "" : File.separator);
             }
             if (name != null) {
                 answer = baseDir + name;
@@ -189,7 +189,7 @@
     }
 
     protected String createTempFileName(String fileName) {
-        int path = fileName.lastIndexOf("/");
+        int path = fileName.lastIndexOf(File.separator);
         if (path == -1) {
             // no path
             return endpoint.getTempPrefix() + fileName;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java?rev=738935&r1=738934&r2=738935&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java Thu Jan 29 17:37:36 2009
@@ -160,6 +160,51 @@
     }
 
     /**
+     * A Test that checks to see if each class is annotated with a specific
+     * annotation. If it is, then the test returns true, otherwise false.
+     */
+    public static class AnnotatedWithAny implements Test {
+        private Set<Class<? extends Annotation>> annotations;
+        private boolean checkMetaAnnotations;
+
+        /**
+         * Constructs an AnnotatedWithAny test for any of the specified annotation types.
+         */
+        public AnnotatedWithAny(Set<Class<? extends Annotation>> annotations) {
+            this(annotations, false);
+        }
+
+        /**
+         * Constructs an AnnotatedWithAny test for any of the specified annotation types.
+         */
+        public AnnotatedWithAny(Set<Class<? extends Annotation>> annotations, boolean checkMetaAnnotations) {
+            this.annotations = annotations;
+            this.checkMetaAnnotations = checkMetaAnnotations;
+        }
+
+        /**
+         * Returns true if the type is annotated with the class provided to the
+         * constructor.
+         */
+        public boolean matches(Class type) {
+            if (type == null) {
+                return false;
+            }
+            for (Class annotation : annotations) {
+                if (ObjectHelper.hasAnnotation(type, annotation, checkMetaAnnotations)) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
+        @Override
+        public String toString() {
+            return "annotated with any @[" + annotations + "]";
+        }
+    }
+
+    /**
      * The set of matches being accumulated.
      */
     private Set<Class<? extends T>> matches = new HashSet<Class<? extends T>>();
@@ -270,6 +315,34 @@
     }
 
     /**
+     * Attempts to discover classes that are annotated with any of the annotation.
+     * Accumulated classes can be accessed by calling {@link #getClasses()}.
+     *
+     * @param annotations  the annotations that should be present on matching
+     *                     classes
+     * @param packageNames one or more package names to scan (including
+     *                     subpackages) for classes
+     */
+    public void findAnnotated(Set<Class<? extends Annotation>> annotations, String... packageNames) {
+        if (packageNames == null) {
+            return;
+        }
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Searching for annotations of " + annotations + " in packages: " + Arrays.asList(packageNames));
+        }
+
+        Test test = new AnnotatedWithAny(annotations, true);
+        for (String pkg : packageNames) {
+            find(test, pkg);
+        }
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Found: " + getClasses());
+        }
+    }
+
+    /**
      * Scans for classes starting at the package provided and descending into
      * subpackages. Each class is offered up to the Test as it is discovered,
      * and if the Test returns true the class is retained. Accumulated classes

Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java?rev=738935&r1=738934&r2=738935&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java (original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java Thu Jan 29 17:37:36 2009
@@ -22,12 +22,13 @@
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
 
 import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
 import org.apache.camel.dataformat.bindy.annotation.DataField;
 import org.apache.camel.dataformat.bindy.annotation.Link;
-import org.apache.camel.dataformat.bindy.util.ClassHelper;
+import org.apache.camel.dataformat.bindy.util.AnnotationModelLoader;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -42,23 +43,21 @@
 
     private static final transient Log LOG = LogFactory.getLog(BindyCsvFactory.class);
 
-    private List<Class<?>> models;
+    private AnnotationModelLoader modelsLoader;
+    private Set<Class<?>> models;
 
     private Map<Integer, DataField> mapDataField = new LinkedHashMap<Integer, DataField>();
-
     private Map<Integer, Field> mapAnnotedField = new LinkedHashMap<Integer, Field>();
-
     private Map<String, Field> mapAnnotedLinkField = new LinkedHashMap<String, Field>();
 
     private String separator;
-
     private boolean skipFirstLine;
-
     private String packageName;
 
     public BindyCsvFactory(String packageName) throws Exception {
+        modelsLoader = new AnnotationModelLoader();
         this.packageName = packageName;
-        this.initModel();
+        initModel();
     }
 
     /**
@@ -72,7 +71,7 @@
     public void initModel() throws Exception {
 
         // Find classes defined as Model
-        initModelClasses(this.packageName);
+        initModelClasses(packageName);
 
         // Find annotated fields declared in the Model classes
         initAnnotedFields();
@@ -85,25 +84,19 @@
 
     /**
      * Find all the classes defined as model
-     * 
-     * @param packageName
-     * @throws Exception
      */
     private void initModelClasses(String packageName) throws Exception {
-        models = ClassHelper.getClasses(packageName);
+        models = modelsLoader.loadModels(packageName);
     }
 
     /**
      * Find fields annoted in each class of the model
-     * 
-     * @throws Exception
      */
     private void initAnnotedFields() throws Exception {
 
         for (Class<?> cl : models) {
 
             for (Field field : cl.getDeclaredFields()) {
-
                 DataField dataField = field.getAnnotation(DataField.class);
                 if (dataField != null) {
                     mapDataField.put(dataField.pos(), dataField);
@@ -115,19 +108,11 @@
                 if (linkField != null) {
                     mapAnnotedLinkField.put(cl.getName(), field);
                 }
-
             }
 
         }
-
     }
 
-    /**
-     * Bind the data of a record to their fields of the model
-     * 
-     * @param data
-     * @throws Exception
-     */
     public void bind(List<String> data, Map<String, Object> model) throws Exception {
 
         int pos = 0;
@@ -160,13 +145,6 @@
 
     }
 
-    /**
-     * Unbind data from model objects and copy them to csv record
-     * 
-     * @return String representing a csv record created
-     * @param model
-     * @throws Exception
-     */
     public String unbind(Map<String, Object> model) throws Exception {
 
         StringBuilder builder = new StringBuilder();
@@ -208,17 +186,12 @@
 
     /**
      * Link objects together (Only 1to1 relation is allowed)
-     * 
-     * @param model
-     * @throws Exception
      */
     public void link(Map<String, Object> model) throws Exception {
 
-        Iterator<?> it = mapAnnotedLinkField.keySet().iterator();
-
-        while (it.hasNext()) {
+        for (String link : mapAnnotedLinkField.keySet()) {
 
-            Field field = mapAnnotedLinkField.get(it.next());
+            Field field = mapAnnotedLinkField.get(link);
             field.setAccessible(true);
 
             // Retrieve linked object
@@ -237,7 +210,7 @@
      * 
      * @return Map is a collection of the objects used to bind data from csv
      *         records
-     * @throws Exception
+     * @throws Exception can be thrown
      */
     public Map<String, Object> factory() throws Exception {
 
@@ -257,24 +230,15 @@
 
     /**
      * Find the separator used to delimit the CSV fields
-     * 
-     * @return String separator to split the content of a csv record into tokens
-     * @throws Exception
      */
-    public String getSeparator() throws Exception {
-
+    public String getSeparator() {
         return separator;
     }
 
     /**
      * Get the parameter skipFirstLine
-     * 
-     * @return String indicates if the first line of the CSV file must be
-     *         skipped. Values are Y (for Yes) or N (for No)
-     * @throws Exception
      */
-    public boolean getSkipFirstLine() throws Exception {
-
+    public boolean getSkipFirstLine() {
         return skipFirstLine;
     }
 

Added: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java?rev=738935&view=auto
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java (added)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java Thu Jan 29 17:37:36 2009
@@ -0,0 +1,48 @@
+/**
+ * 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.camel.dataformat.bindy.util;
+
+import java.lang.annotation.Annotation;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
+import org.apache.camel.dataformat.bindy.annotation.Link;
+import org.apache.camel.util.ResolverUtil;
+
+/**
+ * Annotation based loader for model classes with Bindy annotations.
+ */
+public class AnnotationModelLoader {
+
+    private ResolverUtil<Object> resolver;
+    private Set<Class<? extends Annotation>> annotations;
+
+    public AnnotationModelLoader() {
+        resolver = new ResolverUtil<Object>();
+
+        annotations = new LinkedHashSet<Class<? extends Annotation>>();
+        annotations.add(CsvRecord.class);
+        annotations.add(Link.class);
+    }
+
+    public Set<Class<?>> loadModels(String packageName) throws Exception {
+        resolver.findAnnotated(annotations, packageName);
+        return resolver.getClasses();
+    }
+
+}

Propchange: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvMarshallTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvMarshallTest.java?rev=738935&r1=738934&r2=738935&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvMarshallTest.java (original)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvMarshallTest.java Thu Jan 29 17:37:36 2009
@@ -27,10 +27,10 @@
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
-import org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Client;
-import org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Order;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Client;
+import org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Order;
 import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
 import org.junit.Test;
 import org.springframework.config.java.annotation.Bean;

Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java?rev=738935&r1=738934&r2=738935&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java (original)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java Thu Jan 29 17:37:36 2009
@@ -27,9 +27,9 @@
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
-import org.apache.camel.dataformat.bindy.model.simple.oneclass.Order;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.dataformat.bindy.model.simple.oneclass.Order;
 import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
 import org.junit.Test;
 import org.springframework.config.java.annotation.Bean;

Modified: camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/complex/twoclassesandonelink/Client.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/complex/twoclassesandonelink/Client.java?rev=738935&r1=738934&r2=738935&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/complex/twoclassesandonelink/Client.java (original)
+++ camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/complex/twoclassesandonelink/Client.java Thu Jan 29 17:37:36 2009
@@ -17,7 +17,9 @@
 package org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink;
 
 import org.apache.camel.dataformat.bindy.annotation.DataField;
+import org.apache.camel.dataformat.bindy.annotation.Link;
 
+@Link
 public class Client {
 
     @DataField(pos = 1)