Skip to content

Commit

Permalink
added lots more useful helper extension methods on Exchange and fixed…
Browse files Browse the repository at this point in the history
… up compiler error on newer kotlin compiler
  • Loading branch information
jstrachan committed May 19, 2012
1 parent e8495f3 commit ab4c845
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 2 deletions.
141 changes: 141 additions & 0 deletions koolapp-camel/src/main/kotlin/org/koolapp/camel/Exchanges.kt
Expand Up @@ -2,6 +2,8 @@ package org.koolapp.camel

import org.apache.camel.Exchange
import org.apache.camel.Message
import org.apache.camel.util.ExchangeHelper
import org.apache.camel.CamelContext

/**
* Returns the in message body
Expand All @@ -28,12 +30,106 @@ var Exchange.input: Message
set(value) {
setIn(value)
}

var Exchange.out: Message
get() = getOut()!!
set(value) {
setOut(value)
}

/**
* Returns the result message which is either the input or out message based on the exchange pattern
*/
val Exchange.result: Message
get() = ExchangeHelper.getResultMessage(this)!!

/**
* Returns the request IN [[Message]]
*/
val Exchange.request: Message
get() = getIn()!!

/**
* Returns the response OUT [[Message]]
*/
val Exchange.response: Message
get() = getOut()!!

/**
* Returns true if the given exchange pattern (if defined) can support OUT messages
*/
val Exchange.outCapable: Boolean
get() = ExchangeHelper.isOutCapable(this)

/**
* Returns true if the unit of work is exhausted
*/
val Exchange.unitOfWorkExhausted: Boolean
get() = ExchangeHelper.isUnitOfWorkExhausted(this)

/**
* Returns true if the message has been redelivered
*/
val Exchange.redelivered: Boolean
get() = ExchangeHelper.isRedelivered(this)


/**
* Returns true if the redelivery has been exhausted
*/
val Exchange.redeliveryExhausted: Boolean
get() = ExchangeHelper.isRedeliveryExhausted(this)

/**
* Returns true if the exchange has been interrupted
*/
val Exchange.interrupted: Boolean
get() = ExchangeHelper.isInterrupted(this)


/**
* Returns true if the exchange has a fault message
*/
val Exchange.hasFaultMessage: Boolean
get() = ExchangeHelper.hasFaultMessage(this)

/**
* Returns true if the exception has been handled by an error handler
*/
val Exchange.hasExceptionBeenHandledByErrorHandler: Boolean
get() = ExchangeHelper.hasExceptionBeenHandledByErrorHandler(this)

/**
* Returns true if the failure has been handled
*/
var Exchange.failureHanded: Boolean
get() = ExchangeHelper.isFailureHandled(this)
set(value) {
setProperty(Exchange.FAILURE_HANDLED, value)
if (value) {
// clear exception since its failure handled
setException(null)
}
}

/**
* Returns the [[CamelContext]]
*/
val Exchange.context: CamelContext
get() = this.getContext()!!

/**
* Returns the MIME content type on the input message or null if one is not defined
*/
val Exchange.contentType: String?
get() = ExchangeHelper.getContentType(this)

/**
* Returns the MIME content encoding on the input message or null if one is not defined
*/
val Exchange.contentEncoding: String?
get() = ExchangeHelper.getContentEncoding(this)

/**
* Returns the input message body as a String using the empty string if its null
*/
Expand All @@ -49,3 +145,48 @@ inline fun Exchange.get(propertyName: String): Any? = getProperty(propertyName)
*/
inline fun Exchange.set(propertyName: String, value: Any?): Unit = setProperty(propertyName, value)

/**
* Creates a copy of this exchange, optionally preserving the exchange id
*/
inline fun <T> Exchange.copy(preserveExchangeId: Boolean = false): Exchange = ExchangeHelper.createCopy(this, preserveExchangeId)!!

/**
* Copies the results from the given *source* to this exchange
*/
inline fun <T> Exchange.copyResultsFrom(source: Exchange): Unit {
ExchangeHelper.copyResults(this, source)
}

/**
* Copies the results from the given *source* to this exchange, preserving the pattern
*/
inline fun <T> Exchange.copyResultsPreservePatternFrom(source: Exchange): Unit {
ExchangeHelper.copyResultsPreservePattern(this, source)
}

/**
* Converts the given value to the given type using the current type conversion registry for the camel context
* returning null if the conversion could not be completed
*/
inline fun <T> Exchange.convertToType(value: Any, klass: Class<T>): T? = ExchangeHelper.convertToType(this, klass, value)

/**
* Converts the given value to the given type using the current type conversion registry for the camel context
* throwing an exception if it could not be converted
*/
inline fun <T> Exchange.requireConvertToType(value: Any, klass: Class<T>): T = ExchangeHelper.convertToMandatoryType(this, klass, value)!!

/**
* Creates a new instance of the given type from the injector
*/
inline fun <T> Exchange.newInstance(klass: Class<T>): T = ExchangeHelper.newInstance(this, klass)!!

/**
* Performs a lookup in the registry of the bean name
*/
inline fun <T> Exchange.lookupBean(name: String, klass: Class<T>): T? = ExchangeHelper.lookupBean(this, name, klass)

/**
* Performs a lookup in the registry of the bean name
*/
inline fun Exchange.lookupBean(name: String): Any? = ExchangeHelper.lookupBean(this, name)
17 changes: 17 additions & 0 deletions koolapp-stream/pom.xml
Expand Up @@ -49,6 +49,23 @@
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>

<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Expand Up @@ -103,7 +103,7 @@ open class FollowedByHandler<A,B>(streamA: Stream<A>, streamB: Stream<B>, delega
if (a != null && b != null) {
valueA = null
valueB = null
val next = #(a!!, b!!)
val next: #(A, B) = #(a, b)
onNext(next)
}
}
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Expand Up @@ -100,9 +100,17 @@
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile-kotlin-sources</id>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>

<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
Expand Down

0 comments on commit ab4c845

Please sign in to comment.