ARTEMIS-1024 Management operation causes ClassNotFoundException

Artemis expose createQueue() method to management console like Jon.
If the queue to be created already exists it throws an ActiveMQException
back to the console, which will get a ClassNotFoundException when
deserializing the exception.
The same issue goes also with AddressControl.sendMessage() method.

To fix that it should throw a common java exception like IllegalStateException.
This commit is contained in:
Howard Gao 2017-03-09 21:22:18 +08:00
parent cafc42cc26
commit 6e02caa53e
3 changed files with 139 additions and 25 deletions

View File

@ -47,6 +47,7 @@ import java.util.stream.Collectors;
import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration; import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
import org.apache.activemq.artemis.api.core.ActiveMQAddressDoesNotExistException; import org.apache.activemq.artemis.api.core.ActiveMQAddressDoesNotExistException;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.SimpleString; import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.TransportConfiguration; import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.api.core.management.ActiveMQServerControl; import org.apache.activemq.artemis.api.core.management.ActiveMQServerControl;
@ -669,6 +670,8 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active
clearIO(); clearIO();
try { try {
server.removeAddressInfo(new SimpleString(name), null); server.removeAddressInfo(new SimpleString(name), null);
} catch (ActiveMQException e) {
throw new IllegalStateException(e.getMessage());
} finally { } finally {
blockOnIO(); blockOnIO();
} }
@ -738,6 +741,7 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active
int maxConsumers, int maxConsumers,
boolean purgeOnNoConsumers, boolean purgeOnNoConsumers,
boolean autoCreateAddress) throws Exception { boolean autoCreateAddress) throws Exception {
System.out.println("Target===================================called!");
checkStarted(); checkStarted();
clearIO(); clearIO();
@ -750,6 +754,8 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active
final Queue queue = server.createQueue(SimpleString.toSimpleString(address), RoutingType.valueOf(routingType.toUpperCase()), new SimpleString(name), filter, durable, false, maxConsumers, purgeOnNoConsumers, autoCreateAddress); final Queue queue = server.createQueue(SimpleString.toSimpleString(address), RoutingType.valueOf(routingType.toUpperCase()), new SimpleString(name), filter, durable, false, maxConsumers, purgeOnNoConsumers, autoCreateAddress);
return QueueTextFormatter.Long.format(queue, new StringBuilder()).toString(); return QueueTextFormatter.Long.format(queue, new StringBuilder()).toString();
} catch (ActiveMQException e) {
throw new IllegalStateException(e.getMessage());
} finally { } finally {
blockOnIO(); blockOnIO();
} }

View File

@ -25,6 +25,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.RoutingType; import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString; import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.management.AddressControl; import org.apache.activemq.artemis.api.core.management.AddressControl;
@ -266,35 +267,39 @@ public class AddressControlImpl extends AbstractControl implements AddressContro
boolean durable, boolean durable,
final String user, final String user,
final String password) throws Exception { final String password) throws Exception {
securityStore.check(addressInfo.getName(), CheckType.SEND, new SecurityAuth() { try {
@Override securityStore.check(addressInfo.getName(), CheckType.SEND, new SecurityAuth() {
public String getUsername() { @Override
return user; public String getUsername() {
} return user;
}
@Override @Override
public String getPassword() { public String getPassword() {
return password; return password;
} }
@Override @Override
public RemotingConnection getRemotingConnection() { public RemotingConnection getRemotingConnection() {
return null; return null;
}
});
CoreMessage message = new CoreMessage(storageManager.generateID(), 50);
for (String header : headers.keySet()) {
message.putStringProperty(new SimpleString(header), new SimpleString(headers.get(header)));
} }
}); message.setType((byte) type);
CoreMessage message = new CoreMessage(storageManager.generateID(), 50); message.setDurable(durable);
for (String header : headers.keySet()) { message.setTimestamp(System.currentTimeMillis());
message.putStringProperty(new SimpleString(header), new SimpleString(headers.get(header))); if (body != null) {
message.getBodyBuffer().writeBytes(Base64.decode(body));
}
message.setAddress(addressInfo.getName());
postOffice.route(message, true);
return "" + message.getMessageID();
} catch (ActiveMQException e) {
throw new IllegalStateException(e.getMessage());
} }
message.setType((byte) type);
message.setDurable(durable);
message.setTimestamp(System.currentTimeMillis());
if (body != null) {
message.getBodyBuffer().writeBytes(Base64.decode(body));
}
message.setAddress(addressInfo.getName());
postOffice.route(message, true);
return "" + message.getMessageID();
} }
@Override @Override

View File

@ -0,0 +1,103 @@
/*
* 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.artemis.tests.extras.byteman;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.jboss.byteman.contrib.bmunit.BMRule;
import org.jboss.byteman.contrib.bmunit.BMRules;
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.RuntimeMBeanException;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
@RunWith(BMUnitRunner.class)
public class ManagementExceptionHandlingTest extends ActiveMQTestBase {
protected ActiveMQServer server = null;
@Override
@Before
public void setUp() throws Exception {
super.setUp();
server = createServer(createDefaultNettyConfig());
server.getConfiguration().setJMXManagementEnabled(true);
server.start();
}
@Override
@After
public void tearDown() throws Exception {
if (server != null) {
server.stop();
}
super.tearDown();
}
@Test
@BMRules(
rules = {@BMRule(
name = "checking ActiveMQServerControl methods",
targetClass = "org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl",
targetMethod = "createQueue(org.apache.activemq.artemis.api.core.SimpleString, org.apache.activemq.artemis.api.core.RoutingType, org.apache.activemq.artemis.api.core.SimpleString, org.apache.activemq.artemis.api.core.SimpleString, boolean, boolean, int, boolean, boolean)",
targetLocation = "EXIT",
action = "throw new org.apache.activemq.artemis.api.core.ActiveMQException(\"gotcha\")")})
public void testActiveMQServerControl() throws Exception {
try {
server.getActiveMQServerControl().createQueue("some.address", "ANYCAST", "some.queue", "filter", true, -1, false, true);
fail("test should have gotten an exception!");
} catch (ActiveMQException e) {
fail("Wrong exception got!");
} catch (IllegalStateException e) {
assertEquals("gotcha", e.getMessage());
}
}
@Test
@BMRules(
rules = {@BMRule(
name = "checking ActiveMQServerControl methods",
targetClass = "org.apache.activemq.artemis.core.postoffice.impl.PostOfficeImpl",
targetMethod = "route(org.apache.activemq.artemis.api.core.Message, boolean)",
targetLocation = "ENTRY",
action = "throw new org.apache.activemq.artemis.api.core.ActiveMQException(\"gotcha\")")})
public void testAddressControl() throws Exception {
server.getActiveMQServerControl().createAddress("test.address", "ANYCAST");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
System.out.println("server is " + mbs);
ObjectName objectName = new ObjectName("org.apache.activemq.artemis:broker=\"localhost\",component=addresses,address=\"test.address\"");
Object[] params = new Object[] {new HashMap(), 3, "aGVsbG8=", true, null, null};
String[] signature = new String[] {"java.util.Map", "int", "java.lang.String", "boolean", "java.lang.String", "java.lang.String"};
try {
mbs.invoke(objectName, "sendMessage", params, signature);
fail("test should have gotten an exception!");
} catch (RuntimeMBeanException ex) {
assertTrue(ex.getCause() instanceof IllegalStateException);
IllegalStateException e = (IllegalStateException) ex.getCause();
assertEquals("gotcha", e.getMessage());
}
}
}