[jboss-svn-commits] JBL Code SVN: r24251 - in labs/jbossesb/workspace/skeagh: commons/src/main/java/org/jboss/esb/util and 4 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Dec 5 11:09:57 EST 2008
Author: beve
Date: 2008-12-05 11:09:57 -0500 (Fri, 05 Dec 2008)
New Revision: 24251
Added:
labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/util/FreeMarkerTemplate.java
Modified:
labs/jbossesb/workspace/skeagh/commons/pom.xml
labs/jbossesb/workspace/skeagh/examples/file-routing/src/main/resources/jboss-esb.xml
labs/jbossesb/workspace/skeagh/routing/file/pom.xml
labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/FileOutboundRouter.java
labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/FileRoutingConstants.java
labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/eval/PatternEvaluator.java
labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/eval/PatternEvaluatorImpl.java
Log:
Work for https://jira.jboss.org/jira/browse/JBESB-2215 "FileOutboundRouter: add support for templates when creating the outbound file name."
Modified: labs/jbossesb/workspace/skeagh/commons/pom.xml
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/pom.xml 2008-12-05 15:02:56 UTC (rev 24250)
+++ labs/jbossesb/workspace/skeagh/commons/pom.xml 2008-12-05 16:09:57 UTC (rev 24251)
@@ -34,7 +34,11 @@
<version>10.1.1.0</version>
<scope>provided</scope>
</dependency>
-
+ <dependency>
+ <groupId>org.freemarker</groupId>
+ <artifactId>freemarker</artifactId>
+ <version>2.3.14</version>
+ </dependency>
</dependencies>
<repositories>
Added: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/util/FreeMarkerTemplate.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/util/FreeMarkerTemplate.java (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/util/FreeMarkerTemplate.java 2008-12-05 16:09:57 UTC (rev 24251)
@@ -0,0 +1,129 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2008, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.esb.util;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
+
+/**
+ * FreeMarker template.
+ *
+ * Lifted from Smooks.
+ *
+ * @author <a href="mailto:tom.fennelly at gmail.com">tom.fennelly at gmail.com</a>
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ * @since 5.0
+ */
+public class FreeMarkerTemplate
+{
+ /**
+ * Template text.
+ */
+ private String templateText;
+
+ /**
+ * FreeMarker Template.
+ */
+ private Template template;
+
+ /**
+ * Creates a new FreeMarkerTemplate with the passed in templateText.
+ *
+ * @param templateText The template text.
+ */
+ public FreeMarkerTemplate(final String templateText)
+ {
+ AssertArgument.isNotNullAndNotEmpty(templateText, "templateText");
+ final Reader templateReader = new StringReader(templateText);
+
+ try
+ {
+ try
+ {
+ template = new Template("free-marker-template", templateReader, new Configuration());
+ }
+ finally
+ {
+ templateReader.close();
+ }
+ }
+ catch (IOException e)
+ {
+ throw new IllegalStateException("Unexpected IOException.", e);
+ }
+ }
+
+ /**
+ * Creates a new FreeMarkerTemplate with the passed in templateText.
+ *
+ * @param templatePath The path to a template file.
+ * @param basePath The class that will be used when performing the lookup of the templatePath.
+ */
+ public FreeMarkerTemplate(final String templatePath, final Class<?> basePath)
+ {
+ AssertArgument.isNotNullAndNotEmpty(templatePath, "templatePath");
+ this.templateText = templatePath;
+
+ try
+ {
+ final Configuration configuration = new Configuration();
+
+ if (basePath != null)
+ {
+ configuration.setClassForTemplateLoading(basePath, "");
+ }
+ template = configuration.getTemplate(templatePath);
+ }
+ catch (IOException e)
+ {
+ throw new IllegalStateException("Unexpected IOException.", e);
+ }
+ }
+
+ /**
+ * @return String The template text.
+ */
+ public final String getTemplateText()
+ {
+ return templateText;
+ }
+
+ /**
+ * Applys the template.
+ *
+ * @param contextObject The freemarker datamodel to be used.
+ * @return String The result of the FreeMarker processing.
+ * @throws TemplateException If an exception occurs while processing.
+ * @throws IOException If an exception occurs while processing.
+ */
+ public final String apply(final Object contextObject) throws TemplateException, IOException
+ {
+ StringWriter outputWriter = new StringWriter();
+ template.process(contextObject, outputWriter);
+ return outputWriter.toString();
+ }
+}
Modified: labs/jbossesb/workspace/skeagh/examples/file-routing/src/main/resources/jboss-esb.xml
===================================================================
--- labs/jbossesb/workspace/skeagh/examples/file-routing/src/main/resources/jboss-esb.xml 2008-12-05 15:02:56 UTC (rev 24250)
+++ labs/jbossesb/workspace/skeagh/examples/file-routing/src/main/resources/jboss-esb.xml 2008-12-05 16:09:57 UTC (rev 24251)
@@ -17,7 +17,7 @@
<property name="payloadType">STRING</property>
</inRouter>
<outRouter name="fileOutboundRouter" class="org.jboss.esb.file.FileOutboundRouter">
- <property name="fileNamePattern">${example.work.dir}/another-service-pickup-dir/${name}.out</property>
+ <property name="fileNamePattern">${example.work.dir}/another-service-pickup-dir/${name}-${in_file_lastmod}.out</property>
</outRouter>
</service>
</services>
Modified: labs/jbossesb/workspace/skeagh/routing/file/pom.xml
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/file/pom.xml 2008-12-05 15:02:56 UTC (rev 24250)
+++ labs/jbossesb/workspace/skeagh/routing/file/pom.xml 2008-12-05 16:09:57 UTC (rev 24251)
@@ -25,7 +25,7 @@
<instructions>
<Export-Package>org.jboss.esb.file.*</Export-Package>
<Import-Package>*;resolution:=optional</Import-Package>
- <Embed-Dependency>jbossesb-commons, log4j</Embed-Dependency>
+ <Embed-Dependency>jbossesb-commons, log4j, freemarker</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>
Modified: labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/FileOutboundRouter.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/FileOutboundRouter.java 2008-12-05 15:02:56 UTC (rev 24250)
+++ labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/FileOutboundRouter.java 2008-12-05 16:09:57 UTC (rev 24251)
@@ -140,7 +140,7 @@
public final void route(final Message message) throws RoutingException
{
final InvocationContext invocationContext = InvocationContext.getContext();
- final File toFile = new File(outputDir, patternEvaluator.evalFileName(fileNamePattern, invocationContext));
+ final File toFile = new File(outputDir, patternEvaluator.evalFileName(fileNamePattern, message, invocationContext));
fileWriter.write(message, toFile, invocationContext);
}
Modified: labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/FileRoutingConstants.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/FileRoutingConstants.java 2008-12-05 15:02:56 UTC (rev 24250)
+++ labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/FileRoutingConstants.java 2008-12-05 16:09:57 UTC (rev 24251)
@@ -38,22 +38,22 @@
/**
* Key used in the InvocationContext for the absolute path to the File.
*/
- public static final String IN_FILE_PATH = "in-file-path";
+ public static final String IN_FILE_PATH = "in_file_path";
/**
* Key used in the InvocationContext for the name of inbound File.
*/
- public static final String IN_FILE_NAME = "in-file-name";
+ public static final String IN_FILE_NAME = "in_file_name";
/**
* Key used in the InvocationContext for the file lenght.
*/
- public static final String IN_FILE_LENGTH = "in-file-length";
+ public static final String IN_FILE_LENGTH = "in_file_length";
/**
* Key used in the InvocationContext for the files last modified date.
*/
- public static final String IN_FILE_LASTMOD = "in-file-lastmod";
+ public static final String IN_FILE_LASTMOD = "in_file_lastmod";
}
Modified: labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/eval/PatternEvaluator.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/eval/PatternEvaluator.java 2008-12-05 15:02:56 UTC (rev 24250)
+++ labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/eval/PatternEvaluator.java 2008-12-05 16:09:57 UTC (rev 24251)
@@ -21,6 +21,7 @@
package org.jboss.esb.file.eval;
import org.jboss.esb.api.context.InvocationContext;
+import org.jboss.esb.api.message.Message;
/**
* PatternEvaluator.
@@ -59,8 +60,9 @@
* Evals a pattern into a valid file name.
*
* @param filePattern The file pattern to evaluate.
+ * @param message The ESB Message object.
* @param invocationContext The invokation context.
* @return String A valid file name.
*/
- String evalFileName(final String filePattern, final InvocationContext invocationContext);
+ String evalFileName(final String filePattern, final Message message, final InvocationContext invocationContext);
}
Modified: labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/eval/PatternEvaluatorImpl.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/eval/PatternEvaluatorImpl.java 2008-12-05 15:02:56 UTC (rev 24250)
+++ labs/jbossesb/workspace/skeagh/routing/file/src/main/java/org/jboss/esb/file/eval/PatternEvaluatorImpl.java 2008-12-05 16:09:57 UTC (rev 24251)
@@ -22,38 +22,172 @@
import static org.jboss.esb.file.FileRoutingConstants.IN_FILE_NAME;
+import java.io.IOException;
import java.util.Map;
+
+import org.apache.log4j.Logger;
import org.jboss.esb.api.context.InvocationContext;
+import org.jboss.esb.api.message.Message;
import org.jboss.esb.util.FileUtil;
+import org.jboss.esb.util.FreeMarkerTemplate;
+import freemarker.template.TemplateException;
+
/**
- * Impl.
+ * Concrete PatternEvaluator implementation.
*
* @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
- *
+ * @since 5.0
*/
public class PatternEvaluatorImpl implements PatternEvaluator
{
/**
+ * Logger.
+ */
+ private Logger log = Logger.getLogger(PatternEvaluatorImpl.class);
+
+ /**
+ * Will perform the following variable substitutions.
*
+ * <pre>
+ * 1. Use a simple filename replacement replacing any occurence of '${name}' which
+ * the value of the original file.
+ *
+ * 2. Substitute any existing '${timestamp} with System.currentTimeMillis()
+ *
+ * 3. Will process the filePattern with FreeMarker using the ESB Message payload
+ * as the data model.
+ * For example if you payload is a Order object you can specify a filePattern like so:
+ * '/path/${order.orderId}-${order.quantity}.txt'
+ *
+ * 4. Will process the filePattern with FreeMarker using the ESB InvocationContext
+ * object.
+ * For example if you set a InvocationContext object like this:
+ * context.setContextObject("order", orderObject);
+ * you can reference this in the filePattern like so:
+ * '/path/${order.orderId}-${order.quantity}.txt'
+ *
+ * 5. Will process the filePattern with FreeMarker using the ESB InvocationContext
+ * InvocationParameters. These values are referenced by name like so:
+ * '/path/${in_file_lastmod}.txt'
+ *
+ * </pre>
+ *
+ *
* @param filePattern The file pattern.
- * @param invocationContext The invokation context.
+ * @param message The ESB Message object.
+ * @param invocationContext The invocation context.
* @return String the file name.
*/
- public final String evalFileName(final String filePattern, final InvocationContext invocationContext)
+ public final String evalFileName(final String filePattern, final Message message, final InvocationContext invocationContext)
{
- String newName = FileUtil.removePath(filePattern);
+ String fileName = replaceFileName(FileUtil.removePath(filePattern), invocationContext);
+ fileName = replaceTimestamp(fileName);
+ fileName = replaceUsingTemplate(fileName, message);
+ fileName = replaceUsingTemplate(fileName, invocationContext);
+ return fileName;
+ }
+ /**
+ * @param filePattern The file pattern, not including path.
+ * @param invocationContext The invocation context.
+ * @return String The String with its filename replaced or the pattern unmodified.
+ */
+ private String replaceFileName(final String filePattern, final InvocationContext invocationContext)
+ {
final Map<String, String> invocationParameters = invocationContext.getInvocationParameters();
final String fileName = invocationParameters.get(IN_FILE_NAME);
if (fileName != null)
{
- newName = newName.replace(FILE_NAME, fileName);
+ return filePattern.replace(FILE_NAME, fileName);
}
+ return filePattern;
+ }
- newName = newName.replace(TIMESTAMP_TOKEN, String.valueOf(System.currentTimeMillis()));
+ /**
+ * @param filePattern The file pattern, not including path.
+ * @return String The String with its filename replaced or the pattern unmodified.
+ */
+ private String replaceTimestamp(final String filePattern)
+ {
+ return filePattern.replace(TIMESTAMP_TOKEN, String.valueOf(System.currentTimeMillis()));
+ }
- return newName;
+ /**
+ * Uses FreeMarker to replace the filePattern using the messages payload as the data model.
+ *
+ * @param filePattern The file pattern, not including path.
+ * @param message The ESB Message object.
+ * @return String The String with its tempalate placeholders substituted or the pattern unmodified.
+ */
+ private String replaceUsingTemplate(final String filePattern, final Message message)
+ {
+ if (message == null || message.getPayload() == null)
+ {
+ return filePattern;
+ }
+ return applyTemplate(filePattern, message.getPayload());
}
+ /**
+ * Uses FreeMarker to replace the filePattern using the messages payload as the data model.
+ *
+ * @param filePattern The file pattern, not including path.
+ * @param invocationContext The invocation context.
+ * @return String The String with its tempalate placeholders substituted or the pattern unmodified.
+ */
+ private String replaceUsingTemplate(final String filePattern, final InvocationContext invocationContext)
+ {
+ Map<Object, Object> contextObjects = invocationContext.getAll();
+ Map<String, String> invocationParameters = invocationContext.getInvocationParameters();
+ contextObjects.putAll(invocationParameters);
+
+ if (!contextObjects.isEmpty())
+ {
+ return applyTemplate(filePattern, contextObjects);
+ }
+
+ return filePattern;
+ }
+
+ /**
+ * Applies the template.
+ *
+ * @param pattern The pattern.
+ * @param dataModel The freemarker datamodel.
+ * @return String The processed template of the pattern unchanged.
+ */
+ private String applyTemplate(final String pattern, final Object dataModel)
+ {
+ try
+ {
+ final FreeMarkerTemplate template = new FreeMarkerTemplate(pattern);
+ return template.apply(dataModel);
+ }
+ catch (final IllegalArgumentException ignored)
+ {
+ logTemplateException(ignored, pattern, dataModel);
+ }
+ catch (final TemplateException ignored)
+ {
+ logTemplateException(ignored, pattern, dataModel);
+ }
+ catch (final IOException ignored)
+ {
+ logTemplateException(ignored, pattern, dataModel);
+ }
+ return pattern;
+ }
+
+ /**
+ * @param e The exception.
+ * @param pattern The pattern.
+ * @param dataModel The datamodel.
+ */
+ private void logTemplateException(final Exception e, final String pattern, final Object dataModel)
+ {
+ final String errorMsg = "FreeMarker could not match the pattern '" + pattern + "' to the dataModel'" + dataModel + "'";
+ log.debug(errorMsg + ". Exception was : ", e);
+ }
+
}
More information about the jboss-svn-commits
mailing list