diff --git a/spring-integration-splunk/README.md b/spring-integration-splunk/README.md index 3bc1923..0d208ea 100644 --- a/spring-integration-splunk/README.md +++ b/spring-integration-splunk/README.md @@ -39,7 +39,7 @@ The Inbound channel adapter is used to read data from Splunk and output a messag ### Saved search: - + ```xml @@ -86,8 +86,8 @@ The outbound channel adapter requires a child *-writer element which defines rel @@ -98,7 +98,7 @@ The outbound channel adapter requires a child *-writer element which defines rel ```xml @@ -109,30 +109,30 @@ The outbound channel adapter requires a child *-writer element which defines rel ```xml ``` - + *NOTE: The input must exist and be enabled on the server* ### Configuring The Splunk Server connection ```xml -``` - -Additional server properties include (see [splunk](http://docs.splunk.com/Documentation/Splunk/latest) documentation for details): +``` + +Additional server properties include (see [splunk](http://docs.splunk.com/Documentation/Splunk/latest) documentation for details): * app * scheme * scope * owner -The default host is *localhost* and the default port is *8089*. The *timeout* attribute indicates how long to wait for a connection in miliseconds. +The default host is *localhost* and the default port is *8089*. The *timeout* attribute indicates how long to wait for a connection in miliseconds. Development @@ -146,3 +146,4 @@ Development To generate Eclipse metadata (e.g., .classpath and .project files), do the following: ./gradlew eclipse + diff --git a/spring-integration-splunk/build.gradle b/spring-integration-splunk/build.gradle index 7313d5f..f0ae0ef 100644 --- a/spring-integration-splunk/build.gradle +++ b/spring-integration-splunk/build.gradle @@ -36,7 +36,7 @@ dependencies { compile "org.springframework:spring-context:$springVersion" compile "org.springframework:spring-expression:$springVersion" compile "org.springframework.integration:spring-integration-core:$springIntegrationVersion" - compile "commons-lang:commons-lang:$commonsLangVersion" + compile "joda-time:joda-time:$jodaTimeVersion" compile "commons-pool:commons-pool:$commonsPoolVersion" testCompile "org.mockito:mockito-all:$mockitoVersion" diff --git a/spring-integration-splunk/gradle.properties b/spring-integration-splunk/gradle.properties index 89135de..5212a02 100644 --- a/spring-integration-splunk/gradle.properties +++ b/spring-integration-splunk/gradle.properties @@ -1,10 +1,10 @@ -version=0.5.0.BUILD-SNAPSHOT +version=1.0.0.BUILD-SNAPSHOT cglibVersion=2.2 junitVersion=4.8.2 log4jVersion=1.2.12 mockitoVersion=1.9.0 -springVersion=3.1.2.RELEASE -springIntegrationVersion=2.1.2.RELEASE -commonsLangVersion=2.6 +springVersion=3.1.3.RELEASE +springIntegrationVersion=2.2.1.RELEASE +jodaTimeVersion=2.1 commonsPoolVersion=1.6 splunkVersion =1.0.0 diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/config/xml/SplunkInboundChannelAdapterParser.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/config/xml/SplunkInboundChannelAdapterParser.java index 8649d6a..16b9f17 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/config/xml/SplunkInboundChannelAdapterParser.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/config/xml/SplunkInboundChannelAdapterParser.java @@ -23,9 +23,8 @@ import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.integration.splunk.inbound.SplunkPollingChannelAdapter; -import org.springframework.integration.splunk.support.ConnectionFactoryFactoryBean; -import org.springframework.integration.splunk.support.SplunkConnectionFactory; import org.springframework.integration.splunk.support.SplunkDataReader; +import org.springframework.integration.splunk.support.SplunkServiceFactory; import org.springframework.util.StringUtils; import org.w3c.dom.Element; @@ -61,18 +60,14 @@ public class SplunkInboundChannelAdapterParser extends AbstractPollingInboundCha IntegrationNamespaceUtils.setValueIfAttributeDefined(splunkDataReaderBuilder, element, "earliest-time"); IntegrationNamespaceUtils.setValueIfAttributeDefined(splunkDataReaderBuilder, element, "latest-time"); - - BeanDefinitionBuilder connectionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SplunkConnectionFactory.class); + BeanDefinitionBuilder serviceFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SplunkServiceFactory.class); String splunkServerBeanName = element.getAttribute("splunk-server-ref"); if (StringUtils.hasText(splunkServerBeanName)) { - connectionFactoryBuilder.addConstructorArgReference(splunkServerBeanName); + serviceFactoryBuilder.addConstructorArgReference(splunkServerBeanName); } - BeanDefinitionBuilder connectionFactoryFactoryBeanBuilder = BeanDefinitionBuilder.genericBeanDefinition(ConnectionFactoryFactoryBean.class); - connectionFactoryFactoryBeanBuilder.addConstructorArgValue(connectionFactoryBuilder.getBeanDefinition()); - connectionFactoryFactoryBeanBuilder.addConstructorArgValue(element.getAttribute("pool-server-connection")); - splunkDataReaderBuilder.addConstructorArgValue(connectionFactoryFactoryBeanBuilder.getBeanDefinition()); + splunkDataReaderBuilder.addConstructorArgValue(serviceFactoryBuilder.getBeanDefinition()); String channelAdapterId = this.resolveId(element, splunkPollingChannelAdapterBuilder.getRawBeanDefinition(), parserContext); diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParser.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParser.java index 929e653..a319604 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParser.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * 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. @@ -23,10 +23,9 @@ import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.integration.splunk.outbound.SplunkOutboundChannelAdapter; -import org.springframework.integration.splunk.support.ConnectionFactoryFactoryBean; import org.springframework.integration.splunk.support.SplunkArgsFactoryBean; -import org.springframework.integration.splunk.support.SplunkConnectionFactory; import org.springframework.integration.splunk.support.SplunkIndexWriter; +import org.springframework.integration.splunk.support.SplunkServiceFactory; import org.springframework.integration.splunk.support.SplunkSubmitWriter; import org.springframework.integration.splunk.support.SplunkTcpWriter; import org.springframework.util.StringUtils; @@ -59,27 +58,24 @@ public class SplunkOutboundChannelAdapterParser extends AbstractOutboundChannelA BeanDefinitionBuilder splunkOutboundChannelAdapterBuilder = BeanDefinitionBuilder.genericBeanDefinition(SplunkOutboundChannelAdapter.class); BeanDefinitionBuilder splunkExecutorBuilder = SplunkParserUtils.getSplunkExecutorBuilder(element, parserContext); BeanDefinitionBuilder argsBuilder = BeanDefinitionBuilder.genericBeanDefinition(SplunkArgsFactoryBean.class); - + IntegrationNamespaceUtils.setValueIfAttributeDefined(argsBuilder, element, "source-type"); IntegrationNamespaceUtils.setValueIfAttributeDefined(argsBuilder, element, "source"); IntegrationNamespaceUtils.setValueIfAttributeDefined(argsBuilder, element, "host"); IntegrationNamespaceUtils.setValueIfAttributeDefined(argsBuilder, element, "host-regex"); - - BeanDefinitionBuilder connectionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SplunkConnectionFactory.class); - - String splunkServerBeanName = element.getAttribute("splunk-server-ref"); + + BeanDefinitionBuilder serviceFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SplunkServiceFactory.class); + + String splunkServerBeanName = element.getAttribute("splunk-server-ref"); if (StringUtils.hasText(splunkServerBeanName)) { - connectionFactoryBuilder.addConstructorArgReference(splunkServerBeanName); + serviceFactoryBuilder.addConstructorArgReference(splunkServerBeanName); } - BeanDefinitionBuilder connectionFactoryFactoryBeanBuilder = BeanDefinitionBuilder.genericBeanDefinition(ConnectionFactoryFactoryBean.class); - connectionFactoryFactoryBeanBuilder.addConstructorArgValue(connectionFactoryBuilder.getBeanDefinition()); - connectionFactoryFactoryBeanBuilder.addConstructorArgValue(element.getAttribute("pool-server-connection")); - + BeanDefinitionBuilder dataWriterBuilder = parseDataWriter(element, parserContext); - dataWriterBuilder.addConstructorArgValue(connectionFactoryFactoryBeanBuilder.getBeanDefinition()); + dataWriterBuilder.addConstructorArgValue(serviceFactoryBuilder.getBeanDefinition()); dataWriterBuilder.addConstructorArgValue(argsBuilder.getBeanDefinition()); - + String channelAdapterId = this.resolveId(element, splunkOutboundChannelAdapterBuilder.getRawBeanDefinition(), parserContext); String splunkExecutorBeanName = channelAdapterId + ".splunkExecutor"; @@ -99,7 +95,7 @@ public class SplunkOutboundChannelAdapterParser extends AbstractOutboundChannelA return splunkOutboundChannelAdapterBuilder.getBeanDefinition(); } - + private BeanDefinitionBuilder parseDataWriter(Element element, ParserContext parserContext) { BeanDefinitionBuilder dataWriterBuilder = null; Element dataWriter = null; @@ -118,11 +114,11 @@ public class SplunkOutboundChannelAdapterParser extends AbstractOutboundChannelA dataWriter = DomUtils.getChildElementByTagName(element, "tcp-writer"); dataWriterBuilder = BeanDefinitionBuilder.genericBeanDefinition(SplunkTcpWriter.class); IntegrationNamespaceUtils.setValueIfAttributeDefined(dataWriterBuilder, dataWriter, "port"); - + } - + IntegrationNamespaceUtils.setValueIfAttributeDefined(dataWriterBuilder, element, "auto-startup"); - + return dataWriterBuilder; } diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/Connection.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/Connection.java deleted file mode 100644 index eebcffe..0000000 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/Connection.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2011-2012 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.splunk.core; - - -/** - * Connection to Splunk service - * - * @author Jarred Li - * @since 1.0 - * - */ -public interface Connection { - - T getTarget(); - - void close(); - - boolean isOpen(); - -} diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/ConnectionFactory.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/ConnectionFactory.java deleted file mode 100644 index d69ccdc..0000000 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/ConnectionFactory.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2011-2012 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.splunk.core; - -/** - * Factory pattern to create Connection - * - * @author Jarred Li - * @since 1.0 - * - */ -public interface ConnectionFactory { - - Connection getConnection() throws Exception; - -} diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/DataReader.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/DataReader.java index 6caf942..2bbdd2e 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/DataReader.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/DataReader.java @@ -27,6 +27,6 @@ import org.springframework.integration.splunk.event.SplunkEvent; */ public interface DataReader { - List search() throws Exception; + List read() throws Exception; } diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/ServiceFactory.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/ServiceFactory.java new file mode 100644 index 0000000..e6f7c41 --- /dev/null +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/core/ServiceFactory.java @@ -0,0 +1,23 @@ +/* + * 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.splunk.core; + +import com.splunk.Service; + +/** + * @author David Turanski + * + */ +public interface ServiceFactory { + public Service getService(); +} diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/event/SplunkEvent.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/event/SplunkEvent.java index 4e772d3..e3725f6 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/event/SplunkEvent.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/event/SplunkEvent.java @@ -17,15 +17,19 @@ package org.springframework.integration.splunk.event; import java.io.Serializable; import java.util.Date; +import java.util.HashMap; import java.util.Map; -import org.apache.commons.lang.time.FastDateFormat; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; +import org.springframework.util.Assert; /** * Splunk data entity * * @author Jarred Li * @author Damien Dallimore damien@dtdsoftware.com + * @author David Turanski * @since 1.0 * */ @@ -33,8 +37,6 @@ import org.apache.commons.lang.time.FastDateFormat; @SuppressWarnings("serial") public class SplunkEvent implements Serializable { - private Map eventData; - /** * Contents of the event message */ @@ -65,11 +67,11 @@ public class SplunkEvent implements Serializable { /** * default date format is using internal generated date */ - protected static final String DATEFORMATPATTERN = "yyyy-MM-dd HH:mm:ss:SSSZ"; + protected static final String DATEFORMATPATTERN = "yyyy-MM-dd\tHH:mm:ss:SSSZ"; /** - * Date Formatter instance + * Date Formatter */ - protected static FastDateFormat DATEFORMATTER = FastDateFormat.getInstance(DATEFORMATPATTERN); + protected static final DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATEFORMATPATTERN); /** * Event prefix fields @@ -85,7 +87,7 @@ public class SplunkEvent implements Serializable { protected static final String THROWABLE_STACKTRACE_ELEMENTS = "stacktrace_elements"; protected static final String LINEBREAK = "\n"; - + // ---------------------------------- // Common event fields // ---------------------------------- @@ -227,26 +229,33 @@ public class SplunkEvent implements Serializable { */ public static String UPDATE_PACKAGE = "package"; - + /** + * A Constructor to load data from a Map + * @param data the map + */ public SplunkEvent(Map data) { this.eventMessage = new StringBuffer(); - this.eventData = data; for (String key : data.keySet()) { this.addPair(key, data.get(key)); } } /** - * Constructor. - * - * @param eventName - * the event name - * @param eventID - * the event id - * @param useInternalDate - * Whether or not to add a date to the event string - * @param quoteValues - * Whether or not to put quotes around values + * A Copy constructor + * @param splunkEvent + */ + public SplunkEvent(SplunkEvent splunkEvent) { + this.eventMessage = splunkEvent.eventMessage; + this.quoteValues = splunkEvent.quoteValues; + this.useInternalDate = splunkEvent.useInternalDate; + } + + /** + * Constructor to create a generic event + * @param eventName the event name + * @param eventID the event id + * @param useInternalDate whether or not to add a date to the event string + * @param quoteValues whether or not to put quotes around values */ public SplunkEvent(String eventName, String eventID, boolean useInternalDate, boolean quoteValues) { @@ -259,13 +268,10 @@ public class SplunkEvent implements Serializable { } /** - * Constructor.Will add internally generated date and put quotes around - * values. + * Constructor to create a generic event with the default format * - * @param eventName - * the event name - * @param eventID - * the event ID + * @param eventName the event name + * @param eventID the event ID */ public SplunkEvent(String eventName, String eventID) { @@ -279,20 +285,27 @@ public class SplunkEvent implements Serializable { this.eventMessage = new StringBuffer(); } - /** - * Simple shallow cloning method - */ - public SplunkEvent clone() { - SplunkEvent clone = new SplunkEvent(); - clone.quoteValues = this.quoteValues; - clone.useInternalDate = this.useInternalDate; - clone.eventMessage.append(this.eventMessage); - - return clone; - } - - public Map getEventData() { + Map eventData = new HashMap(); + String eventEntries = eventMessage.toString(); + + String[] entries = eventEntries.split(PAIRDELIM); + + String quote = new String(new char[] { QUOTE }); + + for (String entry : entries) { + String[] pair = entry.split(KVDELIM); + + Assert.isTrue(pair.length == 2, String.format("invalid event data [%s]", entry)); + + String key = pair[0].replaceAll(quote, ""); + String value = pair[1].replaceAll(quote, ""); + if ("null".equals(value)) { + value = null; + } + + eventData.put(key, value); + } return eventData; } @@ -417,7 +430,7 @@ public class SplunkEvent implements Serializable { * @param value */ public void addPair(String key, String value) { - + Assert.notNull(key, "key cannot be null"); if (quoteValues) this.eventMessage.append(key).append(KVDELIM).append(QUOTE).append(value).append(QUOTE).append(PAIRDELIM); else @@ -435,13 +448,13 @@ public class SplunkEvent implements Serializable { if (useInternalDate) { StringBuffer clonedMessage = new StringBuffer(); - clonedMessage.append(DATEFORMATTER.format(new Date())).append(PAIRDELIM).append(this.eventMessage); + clonedMessage.append(DATE_FORMATTER.print(new Date().getTime())).append(PAIRDELIM) + .append(this.eventMessage); event = clonedMessage.toString(); - } - else + } else event = eventMessage.toString(); // trim off trailing pair delim char(s) - String result = event.substring(0, event.length() - PAIRDELIM.length()) + LINEBREAK; + String result = event.substring(0, event.length() - PAIRDELIM.length()) + LINEBREAK; return result; } diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/outbound/SplunkOutboundChannelAdapter.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/outbound/SplunkOutboundChannelAdapter.java index 3c78da0..a24e1d9 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/outbound/SplunkOutboundChannelAdapter.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/outbound/SplunkOutboundChannelAdapter.java @@ -58,7 +58,7 @@ public class SplunkOutboundChannelAdapter extends AbstractReplyProducingMessageH @Override protected Object handleRequestMessage(Message requestMessage) { final Object result; - result = this.splunkExecutor.executeOutboundOperation(requestMessage); + result = this.splunkExecutor.write(requestMessage); if (result == null || !producesReply) { return null; } diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/AbstractSplunkDataWriter.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/AbstractSplunkDataWriter.java index 39158a8..1d480b9 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/AbstractSplunkDataWriter.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/AbstractSplunkDataWriter.java @@ -24,9 +24,8 @@ import java.net.Socket; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.SmartLifecycle; -import org.springframework.integration.splunk.core.Connection; -import org.springframework.integration.splunk.core.ConnectionFactory; import org.springframework.integration.splunk.core.DataWriter; +import org.springframework.integration.splunk.core.ServiceFactory; import org.springframework.integration.splunk.event.SplunkEvent; import org.springframework.util.Assert; @@ -45,8 +44,6 @@ public abstract class AbstractSplunkDataWriter implements DataWriter, SmartLifec protected final Log logger = LogFactory.getLog(getClass()); - protected ConnectionFactory connectionFactory; - protected Socket socket; protected Service service; @@ -58,12 +55,14 @@ public abstract class AbstractSplunkDataWriter implements DataWriter, SmartLifec private int phase; private boolean autoStartup = true; + + private final ServiceFactory serviceFactory; - protected AbstractSplunkDataWriter(ConnectionFactory connectionFactory, Args args) { - Assert.notNull(connectionFactory,"connectionFactory cannot be null"); - this.connectionFactory = connectionFactory; + protected AbstractSplunkDataWriter(ServiceFactory serviceFactory, Args args) { + Assert.notNull(serviceFactory,"service factory cannot be null"); + this.serviceFactory = serviceFactory; Assert.notNull(args, "args cannot be null"); this.args = args; @@ -96,10 +95,8 @@ public abstract class AbstractSplunkDataWriter implements DataWriter, SmartLifec */ public synchronized void start() { try { - Connection connection = connectionFactory.getConnection(); - this.service = connection.getTarget(); - - socket = createSocket(service); + service = serviceFactory.getService(); + socket = createSocket(service); } catch (Exception e) { throw new RuntimeException(e); @@ -118,8 +115,6 @@ public abstract class AbstractSplunkDataWriter implements DataWriter, SmartLifec if (socket != null) { socket.close(); } - - connectionFactory.getConnection().close(); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/ConnectionFactoryFactoryBean.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/ConnectionFactoryFactoryBean.java deleted file mode 100644 index a5c1204..0000000 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/ConnectionFactoryFactoryBean.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2011-2012 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.splunk.support; - -import org.springframework.beans.factory.FactoryBean; -import org.springframework.integration.splunk.core.ConnectionFactory; - -/** - * Factory bean to create ConnectionFactory. - * - * @author Jarred Li - * @since 1.0 - * - */ -public class ConnectionFactoryFactoryBean implements FactoryBean> { - - private final ConnectionFactory connectionFactory; - - public ConnectionFactoryFactoryBean(ConnectionFactory cf, boolean usePool) { - if (usePool) { - this.connectionFactory = new PoolingConnectionFactory(cf); - } - else { - this.connectionFactory = cf; - } - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#getObject() - */ - public ConnectionFactory getObject() throws Exception { - return this.connectionFactory; - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#getObjectType() - */ - public Class getObjectType() { - return connectionFactory.getClass(); - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.FactoryBean#isSingleton() - */ - public boolean isSingleton() { - return true; - } - -} diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/PoolingConnectionFactory.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/PoolingConnectionFactory.java deleted file mode 100644 index 2bf6efe..0000000 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/PoolingConnectionFactory.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright 2011-2012 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.splunk.support; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.commons.pool.BasePoolableObjectFactory; -import org.apache.commons.pool.ObjectPool; -import org.apache.commons.pool.impl.GenericObjectPool; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.integration.splunk.core.Connection; -import org.springframework.integration.splunk.core.ConnectionFactory; - -/** - * Pooling ConnectionFactory to pool Connection with Apache Commons Pool. - * - * @author Jarred Li - * @since 1.0 - * - */ -public class PoolingConnectionFactory implements ConnectionFactory, DisposableBean { - - private final Log log = LogFactory.getLog(this.getClass()); - - private final ConnectionFactory connectionFactory; - - private ObjectPool> pool; - - public PoolingConnectionFactory(ConnectionFactory f) { - this.connectionFactory = f; - this.pool = new GenericObjectPool>(new ConnectionPoolableObjectFactory()); - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.ServiceFactory#getService() - */ - public Connection getConnection() throws Exception { - return new PooledConnection(this.pool.borrowObject()); - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - pool.clear(); - pool.close(); - } - - class ConnectionPoolableObjectFactory extends BasePoolableObjectFactory> { - - /* (non-Javadoc) - * @see org.apache.commons.pool.BasePoolableObjectFactory#makeObject() - */ - @Override - public Connection makeObject() throws Exception { - return connectionFactory.getConnection(); - } - - @Override - public void destroyObject(Connection obj) throws Exception { - obj.close(); - } - - /** - * Whether the object is valid or not. - * - * @param obj object to be validated - * @return true - */ - public boolean validateObject(Connection obj) { - return obj.isOpen(); - } - - /** - * activate the object - * - * @param obj ignored - */ - public void activateObject(Connection obj) throws Exception { - obj.isOpen(); - } - - - } - - class PooledConnection implements Connection { - - private Connection connection; - - public PooledConnection(Connection con) { - this.connection = con; - } - - /* (non-Javadoc)l - * @see org.springframework.integration.splunk.core.IService#close() - */ - public void close() { - try { - pool.returnObject(connection); - } catch (Exception e) { - log.warn("failed to return pooled object", e); - } - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.IService#isOpen() - */ - public boolean isOpen() { - return connection.isOpen(); - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.IService#getService() - */ - public T getTarget() { - return connection.getTarget(); - } - - } - -} diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkConnectionFactory.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkConnectionFactory.java deleted file mode 100644 index 7a15e2b..0000000 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkConnectionFactory.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2011-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.splunk.support; - -import org.springframework.integration.splunk.core.Connection; -import org.springframework.integration.splunk.core.ConnectionFactory; - -import com.splunk.Service; - -/** - * Factory to create Splunk connection. - * - * @author Jarred Li - * @author David Turanski - * @since 1.0 - * - */ -public class SplunkConnectionFactory implements ConnectionFactory { - - private SplunkServer splunkServer; - private SplunkConnection connection; - public SplunkConnectionFactory(SplunkServer server) { - this.splunkServer = server; - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.ServiceFactory#getService() - */ - public synchronized Connection getConnection() throws Exception { - if (connection == null || !connection.isOpen()) { - connection = new SplunkConnection(splunkServer); - } - return connection; - } - -} diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkDataReader.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkDataReader.java index dcff125..76e8476 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkDataReader.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkDataReader.java @@ -27,9 +27,8 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.InitializingBean; -import org.springframework.integration.splunk.core.Connection; -import org.springframework.integration.splunk.core.ConnectionFactory; import org.springframework.integration.splunk.core.DataReader; +import org.springframework.integration.splunk.core.ServiceFactory; import org.springframework.integration.splunk.event.SplunkEvent; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -65,8 +64,6 @@ public class SplunkDataReader implements DataReader, InitializingBean { private static final Log logger = LogFactory.getLog(SplunkDataReader.class); - private ConnectionFactory connectionFactory; - private SearchMode mode; private int count = 0; @@ -89,8 +86,10 @@ public class SplunkDataReader implements DataReader, InitializingBean { private transient Calendar lastSuccessfulReadTime; - public SplunkDataReader(ConnectionFactory f) { - this.connectionFactory = f; + private final ServiceFactory serviceFactory; + + public SplunkDataReader(ServiceFactory serviceFactory) { + this.serviceFactory = serviceFactory; } public void setSearch(String searchStr) { @@ -176,7 +175,7 @@ public class SplunkDataReader implements DataReader, InitializingBean { return app; } - public List search() throws Exception { + public List read() throws Exception { logger.debug("mode:" + mode); switch (mode) { case SAVEDSEARCH: { @@ -217,7 +216,6 @@ public class SplunkDataReader implements DataReader, InitializingBean { return result; } - /** * get earliest time for realtime search * @@ -253,12 +251,10 @@ public class SplunkDataReader implements DataReader, InitializingBean { String lTime = null; if (StringUtils.hasText(latestTime)) { lTime = latestTime; - } - else { + } else { if (realtime) { lTime = "rt"; - } - else { + } else { DateFormat df = new SimpleDateFormat(DATE_FORMAT); lTime = df.format(startTime.getTime()); } @@ -271,18 +267,15 @@ public class SplunkDataReader implements DataReader, InitializingBean { if (lastSuccessfulReadTime == null) { eTime = initEarliestTime; - } - else { + } else { if (StringUtils.hasText(earliestTime)) { eTime = earliestTime; - } - else { + } else { String calculatedEarliestTime = calculateEarliestTime(startTime, realtime); if (calculatedEarliestTime != null) { if (realtime) { eTime = "rt" + calculatedEarliestTime; - } - else { + } else { eTime = calculatedEarliestTime; } } @@ -291,18 +284,14 @@ public class SplunkDataReader implements DataReader, InitializingBean { return eTime; } - private List runQuery(Args queryArgs) throws Exception { - Connection connection = connectionFactory.getConnection(); - try { - Job job = connection.getTarget().getJobs().create(search, queryArgs); - while (!job.isDone()) { - Thread.sleep(2000); - } - return extractData(job); - } finally { - connection.close(); + Service service = serviceFactory.getService(); + Job job = service.getJobs().create(search, queryArgs); + while (!job.isDone()) { + Thread.sleep(2000); } + return extractData(job); + } private List blockingSearch() throws Exception { @@ -317,7 +306,6 @@ public class SplunkDataReader implements DataReader, InitializingBean { return data; } - private List nonBlockingSearch() throws Exception { logger.debug("non block search start"); @@ -331,7 +319,6 @@ public class SplunkDataReader implements DataReader, InitializingBean { return data; } - /** * @return * @throws Exception @@ -364,21 +351,16 @@ public class SplunkDataReader implements DataReader, InitializingBean { populateArgs(queryArgs, startTime, false); queryArgs.put("output_mode", "xml"); - Connection connection = connectionFactory.getConnection(); - try { - InputStream os = connection.getTarget().export(search, queryArgs); - ResultsReaderXml resultsReader = new ResultsReaderXml(os); - while ((data = resultsReader.getNextEvent()) != null) { - splunkData = new SplunkEvent(data); - result.add(splunkData); - } - return result; - } finally { - connection.close(); + Service service = serviceFactory.getService(); + InputStream os = service.export(search, queryArgs); + ResultsReaderXml resultsReader = new ResultsReaderXml(os); + while ((data = resultsReader.getNextEvent()) != null) { + splunkData = new SplunkEvent(data); + result.add(splunkData); } + return result; } - private List savedSearch() throws Exception { logger.debug("saved search start"); @@ -392,13 +374,14 @@ public class SplunkDataReader implements DataReader, InitializingBean { } Calendar startTime = Calendar.getInstance(); - Connection connection = connectionFactory.getConnection(); - try { + SavedSearch search = null; Job job = null; String latestTime = getLatestTime(startTime, false); String earliestTime = getEarliestTime(startTime, false); - SavedSearchCollection savedSearches = connection.getTarget().getSavedSearches(queryArgs); + + Service service = serviceFactory.getService(); + SavedSearchCollection savedSearches = service.getSavedSearches(queryArgs); for (SavedSearch s : savedSearches.values()) { if (s.getName().equals(savedSearch)) { search = s; @@ -417,9 +400,7 @@ public class SplunkDataReader implements DataReader, InitializingBean { List data = extractData(job); this.lastSuccessfulReadTime = startTime; return data; - } finally { - connection.close(); - } + } private List extractData(Job job) throws Exception { @@ -440,8 +421,7 @@ public class SplunkDataReader implements DataReader, InitializingBean { splunkData = new SplunkEvent(data); result.add(splunkData); } - } - else { + } else { int offset = 0; while (offset < total) { InputStream stream = null; @@ -465,6 +445,4 @@ public class SplunkDataReader implements DataReader, InitializingBean { Assert.notNull(initEarliestTime, "initial earliest time can not be null"); } - - } \ No newline at end of file diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkExecutor.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkExecutor.java index 55f194d..773e1a7 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkExecutor.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkExecutor.java @@ -19,7 +19,6 @@ import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.InitializingBean; import org.springframework.integration.Message; import org.springframework.integration.MessageHandlingException; import org.springframework.integration.MessagingException; @@ -31,31 +30,21 @@ import org.springframework.integration.splunk.event.SplunkEvent; * Bundles common core logic for the Splunk components. * * @author Jarred Li + * @author David Turanski * @since 1.0 * */ -public class SplunkExecutor implements InitializingBean { +public class SplunkExecutor { private static final Log logger = LogFactory.getLog(SplunkExecutor.class); private DataReader reader; private DataWriter writer; - public SplunkExecutor() { - } - - /** - * Verifies and sets the parameters. E.g. initializes the to be used - */ - public void afterPropertiesSet() { - - } - /** * Executes the outbound Splunk Operation. - * */ - public Object executeOutboundOperation(final Message message) { + public Object write(final Message message) { try { SplunkEvent payload = (SplunkEvent) message.getPayload(); writer.write(payload); @@ -68,7 +57,7 @@ public class SplunkExecutor implements InitializingBean { } public void handleMessage(final Message message) { - executeOutboundOperation(message); + write(message); } /** @@ -78,7 +67,7 @@ public class SplunkExecutor implements InitializingBean { logger.debug("poll start:"); List queryData = null; try { - queryData = reader.search(); + queryData = reader.read(); } catch (Exception e) { String errorMsg = "search Splunk data failed"; logger.warn(errorMsg, e); diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkIndexWriter.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkIndexWriter.java index c9ec1e8..7bc341f 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkIndexWriter.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkIndexWriter.java @@ -15,7 +15,7 @@ package org.springframework.integration.splunk.support; import java.io.IOException; import java.net.Socket; -import org.springframework.integration.splunk.core.ConnectionFactory; +import org.springframework.integration.splunk.core.ServiceFactory; import org.springframework.util.Assert; import com.splunk.Args; @@ -41,8 +41,8 @@ public class SplunkIndexWriter extends AbstractSplunkDataWriter { * @param connectionFactory * @param args */ - public SplunkIndexWriter(ConnectionFactory connectionFactory, Args args) { - super(connectionFactory, args); + public SplunkIndexWriter(ServiceFactory serviceFactory, Args args) { + super(serviceFactory, args); } /* (non-Javadoc) diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkConnection.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkServiceFactory.java similarity index 58% rename from spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkConnection.java rename to spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkServiceFactory.java index 0cc2b03..55af6f9 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkConnection.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkServiceFactory.java @@ -1,17 +1,14 @@ /* - * Copyright 2011-2012 the original author or authors. + * 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 + * 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 + * 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. + * 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.splunk.support; @@ -23,23 +20,29 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import org.springframework.integration.splunk.core.Connection; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.integration.splunk.core.ServiceFactory; import com.splunk.Service; /** - * Connection to Splunk service - * - * @author Jarred Li - * @since 1.0 + * A {@link FactoryBean} for creating a {@link Service} * + * @author David Turanski */ -public class SplunkConnection implements Connection { - +public class SplunkServiceFactory implements ServiceFactory { + private final SplunkServer splunkServer; private Service service; - - public SplunkConnection(SplunkServer splunkServer) { + public SplunkServiceFactory(SplunkServer splunkServer) { + this.splunkServer = splunkServer; + } + + @Override + public synchronized Service getService() { + if (service != null) { + return service; + } final Map args = new HashMap(); if (splunkServer.getHost() != null) { args.put("host", splunkServer.getHost()); @@ -59,15 +62,15 @@ public class SplunkConnection implements Connection { args.put("username", splunkServer.getUsername()); args.put("password", splunkServer.getPassword()); - + ExecutorService executor = Executors.newSingleThreadExecutor(); - + Future future = executor.submit(new Callable(){ public Service call() throws Exception { return Service.connect(args); } }); - + try { if (splunkServer.getTimeout() > 0) { service = future.get(splunkServer.getTimeout(),TimeUnit.MILLISECONDS); @@ -78,35 +81,6 @@ public class SplunkConnection implements Connection { throw new RuntimeException(String.format("could not connect to Splunk Server @ %s:%d - %s", splunkServer.getHost(),splunkServer.getPort(),e.getMessage())); } - - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.IService#close() - */ - public void close() { - service.logout(); - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.IService#isOpen() - */ - public boolean isOpen() { - boolean result = true; - try { - service.getApplications(); - } catch (Throwable t) { - result = false; - } - return result; - } - - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.IService#getService() - */ - public Service getTarget() { return service; } - } diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkSubmitWriter.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkSubmitWriter.java index bd4bc58..84fc11b 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkSubmitWriter.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkSubmitWriter.java @@ -15,7 +15,7 @@ package org.springframework.integration.splunk.support; import java.io.IOException; import java.net.Socket; -import org.springframework.integration.splunk.core.ConnectionFactory; +import org.springframework.integration.splunk.core.ServiceFactory; import org.springframework.integration.splunk.event.SplunkEvent; import com.splunk.Args; @@ -32,8 +32,8 @@ public class SplunkSubmitWriter extends AbstractSplunkDataWriter { /** * @param connectionFactory */ - public SplunkSubmitWriter(ConnectionFactory connectionFactory, Args args) { - super(connectionFactory, args); + public SplunkSubmitWriter(ServiceFactory serviceFactory, Args args) { + super(serviceFactory, args); } /* (non-Javadoc) diff --git a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkTcpWriter.java b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkTcpWriter.java index a39dae3..ebbbc7a 100644 --- a/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkTcpWriter.java +++ b/spring-integration-splunk/src/main/java/org/springframework/integration/splunk/support/SplunkTcpWriter.java @@ -15,7 +15,7 @@ package org.springframework.integration.splunk.support; import java.io.IOException; import java.net.Socket; -import org.springframework.integration.splunk.core.ConnectionFactory; +import org.springframework.integration.splunk.core.ServiceFactory; import org.springframework.util.Assert; import com.splunk.Args; @@ -35,8 +35,8 @@ public class SplunkTcpWriter extends AbstractSplunkDataWriter { * @param connectionFactory * @param args */ - public SplunkTcpWriter(ConnectionFactory connectionFactory, Args args) { - super(connectionFactory, args); + public SplunkTcpWriter(ServiceFactory serviceFactory, Args args) { + super(serviceFactory, args); } /* (non-Javadoc) diff --git a/spring-integration-splunk/src/main/resources/org/springframework/integration/splunk/config/xml/spring-integration-splunk-1.0.xsd b/spring-integration-splunk/src/main/resources/org/springframework/integration/splunk/config/xml/spring-integration-splunk-1.0.xsd index 0d8ba89..2de6ff4 100644 --- a/spring-integration-splunk/src/main/resources/org/springframework/integration/splunk/config/xml/spring-integration-splunk-1.0.xsd +++ b/spring-integration-splunk/src/main/resources/org/springframework/integration/splunk/config/xml/spring-integration-splunk-1.0.xsd @@ -387,14 +387,6 @@ Indicates the connection timeout in ms. - - - - Whether pool the Splunk connection. - - - diff --git a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/config/xml/SplunkInboundChannelAdapterParserSavedSearchTests.java b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/config/xml/SplunkInboundChannelAdapterParserSavedSearchTests.java index 77da908..c3f7844 100644 --- a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/config/xml/SplunkInboundChannelAdapterParserSavedSearchTests.java +++ b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/config/xml/SplunkInboundChannelAdapterParserSavedSearchTests.java @@ -15,8 +15,8 @@ */ package org.springframework.integration.splunk.config.xml; -import static org.junit.Assert.*; -import junit.framework.Assert; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/event/SplunkEventTests.java b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/event/SplunkEventTests.java new file mode 100644 index 0000000..2728b52 --- /dev/null +++ b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/event/SplunkEventTests.java @@ -0,0 +1,67 @@ +/* + * 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.splunk.event; +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +/** + * @author David Turanski + * + */ +public class SplunkEventTests { + @Test + public void testGetEventData() { + Map data = new HashMap(); + data.put("foo", "foo"); + data.put("bar", null); + SplunkEvent event = new SplunkEvent(data); + Map eventData = event.getEventData(); + assertEquals(data.size(),eventData.size()); + for (String key: eventData.keySet()) { + assertEquals(data.get(key),eventData.get(key)); + } + } + public void testGetEventDataEmpty() { + SplunkEvent event = new SplunkEvent( ); + Map eventData = event.getEventData(); + assertEquals(0,eventData.size()); + } + + @Test(expected=RuntimeException.class) + public void testKeyCannotBeNull() { + SplunkEvent event = new SplunkEvent(); + event.addPair(null, "foo"); + } + + @Test + public void testCopyConstructor() { + Map data = new HashMap(); + data.put("foo", "foo"); + data.put("bar", "bar"); + SplunkEvent event = new SplunkEvent(data); + SplunkEvent event2 = new SplunkEvent(event); + assertEquals(event.quoteValues, event2.quoteValues); + assertEquals(event.useInternalDate,event2.useInternalDate); + Map eventData = event.getEventData(); + Map event2Data = event2.getEventData(); + + assertEquals(eventData.size(),event2Data.size()); + for (String key: eventData.keySet()) { + assertEquals(eventData.get(key),event2Data.get(key)); + } + } +} \ No newline at end of file diff --git a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/outbound/SplunkOutboundChannelAdapterTests.java b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/outbound/SplunkOutboundChannelAdapterTests.java index dc4fbff..de5c655 100644 --- a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/outbound/SplunkOutboundChannelAdapterTests.java +++ b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/outbound/SplunkOutboundChannelAdapterTests.java @@ -47,7 +47,7 @@ public class SplunkOutboundChannelAdapterTests { @Test public void testHandleRequestMessage() { Message message = null; - when(executor.executeOutboundOperation(message)).thenReturn(null); + when(executor.write(message)).thenReturn(null); Object ret = outboundAdapter.handleRequestMessage(message); Assert.assertNull(ret); diff --git a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/ConnectionFactoryFactoryBeanTests.java b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/ConnectionFactoryFactoryBeanTests.java deleted file mode 100644 index 52e61ef..0000000 --- a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/ConnectionFactoryFactoryBeanTests.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2011-2012 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.splunk.support; - -import junit.framework.Assert; - -import org.junit.Test; -import org.springframework.integration.splunk.core.ConnectionFactory; - -import com.splunk.Service; - -/** - * @author Jarred Li - * @since 1.0 - * - */ -public class ConnectionFactoryFactoryBeanTests { - - private ConnectionFactoryFactoryBean factoryBean; - - - /** - * Test method for {@link org.springframework.integration.splunk.support.ConnectionFactoryFactoryBean#ConnectionFactoryFactoryBean(org.springframework.integration.splunk.core.ConnectionFactory, boolean)}. - * @throws Exception - */ - @Test - public void testConnectionFactoryFactoryBean() throws Exception { - SplunkServer server = new SplunkServer(); - SplunkConnectionFactory factory = new SplunkConnectionFactory(server); - factoryBean = new ConnectionFactoryFactoryBean(factory, false); - - ConnectionFactory generatedByFactoryBean = factoryBean.getObject(); - Assert.assertTrue(generatedByFactoryBean instanceof SplunkConnectionFactory); - - } - - @Test - public void testConnectionFactoryFactoryBean_withPoll() throws Exception { - SplunkServer server = new SplunkServer(); - SplunkConnectionFactory factory = new SplunkConnectionFactory(server); - factoryBean = new ConnectionFactoryFactoryBean(factory, true); - - ConnectionFactory generatedByFactoryBean = factoryBean.getObject(); - Assert.assertTrue(generatedByFactoryBean instanceof PoolingConnectionFactory); - - } - - - /** - * Test method for {@link org.springframework.integration.splunk.support.ConnectionFactoryFactoryBean#getObjectType()}. - */ - @Test - public void testGetObjectType() { - SplunkServer server = new SplunkServer(); - SplunkConnectionFactory factory = new SplunkConnectionFactory(server); - factoryBean = new ConnectionFactoryFactoryBean(factory, true); - - Class clazz = factoryBean.getObjectType(); - Assert.assertEquals(PoolingConnectionFactory.class, clazz); - } - - /** - * Test method for {@link org.springframework.integration.splunk.support.ConnectionFactoryFactoryBean#isSingleton()}. - */ - @Test - public void testIsSingleton() { - SplunkServer server = new SplunkServer(); - SplunkConnectionFactory factory = new SplunkConnectionFactory(server); - factoryBean = new ConnectionFactoryFactoryBean(factory, false); - - Assert.assertTrue(factoryBean.isSingleton()); - } - -} diff --git a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/PoolingConnectionFactoryTests.java b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/PoolingConnectionFactoryTests.java deleted file mode 100644 index 328be7e..0000000 --- a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/PoolingConnectionFactoryTests.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2011-2012 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.splunk.support; - -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import junit.framework.Assert; - -import org.junit.Before; -import org.junit.Test; -import org.springframework.integration.splunk.core.Connection; -import org.springframework.integration.splunk.core.ConnectionFactory; - -/** - * @author Jarred Li - * @since 1.0 - * - */ -public class PoolingConnectionFactoryTests { - - private ConnectionFactory conFactory; - - private PoolingConnectionFactory poolConFactory; - - @SuppressWarnings("unchecked") - @Before - public void before() { - conFactory = mock(ConnectionFactory.class); - poolConFactory = new PoolingConnectionFactory(conFactory); - } - - - /** - * Test method for {@link org.springframework.integration.splunk.support.PoolingConnectionFactory#getConnection()}. - * @throws Exception - */ - @Test - public void testGetConnection() throws Exception { - @SuppressWarnings("unchecked") - Connection con = mock(Connection.class); - - when(con.getTarget()).thenReturn(new TestEntity("entity1")); - when(conFactory.getConnection()).thenReturn(con); - Connection returnCon = poolConFactory.getConnection(); - TestEntity obj1 = returnCon.getTarget(); - Assert.assertNotNull(obj1); - Assert.assertEquals("entity1", obj1.getName()); - - when(con.getTarget()).thenReturn(new TestEntity("entity2")); - when(conFactory.getConnection()).thenReturn(con); - returnCon = poolConFactory.getConnection(); - TestEntity obj2 = returnCon.getTarget(); - Assert.assertNotNull(obj2); - Assert.assertEquals("entity2", obj2.getName()); - - when(con.getTarget()).thenReturn(new TestEntity("entity3")); - when(conFactory.getConnection()).thenReturn(con); - returnCon = poolConFactory.getConnection(); - TestEntity obj3 = returnCon.getTarget(); - Assert.assertNotNull(obj3); - Assert.assertEquals("entity3", obj3.getName()); - - - } - - /** - * Test method for {@link org.springframework.integration.splunk.support.PoolingConnectionFactory#destroy()}. - * @throws Exception - */ - @Test(expected = IllegalStateException.class) - public void testDestroy() throws Exception { - try { - poolConFactory.destroy(); - } catch (Exception e) { - fail("exception when destoying pool connection factory"); - } - poolConFactory.getConnection(); - } - - public static class TestEntity { - private String name; - - public TestEntity(String n) { - this.name = n; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - - -} diff --git a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkDataReaderTests.java b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkDataReaderTests.java index a5f80c2..a7abe7b 100644 --- a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkDataReaderTests.java +++ b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkDataReaderTests.java @@ -15,10 +15,6 @@ */ package org.springframework.integration.splunk.support; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -29,9 +25,9 @@ import junit.framework.Assert; import org.junit.Before; import org.junit.Test; +import static org.mockito.Mockito.*; import org.springframework.core.io.ClassPathResource; -import org.springframework.integration.splunk.core.Connection; -import org.springframework.integration.splunk.core.ConnectionFactory; +import org.springframework.integration.splunk.core.ServiceFactory; import org.springframework.integration.splunk.event.SplunkEvent; import com.splunk.Job; @@ -49,7 +45,8 @@ public class SplunkDataReaderTests { @Before public void before() { - reader = new SplunkDataReader(new TestConnectioniFactory()); + ServiceFactory serviceFactory =serviceFactory(); + reader = new SplunkDataReader(serviceFactory); } /** @@ -60,7 +57,7 @@ public class SplunkDataReaderTests { public void testBlockingSearch() throws Exception { reader.setMode(SearchMode.BLOCKING); reader.setSearch("search spring:example"); - List data = reader.search(); + List data = reader.read(); Assert.assertNotNull(data); Assert.assertEquals(5, data.size()); } @@ -69,7 +66,7 @@ public class SplunkDataReaderTests { public void testNonBlockingSearch() throws Exception { reader.setMode(SearchMode.NORMAL); reader.setSearch("search spring:example"); - List data = reader.search(); + List data = reader.read(); Assert.assertNotNull(data); Assert.assertEquals(5, data.size()); } @@ -79,65 +76,34 @@ public class SplunkDataReaderTests { public void testRealtimeSearch() throws Exception { reader.setMode(SearchMode.REALTIME); reader.setSearch("search spring:example"); - List data = reader.search(); + List data = reader.read(); Assert.assertNotNull(data); Assert.assertEquals(5, data.size()); } - public static class TestConnectioniFactory implements ConnectionFactory { + private ServiceFactory serviceFactory() { + InputStream is = null; - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.ConnectionFactory#getConnection() - */ - public Connection getConnection() throws Exception { - return new TestConnection(); + try { + is = new ClassPathResource("splunk-data.xml").getInputStream(); + } catch (FileNotFoundException e) { + Assert.fail("can not read splunk data file"); + } catch (IOException e) { + Assert.fail("can not read splunk data file"); } + + Service service = mock(Service.class); + service.setToken("token"); + JobCollection jobCollection = mock(JobCollection.class); + Job blockingJob = mock(Job.class); + when(blockingJob.isDone()).thenReturn(true); + when(blockingJob.getResultCount()).thenReturn(5); + when(blockingJob.getResults(any(Map.class))).thenReturn(is); + when(jobCollection.create(any(String.class), any(Map.class))).thenReturn(blockingJob); + when(service.getJobs()).thenReturn(jobCollection); + + ServiceFactory serviceFactory = mock(ServiceFactory.class); + when(serviceFactory.getService()).thenReturn(service); + return serviceFactory; } - - public static class TestConnection implements Connection { - - private Service service = mock(Service.class); - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.Connection#getTarget() - */ - public Service getTarget() { - InputStream is = null; - - try { - is = new ClassPathResource("splunk-data.xml").getInputStream(); - } catch (FileNotFoundException e) { - Assert.fail("can not read splunk data file"); - } catch (IOException e) { - Assert.fail("can not read splunk data file"); - } - - - service.setToken("token"); - JobCollection jobCollection = mock(JobCollection.class); - Job blockingJob = mock(Job.class); - when(blockingJob.isDone()).thenReturn(true); - when(blockingJob.getResultCount()).thenReturn(5); - when(blockingJob.getResults(any(Map.class))).thenReturn(is); - when(jobCollection.create(any(String.class), any(Map.class))).thenReturn(blockingJob); - when(service.getJobs()).thenReturn(jobCollection); - return service; - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.Connection#close() - */ - public void close() { - - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.Connection#isOpen() - */ - public boolean isOpen() { - return true; - } - - } - } diff --git a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkDataWriterTests.java b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkDataWriterTests.java index fa2e322..0e6e63f 100644 --- a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkDataWriterTests.java +++ b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkDataWriterTests.java @@ -15,13 +15,15 @@ */ package org.springframework.integration.splunk.support; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.matches; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; -import org.springframework.integration.splunk.core.Connection; -import org.springframework.integration.splunk.core.ConnectionFactory; +import org.springframework.integration.splunk.core.ServiceFactory; import org.springframework.integration.splunk.event.SplunkEvent; import com.splunk.Args; @@ -40,10 +42,11 @@ public class SplunkDataWriterTests { private static Receiver receiver = mock(Receiver.class); private Args args; + @Before - public void before() { + public void before() throws Exception { args = new Args(); - writer = new SplunkSubmitWriter(new TestConnectionFactory(),args); + writer = new SplunkSubmitWriter(serviceFactory(), args); writer.start(); } @@ -53,53 +56,24 @@ public class SplunkDataWriterTests { */ @Test public void testWrite() throws Exception { - SplunkEvent sd = new SplunkEvent("spring", "spring:example"); sd.setCommonDesc("description"); writer.write(sd); - + verify(receiver).submit(eq(args), matches(".*spring:example.*\n")); - + writer.stop(); } - public static class TestConnectionFactory implements ConnectionFactory { + private ServiceFactory serviceFactory() throws Exception { + ServiceFactory serviceFactory = mock(ServiceFactory.class); - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.ConnectionFactory#getConnection() - */ - public Connection getConnection() throws Exception { - return new TestConnection(); - } + Service service = mock(Service.class); + service.setToken("token"); + when(service.getReceiver()).thenReturn(receiver); + when(serviceFactory.getService()).thenReturn(service); + return serviceFactory; } - public static class TestConnection implements Connection { - - private Service service = mock(Service.class); - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.Connection#getTarget() - */ - public Service getTarget() { - service.setToken("token"); - when(service.getReceiver()).thenReturn(receiver); - return service; - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.Connection#close() - */ - public void close() { - - } - - /* (non-Javadoc) - * @see org.springframework.integration.splunk.core.Connection#isOpen() - */ - public boolean isOpen() { - return true; - } - - } } diff --git a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkExecutorTests.java b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkExecutorTests.java index 8ba7612..1317dfe 100644 --- a/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkExecutorTests.java +++ b/spring-integration-splunk/src/test/java/org/springframework/integration/splunk/support/SplunkExecutorTests.java @@ -80,7 +80,7 @@ public class SplunkExecutorTests { sd = new SplunkEvent("spring", "spring:example"); sd.setCommonDesc("description"); data.add(sd); - when(reader.search()).thenReturn(data); + when(reader.read()).thenReturn(data); List result = executor.poll(); Assert.assertEquals(2, result.size()); diff --git a/spring-integration-splunk/src/test/resources/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParserStreamTests-context.xml b/spring-integration-splunk/src/test/resources/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParserStreamTests-context.xml index 0684e92..ec10a53 100644 --- a/spring-integration-splunk/src/test/resources/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParserStreamTests-context.xml +++ b/spring-integration-splunk/src/test/resources/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParserStreamTests-context.xml @@ -16,7 +16,7 @@ diff --git a/spring-integration-splunk/src/test/resources/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParserTests-context.xml b/spring-integration-splunk/src/test/resources/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParserTests-context.xml index 9bb406d..aaa2b16 100644 --- a/spring-integration-splunk/src/test/resources/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParserTests-context.xml +++ b/spring-integration-splunk/src/test/resources/org/springframework/integration/splunk/config/xml/SplunkOutboundChannelAdapterParserTests-context.xml @@ -16,7 +16,7 @@