Migrated the stream-based adapters from the "adapters" module to "org.springframework.integration.stream" (INT-375).
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.message.PollableSource;
|
||||
|
||||
/**
|
||||
* A pollable source for receiving bytes from an {@link InputStream}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ByteStreamSource implements PollableSource<byte[]> {
|
||||
|
||||
private BufferedInputStream stream;
|
||||
|
||||
private Object streamMonitor;
|
||||
|
||||
private int bytesPerMessage = 1024;
|
||||
|
||||
private boolean shouldTruncate = true;
|
||||
|
||||
|
||||
public ByteStreamSource(InputStream stream) {
|
||||
this(stream, -1);
|
||||
}
|
||||
|
||||
public ByteStreamSource(InputStream stream, int bufferSize) {
|
||||
this.streamMonitor = stream;
|
||||
if (stream instanceof BufferedInputStream) {
|
||||
this.stream = (BufferedInputStream) stream;
|
||||
}
|
||||
else if (bufferSize > 0) {
|
||||
this.stream = new BufferedInputStream(stream, bufferSize);
|
||||
}
|
||||
else {
|
||||
this.stream = new BufferedInputStream(stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setBytesPerMessage(int bytesPerMessage) {
|
||||
this.bytesPerMessage = bytesPerMessage;
|
||||
}
|
||||
|
||||
public void setShouldTruncate(boolean shouldTruncate) {
|
||||
this.shouldTruncate = shouldTruncate;
|
||||
}
|
||||
|
||||
public Message<byte[]> receive() {
|
||||
try {
|
||||
byte[] bytes;
|
||||
int bytesRead = 0;
|
||||
synchronized (this.streamMonitor) {
|
||||
if (stream.available() == 0) {
|
||||
return null;
|
||||
}
|
||||
bytes = new byte[bytesPerMessage];
|
||||
bytesRead = stream.read(bytes, 0, bytes.length);
|
||||
}
|
||||
if (bytesRead <= 0) {
|
||||
return null;
|
||||
}
|
||||
if (!this.shouldTruncate) {
|
||||
return new GenericMessage<byte[]>(bytes);
|
||||
}
|
||||
else {
|
||||
byte[] result = new byte[bytesRead];
|
||||
System.arraycopy(bytes, 0, result, 0, result.length);
|
||||
return new GenericMessage<byte[]>(result);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessagingException("IO failure occurred in adapter", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.integration.endpoint.AbstractMessageConsumingEndpoint;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
|
||||
/**
|
||||
* A target that writes a byte array to an {@link OutputStream}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ByteStreamTarget extends AbstractMessageConsumingEndpoint {
|
||||
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private final BufferedOutputStream stream;
|
||||
|
||||
|
||||
public ByteStreamTarget(OutputStream stream) {
|
||||
this(stream, -1);
|
||||
}
|
||||
|
||||
public ByteStreamTarget(OutputStream stream, int bufferSize) {
|
||||
if (bufferSize > 0) {
|
||||
this.stream = new BufferedOutputStream(stream, bufferSize);
|
||||
}
|
||||
else {
|
||||
this.stream = new BufferedOutputStream(stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void processMessage(Message<?> message) {
|
||||
Object payload = message.getPayload();
|
||||
if (payload == null) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn(this.getClass().getSimpleName() + " received null object");
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (payload instanceof String) {
|
||||
this.stream.write(((String) payload).getBytes());
|
||||
}
|
||||
else if (payload instanceof byte[]){
|
||||
this.stream.write((byte[]) payload);
|
||||
}
|
||||
else {
|
||||
throw new MessagingException(this.getClass().getSimpleName() +
|
||||
" only supports byte array and String-based messages");
|
||||
}
|
||||
this.stream.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessagingException("IO failure occurred in target", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.message.PollableSource;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A pollable source for {@link Reader Readers}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class CharacterStreamSource implements PollableSource<String> {
|
||||
|
||||
private final BufferedReader reader;
|
||||
|
||||
private final Object monitor;
|
||||
|
||||
|
||||
public CharacterStreamSource(Reader reader) {
|
||||
this(reader, -1);
|
||||
}
|
||||
|
||||
public CharacterStreamSource(Reader reader, int bufferSize) {
|
||||
Assert.notNull(reader, "reader must not be null");
|
||||
this.monitor = reader;
|
||||
if (reader instanceof BufferedReader) {
|
||||
this.reader = (BufferedReader) reader;
|
||||
}
|
||||
else if (bufferSize > 0) {
|
||||
this.reader = new BufferedReader(reader, bufferSize);
|
||||
}
|
||||
else {
|
||||
this.reader = new BufferedReader(reader);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public StringMessage receive() {
|
||||
try {
|
||||
synchronized (this.monitor) {
|
||||
if (!this.reader.ready()) {
|
||||
return null;
|
||||
}
|
||||
String line = this.reader.readLine();
|
||||
return (line != null) ? new StringMessage(line) : null;
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessagingException("IO failure occurred in adapter", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final CharacterStreamSource stdin() {
|
||||
return new CharacterStreamSource(new InputStreamReader(System.in));
|
||||
}
|
||||
|
||||
public static final CharacterStreamSource stdin(String charsetName) {
|
||||
try {
|
||||
return new CharacterStreamSource(new InputStreamReader(System.in, charsetName));
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
throw new ConfigurationException("unsupported encoding: " + charsetName, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.endpoint.AbstractMessageConsumingEndpoint;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A target that writes to a {@link Writer}. String-based objects will be
|
||||
* written directly, but if the object is not itself a {@link String}, the
|
||||
* target will write the result of the object's {@link #toString()} method.
|
||||
* To append a new-line after each write, set the {@link #shouldAppendNewLine}
|
||||
* flag to <em>true</em>. It is <em>false</em> by default.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class CharacterStreamTarget extends AbstractMessageConsumingEndpoint {
|
||||
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private final BufferedWriter writer;
|
||||
|
||||
private volatile boolean shouldAppendNewLine = false;
|
||||
|
||||
|
||||
public CharacterStreamTarget(Writer writer) {
|
||||
this(writer, -1);
|
||||
}
|
||||
|
||||
public CharacterStreamTarget(Writer writer, int bufferSize) {
|
||||
Assert.notNull(writer, "writer must not be null");
|
||||
if (writer instanceof BufferedWriter) {
|
||||
this.writer = (BufferedWriter) writer;
|
||||
}
|
||||
else if (bufferSize > 0) {
|
||||
this.writer = new BufferedWriter(writer, bufferSize);
|
||||
}
|
||||
else {
|
||||
this.writer = new BufferedWriter(writer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Factory method that creates a target for stdout (System.out) with the
|
||||
* default charset encoding.
|
||||
*/
|
||||
public static CharacterStreamTarget stdout() {
|
||||
return stdout(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method that creates a target for stdout (System.out) with the
|
||||
* specified charset encoding.
|
||||
*/
|
||||
public static CharacterStreamTarget stdout(String charsetName) {
|
||||
return createTargetForStream(System.out, charsetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method that creates a target for stderr (System.err) with the
|
||||
* default charset encoding.
|
||||
*/
|
||||
public static CharacterStreamTarget stderr() {
|
||||
return stderr(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method that creates a target for stderr (System.err) with the
|
||||
* specified charset encoding.
|
||||
*/
|
||||
public static CharacterStreamTarget stderr(String charsetName) {
|
||||
return createTargetForStream(System.err, charsetName);
|
||||
}
|
||||
|
||||
private static CharacterStreamTarget createTargetForStream(OutputStream stream, String charsetName) {
|
||||
if (charsetName == null) {
|
||||
return new CharacterStreamTarget(new OutputStreamWriter(stream));
|
||||
}
|
||||
try {
|
||||
return new CharacterStreamTarget(new OutputStreamWriter(stream, charsetName));
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
throw new ConfigurationException("unsupported encoding: " + charsetName, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setShouldAppendNewLine(boolean shouldAppendNewLine) {
|
||||
this.shouldAppendNewLine = shouldAppendNewLine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processMessage(Message<?> message) {
|
||||
Object payload = message.getPayload();
|
||||
if (payload == null) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("target received null payload");
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (payload instanceof String) {
|
||||
writer.write((String) payload);
|
||||
}
|
||||
else if (payload instanceof char[]) {
|
||||
this.writer.write((char[]) payload);
|
||||
}
|
||||
else if (payload instanceof byte[]) {
|
||||
this.writer.write(new String((byte[]) payload));
|
||||
}
|
||||
else {
|
||||
writer.write(payload.toString());
|
||||
}
|
||||
if (this.shouldAppendNewLine) {
|
||||
writer.newLine();
|
||||
}
|
||||
writer.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessagingException("IO failure occurred in target", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.AbstractInboundChannelAdapterParser;
|
||||
import org.springframework.integration.stream.CharacterStreamSource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parser for the <stdin-channel-adapter> element.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ConsoleSourceParser extends AbstractInboundChannelAdapterParser {
|
||||
|
||||
@Override
|
||||
protected String parseSource(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CharacterStreamSource.class);
|
||||
builder.setFactoryMethod("stdin");
|
||||
String charsetName = element.getAttribute("charset");
|
||||
if (StringUtils.hasText(charsetName)) {
|
||||
builder.addConstructorArgValue(charsetName);
|
||||
}
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.stream.CharacterStreamTarget;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parser for the "stdout-" and "stderr-channel-adapter" elements.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ConsoleTargetParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
@Override
|
||||
protected Class<?> getBeanClass(Element element) {
|
||||
return CharacterStreamTarget.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException {
|
||||
String id = element.getAttribute("id");
|
||||
if (!element.hasAttribute("channel")) {
|
||||
// the created channel will get the 'id', so the adapter's bean name includes a suffix
|
||||
id = id + ".adapter";
|
||||
}
|
||||
else if (!StringUtils.hasText(id)) {
|
||||
id = parserContext.getReaderContext().generateBeanName(definition);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
if (element.getLocalName().startsWith("stderr")) {
|
||||
builder.setFactoryMethod("stderr");
|
||||
}
|
||||
else {
|
||||
builder.setFactoryMethod("stdout");
|
||||
}
|
||||
String charsetName = element.getAttribute("charset");
|
||||
if (StringUtils.hasText(charsetName)) {
|
||||
builder.addConstructorArgValue(charsetName);
|
||||
}
|
||||
if ("true".equals(element.getAttribute("append-newline"))) {
|
||||
builder.addPropertyValue("shouldAppendNewLine", Boolean.TRUE);
|
||||
}
|
||||
String channelName = element.getAttribute("channel");
|
||||
if (StringUtils.hasText(channelName)) {
|
||||
builder.addPropertyReference("inputChannel", channelName);
|
||||
}
|
||||
else {
|
||||
builder.addPropertyReference("inputChannel", this.createDirectChannel(element, parserContext));
|
||||
}
|
||||
}
|
||||
|
||||
private String createDirectChannel(Element element, ParserContext parserContext) {
|
||||
String channelId = element.getAttribute("id");
|
||||
if (!StringUtils.hasText(channelId)) {
|
||||
throw new ConfigurationException("The channel-adapter's 'id' attribute is required when no 'channel' "
|
||||
+ "reference has been provided, because that 'id' would be used for the created channel.");
|
||||
}
|
||||
BeanDefinitionBuilder channelBuilder = BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class);
|
||||
BeanDefinitionHolder holder = new BeanDefinitionHolder(channelBuilder.getBeanDefinition(), channelId);
|
||||
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
|
||||
return channelId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream.config;
|
||||
|
||||
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class StreamNamespaceHandler extends NamespaceHandlerSupport {
|
||||
|
||||
public void init() {
|
||||
this.registerBeanDefinitionParser("stdin-channel-adapter", new ConsoleSourceParser());
|
||||
this.registerBeanDefinitionParser("stdout-channel-adapter", new ConsoleTargetParser());
|
||||
this.registerBeanDefinitionParser("stderr-channel-adapter", new ConsoleTargetParser());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns="http://www.springframework.org/schema/integration/stream"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:tool="http://www.springframework.org/schema/tool"
|
||||
xmlns:integration="http://www.springframework.org/schema/integration"
|
||||
targetNamespace="http://www.springframework.org/schema/integration/stream"
|
||||
elementFormDefault="qualified"
|
||||
attributeFormDefault="unqualified">
|
||||
|
||||
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
|
||||
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
|
||||
<xsd:import namespace="http://www.springframework.org/schema/integration"/>
|
||||
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines the configuration elements for Spring Integration Stream-based Channel Adapters.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
|
||||
<xsd:element name="stdin-channel-adapter">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Configures a source that reads from stdin (System.in).
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="integration:poller" minOccurs="0" maxOccurs="1"/>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="charset" type="xsd:string"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="stdout-channel-adapter" type="consoleOutboundChannelAdapterType"/>
|
||||
|
||||
<xsd:element name="stderr-channel-adapter" type="consoleOutboundChannelAdapterType"/>
|
||||
|
||||
<xsd:complexType name="consoleOutboundChannelAdapterType">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Configures an outbound Channel Adapter that writes to stdout (System.out)
|
||||
or to stderr (System.err) depending on the element name.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="channel" type="xsd:string"/>
|
||||
<xsd:attribute name="charset" type="xsd:string"/>
|
||||
<xsd:attribute name="append-newline" type="xsd:boolean" default="false"/>
|
||||
</xsd:complexType>
|
||||
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1 @@
|
||||
http\://www.springframework.org/schema/integration/stream=org.springframework.integration.stream.config.StreamNamespaceHandler
|
||||
@@ -0,0 +1 @@
|
||||
http\://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd=org/springframework/integration/stream/config/spring-integration-stream-1.0.xsd
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ByteStreamSourceTests {
|
||||
|
||||
@Test
|
||||
public void testEndOfStream() {
|
||||
byte[] bytes = new byte[] {1,2,3};
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
|
||||
ByteStreamSource source = new ByteStreamSource(stream);
|
||||
Message<?> message1 = source.receive();
|
||||
byte[] payload = (byte[]) message1.getPayload();
|
||||
assertEquals(3, payload.length);
|
||||
assertEquals(1, payload[0]);
|
||||
assertEquals(2, payload[1]);
|
||||
assertEquals(3, payload[2]);
|
||||
Message<?> message2 = source.receive();
|
||||
assertNull(message2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayIsTruncated() {
|
||||
byte[] bytes = new byte[] {0,1,2,3,4,5};
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
|
||||
ByteStreamSource source = new ByteStreamSource(stream);
|
||||
source.setBytesPerMessage(4);
|
||||
Message<?> message1 = source.receive();
|
||||
assertEquals(4, ((byte[]) message1.getPayload()).length);
|
||||
Message<?> message2 = source.receive();
|
||||
assertEquals(2, ((byte[]) message2.getPayload()).length);
|
||||
Message<?> message3 = source.receive();
|
||||
assertNull(message3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayIsNotTruncated() {
|
||||
byte[] bytes = new byte[] {0,1,2,3,4,5};
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
|
||||
ByteStreamSource source = new ByteStreamSource(stream);
|
||||
source.setBytesPerMessage(4);
|
||||
source.setShouldTruncate(false);
|
||||
Message<?> message1 = source.receive();
|
||||
assertEquals(4, ((byte[]) message1.getPayload()).length);
|
||||
Message<?> message2 = source.receive();
|
||||
assertEquals(4, ((byte[]) message2.getPayload()).length);
|
||||
assertEquals(4, ((byte[]) message2.getPayload())[0]);
|
||||
assertEquals(5, ((byte[]) message2.getPayload())[1]);
|
||||
assertEquals(0, ((byte[]) message2.getPayload())[2]);
|
||||
assertEquals(0, ((byte[]) message2.getPayload())[3]);
|
||||
Message<?> message3 = source.receive();
|
||||
assertNull(message3);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.endpoint.ChannelPoller;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.scheduling.PollingSchedule;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ByteStreamTargetTests {
|
||||
|
||||
private QueueChannel channel;
|
||||
|
||||
private ChannelPoller poller;
|
||||
|
||||
|
||||
@Before
|
||||
public void initialize() {
|
||||
this.channel = new QueueChannel(10);
|
||||
this.poller = new ChannelPoller(channel, new PollingSchedule(0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSingleByteArray() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
target.onMessage(new GenericMessage<byte[]>(new byte[] {1,2,3}));
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(3, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleString() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
target.onMessage(new StringMessage("foo"));
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(3, result.length);
|
||||
assertEquals("foo", new String(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskSameAsMessageCount() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(3);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(9, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(9, result[8]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskLessThanMessageCount() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(6, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskExceedsMessageCount() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(5);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(9, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesLessThanMessageCountWithMultipleDispatches() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result1 = stream.toByteArray();
|
||||
assertEquals(6, result1.length);
|
||||
assertEquals(1, result1[0]);
|
||||
poller.run();
|
||||
byte[] result2 = stream.toByteArray();
|
||||
assertEquals(9, result2.length);
|
||||
assertEquals(1, result2[0]);
|
||||
assertEquals(7, result2[6]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesExceedsMessageCountWithMultipleDispatches() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(5);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result1 = stream.toByteArray();
|
||||
assertEquals(9, result1.length);
|
||||
assertEquals(1, result1[0]);
|
||||
poller.run();
|
||||
byte[] result2 = stream.toByteArray();
|
||||
assertEquals(9, result2.length);
|
||||
assertEquals(1, result2[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamResetBetweenDispatches() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result1 = stream.toByteArray();
|
||||
assertEquals(6, result1.length);
|
||||
stream.reset();
|
||||
poller.run();
|
||||
byte[] result2 = stream.toByteArray();
|
||||
assertEquals(3, result2.length);
|
||||
assertEquals(7, result2[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamWriteBetweenDispatches() throws IOException {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result1 = stream.toByteArray();
|
||||
assertEquals(6, result1.length);
|
||||
stream.write(new byte[] {123});
|
||||
stream.flush();
|
||||
poller.run();
|
||||
byte[] result2 = stream.toByteArray();
|
||||
assertEquals(10, result2.length);
|
||||
assertEquals(1, result2[0]);
|
||||
assertEquals(123, result2[6]);
|
||||
assertEquals(7, result2[7]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class CharacterStreamSourceTests {
|
||||
|
||||
@Test
|
||||
public void testEndOfStream() {
|
||||
StringReader reader = new StringReader("test");
|
||||
CharacterStreamSource source = new CharacterStreamSource(reader);
|
||||
Message<?> message1 = source.receive();
|
||||
assertEquals("test", message1.getPayload());
|
||||
Message<?> message2 = source.receive();
|
||||
assertNull(message2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.endpoint.ChannelPoller;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.scheduling.PollingSchedule;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class CharacterStreamTargetTests {
|
||||
|
||||
private QueueChannel channel;
|
||||
|
||||
private ChannelPoller poller;
|
||||
|
||||
|
||||
@Before
|
||||
public void initialize() {
|
||||
this.channel = new QueueChannel(10);
|
||||
this.poller = new ChannelPoller(channel, new PollingSchedule(0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSingleString() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
target.onMessage(new StringMessage("foo"));
|
||||
assertEquals("foo", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoStringsAndNoNewLinesByDefault() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.subscribe(target);
|
||||
poller.setMaxMessagesPerPoll(1);
|
||||
channel.send(new StringMessage("foo"), 0);
|
||||
channel.send(new StringMessage("bar"), 0);
|
||||
poller.run();
|
||||
assertEquals("foo", writer.toString());
|
||||
poller.run();
|
||||
assertEquals("foobar", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoStringsWithNewLines() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
target.setShouldAppendNewLine(true);
|
||||
poller.subscribe(target);
|
||||
poller.setMaxMessagesPerPoll(1);
|
||||
channel.send(new StringMessage("foo"), 0);
|
||||
channel.send(new StringMessage("bar"), 0);
|
||||
poller.run();
|
||||
String newLine = System.getProperty("line.separator");
|
||||
assertEquals("foo" + newLine, writer.toString());
|
||||
poller.run();
|
||||
assertEquals("foo" + newLine + "bar" + newLine, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskSameAsMessageCount() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.subscribe(target);
|
||||
channel.send(new StringMessage("foo"), 0);
|
||||
channel.send(new StringMessage("bar"), 0);
|
||||
poller.run();
|
||||
assertEquals("foobar", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskExceedsMessageCountWithAppendedNewLines() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.setMaxMessagesPerPoll(10);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
target.setShouldAppendNewLine(true);
|
||||
channel.send(new StringMessage("foo"), 0);
|
||||
channel.send(new StringMessage("bar"), 0);
|
||||
poller.run();
|
||||
String newLine = System.getProperty("line.separator");
|
||||
assertEquals("foo" + newLine + "bar" + newLine, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleNonStringObject() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.subscribe(target);
|
||||
poller.setMaxMessagesPerPoll(1);
|
||||
TestObject testObject = new TestObject("foo");
|
||||
channel.send(new GenericMessage<TestObject>(testObject));
|
||||
poller.run();
|
||||
assertEquals("foo", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoNonStringObjectWithOutNewLines() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.subscribe(target);
|
||||
TestObject testObject1 = new TestObject("foo");
|
||||
TestObject testObject2 = new TestObject("bar");
|
||||
channel.send(new GenericMessage<TestObject>(testObject1), 0);
|
||||
channel.send(new GenericMessage<TestObject>(testObject2), 0);
|
||||
poller.run();
|
||||
assertEquals("foobar", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoNonStringObjectWithNewLines() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
target.setShouldAppendNewLine(true);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.subscribe(target);
|
||||
TestObject testObject1 = new TestObject("foo");
|
||||
TestObject testObject2 = new TestObject("bar");
|
||||
channel.send(new GenericMessage<TestObject>(testObject1), 0);
|
||||
channel.send(new GenericMessage<TestObject>(testObject2), 0);
|
||||
poller.run();
|
||||
String newLine = System.getProperty("line.separator");
|
||||
assertEquals("foo" + newLine + "bar" + newLine, writer.toString());
|
||||
}
|
||||
|
||||
|
||||
private static class TestObject {
|
||||
|
||||
private String text;
|
||||
|
||||
TestObject(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.text;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.stream.CharacterStreamSource;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ConsoleSourceParserTests {
|
||||
|
||||
@Before
|
||||
public void writeTestInput() {
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes());
|
||||
System.setIn(stream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleSourceWithDefaultCharset() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleSourceParserTests.xml", ConsoleSourceParserTests.class);
|
||||
CharacterStreamSource source =
|
||||
(CharacterStreamSource) context.getBean("sourceWithDefaultCharset");
|
||||
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(source);
|
||||
Reader bufferedReader = (Reader) sourceAccessor.getPropertyValue("reader");
|
||||
assertEquals(BufferedReader.class, bufferedReader.getClass());
|
||||
DirectFieldAccessor bufferedReaderAccessor = new DirectFieldAccessor(bufferedReader);
|
||||
Reader reader = (Reader) bufferedReaderAccessor.getPropertyValue("in");
|
||||
assertEquals(InputStreamReader.class, reader.getClass());
|
||||
Charset readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding());
|
||||
assertEquals(Charset.defaultCharset(), readerCharset);
|
||||
Message<?> message = source.receive();
|
||||
assertNotNull(message);
|
||||
assertEquals("foo", message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleSourceWithProvidedCharset() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleSourceParserTests.xml", ConsoleSourceParserTests.class);
|
||||
CharacterStreamSource source =
|
||||
(CharacterStreamSource) context.getBean("sourceWithProvidedCharset");
|
||||
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(source);
|
||||
Reader bufferedReader = (Reader) sourceAccessor.getPropertyValue("reader");
|
||||
assertEquals(BufferedReader.class, bufferedReader.getClass());
|
||||
DirectFieldAccessor bufferedReaderAccessor = new DirectFieldAccessor(bufferedReader);
|
||||
Reader reader = (Reader) bufferedReaderAccessor.getPropertyValue("in");
|
||||
assertEquals(InputStreamReader.class, reader.getClass());
|
||||
Charset readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding());
|
||||
assertEquals(Charset.forName("UTF-8"), readerCharset);
|
||||
Message<?> message = source.receive();
|
||||
assertNotNull(message);
|
||||
assertEquals("foo", message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleSourceWithInvalidCharset() {
|
||||
BeanCreationException beanCreationException = null;
|
||||
try {
|
||||
new ClassPathXmlApplicationContext(
|
||||
"invalidConsoleSourceParserTests.xml", ConsoleSourceParserTests.class);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
beanCreationException = e;
|
||||
}
|
||||
Throwable parentCause = beanCreationException.getCause().getCause();
|
||||
assertEquals(ConfigurationException.class, parentCause.getClass());
|
||||
Throwable configurationExceptionCause = ((ConfigurationException) parentCause).getCause();
|
||||
assertEquals(UnsupportedEncodingException.class, configurationExceptionCause.getClass());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.stream.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.stream.CharacterStreamTarget;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ConsoleTargetParserTests {
|
||||
|
||||
private final ByteArrayOutputStream err = new ByteArrayOutputStream();
|
||||
|
||||
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
|
||||
@Before
|
||||
public void setupStreams() {
|
||||
System.setErr(new PrintStream(this.err));
|
||||
System.setOut(new PrintStream(this.out));
|
||||
}
|
||||
|
||||
private void resetStreams() {
|
||||
this.err.reset();
|
||||
this.out.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleTargetWithDefaultCharset() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
CharacterStreamTarget target =
|
||||
(CharacterStreamTarget) context.getBean("targetWithDefaultCharset");
|
||||
DirectFieldAccessor targetAccessor = new DirectFieldAccessor(target);
|
||||
Writer bufferedWriter = (Writer) targetAccessor.getPropertyValue("writer");
|
||||
assertEquals(BufferedWriter.class, bufferedWriter.getClass());
|
||||
DirectFieldAccessor bufferedWriterAccessor = new DirectFieldAccessor(bufferedWriter);
|
||||
Writer writer = (Writer) bufferedWriterAccessor.getPropertyValue("out");
|
||||
assertEquals(OutputStreamWriter.class, writer.getClass());
|
||||
Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding());
|
||||
assertEquals(Charset.defaultCharset(), writerCharset);
|
||||
this.resetStreams();
|
||||
target.onMessage(new StringMessage("foo"));
|
||||
assertEquals("foo", out.toString());
|
||||
assertEquals("", err.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleTargetWithProvidedCharset() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
CharacterStreamTarget target =
|
||||
(CharacterStreamTarget) context.getBean("targetWithProvidedCharset");
|
||||
DirectFieldAccessor targetAccessor = new DirectFieldAccessor(target);
|
||||
Writer bufferedWriter = (Writer) targetAccessor.getPropertyValue("writer");
|
||||
assertEquals(BufferedWriter.class, bufferedWriter.getClass());
|
||||
DirectFieldAccessor bufferedWriterAccessor = new DirectFieldAccessor(bufferedWriter);
|
||||
Writer writer = (Writer) bufferedWriterAccessor.getPropertyValue("out");
|
||||
assertEquals(OutputStreamWriter.class, writer.getClass());
|
||||
Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding());
|
||||
assertEquals(Charset.forName("UTF-8"), writerCharset);
|
||||
this.resetStreams();
|
||||
target.onMessage(new StringMessage("bar"));
|
||||
assertEquals("bar", out.toString());
|
||||
assertEquals("", err.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleTargetWithInvalidCharset() {
|
||||
BeanCreationException beanCreationException = null;
|
||||
try {
|
||||
new ClassPathXmlApplicationContext(
|
||||
"invalidConsoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
beanCreationException = e;
|
||||
}
|
||||
Throwable parentCause = beanCreationException.getCause().getCause();
|
||||
assertEquals(ConfigurationException.class, parentCause.getClass());
|
||||
Throwable configurationExceptionCause = ((ConfigurationException) parentCause).getCause();
|
||||
assertEquals(UnsupportedEncodingException.class, configurationExceptionCause.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorTarget() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
CharacterStreamTarget target =
|
||||
(CharacterStreamTarget) context.getBean("stderrTarget");
|
||||
DirectFieldAccessor targetAccessor = new DirectFieldAccessor(target);
|
||||
Writer bufferedWriter = (Writer) targetAccessor.getPropertyValue("writer");
|
||||
assertEquals(BufferedWriter.class, bufferedWriter.getClass());
|
||||
DirectFieldAccessor bufferedWriterAccessor = new DirectFieldAccessor(bufferedWriter);
|
||||
Writer writer = (Writer) bufferedWriterAccessor.getPropertyValue("out");
|
||||
assertEquals(OutputStreamWriter.class, writer.getClass());
|
||||
Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding());
|
||||
assertEquals(Charset.defaultCharset(), writerCharset);
|
||||
this.resetStreams();
|
||||
target.onMessage(new StringMessage("bad"));
|
||||
assertEquals("", out.toString());
|
||||
assertEquals("bad", err.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendNewLine() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
CharacterStreamTarget target =
|
||||
(CharacterStreamTarget) context.getBean("newlineTarget");
|
||||
DirectFieldAccessor targetAccessor = new DirectFieldAccessor(target);
|
||||
Writer bufferedWriter = (Writer) targetAccessor.getPropertyValue("writer");
|
||||
assertEquals(BufferedWriter.class, bufferedWriter.getClass());
|
||||
DirectFieldAccessor bufferedWriterAccessor = new DirectFieldAccessor(bufferedWriter);
|
||||
Writer writer = (Writer) bufferedWriterAccessor.getPropertyValue("out");
|
||||
assertEquals(OutputStreamWriter.class, writer.getClass());
|
||||
Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding());
|
||||
assertEquals(Charset.defaultCharset(), writerCharset);
|
||||
this.resetStreams();
|
||||
target.onMessage(new StringMessage("foo"));
|
||||
assertEquals("foo\n", out.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<stdin-channel-adapter id="sourceWithDefaultCharset"/>
|
||||
|
||||
<stdin-channel-adapter id="sourceWithProvidedCharset" charset="UTF-8"/>
|
||||
|
||||
</beans:beans>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<channel id="testChannel"/>
|
||||
|
||||
<stdout-channel-adapter id="targetWithDefaultCharset" channel="testChannel"/>
|
||||
|
||||
<stdout-channel-adapter id="targetWithProvidedCharset" charset="UTF-8" channel="testChannel"/>
|
||||
|
||||
<stderr-channel-adapter id="stderrTarget" channel="testChannel"/>
|
||||
|
||||
<stdout-channel-adapter id="newlineTarget" append-newline="true" channel="testChannel"/>
|
||||
|
||||
</beans:beans>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<stdin-channel-adapter id="sourceWithInvalidCharset" charset="invalid-charset-name"/>
|
||||
|
||||
</beans:beans>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<stdout-channel-adapter id="targetWithInvalidCharset" charset="invalid-charset-name"/>
|
||||
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user