CAMEL-5958: Bindy ignores bean class type (some more work)
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a052c1cf Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a052c1cf Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a052c1cf Branch: refs/heads/master Commit: a052c1cf63e4cae94427d082ae47322328326827 Parents: cfb8f92 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Tue Nov 26 18:01:28 2013 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Aug 7 11:05:46 2015 +0200 ---------------------------------------------------------------------- .../bindy/BindyAbstractDataFormat.java | 8 +- .../dataformat/bindy/BindyAbstractFactory.java | 70 +++---- .../camel/dataformat/bindy/BindyCsvFactory.java | 174 ++++++++--------- .../bindy/BindyFixedLengthFactory.java | 191 ++++++++++--------- .../bindy/BindyKeyValuePairFactory.java | 121 ++++++------ .../bindy/fixed/BindyFixedLengthDataFormat.java | 34 ---- .../bindy/kvp/BindyKeyValuePairDataFormat.java | 21 +- .../bindy/util/AnnotationModelLoader.java | 18 +- 8 files changed, 315 insertions(+), 322 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a052c1cf/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java index af4609a..b790424 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java @@ -16,12 +16,12 @@ */ package org.apache.camel.dataformat.bindy; -import org.apache.camel.spi.DataFormat; - import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.apache.camel.spi.DataFormat; + public abstract class BindyAbstractDataFormat implements DataFormat { private String locale; private BindyAbstractFactory modelFactory; @@ -29,7 +29,6 @@ public abstract class BindyAbstractDataFormat implements DataFormat { public BindyAbstractDataFormat() { } - protected BindyAbstractDataFormat(Class<?> classType) { this.classType = classType; } @@ -62,6 +61,7 @@ public abstract class BindyAbstractDataFormat implements DataFormat { this.modelFactory = modelFactory; } + protected abstract BindyAbstractFactory createModelFactory() throws Exception; protected Object extractUnmarshalResult(List<Map<String, Object>> models) { if (getClassType() != null) { @@ -83,6 +83,4 @@ public abstract class BindyAbstractDataFormat implements DataFormat { return models; } } - - protected abstract BindyAbstractFactory createModelFactory() throws Exception; } http://git-wip-us.apache.org/repos/asf/camel/blob/a052c1cf/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java index 1694847..43bca89 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java @@ -16,23 +16,20 @@ */ package org.apache.camel.dataformat.bindy; -import org.apache.camel.dataformat.bindy.annotation.CsvRecord; -import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord; -import org.apache.camel.dataformat.bindy.annotation.Link; -import org.apache.camel.dataformat.bindy.annotation.Message; -import org.apache.camel.dataformat.bindy.annotation.Section; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.lang.reflect.Field; import java.text.NumberFormat; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.camel.dataformat.bindy.annotation.Link; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * The {@link BindyAbstractFactory} implements what its common to all the formats * supported by Camel Bindy @@ -40,7 +37,11 @@ import java.util.Set; public abstract class BindyAbstractFactory implements BindyFactory { private static final Logger LOG = LoggerFactory.getLogger(BindyAbstractFactory.class); protected final Map<String, List<Field>> annotatedLinkFields = new LinkedHashMap<String, List<Field>>(); + protected Set<Class<?>> models; + protected Set<String> modelClassNames; protected String crlf; + + private String[] packageNames; private String locale; private Class<?> type; @@ -50,21 +51,10 @@ public abstract class BindyAbstractFactory implements BindyFactory { if (LOG.isDebugEnabled()) { LOG.debug("Class name: {}", type.getName()); } - - if(!validateType(type)) { - throw new IllegalArgumentException("..."); - } - + initModel(); } - - protected boolean validateType(Class<?> type) { - return type.getAnnotation(CsvRecord.class) != null - || type.getAnnotation(Message.class) != null - || type.getAnnotation(Section.class) != null - || type.getAnnotation(FixedLengthRecord.class) != null; - } - + /** * method uses to initialize the model representing the classes who will * bind the data. This process will scan for classes according to the @@ -73,6 +63,24 @@ public abstract class BindyAbstractFactory implements BindyFactory { * @throws Exception */ public void initModel() throws Exception { + models = new HashSet<Class<?>>(); + models.add(type); + + for (Field field : type.getDeclaredFields()) { + Link linkField = field.getAnnotation(Link.class); + + if (linkField != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Class linked: {}, Field: {}", field.getType(), field); + } + models.add(field.getType()); + } + } + + modelClassNames = new HashSet<String>(); + for (Class<?> clazz : models) { + modelClassNames.add(clazz.getName()); + } } /** @@ -110,14 +118,6 @@ public abstract class BindyAbstractFactory implements BindyFactory { } /** - * - * @return - */ - protected Class<?> type() { - return type; - } - - /** * Factory method generating new instances of the model and adding them to a * HashMap * @@ -128,8 +128,12 @@ public abstract class BindyAbstractFactory implements BindyFactory { public Map<String, Object> factory() throws Exception { Map<String, Object> mapModel = new HashMap<String, Object>(); - Object obj = ObjectHelper.newInstance(type); - mapModel.put(obj.getClass().getName(), obj); + for (Class<?> cl : models) { + Object obj = ObjectHelper.newInstance(cl); + + // Add instance of the class to the Map Model + mapModel.put(obj.getClass().getName(), obj); + } return mapModel; } @@ -140,7 +144,7 @@ public abstract class BindyAbstractFactory implements BindyFactory { * @return true if the model supports the identified classes */ public boolean supportsModel(Set<String> classes) { - return classes.contains(type); + return modelClassNames.containsAll(classes); } /** http://git-wip-us.apache.org/repos/asf/camel/blob/a052c1cf/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java index 389ede2..a699c04 100755 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java @@ -96,57 +96,58 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor public void initAnnotatedFields() { maxpos = 0; - Class<?> cl = type(); - List<Field> linkFields = new ArrayList<Field>(); + for (Class<?> cl : models) { + List<Field> linkFields = new ArrayList<Field>(); - if (LOG.isDebugEnabled()) { - LOG.debug("Class retrieved: {}", cl.getName()); - } + if (LOG.isDebugEnabled()) { + LOG.debug("Class retrieved: {}", cl.getName()); + } - for (Field field : cl.getDeclaredFields()) { - DataField dataField = field.getAnnotation(DataField.class); - if (dataField != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("Position defined in the class: {}, position: {}, Field: {}", - new Object[]{cl.getName(), dataField.pos(), dataField}); - } + for (Field field : cl.getDeclaredFields()) { + DataField dataField = field.getAnnotation(DataField.class); + if (dataField != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Position defined in the class: {}, position: {}, Field: {}", + new Object[]{cl.getName(), dataField.pos(), dataField}); + } - if (dataField.required()) { - ++numberMandatoryFields; - } else { - ++numberOptionalFields; - } + if (dataField.required()) { + ++numberMandatoryFields; + } else { + ++numberOptionalFields; + } - int pos = dataField.pos(); - if (annotatedFields.containsKey(pos)) { - Field f = annotatedFields.get(pos); - LOG.warn("Potentially invalid model: existing @DataField '{}' replaced by '{}'", f.getName(), field.getName()); + int pos = dataField.pos(); + if (annotatedFields.containsKey(pos)) { + Field f = annotatedFields.get(pos); + LOG.warn("Potentially invalid model: existing @DataField '{}' replaced by '{}'", f.getName(), field.getName()); + } + dataFields.put(pos, dataField); + annotatedFields.put(pos, field); + maxpos = Math.max(maxpos, pos); } - dataFields.put(pos, dataField); - annotatedFields.put(pos, field); - maxpos = Math.max(maxpos, pos); - } - Link linkField = field.getAnnotation(Link.class); + Link linkField = field.getAnnotation(Link.class); - if (linkField != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("Class linked: {}, Field: {}", cl.getName(), field); + if (linkField != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Class linked: {}, Field: {}", cl.getName(), field); + } + linkFields.add(field); } - linkFields.add(field); } - } - if (!linkFields.isEmpty()) { - annotatedLinkFields.put(cl.getName(), linkFields); - } + if (!linkFields.isEmpty()) { + annotatedLinkFields.put(cl.getName(), linkFields); + } - totalFields = numberMandatoryFields + numberOptionalFields; + totalFields = numberMandatoryFields + numberOptionalFields; - if (LOG.isDebugEnabled()) { - LOG.debug("Number of optional fields: {}", numberOptionalFields); - LOG.debug("Number of mandatory fields: {}", numberMandatoryFields); - LOG.debug("Total: {}", totalFields); + if (LOG.isDebugEnabled()) { + LOG.debug("Number of optional fields: {}", numberOptionalFields); + LOG.debug("Number of mandatory fields: {}", numberMandatoryFields); + LOG.debug("Total: {}", totalFields); + } } if (annotatedFields.size() < maxpos) { @@ -245,18 +246,19 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor LOG.debug("Separator converted: '0x{}', from: {}", Integer.toHexString(separator), this.getSeparator()); } - Class<?> clazz = type(); - if (model.containsKey(clazz.getName())) { + for (Class<?> clazz : models) { + if (model.containsKey(clazz.getName())) { - Object obj = model.get(clazz.getName()); - if (LOG.isDebugEnabled()) { - LOG.debug("Model object: {}, class: {}", obj, obj.getClass().getName()); - } - if (obj != null) { + Object obj = model.get(clazz.getName()); + if (LOG.isDebugEnabled()) { + LOG.debug("Model object: {}, class: {}", obj, obj.getClass().getName()); + } + if (obj != null) { - // Generate Csv table - generateCsvPositionMap(clazz, obj, results); + // Generate Csv table + generateCsvPositionMap(clazz, obj, results); + } } } @@ -515,60 +517,60 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor */ private void initCsvRecordParameters() { if (separator == null) { - Class<?> cl = type(); + for (Class<?> cl : models) { - // Get annotation @CsvRecord from the class - CsvRecord record = cl.getAnnotation(CsvRecord.class); + // Get annotation @CsvRecord from the class + CsvRecord record = cl.getAnnotation(CsvRecord.class); - // Get annotation @Section from the class - Section section = cl.getAnnotation(Section.class); + // Get annotation @Section from the class + Section section = cl.getAnnotation(Section.class); - if (record != null) { - LOG.debug("Csv record: {}", record); + if (record != null) { + LOG.debug("Csv record: {}", record); - // Get skipFirstLine parameter - skipFirstLine = record.skipFirstLine(); - LOG.debug("Skip First Line parameter of the CSV: {}" + skipFirstLine); + // Get skipFirstLine parameter + skipFirstLine = record.skipFirstLine(); + LOG.debug("Skip First Line parameter of the CSV: {}" + skipFirstLine); - // Get generateHeaderColumnNames parameter - generateHeaderColumnNames = record.generateHeaderColumns(); - LOG.debug("Generate header column names parameter of the CSV: {}", generateHeaderColumnNames); + // Get generateHeaderColumnNames parameter + generateHeaderColumnNames = record.generateHeaderColumns(); + LOG.debug("Generate header column names parameter of the CSV: {}", generateHeaderColumnNames); - // Get Separator parameter - ObjectHelper.notNull(record.separator(), "No separator has been defined in the @Record annotation"); - separator = record.separator(); - LOG.debug("Separator defined for the CSV: {}", separator); + // Get Separator parameter + ObjectHelper.notNull(record.separator(), "No separator has been defined in the @Record annotation"); + separator = record.separator(); + LOG.debug("Separator defined for the CSV: {}", separator); - // Get carriage return parameter - crlf = record.crlf(); - LOG.debug("Carriage return defined for the CSV: {}", crlf); + // Get carriage return parameter + crlf = record.crlf(); + LOG.debug("Carriage return defined for the CSV: {}", crlf); - // Get isOrdered parameter - messageOrdered = record.isOrdered(); - LOG.debug("Must CSV record be ordered: {}", messageOrdered); + // Get isOrdered parameter + messageOrdered = record.isOrdered(); + LOG.debug("Must CSV record be ordered: {}", messageOrdered); - if (ObjectHelper.isNotEmpty(record.quote())) { - quote = record.quote(); - LOG.debug("Quoting columns with: {}", quote); - } + if (ObjectHelper.isNotEmpty(record.quote())) { + quote = record.quote(); + LOG.debug("Quoting columns with: {}", quote); + } - quoting = record.quoting(); - LOG.debug("CSV will be quoted: {}", quoting); + quoting = record.quoting(); + LOG.debug("CSV will be quoted: {}", quoting); - autospanLine = record.autospanLine(); - LOG.debug("Autospan line in last record: {}", autospanLine); - } + autospanLine = record.autospanLine(); + LOG.debug("Autospan line in last record: {}", autospanLine); + } - if (section != null) { - // Test if section number is not null - ObjectHelper.notNull(section.number(), "No number has been defined for the section"); + if (section != null) { + // Test if section number is not null + ObjectHelper.notNull(section.number(), "No number has been defined for the section"); - // Get section number and add it to the sections - sections.put(cl.getName(), section.number()); + // Get section number and add it to the sections + sections.put(cl.getName(), section.number()); + } } } } - /** * Set the default values for the non defined fields. * @param model the model which has its default fields set. http://git-wip-us.apache.org/repos/asf/camel/blob/a052c1cf/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java index e44432b..ac3bfbc 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java @@ -31,8 +31,6 @@ import org.apache.camel.dataformat.bindy.annotation.DataField; import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord; import org.apache.camel.dataformat.bindy.annotation.Link; import org.apache.camel.dataformat.bindy.format.FormatException; -import org.apache.camel.spi.PackageScanClassResolver; -import org.apache.camel.spi.PackageScanFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,7 +69,7 @@ public class BindyFixedLengthFactory extends BindyAbstractFactory implements Bin // initialize specific parameters of the fixed length model initFixedLengthModel(); } - + /** * method uses to initialize the model representing the classes who will * bind the data. This process will scan for classes according to the @@ -88,52 +86,56 @@ public class BindyFixedLengthFactory extends BindyAbstractFactory implements Bin } public void initAnnotatedFields() { - Class<?> cl = type(); - List<Field> linkFields = new ArrayList<Field>(); - if (LOG.isDebugEnabled()) { - LOG.debug("Class retrieved: {}", cl.getName()); - } + for (Class<?> cl : models) { - for (Field field : cl.getDeclaredFields()) { - DataField dataField = field.getAnnotation(DataField.class); - if (dataField != null) { + List<Field> linkFields = new ArrayList<Field>(); - if (LOG.isDebugEnabled()) { - LOG.debug("Position defined in the class: {}, position: {}, Field: {}", new Object[]{cl.getName(), dataField.pos(), dataField}); - } + if (LOG.isDebugEnabled()) { + LOG.debug("Class retrieved: {}", cl.getName()); + } - if (dataField.required()) { - ++numberMandatoryFields; - } else { - ++numberOptionalFields; - } + for (Field field : cl.getDeclaredFields()) { + DataField dataField = field.getAnnotation(DataField.class); + if (dataField != null) { + + if (LOG.isDebugEnabled()) { + LOG.debug("Position defined in the class: {}, position: {}, Field: {}", new Object[]{cl.getName(), dataField.pos(), dataField}); + } - dataFields.put(dataField.pos(), dataField); - annotatedFields.put(dataField.pos(), field); - } + if (dataField.required()) { + ++numberMandatoryFields; + } else { + ++numberOptionalFields; + } - Link linkField = field.getAnnotation(Link.class); + dataFields.put(dataField.pos(), dataField); + annotatedFields.put(dataField.pos(), field); + } - if (linkField != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("Class linked: {}, Field: {}", cl.getName(), field); + Link linkField = field.getAnnotation(Link.class); + + if (linkField != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Class linked: {}, Field: {}", cl.getName(), field); + } + linkFields.add(field); } - linkFields.add(field); + } - } + if (!linkFields.isEmpty()) { + annotatedLinkFields.put(cl.getName(), linkFields); + } - if (!linkFields.isEmpty()) { - annotatedLinkFields.put(cl.getName(), linkFields); - } + totalFields = numberMandatoryFields + numberOptionalFields; - totalFields = numberMandatoryFields + numberOptionalFields; + if (LOG.isDebugEnabled()) { + LOG.debug("Number of optional fields: {}", numberOptionalFields); + LOG.debug("Number of mandatory fields: {}", numberMandatoryFields); + LOG.debug("Total: {}", totalFields); + } - if (LOG.isDebugEnabled()) { - LOG.debug("Number of optional fields: {}", numberOptionalFields); - LOG.debug("Number of mandatory fields: {}", numberMandatoryFields); - LOG.debug("Total: {}", totalFields); } } @@ -277,21 +279,23 @@ public class BindyFixedLengthFactory extends BindyAbstractFactory implements Bin StringBuilder buffer = new StringBuilder(); Map<Integer, List<String>> results = new HashMap<Integer, List<String>>(); - Class<?> clazz = type(); - if (model.containsKey(clazz.getName())) { + for (Class<?> clazz : models) { - Object obj = model.get(clazz.getName()); + if (model.containsKey(clazz.getName())) { - if (LOG.isDebugEnabled()) { - LOG.debug("Model object: {}, class: {}", obj, obj.getClass().getName()); - } + Object obj = model.get(clazz.getName()); + + if (LOG.isDebugEnabled()) { + LOG.debug("Model object: {}, class: {}", obj, obj.getClass().getName()); + } - if (obj != null) { + if (obj != null) { - // Generate Fixed Length table - // containing the positions of the fields - generateFixedLengthPositionMap(clazz, obj, results); + // Generate Fixed Length table + // containing the positions of the fields + generateFixedLengthPositionMap(clazz, obj, results); + } } } @@ -443,53 +447,54 @@ public class BindyFixedLengthFactory extends BindyAbstractFactory implements Bin */ private void initFixedLengthRecordParameters() { - Class<?> cl = type(); - - // Get annotation @FixedLengthRecord from the class - FixedLengthRecord record = cl.getAnnotation(FixedLengthRecord.class); - - if (record != null) { - LOG.debug("Fixed length record: {}", record); - - // Get carriage return parameter - crlf = record.crlf(); - LOG.debug("Carriage return defined for the CSV: {}", crlf); - - // Get hasHeader parameter - hasHeader = record.hasHeader(); - LOG.debug("Has Header: {}", hasHeader); - - // Get skipHeader parameter - skipHeader = record.skipHeader(); - LOG.debug("Skip Header: {}", skipHeader); - - // Get hasFooter parameter - hasFooter = record.hasFooter(); - LOG.debug("Has Footer: {}", hasFooter); - - // Get skipFooter parameter - skipFooter = record.skipFooter(); - LOG.debug("Skip Footer: {}", skipFooter); - - // Get isHeader parameter - isHeader = record.isHeader(); - LOG.debug("Is Header: {}", isHeader); - - // Get isFooter parameter - isFooter = record.isFooter(); - LOG.debug("Is Footer: {}", isFooter); - - // Get padding character - paddingChar = record.paddingChar(); - LOG.debug("Padding char: {}", paddingChar); - - // Get length of the record - recordLength = record.length(); - LOG.debug("Length of the record: {}", recordLength); - - // Get flag for ignore trailing characters - ignoreTrailingChars = record.ignoreTrailingChars(); - LOG.debug("Ignore trailing chars: {}", ignoreTrailingChars); + for (Class<?> cl : models) { + + // Get annotation @FixedLengthRecord from the class + FixedLengthRecord record = cl.getAnnotation(FixedLengthRecord.class); + + if (record != null) { + LOG.debug("Fixed length record: {}", record); + + // Get carriage return parameter + crlf = record.crlf(); + LOG.debug("Carriage return defined for the CSV: {}", crlf); + + // Get hasHeader parameter + hasHeader = record.hasHeader(); + LOG.debug("Has Header: {}", hasHeader); + + // Get skipHeader parameter + skipHeader = record.skipHeader(); + LOG.debug("Skip Header: {}", skipHeader); + + // Get hasFooter parameter + hasFooter = record.hasFooter(); + LOG.debug("Has Footer: {}", hasFooter); + + // Get skipFooter parameter + skipFooter = record.skipFooter(); + LOG.debug("Skip Footer: {}", skipFooter); + + // Get isHeader parameter + isHeader = record.isHeader(); + LOG.debug("Is Header: {}", isHeader); + + // Get isFooter parameter + isFooter = record.isFooter(); + LOG.debug("Is Footer: {}", isFooter); + + // Get padding character + paddingChar = record.paddingChar(); + LOG.debug("Padding char: {}", paddingChar); + + // Get length of the record + recordLength = record.length(); + LOG.debug("Length of the record: {}", recordLength); + + // Get flag for ignore trailing characters + ignoreTrailingChars = record.ignoreTrailingChars(); + LOG.debug("Ignore trailing chars: {}", ignoreTrailingChars); + } } if (hasHeader && isHeader) { http://git-wip-us.apache.org/repos/asf/camel/blob/a052c1cf/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java index 9418f40..625a415 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java @@ -32,7 +32,6 @@ import org.apache.camel.dataformat.bindy.annotation.Message; import org.apache.camel.dataformat.bindy.annotation.OneToMany; import org.apache.camel.dataformat.bindy.annotation.Section; import org.apache.camel.dataformat.bindy.util.ConverterUtils; -import org.apache.camel.spi.PackageScanClassResolver; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,7 +54,7 @@ public class BindyKeyValuePairFactory extends BindyAbstractFactory implements Bi private String pairSeparator; private boolean messageOrdered; - + public BindyKeyValuePairFactory(Class<?> type) throws Exception { super(type); @@ -83,31 +82,34 @@ public class BindyKeyValuePairFactory extends BindyAbstractFactory implements Bi public void initAnnotatedFields() { - Class<?> cl = type(); - List<Field> linkFields = new ArrayList<Field>(); + for (Class<?> cl : models) { - for (Field field : cl.getDeclaredFields()) { - KeyValuePairField keyValuePairField = field.getAnnotation(KeyValuePairField.class); - if (keyValuePairField != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("Key declared in the class : {}, key : {}, Field : {}", new Object[]{cl.getName(), keyValuePairField.tag(), keyValuePairField}); + List<Field> linkFields = new ArrayList<Field>(); + + for (Field field : cl.getDeclaredFields()) { + KeyValuePairField keyValuePairField = field.getAnnotation(KeyValuePairField.class); + if (keyValuePairField != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Key declared in the class : {}, key : {}, Field : {}", new Object[]{cl.getName(), keyValuePairField.tag(), keyValuePairField}); + } + keyValuePairFields.put(keyValuePairField.tag(), keyValuePairField); + annotatedFields.put(keyValuePairField.tag(), field); } - keyValuePairFields.put(keyValuePairField.tag(), keyValuePairField); - annotatedFields.put(keyValuePairField.tag(), field); - } - Link linkField = field.getAnnotation(Link.class); + Link linkField = field.getAnnotation(Link.class); - if (linkField != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("Class linked : {}, Field {}", cl.getName(), field); + if (linkField != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Class linked : {}, Field {}", cl.getName(), field); + } + linkFields.add(field); } - linkFields.add(field); } - } - if (!linkFields.isEmpty()) { - annotatedLinkFields.put(cl.getName(), linkFields); + if (!linkFields.isEmpty()) { + annotatedLinkFields.put(cl.getName(), linkFields); + } + } } @@ -161,15 +163,19 @@ public class BindyKeyValuePairFactory extends BindyAbstractFactory implements Bi } - Class<?> clazz = type(); - Object obj = model.get(clazz.getName()); + // Iterate over the model + for (Class<?> clazz : models) { + + Object obj = model.get(clazz.getName()); - if (obj != null) { + if (obj != null) { - // Generate model from key value map - generateModelFromKeyValueMap(clazz, obj, results, line, lists); + // Generate model from key value map + generateModelFromKeyValueMap(clazz, obj, results, line, lists); + } } + } private void generateModelFromKeyValueMap(Class<?> clazz, Object obj, Map<Integer, List<String>> results, int line, Map<String, List<Object>> lists) throws Exception { @@ -581,40 +587,43 @@ public class BindyKeyValuePairFactory extends BindyAbstractFactory implements Bi */ private void initMessageParameters() { if ((pairSeparator == null) || (keyValuePairSeparator == null)) { - Class<?> cl = type(); - // Get annotation @Message from the class - Message message = cl.getAnnotation(Message.class); - - // Get annotation @Section from the class - Section section = cl.getAnnotation(Section.class); - - if (message != null) { - // Get Pair Separator parameter - ObjectHelper.notNull(message.pairSeparator(), "No Pair Separator has been defined in the @Message annotation"); - pairSeparator = message.pairSeparator(); - LOG.debug("Pair Separator defined for the message: {}", pairSeparator); - - // Get KeyValuePair Separator parameter - ObjectHelper.notNull(message.keyValuePairSeparator(), "No Key Value Pair Separator has been defined in the @Message annotation"); - keyValuePairSeparator = message.keyValuePairSeparator(); - LOG.debug("Key Value Pair Separator defined for the message: {}", keyValuePairSeparator); - - // Get carriage return parameter - crlf = message.crlf(); - LOG.debug("Carriage return defined for the message: {}", crlf); - - // Get isOrdered parameter - messageOrdered = message.isOrdered(); - LOG.debug("Is the message ordered in output: {}", messageOrdered); - } + for (Class<?> cl : models) { + // Get annotation @Message from the class + Message message = cl.getAnnotation(Message.class); + + // Get annotation @Section from the class + Section section = cl.getAnnotation(Section.class); + + if (message != null) { + // Get Pair Separator parameter + ObjectHelper.notNull(message.pairSeparator(), "No Pair Separator has been defined in the @Message annotation"); + pairSeparator = message.pairSeparator(); + LOG.debug("Pair Separator defined for the message: {}", pairSeparator); + + // Get KeyValuePair Separator parameter + ObjectHelper.notNull(message.keyValuePairSeparator(), "No Key Value Pair Separator has been defined in the @Message annotation"); + keyValuePairSeparator = message.keyValuePairSeparator(); + LOG.debug("Key Value Pair Separator defined for the message: {}", keyValuePairSeparator); + + // Get carriage return parameter + crlf = message.crlf(); + LOG.debug("Carriage return defined for the message: {}", crlf); + + // Get isOrdered parameter + messageOrdered = message.isOrdered(); + LOG.debug("Is the message ordered in output: {}", messageOrdered); + } - if (section != null) { - // Test if section number is not null - ObjectHelper.notNull(section.number(), "No number has been defined for the section"); + if (section != null) { + // Test if section number is not null + ObjectHelper.notNull(section.number(), "No number has been defined for the section"); - // Get section number and add it to the sections - sections.put(cl.getName(), section.number()); + // Get section number and add it to the sections + sections.put(cl.getName(), section.number()); + } } } } + + } http://git-wip-us.apache.org/repos/asf/camel/blob/a052c1cf/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java index e65c715..9d19874 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthDataFormat.java @@ -32,14 +32,10 @@ import org.apache.camel.Exchange; import org.apache.camel.dataformat.bindy.BindyAbstractDataFormat; import org.apache.camel.dataformat.bindy.BindyAbstractFactory; import org.apache.camel.dataformat.bindy.BindyFixedLengthFactory; -import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord; import org.apache.camel.dataformat.bindy.util.ConverterUtils; import org.apache.camel.spi.DataFormat; -import org.apache.camel.spi.PackageScanClassResolver; -import org.apache.camel.spi.PackageScanFilter; import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,7 +62,6 @@ public class BindyFixedLengthDataFormat extends BindyAbstractDataFormat { @SuppressWarnings("unchecked") public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception { - PackageScanClassResolver resolver = exchange.getContext().getPackageScanClassResolver(); BindyFixedLengthFactory factory = (BindyFixedLengthFactory) getFactory(); ObjectHelper.notNull(factory, "not instantiated"); @@ -148,7 +143,6 @@ public class BindyFixedLengthDataFormat extends BindyAbstractDataFormat { } public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception { - PackageScanClassResolver resolver = exchange.getContext().getPackageScanClassResolver(); BindyFixedLengthFactory factory = (BindyFixedLengthFactory) getFactory(); ObjectHelper.notNull(factory, "not instantiated"); @@ -266,45 +260,17 @@ public class BindyFixedLengthDataFormat extends BindyAbstractDataFormat { @Override protected BindyAbstractFactory createModelFactory() throws Exception { - - // Initialize the primary (body) model factory ignoring header and footer model classes - PackageScanFilter defaultRecordScanFilter = new PackageScanFilter() { - @Override - public boolean matches(Class<?> type) { - FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class); - return record != null && !record.isFooter() && !record.isHeader(); - } - }; BindyFixedLengthFactory factory = new BindyFixedLengthFactory(getClassType()); // Optionally initialize the header factory... using header model classes if (factory.hasHeader()) { - PackageScanFilter headerScanFilter = new PackageScanFilter() { - @Override - public boolean matches(Class<?> type) { - FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class); - return record != null && record.isHeader(); - } - }; - this.headerFactory = new BindyFixedLengthFactory(getClassType()); - } // Optionally initialize the footer factory... using footer model classes if (factory.hasFooter()) { - - PackageScanFilter footerScanFilter = new PackageScanFilter() { - @Override - public boolean matches(Class<?> type) { - FixedLengthRecord record = type.getAnnotation(FixedLengthRecord.class); - return record != null && record.isFooter(); - } - }; - this.footerFactory = new BindyFixedLengthFactory(getClassType()); - } return factory; http://git-wip-us.apache.org/repos/asf/camel/blob/a052c1cf/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java index f9fad09..7c4a028 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java @@ -16,27 +16,28 @@ */ package org.apache.camel.dataformat.bindy.kvp; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + import org.apache.camel.Exchange; import org.apache.camel.dataformat.bindy.BindyAbstractDataFormat; import org.apache.camel.dataformat.bindy.BindyAbstractFactory; import org.apache.camel.dataformat.bindy.BindyKeyValuePairFactory; import org.apache.camel.dataformat.bindy.util.ConverterUtils; import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.PackageScanClassResolver; import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; - /** * A <a href="http://camel.apache.org/data-format.html">data format</a> ( * {@link DataFormat}) using Bindy to marshal to and from CSV files http://git-wip-us.apache.org/repos/asf/camel/blob/a052c1cf/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java ---------------------------------------------------------------------- diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java index 7c86d4e..ae3e1f9 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/util/AnnotationModelLoader.java @@ -35,7 +35,7 @@ import org.apache.camel.spi.PackageScanFilter; public class AnnotationModelLoader { private PackageScanClassResolver resolver; - //private PackageScanFilter filter; + private PackageScanFilter filter; private Set<Class<? extends Annotation>> annotations; public AnnotationModelLoader(PackageScanClassResolver resolver) { @@ -48,18 +48,26 @@ public class AnnotationModelLoader { annotations.add(Section.class); annotations.add(FixedLengthRecord.class); } - - /* + public AnnotationModelLoader(PackageScanClassResolver resolver, PackageScanFilter filter) { this(resolver); this.filter = filter; } - */ public Set<Class<?>> loadModels(String... packageNames) throws Exception { Set<Class<?>> results = resolver.findAnnotated(annotations, packageNames); - + //TODO; this logic could be moved into the PackageScanClassResolver by creating: + // findAnnotated(annotations, packageNames, filter) + Set<Class<?>> resultsToRemove = new HashSet<Class<?>>(); + if (filter != null) { + for (Class<?> clazz : results) { + if (!filter.matches(clazz)) { + resultsToRemove.add(clazz); + } + } + } + results.removeAll(resultsToRemove); return results; }