camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r832743 - in /camel/trunk/camel-core/src/main/java/org/apache/camel: CamelContext.java impl/DefaultCamelContext.java impl/DefaultDataFormatResolver.java model/DataFormatDefinition.java spi/DataFormatResolver.java
Date Wed, 04 Nov 2009 13:58:42 GMT
Author: davsclaus
Date: Wed Nov  4 13:58:41 2009
New Revision: 832743

URL: http://svn.apache.org/viewvc?rev=832743&view=rev
Log:
CAMEL-2131: Introduced a DataFormatResolver

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
  (with props)
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java   (with
props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=832743&r1=832742&r2=832743&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Wed Nov  4 13:58:41
2009
@@ -25,6 +25,7 @@
 import org.apache.camel.model.DataFormatDefinition;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.spi.ClassResolver;
+import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.EndpointStrategy;
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.FactoryFinderResolver;
@@ -446,6 +447,7 @@
 
     /**
      * Sets the data formats that can be referenced in the routes.
+     *
      * @param dataFormats the data formats
      */
     void setDataFormats(Map<String, DataFormatDefinition> dataFormats);
@@ -458,6 +460,22 @@
     Map<String, DataFormatDefinition> getDataFormats();
     
     /**
+     * Resolve a data format given its definition
+     *
+     * @param def the data format definition
+     * @return the resolved data format
+     */
+    DataFormat resolveDataFormat(DataFormatDefinition def);
+
+    /**
+     * Resolve a data format given its name
+     *
+     * @param name the data format name
+     * @return the resolved data format
+     */
+    DataFormat resolveDataFormat(String name);
+
+    /**
      * Sets the properties that can be referenced in the camel context
      *
      * @param properties properties
@@ -591,14 +609,16 @@
     void setInflightRepository(InflightRepository repository);
     
     /**
-     * Gets the the application context class loader which may be helpful for running camel
in other container     
+     * Gets the the application context class loader which may be helpful for running camel
in other containers
+     *
      * @return the application context class loader
      */
     ClassLoader getApplicationContextClassLoader();
 
     /**
      * Sets the application context class loader
-     * @param classLoader
+     *
+     * @param classLoader the class loader
      */
     void setApplicationContextClassLoader(ClassLoader classLoader);
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=832743&r1=832742&r2=832743&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Wed
Nov  4 13:58:41 2009
@@ -62,6 +62,8 @@
 import org.apache.camel.processor.interceptor.Tracer;
 import org.apache.camel.spi.ClassResolver;
 import org.apache.camel.spi.ComponentResolver;
+import org.apache.camel.spi.DataFormat;
+import org.apache.camel.spi.DataFormatResolver;
 import org.apache.camel.spi.EndpointStrategy;
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.FactoryFinderResolver;
@@ -127,6 +129,7 @@
     private Long delay;
     private ErrorHandlerBuilder errorHandlerBuilder;
     private Map<String, DataFormatDefinition> dataFormats = new HashMap<String,
DataFormatDefinition>();
+    private DataFormatResolver dataFormatResolver = new DefaultDataFormatResolver();
     private Map<String, String> properties = new HashMap<String, String>();
     private FactoryFinderResolver factoryFinderResolver = new DefaultFactoryFinderResolver();
     private FactoryFinder defaultFactoryFinder;
@@ -1342,6 +1345,30 @@
         return autoStartup != null && autoStartup;
     }
 
