mirror of https://github.com/apache/activemq.git
https://issues.apache.org/jira/browse/AMQ-3411 - user/pass for consolne commands
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1150708 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4a015a95a4
commit
e34ec0b7f0
|
@ -33,7 +33,11 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
|
|||
private URI brokerUrl;
|
||||
private ConnectionFactory factory;
|
||||
private String factoryClassString;
|
||||
private String username;
|
||||
private String password;
|
||||
private PasswordFactory passwordFactory;
|
||||
private final List<Connection> connections = new ArrayList<Connection>();
|
||||
private String passwordFactoryClassString;
|
||||
|
||||
/**
|
||||
* Establishes a connection to the remote broker specified by the broker
|
||||
|
@ -43,17 +47,7 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
|
|||
* @throws JMSException
|
||||
*/
|
||||
protected Connection createConnection() throws JMSException {
|
||||
if (getBrokerUrl() == null) {
|
||||
context
|
||||
.printException(new IllegalStateException("You must specify a broker "
|
||||
+ "URL to connect to using the --amqurl option."));
|
||||
return null;
|
||||
}
|
||||
|
||||
Connection conn = getFactory().createConnection();
|
||||
connections.add(conn);
|
||||
|
||||
return conn;
|
||||
return createConnection(getUsername(), getPassword());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,7 +67,14 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
|
|||
return null;
|
||||
}
|
||||
|
||||
Connection conn = getFactory().createConnection(username, password);
|
||||
ConnectionFactory factory = getConnectionFactory();
|
||||
Connection conn;
|
||||
|
||||
if (null == username && null == password)
|
||||
conn = factory.createConnection();
|
||||
else
|
||||
conn = factory.createConnection(username, password);
|
||||
|
||||
connections.add(conn);
|
||||
conn.start();
|
||||
|
||||
|
@ -130,6 +131,12 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
|
|||
}
|
||||
} else if (token.equals("--factory")) {
|
||||
factoryClassString = (String) tokens.remove(0);
|
||||
} else if (token.equals("--passwordFactory")) {
|
||||
passwordFactoryClassString = (String) tokens.remove(0);
|
||||
} else if (token.equals("--password")) {
|
||||
password = (String) tokens.remove(0);
|
||||
} else if (token.equals("--user")) {
|
||||
username = (String) tokens.remove(0);
|
||||
} else {
|
||||
// Let the super class handle the option
|
||||
super.handleOption(token, tokens);
|
||||
|
@ -164,48 +171,104 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
|
|||
return brokerUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the factory
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public ConnectionFactory getFactory() {
|
||||
/**
|
||||
* @return the factory
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public ConnectionFactory getConnectionFactory() {
|
||||
if (factory == null && factoryClassString != null) {
|
||||
try {
|
||||
Class klass = Class.forName(factoryClassString);
|
||||
if (klass.isInstance(ConnectionFactory.class)) {
|
||||
Class<ConnectionFactory> factoryClass = (Class<ConnectionFactory>) klass;
|
||||
factory = factoryClass.getConstructor(URI.class)
|
||||
.newInstance(getBrokerUrl());
|
||||
|
||||
if (getUsername() != null || getPassword() != null) {
|
||||
factory = (ConnectionFactory) klass.getConstructor(
|
||||
String.class, String.class, URI.class).newInstance(
|
||||
getUsername(), getPassword(), getBrokerUrl());
|
||||
} else {
|
||||
factory = (ConnectionFactory) klass.getConstructor(
|
||||
URI.class).newInstance(getBrokerUrl());
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Preserve the fallback case, if someone did specify a bad class, let them realize when things don't work.
|
||||
if (factory == null) {
|
||||
factory = new ActiveMQConnectionFactory(getBrokerUrl());
|
||||
if (getUsername() != null || getPassword() != null) {
|
||||
factory = new ActiveMQConnectionFactory(getUsername(),
|
||||
getPassword(), getBrokerUrl());
|
||||
} else {
|
||||
factory = new ActiveMQConnectionFactory(getBrokerUrl());
|
||||
}
|
||||
}
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param factory the factory to set
|
||||
*/
|
||||
public void setFactory(ConnectionFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
/**
|
||||
* @return the username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param factory the factory to set
|
||||
*/
|
||||
public void setFactory(ConnectionFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username the username to set
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the password
|
||||
*/
|
||||
public String getPassword() {
|
||||
if (null == password)
|
||||
return null;
|
||||
|
||||
return getPasswordFactory().getPassword(password);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param password the password to set
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the passwordFactory
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public PasswordFactory getPasswordFactory() {
|
||||
if (passwordFactory == null && passwordFactoryClassString != null) {
|
||||
try {
|
||||
Class klass = Class.forName(passwordFactoryClassString);
|
||||
passwordFactory = (PasswordFactory) klass.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Preserve the fallback case, if someone did specify a bad class, let them realize when things don't work.
|
||||
if (passwordFactory == null) {
|
||||
passwordFactory = DefaultPasswordFactory.factory;
|
||||
}
|
||||
|
||||
return passwordFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param passwordFactory the passwordFactory to set
|
||||
*/
|
||||
public void setPasswordFactory(PasswordFactory passwordFactory) {
|
||||
this.passwordFactory = passwordFactory;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,11 +114,11 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
|
|||
}
|
||||
|
||||
// Query for the messages to view
|
||||
List addMsgs = AmqMessagesUtil.getMessages(getBrokerUrl(), dest, queryAddObjects);
|
||||
List addMsgs = AmqMessagesUtil.getMessages(getConnectionFactory(), dest, queryAddObjects);
|
||||
|
||||
// Query for the messages to remove from view
|
||||
if (querySubObjects.size() > 0) {
|
||||
List subMsgs = AmqMessagesUtil.getMessages(getBrokerUrl(), dest, querySubObjects);
|
||||
List subMsgs = AmqMessagesUtil.getMessages(getConnectionFactory(), dest, querySubObjects);
|
||||
addMsgs.removeAll(subMsgs);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.activemq.console.command;
|
||||
|
||||
/**
|
||||
* This is a simple dummy implementation that can be used for people who aren't in need of a keystore.
|
||||
*/
|
||||
public class DefaultPasswordFactory implements PasswordFactory{
|
||||
// everyone can share this, since it has no state at all.
|
||||
public static PasswordFactory factory = new DefaultPasswordFactory();
|
||||
|
||||
public String getPassword(String password) {
|
||||
return password;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.activemq.console.command;
|
||||
|
||||
/**
|
||||
* This interface is used to allow people to provide a mechanism to override where the password comes from.
|
||||
* Implementors of this interface will typically use the specified password to look up the real password in a
|
||||
* keystore of some sort.
|
||||
* @author areese@yahoo-inc.com
|
||||
*
|
||||
*/
|
||||
public interface PasswordFactory {
|
||||
String getPassword(String password);
|
||||
}
|
|
@ -22,6 +22,7 @@ import java.util.Iterator;
|
|||
import java.util.List;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.QueueBrowser;
|
||||
|
@ -36,10 +37,12 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
private URI brokerUrl;
|
||||
private Destination destination;
|
||||
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
/**
|
||||
* Create a JMS message query filter
|
||||
*
|
||||
* @param brokerUrl - broker url to connect to
|
||||
*
|
||||
* @param brokerUrl - broker url to connect to
|
||||
* @param destination - JMS destination to query
|
||||
*/
|
||||
public AmqMessagesQueryFilter(URI brokerUrl, Destination destination) {
|
||||
|
@ -48,9 +51,21 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
this.destination = destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JMS message query filter
|
||||
*
|
||||
* @param brokerUrl - broker url to connect to
|
||||
* @param destination - JMS destination to query
|
||||
*/
|
||||
public AmqMessagesQueryFilter(ConnectionFactory connectionFactory, Destination destination) {
|
||||
super(null);
|
||||
this.destination = destination;
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries the specified destination using the message selector format query
|
||||
*
|
||||
*
|
||||
* @param queries - message selector queries
|
||||
* @return list messages that matches the selector
|
||||
* @throws Exception
|
||||
|
@ -59,7 +74,7 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
String selector = "";
|
||||
|
||||
// Convert to message selector
|
||||
for (Iterator i = queries.iterator(); i.hasNext();) {
|
||||
for (Iterator i = queries.iterator(); i.hasNext(); ) {
|
||||
selector = selector + "(" + i.next().toString() + ") AND ";
|
||||
}
|
||||
|
||||
|
@ -69,22 +84,22 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
}
|
||||
|
||||
if (destination instanceof ActiveMQQueue) {
|
||||
return queryMessages((ActiveMQQueue)destination, selector);
|
||||
return queryMessages((ActiveMQQueue) destination, selector);
|
||||
} else {
|
||||
return queryMessages((ActiveMQTopic)destination, selector);
|
||||
return queryMessages((ActiveMQTopic) destination, selector);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the messages of a queue destination using a queue browser
|
||||
*
|
||||
* @param queue - queue destination
|
||||
*
|
||||
* @param queue - queue destination
|
||||
* @param selector - message selector
|
||||
* @return list of messages that matches the selector
|
||||
* @throws Exception
|
||||
*/
|
||||
protected List queryMessages(ActiveMQQueue queue, String selector) throws Exception {
|
||||
Connection conn = createConnection(getBrokerUrl());
|
||||
Connection conn = createConnection();
|
||||
|
||||
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
QueueBrowser browser = sess.createBrowser(queue, selector);
|
||||
|
@ -98,8 +113,8 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
|
||||
/**
|
||||
* Query the messages of a topic destination using a message consumer
|
||||
*
|
||||
* @param topic - topic destination
|
||||
*
|
||||
* @param topic - topic destination
|
||||
* @param selector - message selector
|
||||
* @return list of messages that matches the selector
|
||||
* @throws Exception
|
||||
|
@ -114,20 +129,39 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
|
||||
/**
|
||||
* Create and start a JMS connection
|
||||
*
|
||||
*
|
||||
* @param brokerUrl - broker url to connect to.
|
||||
* @return JMS connection
|
||||
* @throws JMSException
|
||||
* @deprecated Use createConnection() instead, and pass the url to the ConnectionFactory when it's created.
|
||||
*/
|
||||
@Deprecated
|
||||
protected Connection createConnection(URI brokerUrl) throws JMSException {
|
||||
Connection conn = (new ActiveMQConnectionFactory(brokerUrl)).createConnection();
|
||||
// maintain old behaviour, when called this way.
|
||||
connectionFactory = (new ActiveMQConnectionFactory(brokerUrl));
|
||||
return createConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and start a JMS connection
|
||||
*
|
||||
* @return JMS connection
|
||||
* @throws JMSException
|
||||
*/
|
||||
protected Connection createConnection() throws JMSException {
|
||||
// maintain old behaviour, when called either way.
|
||||
if (null == connectionFactory)
|
||||
connectionFactory = (new ActiveMQConnectionFactory(getBrokerUrl()));
|
||||
|
||||
Connection conn = connectionFactory.createConnection();
|
||||
conn.start();
|
||||
return conn;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the broker url being used.
|
||||
*
|
||||
*
|
||||
* @return broker url
|
||||
*/
|
||||
public URI getBrokerUrl() {
|
||||
|
@ -136,7 +170,7 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
|
||||
/**
|
||||
* Set the broker url to use.
|
||||
*
|
||||
*
|
||||
* @param brokerUrl - broker url
|
||||
*/
|
||||
public void setBrokerUrl(URI brokerUrl) {
|
||||
|
@ -145,7 +179,7 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
|
||||
/**
|
||||
* Get the destination being used.
|
||||
*
|
||||
*
|
||||
* @return - JMS destination
|
||||
*/
|
||||
public Destination getDestination() {
|
||||
|
@ -154,7 +188,7 @@ public class AmqMessagesQueryFilter extends AbstractQueryFilter {
|
|||
|
||||
/**
|
||||
* Set the destination to use.
|
||||
*
|
||||
*
|
||||
* @param destination - JMS destination
|
||||
*/
|
||||
public void setDestination(Destination destination) {
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.net.URI;
|
|||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Destination;
|
||||
|
||||
import org.apache.activemq.console.filter.AmqMessagesQueryFilter;
|
||||
|
@ -47,10 +48,18 @@ public final class AmqMessagesUtil {
|
|||
return createMessageQueryFilter(brokerUrl, dest).query(selector);
|
||||
}
|
||||
|
||||
public static List getMessages(ConnectionFactory connectionFactory, Destination dest, String selector) throws Exception {
|
||||
return createMessageQueryFilter(connectionFactory, dest).query(selector);
|
||||
}
|
||||
|
||||
public static List getMessages(URI brokerUrl, Destination dest, List selectors) throws Exception {
|
||||
return createMessageQueryFilter(brokerUrl, dest).query(selectors);
|
||||
}
|
||||
|
||||
public static List getMessages(ConnectionFactory connectionFactory, Destination dest, List selectors) throws Exception {
|
||||
return createMessageQueryFilter(connectionFactory, dest).query(selectors);
|
||||
}
|
||||
|
||||
public static List filterMessagesView(List messages, Set groupViews, Set attributeViews) throws Exception {
|
||||
return (new PropertiesViewFilter(attributeViews, new GroupPropertiesViewFilter(groupViews, new MapTransformFilter(new StubQueryFilter(messages))))).query("");
|
||||
}
|
||||
|
@ -58,4 +67,8 @@ public final class AmqMessagesUtil {
|
|||
public static QueryFilter createMessageQueryFilter(URI brokerUrl, Destination dest) {
|
||||
return new WildcardToMsgSelectorTransformFilter(new AmqMessagesQueryFilter(brokerUrl, dest));
|
||||
}
|
||||
|
||||
public static QueryFilter createMessageQueryFilter(ConnectionFactory connectionFactory, Destination dest) {
|
||||
return new WildcardToMsgSelectorTransformFilter(new AmqMessagesQueryFilter(connectionFactory, dest));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package org.apache.activemq.console.command;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
|
||||
public class DummyConnectionFactory extends ActiveMQConnectionFactory {
|
||||
public DummyConnectionFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DummyConnectionFactory(String userName, String password, String brokerURL) {
|
||||
super(userName, password, brokerURL);
|
||||
}
|
||||
|
||||
public DummyConnectionFactory(String userName, String password, URI brokerURL) {
|
||||
super(userName, password, brokerURL);
|
||||
}
|
||||
|
||||
public DummyConnectionFactory(String brokerURL) {
|
||||
super(brokerURL);
|
||||
}
|
||||
|
||||
public DummyConnectionFactory(URI brokerURL) {
|
||||
super(brokerURL);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.apache.activemq.console.command;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
|
||||
public class InvalidConnectionFactory extends ActiveMQConnectionFactory {
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.apache.activemq.console.command;
|
||||
|
||||
public class LowercasingPasswordFactory implements PasswordFactory {
|
||||
@Override
|
||||
public String getPassword(String password) {
|
||||
return password.toLowerCase();
|
||||
}
|
||||
|
||||
};
|
|
@ -16,7 +16,6 @@
|
|||
*/
|
||||
package org.apache.activemq.console.command;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
@ -34,40 +33,15 @@ import org.springframework.context.support.AbstractApplicationContext;
|
|||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class TestAMQ3410 extends TestCase {
|
||||
public static class InvalidFactory extends ActiveMQConnectionFactory {
|
||||
}
|
||||
|
||||
public static class NotAFactory {
|
||||
}
|
||||
|
||||
public static class DummyFactory extends ActiveMQConnectionFactory {
|
||||
public DummyFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DummyFactory(String userName, String password, String brokerURL) {
|
||||
super(userName, password, brokerURL);
|
||||
}
|
||||
|
||||
public DummyFactory(String userName, String password, URI brokerURL) {
|
||||
super(userName, password, brokerURL);
|
||||
}
|
||||
|
||||
public DummyFactory(String brokerURL) {
|
||||
super(brokerURL);
|
||||
}
|
||||
|
||||
public DummyFactory(URI brokerURL) {
|
||||
super(brokerURL);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static final Logger LOG = LoggerFactory
|
||||
.getLogger(TestPurgeCommand.class);
|
||||
private static final Collection<String> DEFAULT_OPTIONS = Arrays
|
||||
.asList(new String[] { "--amqurl", "tcp://localhost:61616", });
|
||||
|
||||
private static final Collection<String> DEFAULT_TOKENS = Arrays
|
||||
.asList(new String[] { "--amqurl", "tcp://localhost:61616",
|
||||
"FOO.QUEUE" });
|
||||
.asList(new String[] { "FOO.QUEUE" });
|
||||
|
||||
protected AbstractApplicationContext context;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
|
@ -98,9 +72,12 @@ public class TestAMQ3410 extends TestCase {
|
|||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
command.execute(tokens);
|
||||
assertNotNull(command.getFactory());
|
||||
assertNotNull(command.getConnectionFactory());
|
||||
assertTrue(command.getConnectionFactory() instanceof ActiveMQConnectionFactory);
|
||||
}
|
||||
|
||||
public void testFactorySet() throws Exception {
|
||||
|
@ -112,12 +89,17 @@ public class TestAMQ3410 extends TestCase {
|
|||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.add("--factory");
|
||||
tokens
|
||||
.add("org.apache.activemq.console.command.TestAMQ3410.DummyFactory");
|
||||
tokens.add(DummyConnectionFactory.class.getCanonicalName());
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
command.execute(tokens);
|
||||
assertNotNull(command.getFactory());
|
||||
|
||||
assertNotNull(command.getConnectionFactory());
|
||||
assertTrue("wrong instance returned: "
|
||||
+ command.getConnectionFactory().getClass().getName(), command
|
||||
.getConnectionFactory() instanceof DummyConnectionFactory);
|
||||
}
|
||||
|
||||
public void testFactorySetWrong1() throws Exception {
|
||||
|
@ -129,12 +111,22 @@ public class TestAMQ3410 extends TestCase {
|
|||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.add("--factory");
|
||||
tokens
|
||||
.add("org.apache.activemq.console.command.TestAMQ3410.DoesntExistFactory");
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
try {
|
||||
command.execute(tokens);
|
||||
assertNotNull(command.getFactory());
|
||||
} catch (Throwable cause) {
|
||||
while (null != cause) {
|
||||
if (cause instanceof java.lang.ClassNotFoundException)
|
||||
return;
|
||||
cause = cause.getCause();
|
||||
}
|
||||
}
|
||||
assertFalse("No exception caught", true);
|
||||
}
|
||||
|
||||
public void testFactorySetWrong2() throws Exception {
|
||||
|
@ -146,12 +138,49 @@ public class TestAMQ3410 extends TestCase {
|
|||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.add("--factory");
|
||||
tokens
|
||||
.add("org.apache.activemq.console.command.TestAMQ3410.InvalidFactory");
|
||||
tokens.add(InvalidConnectionFactory.class.getCanonicalName());
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
try {
|
||||
command.execute(tokens);
|
||||
} catch (Throwable e) {
|
||||
Throwable cause = e;
|
||||
while (null != cause) {
|
||||
if (cause instanceof java.lang.NoSuchMethodException)
|
||||
return;
|
||||
cause = cause.getCause();
|
||||
}
|
||||
assertFalse(e.toString(), true);
|
||||
}
|
||||
assertFalse("No exception caught", true);
|
||||
}
|
||||
|
||||
public void testFactorySetWrong3() throws Exception {
|
||||
AmqBrowseCommand command = new AmqBrowseCommand();
|
||||
CommandContext context = new CommandContext();
|
||||
|
||||
context.setFormatter(new CommandShellOutputFormatter(System.out));
|
||||
|
||||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.add("--factory");
|
||||
tokens.add("java.lang.Object");
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
try {
|
||||
command.execute(tokens);
|
||||
assertNotNull(command.getFactory());
|
||||
} catch (Throwable cause) {
|
||||
while (null != cause) {
|
||||
if (cause instanceof java.lang.NoSuchMethodException)
|
||||
return;
|
||||
cause = cause.getCause();
|
||||
}
|
||||
}
|
||||
assertFalse(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,197 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.activemq.console.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.activemq.broker.BrokerService;
|
||||
import org.apache.activemq.console.CommandContext;
|
||||
import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class TestAMQ3411 extends TestCase {
|
||||
@SuppressWarnings("unused")
|
||||
private static final Logger LOG = LoggerFactory
|
||||
.getLogger(TestPurgeCommand.class);
|
||||
private static final Collection<String> DEFAULT_OPTIONS = Arrays
|
||||
.asList(new String[] { "--amqurl", "tcp://localhost:61616", });
|
||||
|
||||
private static final Collection<String> DEFAULT_TOKENS = Arrays
|
||||
.asList(new String[] { "FOO.QUEUE" });
|
||||
protected AbstractApplicationContext context;
|
||||
protected static final String origPassword = "ABCDEFG";
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
context = createApplicationContext();
|
||||
|
||||
}
|
||||
|
||||
protected AbstractApplicationContext createApplicationContext() {
|
||||
return new ClassPathXmlApplicationContext("activemq.xml");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
BrokerService broker = (BrokerService) context.getBean("localbroker");
|
||||
broker.stop();
|
||||
broker = (BrokerService) context.getBean("default");
|
||||
broker.stop();
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testNoFactorySet() throws Exception {
|
||||
AmqBrowseCommand command = new AmqBrowseCommand();
|
||||
CommandContext context = new CommandContext();
|
||||
|
||||
context.setFormatter(new CommandShellOutputFormatter(System.out));
|
||||
|
||||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
command.execute(tokens);
|
||||
|
||||
assertNotNull(command.getPasswordFactory());
|
||||
assertTrue(command.getPasswordFactory() instanceof DefaultPasswordFactory);
|
||||
assertNull(command.getPassword());
|
||||
}
|
||||
|
||||
public void testUsernamePasswordSet() throws Exception {
|
||||
AmqBrowseCommand command = new AmqBrowseCommand();
|
||||
CommandContext context = new CommandContext();
|
||||
|
||||
String username = "user";
|
||||
String password = "password";
|
||||
|
||||
context.setFormatter(new CommandShellOutputFormatter(System.out));
|
||||
|
||||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.add("--password");
|
||||
tokens.add(password);
|
||||
|
||||
tokens.add("--user");
|
||||
tokens.add(username);
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
command.execute(tokens);
|
||||
|
||||
assertNotNull(command.getPasswordFactory());
|
||||
assertTrue(command.getPasswordFactory() instanceof DefaultPasswordFactory);
|
||||
assertEquals(password, command.getPassword());
|
||||
assertEquals(username, command.getUsername());
|
||||
}
|
||||
|
||||
public void testFactorySet() throws Exception {
|
||||
AmqBrowseCommand command = new AmqBrowseCommand();
|
||||
CommandContext context = new CommandContext();
|
||||
|
||||
context.setFormatter(new CommandShellOutputFormatter(System.out));
|
||||
|
||||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.add("--passwordFactory");
|
||||
tokens.add(LowercasingPasswordFactory.class.getCanonicalName());
|
||||
tokens.add("--password");
|
||||
tokens.add(origPassword);
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
command.execute(tokens);
|
||||
assertNotNull(command.getPasswordFactory());
|
||||
assertTrue(command.getPasswordFactory() instanceof LowercasingPasswordFactory);
|
||||
|
||||
// validate that the factory is indeed being used for the password.
|
||||
assertEquals(origPassword.toLowerCase(), command.getPassword());
|
||||
}
|
||||
|
||||
public void testFactorySetWrong1() throws Exception {
|
||||
AmqBrowseCommand command = new AmqBrowseCommand();
|
||||
CommandContext context = new CommandContext();
|
||||
|
||||
context.setFormatter(new CommandShellOutputFormatter(System.out));
|
||||
|
||||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.add("--passwordFactory");
|
||||
tokens
|
||||
.add("org.apache.activemq.console.command.TestAMQ3411.DoesntExistFactory");
|
||||
tokens.add("--password");
|
||||
tokens.add(origPassword);
|
||||
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
try {
|
||||
command.execute(tokens);
|
||||
} catch (Throwable e) {
|
||||
Throwable cause = e;
|
||||
while (null != cause) {
|
||||
if (cause instanceof java.lang.ClassNotFoundException)
|
||||
return;
|
||||
cause = cause.getCause();
|
||||
}
|
||||
assertFalse(e.toString(), true);
|
||||
}
|
||||
assertFalse("No exception caught", true);
|
||||
}
|
||||
|
||||
public void testFactorySetWrong2() throws Exception {
|
||||
AmqBrowseCommand command = new AmqBrowseCommand();
|
||||
CommandContext context = new CommandContext();
|
||||
|
||||
context.setFormatter(new CommandShellOutputFormatter(System.out));
|
||||
|
||||
command.setCommandContext(context);
|
||||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(DEFAULT_OPTIONS);
|
||||
tokens.add("--passwordFactory");
|
||||
tokens.add("java.lang.Object");
|
||||
tokens.add("--password");
|
||||
tokens.add(origPassword);
|
||||
tokens.addAll(DEFAULT_TOKENS);
|
||||
|
||||
try {
|
||||
command.execute(tokens);
|
||||
} catch (Throwable e) {
|
||||
Throwable cause = e;
|
||||
while (null != cause) {
|
||||
if (cause instanceof java.lang.ClassCastException)
|
||||
return;
|
||||
cause = cause.getCause();
|
||||
}
|
||||
assertFalse(e.toString(), true);
|
||||
}
|
||||
assertFalse("No exception caught", true);
|
||||
}
|
||||
}
|
|
@ -246,8 +246,6 @@ public class TestPurgeCommand extends TestCase {
|
|||
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
tokens.add("--msgsel");
|
||||
// String[] extras = MSG_SEL_WITH_PROPERTY.split(" ");
|
||||
// tokens.addAll(Arrays.asList(extras));
|
||||
tokens.add(MSG_SEL_WITH_PROPERTY);
|
||||
|
||||
addMessages();
|
||||
|
|
Loading…
Reference in New Issue