You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/12/02 15:27:34 UTC
[1/3] camel git commit: CAMEL-8101 CAMEL-8100 Add cmd operation to
MongoDB Component
Repository: camel
Updated Branches:
refs/heads/master 9f67a66f5 -> 96bf5725f
CAMEL-8101 CAMEL-8100 Add cmd operation to MongoDB Component
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5b3826ea
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5b3826ea
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5b3826ea
Branch: refs/heads/master
Commit: 5b3826ea53111bba65c7a6238e1378927cfb1897
Parents: 9f67a66
Author: Pierre-Alban DEWITTE <pa...@gmail.com>
Authored: Mon Dec 1 21:33:46 2014 +0100
Committer: Willem Jiang <wi...@gmail.com>
Committed: Tue Dec 2 22:26:33 2014 +0800
----------------------------------------------------------------------
.../component/mongodb/MongoDbEndpoint.java | 26 +++++-----
.../component/mongodb/MongoDbOperation.java | 3 +-
.../component/mongodb/MongoDbProducer.java | 50 ++++++++++++++++----
.../mongodb/MongoDbOperationsTest.java | 16 +++++--
.../mongodb/mongoBasicOperationsTest.xml | 8 +++-
5 files changed, 75 insertions(+), 28 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/5b3826ea/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
index 78978b8..b5572da 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java
@@ -163,27 +163,29 @@ public class MongoDbEndpoint extends DefaultEndpoint {
*/
public void initializeConnection() throws CamelMongoDbException {
LOG.info("Initialising MongoDb endpoint: {}", this.toString());
- if (database == null || collection == null) {
+ if (database == null || (collection == null && !(MongoDbOperation.getDbStats.equals(operation) || MongoDbOperation.command.equals(operation)))) {
throw new CamelMongoDbException("Missing required endpoint configuration: database and/or collection");
}
db = mongoConnection.getDB(database);
if (db == null) {
throw new CamelMongoDbException("Could not initialise MongoDbComponent. Database " + database + " does not exist.");
}
- if (!createCollection && !db.collectionExists(collection)) {
- throw new CamelMongoDbException("Could not initialise MongoDbComponent. Collection " + collection + " and createCollection is false.");
- }
- dbCollection = db.getCollection(collection);
+ if(collection != null) {
+ if (!createCollection && !db.collectionExists(collection)) {
+ throw new CamelMongoDbException("Could not initialise MongoDbComponent. Collection " + collection + " and createCollection is false.");
+ }
+ dbCollection = db.getCollection(collection);
- LOG.debug("MongoDb component initialised and endpoint bound to MongoDB collection with the following parameters. Address list: {}, Db: {}, Collection: {}",
- new Object[]{mongoConnection.getAllAddress().toString(), db.getName(), dbCollection.getName()});
+ LOG.debug("MongoDb component initialised and endpoint bound to MongoDB collection with the following parameters. Address list: {}, Db: {}, Collection: {}",
+ new Object[]{mongoConnection.getAllAddress().toString(), db.getName(), dbCollection.getName()});
- try {
- if (ObjectHelper.isNotEmpty(collectionIndex)) {
- ensureIndex(dbCollection, createIndex());
+ try {
+ if (ObjectHelper.isNotEmpty(collectionIndex)) {
+ ensureIndex(dbCollection, createIndex());
+ }
+ } catch (Exception e) {
+ throw new CamelMongoDbException("Error creating index", e);
}
- } catch (Exception e) {
- throw new CamelMongoDbException("Error creating index", e);
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/5b3826ea/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbOperation.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbOperation.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbOperation.java
index fe7570f..f7b5de5 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbOperation.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbOperation.java
@@ -39,6 +39,7 @@ public enum MongoDbOperation {
// others
getDbStats,
getColStats,
- count,
+ count,
+ command
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/5b3826ea/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
index b76845e..b15140d 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
@@ -128,7 +128,9 @@ public class MongoDbProducer extends DefaultProducer {
case getColStats:
doGetStats(exchange, MongoDbOperation.getColStats);
break;
-
+ case command:
+ doCommand(exchange);
+ break;
default:
throw new CamelMongoDbException("Operation not supported. Value: " + operation);
}
@@ -136,6 +138,19 @@ public class MongoDbProducer extends DefaultProducer {
// ----------- MongoDB operations ----------------
+ protected void doCommand(Exchange exchange) throws Exception {
+ DBObject result = null;
+ DB db = calculateDb(exchange);
+ DBObject cmdObj = exchange.getIn().getMandatoryBody(DBObject.class);
+
+ //TODO Manage the read preference
+ result = db.command(cmdObj);
+
+
+ Message responseMessage = prepareResponseMessage(exchange, MongoDbOperation.command);
+ responseMessage.setBody(result);
+ }
+
protected void doGetStats(Exchange exchange, MongoDbOperation operation) throws Exception {
DBObject result = null;
@@ -143,7 +158,7 @@ public class MongoDbProducer extends DefaultProducer {
result = calculateCollection(exchange).getStats();
} else if (operation == MongoDbOperation.getDbStats) {
// if it's a DB, also take into account the dynamicity option and the DB that is used
- result = calculateCollection(exchange).getDB().getStats();
+ result = calculateDb(exchange).getStats();
} else {
throw new CamelMongoDbException("Internal error: wrong operation for getStats variant" + operation);
}
@@ -388,7 +403,28 @@ public class MongoDbProducer extends DefaultProducer {
resultMessage.setBody(dbIterator);
}
// --------- Convenience methods -----------------------
-
+ private DB calculateDb(Exchange exchange) throws Exception {
+ // dynamic calculation is an option. In most cases it won't be used and we should not penalise all users with running this
+ // resolution logic on every Exchange if they won't be using this functionality at all
+ if (!endpoint.isDynamicity()) {
+ return endpoint.getDb();
+ }
+
+ String dynamicDB = exchange.getIn().getHeader(MongoDbConstants.DATABASE, String.class);
+ DB db = null;
+
+ if (dynamicDB == null) {
+ db = endpoint.getDb();
+ } else {
+ db = endpoint.getMongoConnection().getDB(dynamicDB);
+ }
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Dynamic database selected: {}", db.getName());
+ }
+ return db;
+ }
+
private DBCollection calculateCollection(Exchange exchange) throws Exception {
// dynamic calculation is an option. In most cases it won't be used and we should not penalise all users with running this
// resolution logic on every Exchange if they won't be using this functionality at all
@@ -407,13 +443,7 @@ public class MongoDbProducer extends DefaultProducer {
if (dynamicDB == null && dynamicCollection == null) {
dbCol = endpoint.getDbCollection();
} else {
- DB db = null;
-
- if (dynamicDB == null) {
- db = endpoint.getDb();
- } else {
- db = endpoint.getMongoConnection().getDB(dynamicDB);
- }
+ DB db = calculateDb(exchange);
if (dynamicCollection == null) {
dbCol = db.getCollection(endpoint.getCollection());
http://git-wip-us.apache.org/repos/asf/camel/blob/5b3826ea/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
index c0e6001..0c70546 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
@@ -213,7 +213,17 @@ public class MongoDbOperationsTest extends AbstractMongoDbTest {
assertTrue("Result is not of type DBObject", result instanceof DBObject);
assertTrue("The result should contain keys", ((DBObject) result).keySet().size() > 0);
}
-
+
+ @Test
+ public void testCommand() throws Exception {
+ //Call hostInfo, command working with every configuration
+ Object result = template
+ .requestBody("direct:command",
+ "{\"hostInfo\":\"1\"}");
+ assertTrue("Result is not of type DBObject", result instanceof DBObject);
+ assertTrue("The result should contain keys", ((DBObject) result).keySet().size() > 0);
+ }
+
@Test
public void testOperationHeader() throws Exception {
// Test that the collection has 0 documents in it
@@ -247,9 +257,9 @@ public class MongoDbOperationsTest extends AbstractMongoDbTest {
from("direct:update").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=update&writeConcern=SAFE");
from("direct:remove").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=remove&writeConcern=SAFE");
from("direct:aggregate").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=aggregate&writeConcern=SAFE");
- from("direct:getDbStats").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=getDbStats");
+ from("direct:getDbStats").to("mongodb:myDb?database={{mongodb.testDb}}&operation=getDbStats");
from("direct:getColStats").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=getColStats");
-
+ from("direct:command").to("mongodb:myDb?database={{mongodb.testDb}}&operation=command");
}
};
http://git-wip-us.apache.org/repos/asf/camel/blob/5b3826ea/components/camel-mongodb/src/test/resources/org/apache/camel/component/mongodb/mongoBasicOperationsTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/resources/org/apache/camel/component/mongodb/mongoBasicOperationsTest.xml b/components/camel-mongodb/src/test/resources/org/apache/camel/component/mongodb/mongoBasicOperationsTest.xml
index 0263c11..d46008f 100644
--- a/components/camel-mongodb/src/test/resources/org/apache/camel/component/mongodb/mongoBasicOperationsTest.xml
+++ b/components/camel-mongodb/src/test/resources/org/apache/camel/component/mongodb/mongoBasicOperationsTest.xml
@@ -74,14 +74,18 @@
<route>
<from uri="direct:getDbStats" />
- <to uri="mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=getDbStats" />
+ <to uri="mongodb:myDb?database={{mongodb.testDb}}&operation=getDbStats" />
</route>
<route>
<from uri="direct:getColStats" />
<to uri="mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=getColStats" />
</route>
-
+
+ <route>
+ <from uri="direct:command" />
+ <to uri="mongodb:myDb?database={{mongodb.testDb}}&operation=command" />
+ </route>
</camelContext>
</beans>
[2/3] camel git commit: CAMEl-8104 Fixed the unit test error of
camel-core
Posted by ni...@apache.org.
CAMEl-8104 Fixed the unit test error of camel-core
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5d96933e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5d96933e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5d96933e
Branch: refs/heads/master
Commit: 5d96933e6890185f985e4c3d68a920f0240204b8
Parents: 5b3826e
Author: Willem Jiang <wi...@gmail.com>
Authored: Tue Dec 2 22:22:32 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Tue Dec 2 22:27:03 2014 +0800
----------------------------------------------------------------------
.../java/org/apache/camel/model/rest/RestDefinition.java | 9 +++++++++
1 file changed, 9 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/5d96933e/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
index adc0df9..2ddbcca 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
@@ -103,10 +103,19 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
public void setVerbs(List<VerbDefinition> verbs) {
this.verbs = verbs;
}
+
+ public Boolean getSkipBindingOnErrorCode() {
+ return skipBindingOnErrorCode;
+ }
+ public void setSkipBindingOnErrorCode(Boolean skipBindingOnErrorCode) {
+ this.skipBindingOnErrorCode = skipBindingOnErrorCode;
+ }
+
// Fluent API
//-------------------------------------------------------------------------
+
/**
* To set the base path of this REST service
*/
[3/3] camel git commit: CAMEL-8102 Add support to ObjectHelper to
create Iterable's with thanks to Aaron
Posted by ni...@apache.org.
CAMEL-8102 Add support to ObjectHelper to create Iterable's with thanks to Aaron
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/96bf5725
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/96bf5725
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/96bf5725
Branch: refs/heads/master
Commit: 96bf5725f22895d81f8abc4faf3ca1989170609c
Parents: 5d96933
Author: Willem Jiang <wi...@gmail.com>
Authored: Tue Dec 2 22:25:27 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Tue Dec 2 22:27:04 2014 +0800
----------------------------------------------------------------------
.../apache/camel/converter/ObjectConverter.java | 11 +-
.../apache/camel/processor/CatchProcessor.java | 7 +-
.../org/apache/camel/util/ObjectHelper.java | 268 ++++++++++++-------
.../camel/converter/ObjectConverterTest.java | 13 +
4 files changed, 192 insertions(+), 107 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/96bf5725/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java b/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
index 2fe8580..4d375c5 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
@@ -70,8 +70,15 @@ public final class ObjectConverter {
public static Iterator<?> iterator(Object value) {
return ObjectHelper.createIterator(value);
}
-
-
+
+ /**
+ * Creates an iterable over the value
+ */
+ @Converter
+ public static Iterable<?> iterable(Object value) {
+ return ObjectHelper.createIterable(value);
+ }
+
/**
* Returns the converted value, or null if the value is null
*/
http://git-wip-us.apache.org/repos/asf/camel/blob/96bf5725/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
index 4998755..5cc5b9c 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
@@ -16,7 +16,6 @@
*/
package org.apache.camel.processor;
-import java.util.Iterator;
import java.util.List;
import org.apache.camel.AsyncCallback;
@@ -124,11 +123,9 @@ public class CatchProcessor extends DelegateAsyncProcessor implements Traceable
*/
protected Throwable catches(Exchange exchange, Throwable exception) {
// use the exception iterator to walk the caused by hierarchy
- Iterator<Throwable> it = ObjectHelper.createExceptionIterator(exception);
- while (it.hasNext()) {
- Throwable e = it.next();
+ for (final Throwable e : ObjectHelper.createExceptionIterable(exception)) {
// see if we catch this type
- for (Class<?> type : exceptions) {
+ for (final Class<?> type : exceptions) {
if (type.isInstance(e) && matchesWhen(exchange)) {
return e;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/96bf5725/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
index 3162d38..55bcf8c 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -479,6 +479,38 @@ public final class ObjectHelper {
}
/**
+ * Creates an iterable over the value if the value is a collection, an
+ * Object[], a String with values separated by comma,
+ * or a primitive type array; otherwise to simplify the caller's code,
+ * we just create a singleton collection iterator over a single value
+ * <p/>
+ * Will default use comma for String separating String values.
+ * This method does <b>not</b> allow empty values
+ *
+ * @param value the value
+ * @return the iterable
+ */
+ public static Iterable<Object> createIterable(Object value) {
+ return createIterable(value, DEFAULT_DELIMITER);
+ }
+
+ /**
+ * Creates an iterable over the value if the value is a collection, an
+ * Object[], a String with values separated by the given delimiter,
+ * or a primitive type array; otherwise to simplify the caller's
+ * code, we just create a singleton collection iterator over a single value
+ * <p/>
+ * This method does <b>not</b> allow empty values
+ *
+ * @param value the value
+ * @param delimiter delimiter for separating String values
+ * @return the iterable
+ */
+ public static Iterable<Object> createIterable(Object value, String delimiter) {
+ return createIterable(value, delimiter, false);
+ }
+
+ /**
* Creates an iterator over the value if the value is a collection, an
* Object[], a String with values separated by comma,
* or a primitive type array; otherwise to simplify the caller's code,
@@ -515,19 +547,40 @@ public final class ObjectHelper {
* Object[], a String with values separated by the given delimiter,
* or a primitive type array; otherwise to simplify the caller's
* code, we just create a singleton collection iterator over a single value
- *
+ *
* </p> In case of primitive type arrays the returned {@code Iterator} iterates
* over the corresponding Java primitive wrapper objects of the given elements
* inside the {@code value} array. That's we get an autoboxing of the primitive
* types here for free as it's also the case in Java language itself.
- *
+ *
* @param value the value
* @param delimiter delimiter for separating String values
* @param allowEmptyValues whether to allow empty values
* @return the iterator
*/
- @SuppressWarnings("unchecked")
public static Iterator<Object> createIterator(Object value, String delimiter, final boolean allowEmptyValues) {
+ return createIterable(value, delimiter, allowEmptyValues).iterator();
+ }
+
+ /**
+ * Creates an iterable over the value if the value is a collection, an
+ * Object[], a String with values separated by the given delimiter,
+ * or a primitive type array; otherwise to simplify the caller's
+ * code, we just create a singleton collection iterator over a single value
+ *
+ * </p> In case of primitive type arrays the returned {@code Iterable} iterates
+ * over the corresponding Java primitive wrapper objects of the given elements
+ * inside the {@code value} array. That's we get an autoboxing of the primitive
+ * types here for free as it's also the case in Java language itself.
+ *
+ * @param value the value
+ * @param delimiter delimiter for separating String values
+ * @param allowEmptyValues whether to allow empty values
+ * @return the iterable
+ * @see java.lang.Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static Iterable<Object> createIterable(Object value, String delimiter, final boolean allowEmptyValues) {
// if its a message than we want to iterate its body
if (value instanceof Message) {
@@ -535,58 +588,72 @@ public final class ObjectHelper {
}
if (value == null) {
- return Collections.emptyList().iterator();
+ return Collections.emptyList();
} else if (value instanceof Iterator) {
- return (Iterator<Object>)value;
+ final Iterator<Object> iterator = (Iterator<Object>)value;
+ return new Iterable<Object>() {
+ @Override
+ public Iterator<Object> iterator() {
+ return iterator;
+ }
+ };
} else if (value instanceof Iterable) {
- return ((Iterable<Object>)value).iterator();
+ return (Iterable<Object>)value;
} else if (value.getClass().isArray()) {
if (isPrimitiveArrayType(value.getClass())) {
final Object array = value;
- return new Iterator<Object>() {
- private int idx;
-
- public boolean hasNext() {
- return idx < Array.getLength(array);
- }
-
- public Object next() {
- if (!hasNext()) {
- throw new NoSuchElementException("no more element available for '" + array + "' at the index " + idx);
- }
-
- return Array.get(array, idx++);
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
+ return new Iterable<Object>() {
+ @Override
+ public Iterator<Object> iterator() {
+ return new Iterator<Object>() {
+ private int idx;
+
+ public boolean hasNext() {
+ return idx < Array.getLength(array);
+ }
+
+ public Object next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("no more element available for '" + array + "' at the index " + idx);
+ }
+
+ return Array.get(array, idx++);
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
}
-
};
} else {
- List<Object> list = Arrays.asList((Object[]) value);
- return list.iterator();
+ return Arrays.asList((Object[]) value);
}
} else if (value instanceof NodeList) {
// lets iterate through DOM results after performing XPaths
final NodeList nodeList = (NodeList) value;
- return new Iterator<Object>() {
- private int idx;
-
- public boolean hasNext() {
- return idx < nodeList.getLength();
- }
+ return new Iterable<Object>() {
+ @Override
+ public Iterator<Object> iterator() {
+ return new Iterator<Object>() {
+ private int idx;
+
+ public boolean hasNext() {
+ return idx < nodeList.getLength();
+ }
- public Object next() {
- if (!hasNext()) {
- throw new NoSuchElementException("no more element available for '" + nodeList + "' at the index " + idx);
- }
+ public Object next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("no more element available for '" + nodeList + "' at the index " + idx);
+ }
- return nodeList.item(idx++);
- }
+ return nodeList.item(idx++);
+ }
- public void remove() {
- throw new UnsupportedOperationException();
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
}
};
} else if (value instanceof String) {
@@ -596,7 +663,7 @@ public final class ObjectHelper {
if (delimiter != null && s.contains(delimiter)) {
// use a scanner if it contains the delimiter
- Scanner scanner = new Scanner((String)value);
+ final Scanner scanner = new Scanner((String)value);
if (DEFAULT_DELIMITER.equals(delimiter)) {
// we use the default delimiter which is a comma, then cater for bean expressions with OGNL
@@ -611,34 +678,44 @@ public final class ObjectHelper {
// http://stackoverflow.com/questions/1516090/splitting-a-title-into-separate-parts
delimiter = ",(?!(?:[^\\(,]|[^\\)],[^\\)])+\\))";
}
-
scanner.useDelimiter(delimiter);
- return CastUtils.cast(scanner);
- } else {
- // use a plain iterator that returns the value as is as there are only a single value
- return new Iterator<Object>() {
- private int idx;
-
- public boolean hasNext() {
- return idx == 0 && (allowEmptyValues || ObjectHelper.isNotEmpty(s));
- }
-
- public Object next() {
- if (!hasNext()) {
- throw new NoSuchElementException("no more element available for '" + s + "' at the index " + idx);
- }
- idx++;
- return s;
+ return new Iterable<Object>() {
+ @Override
+ public Iterator<Object> iterator() {
+ return CastUtils.cast(scanner);
}
-
- public void remove() {
- throw new UnsupportedOperationException();
+ };
+ } else {
+ return new Iterable<Object>() {
+ @Override
+ public Iterator<Object> iterator() {
+ // use a plain iterator that returns the value as is as there are only a single value
+ return new Iterator<Object>() {
+ private int idx;
+
+ public boolean hasNext() {
+ return idx == 0 && (allowEmptyValues || ObjectHelper.isNotEmpty(s));
+ }
+
+ public Object next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException("no more element available for '" + s + "' at the index " + idx);
+ }
+
+ idx++;
+ return s;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
}
};
}
} else {
- return Collections.singletonList(value).iterator();
+ return Collections.singletonList(value);
}
}
@@ -1406,14 +1483,37 @@ public final class ObjectHelper {
}
/**
- * Creates an iterator to walk the exception from the bottom up
+ * Creates an Iterable to walk the exception from the bottom up
* (the last caused by going upwards to the root exception).
*
+ * @see java.lang.Iterable
* @param exception the exception
- * @return the iterator
+ * @return the Iterable
+ */
+ public static Iterable<Throwable> createExceptionIterable(Throwable exception) {
+ List<Throwable> throwables = new ArrayList<>();
+
+ Throwable current = exception;
+ // spool to the bottom of the caused by tree
+ while (current != null) {
+ throwables.add(current);
+ current = current.getCause();
+ }
+ Collections.reverse(throwables);
+
+ return throwables;
+ }
+
+ /**
+ * Creates an Iterator to walk the exception from the bottom up
+ * (the last caused by going upwards to the root exception).
+ *
+ * @see Iterator
+ * @param exception the exception
+ * @return the Iterator
*/
public static Iterator<Throwable> createExceptionIterator(Throwable exception) {
- return new ExceptionIterator(exception);
+ return createExceptionIterable(exception).iterator();
}
/**
@@ -1435,11 +1535,9 @@ public final class ObjectHelper {
}
// walk the hierarchy and look for it
- Iterator<Throwable> it = createExceptionIterator(exception);
- while (it.hasNext()) {
- Throwable e = it.next();
- if (type.isInstance(e)) {
- return type.cast(e);
+ for (final Throwable throwable : createExceptionIterable(exception)) {
+ if (type.isInstance(throwable)) {
+ return type.cast(throwable);
}
}
@@ -1544,34 +1642,4 @@ public final class ObjectHelper {
// value must be a number
return value.equals(Float.NaN) || value.equals(Double.NaN);
}
-
- private static final class ExceptionIterator implements Iterator<Throwable> {
- private List<Throwable> tree = new ArrayList<Throwable>();
- private Iterator<Throwable> it;
-
- public ExceptionIterator(Throwable exception) {
- Throwable current = exception;
- // spool to the bottom of the caused by tree
- while (current != null) {
- tree.add(current);
- current = current.getCause();
- }
-
- // reverse tree so we go from bottom to top
- Collections.reverse(tree);
- it = tree.iterator();
- }
-
- public boolean hasNext() {
- return it.hasNext();
- }
-
- public Throwable next() {
- return it.next();
- }
-
- public void remove() {
- it.remove();
- }
- }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/96bf5725/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java b/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
index 6255c08..9b911fb 100644
--- a/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
+++ b/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
@@ -41,6 +41,19 @@ public class ObjectConverterTest extends TestCase {
assertEquals(false, it.hasNext());
}
+ @SuppressWarnings("unchecked")
+ public void testIterable() {
+ for (final String name : (Iterable<String>)ObjectConverter.iterable("Claus,Jonathan")) {
+ switch (name) {
+ case "Claus":
+ case "Jonathan":
+ break;
+ default:
+ fail();
+ }
+ }
+ }
+
public void testToByte() {
assertEquals(Byte.valueOf("4"), ObjectConverter.toByte(Byte.valueOf("4")));
assertEquals(Byte.valueOf("4"), ObjectConverter.toByte(Integer.valueOf("4")));