+    public ClassLoader getApplicationContextClassLoader() {
+        return applicationContextClassLoader;
+    }
+
+    public void setApplicationContextClassLoader(ClassLoader classLoader) {
+        applicationContextClassLoader = classLoader;
+    }
+
+    public DataFormatResolver getDataFormatResolver() {
+        return dataFormatResolver;
+    }
+
+    public void setDataFormatResolver(DataFormatResolver dataFormatResolver) {
+        this.dataFormatResolver = dataFormatResolver;
+    }
+
+    public DataFormat resolveDataFormat(DataFormatDefinition def) {
+        return dataFormatResolver.resolveDataFormat(def, this);
+    }
+
+    public DataFormat resolveDataFormat(String ref) {
+        return dataFormatResolver.resolveDataFormat(ref, this);
+    }
+
     protected String getEndpointKey(String uri, Endpoint endpoint) {
         if (endpoint.isSingleton()) {
             return uri;
@@ -1392,12 +1419,4 @@
         return "CamelContext(" + getName() + ")";
     }
 
-    public ClassLoader getApplicationContextClassLoader() {
-        return applicationContextClassLoader;
-    }
-
-    public void setApplicationContextClassLoader(ClassLoader classLoader) {
-        applicationContextClassLoader = classLoader;        
-    }
-
 }

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java?rev=832743&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
Wed Nov  4 13:58:41 2009
@@ -0,0 +1,59 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.model.DataFormatDefinition;
+import org.apache.camel.spi.DataFormat;
+import org.apache.camel.spi.DataFormatResolver;
+
+/**
+ * Default data format resolver
+ *
+ * @version $Revision$
+ */
+public class DefaultDataFormatResolver implements DataFormatResolver {
+
+    @SuppressWarnings("unchecked")
+    public DataFormat resolveDataFormat(DataFormatDefinition definition, CamelContext context)
{
+        Class type = context.getClassResolver().resolveClass(definition.getDataFormatName());
+        if (type == null) {
+            throw new IllegalArgumentException("The class " + definition.getDataFormatName()
+                + " is not on the classpath! Cannot use the dataFormat " + this);
+        }
+        return (DataFormat) context.getInjector().newInstance(type);
+    }
+
+    public DataFormat resolveDataFormat(String ref, CamelContext context) {
+        DataFormat dataFormat = context.getRegistry().lookup(ref, DataFormat.class);
+        if (dataFormat == null) {
+            // lookup type and create the data format from it
+            DataFormatDefinition type = context.getRegistry().lookup(ref, DataFormatDefinition.class);
+            if (type == null && context.getDataFormats() != null) {
+                type = context.getDataFormats().get(ref);
+            }
+            if (type != null) {
+                dataFormat = resolveDataFormat(type, context);
+            }
+        }
+        if (dataFormat == null) {
+            throw new IllegalArgumentException("Cannot find data format in registry with
ref: " + ref);
+        }
+        return dataFormat;
+    }
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java?rev=832743&r1=832742&r2=832743&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java
Wed Nov  4 13:58:41 2009
@@ -52,43 +52,18 @@
 
     /**
      * Factory method to create the data format
+     *
      * @param routeContext route context
-     * @param type the data format type
-     * @param ref  reference to lookup for a data format
+     * @param type         the data format type
+     * @param ref          reference to lookup for a data format
      * @return the data format or null if not possible to create
      */
     public static DataFormat getDataFormat(RouteContext routeContext, DataFormatDefinition
type, String ref) {
         if (type == null) {
             ObjectHelper.notNull(ref, "ref or dataFormat");
-
-            DataFormat dataFormat = lookup(routeContext, ref, DataFormat.class);
-            if (dataFormat == null) {
-                // lookup type and create the data format from it
-                type = lookup(routeContext, ref, DataFormatDefinition.class);
-                if (type == null) {
-                    type = routeContext.getDataFormat(ref);
-                }
-                if (type != null) {
-                    dataFormat = type.getDataFormat(routeContext);
-                }
-            }
-
-            if (dataFormat == null) {
-                throw new IllegalArgumentException("Cannot find data format in registry with
ref: " + ref);
-            }
-
-            return dataFormat;
+            return routeContext.getCamelContext().resolveDataFormat(ref);
         } else {
-            return type.getDataFormat(routeContext);
-        }
-    }
-
-    private static <T> T lookup(RouteContext routeContext, String ref, Class<T>
type) {
-        try {
-            return routeContext.lookup(ref, type);
-        } catch (Exception e) {
-            // need to ignore not same type and return it as null
-            return null;
+            return type.createDataFormat(routeContext);
         }
     }
 
@@ -107,11 +82,7 @@
     @SuppressWarnings("unchecked")
     protected DataFormat createDataFormat(RouteContext routeContext) {
         if (dataFormatName != null) {
-            Class type = routeContext.getCamelContext().getClassResolver().resolveClass(dataFormatName);
-            if (type == null) {
-                throw new IllegalArgumentException("The class " + dataFormatName + " is not
on the classpath! Cannot use the dataFormat " + this);
-            }
-            return (DataFormat) ObjectHelper.newInstance(type);
+            return routeContext.getCamelContext().resolveDataFormat(this);
         }
         return null;
     }
@@ -148,5 +119,14 @@
     public void setDataFormat(DataFormat dataFormat) {
         this.dataFormat = dataFormat;
     }
+
+    public String getShortName() {
+        String name = getClass().getSimpleName();
+        if (name.endsWith("DataFormat")) {
+            name = name.substring(0, name.indexOf("DataFormat"));
+        }
+        return name;
+    }
+
 }
 

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java?rev=832743&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java Wed
Nov  4 13:58:41 2009
@@ -0,0 +1,47 @@
+/**
+ * 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.spi;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.model.DataFormatDefinition;
+
+/**
+ * Represents a resolver of data formats.
+ *
+ * @version $Revision$
+ */
+public interface DataFormatResolver {
+
+    /**
+     * Resolves the given data format by its definition.
+     *
+     * @param definition the definition
+     * @param context the camel context
+     * @return the data format
+     */
+    DataFormat resolveDataFormat(DataFormatDefinition definition, CamelContext context);
+
+    /**
+     * Resolves the given data format by its name.
+     *
+     * @param name the name
+     * @param context the camel context
+     * @return the data format
+     */
+    DataFormat resolveDataFormat(String name, CamelContext context);
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message