INT-1580, more polishing, added Lifecycle to XmppConnectionFactoryBean, added tests
This commit is contained in:
@@ -18,7 +18,9 @@ package org.springframework.integration.xmpp;
|
||||
import org.jivesoftware.smack.ConnectionConfiguration;
|
||||
import org.jivesoftware.smack.Roster;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -32,9 +34,9 @@ import org.springframework.util.StringUtils;
|
||||
* @see org.jivesoftware.smack.XMPPConnection
|
||||
* @since 2.0
|
||||
*/
|
||||
public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnection> {
|
||||
public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnection> implements SmartLifecycle{
|
||||
|
||||
private volatile ConnectionConfiguration connectionConfiguration;
|
||||
private final ConnectionConfiguration connectionConfiguration;
|
||||
|
||||
private volatile String resource = "Smack"; // default value used by Smack
|
||||
|
||||
@@ -43,32 +45,30 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
|
||||
private volatile String password;
|
||||
|
||||
private volatile String subscriptionMode = "accept_all";
|
||||
|
||||
private volatile XMPPConnection connection;
|
||||
|
||||
private volatile boolean autoStartup;
|
||||
|
||||
private volatile boolean started;
|
||||
|
||||
public XmppConnectionFactoryBean(ConnectionConfiguration connectionConfiguration) {
|
||||
Assert.notNull(connectionConfiguration, "'connectionConfiguration' must not be null");
|
||||
this.connectionConfiguration = connectionConfiguration;
|
||||
}
|
||||
|
||||
public String getSubscriptionMode() {
|
||||
return subscriptionMode;
|
||||
public void setAutoStartup(boolean autoStartup) {
|
||||
this.autoStartup = autoStartup;
|
||||
}
|
||||
|
||||
public void setSubscriptionMode(String subscriptionMode) {
|
||||
this.subscriptionMode = subscriptionMode;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
@@ -77,10 +77,6 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public String getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends XMPPConnection> getObjectType() {
|
||||
return XMPPConnection.class;
|
||||
@@ -88,24 +84,59 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
|
||||
|
||||
@Override
|
||||
protected XMPPConnection createInstance() throws Exception {
|
||||
Assert.notNull(connectionConfiguration, "'connectionConfiguration' must not be null");
|
||||
|
||||
XMPPConnection connection = new XMPPConnection(connectionConfiguration);
|
||||
connection.connect();
|
||||
if (StringUtils.hasText(user)){
|
||||
connection.login(user, password, resource);
|
||||
|
||||
Assert.isTrue(connection.isAuthenticated(), "Failed to authenticate user: " + user);
|
||||
|
||||
if (StringUtils.hasText(this.subscriptionMode)) {
|
||||
Roster.SubscriptionMode subscriptionMode = Roster.SubscriptionMode.valueOf(this.subscriptionMode);
|
||||
connection.getRoster().setSubscriptionMode(subscriptionMode);
|
||||
}
|
||||
}
|
||||
else {
|
||||
connection.loginAnonymously();
|
||||
}
|
||||
connection = new XMPPConnection(connectionConfiguration);
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
try {
|
||||
connection.connect();
|
||||
if (StringUtils.hasText(user)){
|
||||
connection.login(user, password, resource);
|
||||
|
||||
Assert.isTrue(connection.isAuthenticated(), "Failed to authenticate user: " + user);
|
||||
|
||||
if (StringUtils.hasText(this.subscriptionMode)) {
|
||||
Roster.SubscriptionMode subscriptionMode = Roster.SubscriptionMode.valueOf(this.subscriptionMode);
|
||||
connection.getRoster().setSubscriptionMode(subscriptionMode);
|
||||
}
|
||||
}
|
||||
else {
|
||||
connection.loginAnonymously();
|
||||
}
|
||||
this.started = true;
|
||||
} catch (Exception e) {
|
||||
throw new BeanInitializationException("Failed to connect to " + this.connectionConfiguration.getHost(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (this.isRunning()){
|
||||
this.connection.disconnect();
|
||||
this.started = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return this.started;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPhase() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoStartup() {
|
||||
return this.autoStartup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(Runnable callback) {
|
||||
callback.run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
/**
|
||||
*
|
||||
/*
|
||||
* Copyright 2002-2010 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.xmpp.config;
|
||||
|
||||
@@ -12,12 +24,12 @@ import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* @author ozhurakousky
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class XmppConnectionParser extends AbstractSingleBeanDefinitionParser {
|
||||
private static String[] connectionFactoryAttributes =
|
||||
new String[]{"userid", "password", "resource","subscription-mode"};
|
||||
new String[]{"user", "password", "resource","subscription-mode"};
|
||||
|
||||
@Override
|
||||
protected String getBeanClassName(Element element) {
|
||||
@@ -50,6 +62,7 @@ public class XmppConnectionParser extends AbstractSingleBeanDefinitionParser {
|
||||
for (String attribute : connectionFactoryAttributes) {
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, attribute);
|
||||
}
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup");
|
||||
builder.addConstructorArgValue(connectionConfigurationBuilder.getBeanDefinition());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.springframework.integration.config.xml.AbstractOutboundChannelAdapter
|
||||
import org.springframework.integration.config.xml.HeaderEnricherParserSupport;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.integration.xmpp.XmppHeaders;
|
||||
import org.springframework.util.Assert;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
@@ -39,12 +40,6 @@ public class XmppNamespaceHandler extends NamespaceHandlerSupport {
|
||||
|
||||
private static final String PACKAGE_NAME = "org.springframework.integration.xmpp";
|
||||
|
||||
// private static String[] connectionFactoryAttributes =
|
||||
// new String[]{"userid", "password", "resource","subscription-mode"};
|
||||
|
||||
|
||||
|
||||
|
||||
public void init() {
|
||||
// connection
|
||||
registerBeanDefinitionParser("xmpp-connection", new XmppConnectionParser());
|
||||
@@ -120,7 +115,9 @@ public class XmppNamespaceHandler extends NamespaceHandlerSupport {
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
//configureXMPPConnection(element, builder, parserContext);
|
||||
String connectionName = element.getAttribute("xmpp-connection");
|
||||
Assert.hasText(connectionName, "'xmpp-connection' must be defined");
|
||||
builder.addPropertyReference("xmppConnection", connectionName);
|
||||
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "requestChannel");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup");
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="id" type="xsd:string" use="required"/>
|
||||
<xsd:attribute name="user" type="xsd:string" use="required"/>
|
||||
<xsd:attribute name="auto-startup" type="xsd:string" default="true"/>
|
||||
<xsd:attribute name="password" type="xsd:string" use="required"/>
|
||||
<xsd:attribute name="host" type="xsd:string"/>
|
||||
<xsd:attribute name="service-name" type="xsd:string"/>
|
||||
|
||||
@@ -1,27 +1,38 @@
|
||||
/**
|
||||
*
|
||||
/*
|
||||
* Copyright 2002-2010 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.xmpp.messages;
|
||||
package org.springframework.integration.xmpp.config;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.xmpp.messages.XmppMessageDrivenEndpoint;
|
||||
|
||||
/**
|
||||
* @author ozhurakousky
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class InboundXmppEndpointParserTests {
|
||||
|
||||
@Test
|
||||
@Ignore // temporary
|
||||
public void testInboundAdapter(){
|
||||
ApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("InboundXmppEndpointParserTests-context.xml", this.getClass());
|
||||
@@ -7,7 +7,7 @@
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
|
||||
|
||||
<int-xmpp:xmpp-connection id="connection" user="foo" password="foo" host="localhost"/>
|
||||
<int-xmpp:xmpp-connection id="connection"
|
||||
user="happy.user" password="blah" host="localhost" auto-startup="false"/>
|
||||
|
||||
<int:bridge ></int:bridge>
|
||||
</beans>
|
||||
|
||||
@@ -1,24 +1,49 @@
|
||||
/**
|
||||
*
|
||||
/*
|
||||
* Copyright 2002-2010 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.xmpp.config;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.jivesoftware.smack.ConnectionConfiguration;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.xmpp.XmppConnectionFactoryBean;
|
||||
|
||||
/**
|
||||
* @author ozhurakousky
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class XmppConnectionParserTests {
|
||||
|
||||
@Test
|
||||
@Ignore // temporary
|
||||
public void testSimpleConfiguration(){
|
||||
ApplicationContext ac = new ClassPathXmlApplicationContext("XmppConnectionParserTest-simple.xml", this.getClass());
|
||||
XMPPConnection connection = ac.getBean("connection", XMPPConnection.class);
|
||||
assertNull(connection.getServiceName());
|
||||
assertFalse(connection.isConnected());
|
||||
XmppConnectionFactoryBean xmppFb = ac.getBean("&connection", XmppConnectionFactoryBean.class);
|
||||
assertEquals("happy.user", TestUtils.getPropertyValue(xmppFb, "user"));
|
||||
assertEquals("blah", TestUtils.getPropertyValue(xmppFb, "password"));
|
||||
ConnectionConfiguration configuration = (ConnectionConfiguration) TestUtils.getPropertyValue(connection, "configuration");
|
||||
assertEquals("localhost", configuration.getHost());
|
||||
assertEquals(5222, configuration.getPort());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user