Skip to content
This repository was archived by the owner on Jul 9, 2022. It is now read-only.

Intscala 55 - logging channel adapter DSL components #17

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ private final class FunctionInvoker(f: => Any) {
result
}

/**
*
*/
def invokeAndReceive() = {
val result = this.invokeMethod[Object]
result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object inbound {

def poll(tgt: Any) = new {

def atFixedRate(rate: Int) = {
def atFixedRate(rate: Int) = {
val poller = new Poller(fixedRate = rate)
new ListeningIntegrationComposition(null, new InboundChannelAdapterConfig(target= tgt, poller = poller)) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed 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.springframework.integration.dsl

/**
* @author Soby Chacko
*/
object LogLevel extends Enumeration {
type LogLevel = Value
val INFO, WARN, DEBUG, TRACE, ERROR, FATAL = Value
}

class LogLevel
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed 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.springframework.integration.dsl

import LogLevel._
import java.util.UUID
import org.w3c.dom.{Element, Document}

/**
* @author Soby Chacko
*
*/
object log {

def apply(loggerName: String = null) = new SendingEndpointComposition(null, new Logger(loggerName = loggerName)){
def withFullMessage(logFullMessage: Boolean) = getSendingEndpointWithLogFullMessageAndLogLevel(loggerName, logFullMessage)
def withLogLevel(logLevel: Value) = getSendingEndpointWithLogLevelAndLogFullLogOrExpression(loggerName, logLevel)
def withExpression(expression: String) = getSendingEndpointWithExpressionAndLogLevel(loggerName, expression)
}

private def getSendingEndpointWithLogFullMessageAndLogLevel(loggerName: String = null, logFullMessage: Boolean) = {
new SendingEndpointComposition(null, new Logger(loggerName = loggerName, logFullMessage = logFullMessage)) {
def withLogLevel(logLevel: Value): SendingEndpointComposition = {
new SendingEndpointComposition(null, new Logger(loggerName = loggerName, logFullMessage = logFullMessage, logLevel = logLevel))
}
}
}

private def getSendingEndpointWithLogLevelAndLogFullLogOrExpression(loggerName: String = null, logLevel: Value) = {
new SendingEndpointComposition(null, new Logger(loggerName = loggerName, logLevel = logLevel)) {
def withFullMessage(logFullMessage: Boolean): SendingEndpointComposition = {
new SendingEndpointComposition(null, new Logger(loggerName = loggerName, logFullMessage = logFullMessage, logLevel = logLevel))
}

def withExpression(expression: String): SendingEndpointComposition = {
new SendingEndpointComposition(null, new Logger(loggerName = loggerName, logLevel = logLevel, expression = expression))
}
}
}

private def getSendingEndpointWithExpressionAndLogLevel(loggerName: String = null, expression: String) = {
new SendingEndpointComposition(null, new Logger(loggerName = loggerName, expression = expression)) {
def withLogLevel(logLevel: Value): SendingEndpointComposition = {
new SendingEndpointComposition(null, new Logger(loggerName = loggerName, logLevel = logLevel, expression = expression))
}
}
}

def withFullMessage(logFullMessage: Boolean) = getSendingEndpointWithLogFullMessageAndLogLevel(logFullMessage = logFullMessage)
def withLogLevel(logLevel: Value) = getSendingEndpointWithLogLevelAndLogFullLogOrExpression(logLevel = logLevel)
def withExpression(expression: String) = getSendingEndpointWithExpressionAndLogLevel(expression = expression)
}

private[dsl] class Logger(name: String = "$logging_ch_" + UUID.randomUUID().toString.substring(0, 8),
val loggerName: String = null,
val logFullMessage: Boolean = false,
val logLevel: Value = INFO,
val expression: String = null) extends SimpleEndpoint(name, null) with OutboundAdapterEndpoint {

override def build(document: Document = null,
targetDefinitionFunction: Function1[Any, Tuple2[String, String]],
compositionInitFunction: Function2[BaseIntegrationComposition, AbstractChannel, Unit] = null,
inputChannel: AbstractChannel,
outputChannel: AbstractChannel): Element = {

val element = document.createElement("int:logging-channel-adapter")
element.setAttribute("id", this.name)
element.setAttribute("logger-name", loggerName)
element.setAttribute("channel", inputChannel.name)
if (expression == null) {
element.setAttribute("log-full-message", logFullMessage.toString)
}
element.setAttribute("level", logLevel.toString)
element.setAttribute("expression", expression)
element
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import org.junit.Test
import org.springframework.expression.spel.standard.SpelExpressionParser
import org.springframework.expression.spel.SpelParserConfiguration
import org.springframework.integration.dsl.utils.DslUtils
import org.springframework.integration.dsl.LogLevel._
import org.springframework.integration.dsl._
import org.springframework.integration.Message
import org.springframework.core.task.SyncTaskExecutor
import scala.Some

/**
* @author Oleg Zhurakousky
Expand Down Expand Up @@ -344,4 +346,106 @@ class DslUsageDemoTests {
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterToLoggingChannelWithLogLevelWithFullMessage = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log("logger").withLogLevel(INFO).withFullMessage(true)

fl.start
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterToLoggingChannelWithFullMessageWithLogLevel = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log("logger").withFullMessage(true).withLogLevel(INFO)

fl.start
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterToLoggingChannelWithDefaults = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log()

fl.start
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterToLoggingChannelWithoutExplicityLoggerName = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log.withFullMessage(true).withLogLevel(INFO)

fl.start
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterToLoggingChannelWithSpelExpression = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log("logger").withExpression("'Value of header id: '.concat(headers.id.toString())")

fl.start
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterTologWithExpressionWithLogLevel = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log("logger").withExpression("'Value of header id: '.concat(headers.id.toString())")
.withLogLevel(INFO)

fl.start
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterTologWithLogLevelWithExpression = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log("logger").withLogLevel(INFO)
.withExpression("'Value of header id: '.concat(headers.id.toString())")

fl.start
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterTologWithoutNameWithLogLevelWithExpression = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log.withLogLevel(INFO).withExpression("'Value of header id: '.concat(headers.id.toString())")

fl.start
Thread.sleep(400)
println("done")
}

@Test
def inboundChannelAdapterTologWithoutNameWithExpressionWithLogLevel = {

val fl = inbound.poll("T(java.lang.System).currentTimeMillis()").withFixedDelay(100)
.withMaxMessagesPerPoll(2) --> log.withExpression("'Value of header id: '.concat(headers.id.toString())")
.withLogLevel(INFO)

fl.start
Thread.sleep(400)
println("done")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed 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.springframework.integration.dsl

import LogLevel._
import org.junit.{Test, Assert}

/**
* @author Soby Chacko
*/
class LoggingChannelAdapterEndpointTests {

@Test
def basicLoggingChannelAdpater() {

val lc = log("logger")

Assert.assertTrue(lc.isInstanceOf[SendingEndpointComposition])
Assert.assertTrue(lc.target.isInstanceOf[Logger])

val lcae = lc.target.asInstanceOf[Logger]
Assert.assertEquals(lcae.loggerName, "logger")
Assert.assertFalse(lcae.logFullMessage)
Assert.assertEquals(lcae.logLevel, INFO)
}

@Test
def basicLoggingChannelAdpaterWithoutExplictLoggerName() {

val lc = log.withFullMessage(true).withLogLevel(TRACE)

Assert.assertTrue(lc.isInstanceOf[SendingEndpointComposition])
Assert.assertTrue(lc.target.isInstanceOf[Logger])

val lcae = lc.target.asInstanceOf[Logger]
Assert.assertNull(lcae.loggerName)
Assert.assertTrue(lcae.logFullMessage)
Assert.assertEquals(lcae.logLevel, TRACE)
}

@Test
def loggingChannelAdpaterWithFullLogMessage() {

val lc = log("logger").withFullMessage(true)

Assert.assertTrue(lc.isInstanceOf[SendingEndpointComposition])
Assert.assertTrue(lc.target.isInstanceOf[Logger])

val lcae = lc.target.asInstanceOf[Logger]
Assert.assertEquals(lcae.loggerName, "logger")
Assert.assertTrue(lcae.logFullMessage)
}

@Test
def loggingChannelAdpaterWithFullLogMessageWithLogLevel() {

val lc = log("logger").withFullMessage(true).withLogLevel(DEBUG)

Assert.assertTrue(lc.isInstanceOf[SendingEndpointComposition])
Assert.assertTrue(lc.target.isInstanceOf[Logger])

val lcae = lc.target.asInstanceOf[Logger]
Assert.assertEquals(lcae.loggerName, "logger")
Assert.assertTrue(lcae.logFullMessage)
Assert.assertEquals(lcae.logLevel, DEBUG)
}

@Test
def loggingChannelAdpaterWithLogLevel() {

val lc = log("logger").withLogLevel(DEBUG)

Assert.assertTrue(lc.isInstanceOf[SendingEndpointComposition])
Assert.assertTrue(lc.target.isInstanceOf[Logger])

val lcae = lc.target.asInstanceOf[Logger]
Assert.assertEquals(lcae.loggerName, "logger")
Assert.assertEquals(lcae.logLevel, DEBUG)
}

@Test
def loggingChannelAdpaterWithLogLevelWithFullLogMessage() {

val lc = log("logger").withLogLevel(DEBUG).withFullMessage(true)

Assert.assertTrue(lc.isInstanceOf[SendingEndpointComposition])
Assert.assertTrue(lc.target.isInstanceOf[Logger])

val lcae = lc.target.asInstanceOf[Logger]
Assert.assertEquals(lcae.loggerName, "logger")
Assert.assertTrue(lcae.logFullMessage)
Assert.assertEquals(lcae.logLevel, DEBUG)
}

@Test
def loggingChannelAdpaterWithExpressionAndLogLevel() {

val lc = log("logger").withExpression("hello").withLogLevel(DEBUG)

Assert.assertTrue(lc.isInstanceOf[SendingEndpointComposition])
Assert.assertTrue(lc.target.isInstanceOf[Logger])

val lcae = lc.target.asInstanceOf[Logger]
Assert.assertEquals(lcae.loggerName, "logger")
Assert.assertEquals(lcae.expression, "hello")
Assert.assertEquals(lcae.logLevel, DEBUG)
}
}