Skip to content

Commit

Permalink
ARTEMIS-2047 Compatible option for ActiveMQJMSClient
Browse files Browse the repository at this point in the history
(cherry picked from commit 55b0d5b)
  • Loading branch information
clebertsuconic committed Aug 22, 2018
1 parent e617f32 commit ca113c1
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 140 deletions.
Expand Up @@ -17,6 +17,9 @@
package org.apache.activemq.artemis.utils;

import java.net.URL;
import java.util.Properties;

import org.jboss.logging.Logger;

/**
* This class will be used to perform generic class-loader operations,
Expand All @@ -27,6 +30,8 @@

public final class ClassloadingUtil {

private static final Logger logger = Logger.getLogger(ClassloadingUtil.class);

private static final String INSTANTIATION_EXCEPTION_MESSAGE = "Your class must have a constructor without arguments. If it is an inner class, it must be static!";

public static Object newInstanceFromClassLoader(final String className) {
Expand Down Expand Up @@ -84,7 +89,10 @@ public static Object newInstanceFromClassLoader(final String className, Object..
}

public static URL findResource(final String resourceName) {
ClassLoader loader = ClassloadingUtil.class.getClassLoader();
return findResource(ClassloadingUtil.class.getClassLoader(), resourceName);
}

public static URL findResource(ClassLoader loader, final String resourceName) {
try {
URL resource = loader.getResource(resourceName);
if (resource != null)
Expand All @@ -98,4 +106,26 @@ public static URL findResource(final String resourceName) {

return loader.getResource(resourceName);
}


public static String loadProperty(ClassLoader loader, String propertiesFile, String name) {
Properties properties = loadProperties(loader, propertiesFile);

return (String)properties.get(name);
}

public static Properties loadProperties(ClassLoader loader, String propertiesFile) {
Properties properties = new Properties();

try {
URL url = findResource(loader, propertiesFile);
if (url != null) {
properties.load(url.openStream());
}
} catch (Throwable ignored) {
logger.warn(ignored);
}
return properties;
}

}
Expand Up @@ -137,8 +137,6 @@ public final class ActiveMQClient {

public static final boolean DEFAULT_USE_TOPOLOGY_FOR_LOADBALANCING = true;

public static final boolean DEFAULT_ENABLE_1X_PREFIXES = false;

public static final String THREAD_POOL_MAX_SIZE_PROPERTY_KEY = "activemq.artemis.client.global.thread.pool.max.size";

public static final String SCHEDULED_THREAD_POOL_SIZE_PROPERTY_KEY = "activemq.artemis.client.global.scheduled.thread.pool.core.size";
Expand Down
Expand Up @@ -21,14 +21,42 @@

import org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration;
import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.core.protocol.core.impl.PacketImpl;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
import org.apache.activemq.artemis.uri.ConnectionFactoryParser;
import org.apache.activemq.artemis.utils.ClassloadingUtil;
import org.jboss.logging.Logger;

/**
* A utility class for creating ActiveMQ Artemis client-side JMS managed resources.
*/
public class ActiveMQJMSClient {
private static final Logger logger = Logger.getLogger(ActiveMQJMSClient.class);

public static final boolean DEFAULT_ENABLE_1X_PREFIXES;


static {

String value1X = System.getProperty(ActiveMQJMSClient.class.getName() + ".enable1xPrefixes");

if (value1X == null) {
value1X = ClassloadingUtil.loadProperty(ActiveMQJMSClient.class.getClassLoader(), ActiveMQJMSClient.class.getName() + ".properties", "enable1xPrefixes");
}

boolean prefixes = false;


if (value1X != null) {
try {
prefixes = Boolean.parseBoolean(value1X);
} catch (Throwable e) {
logger.warn(e);
}
}
DEFAULT_ENABLE_1X_PREFIXES = prefixes;
}

/**
* Creates an ActiveMQConnectionFactory;
Expand Down Expand Up @@ -115,21 +143,37 @@ public static ActiveMQConnectionFactory createConnectionFactoryWithoutHA(JMSFact
/**
* Creates a client-side representation of a JMS Topic.
*
* This method is deprecated. Use {@link org.apache.activemq.artemis.jms.client.ActiveMQSession#createTopic(String)} as that method will know the proper
* prefix used at the target server.
*
* @param name the name of the topic
* @return The Topic
*/
@Deprecated
public static Topic createTopic(final String name) {
return ActiveMQDestination.createTopic(name);
if (DEFAULT_ENABLE_1X_PREFIXES) {
return ActiveMQDestination.createTopic(PacketImpl.OLD_TOPIC_PREFIX + name, name);
} else {
return ActiveMQDestination.createTopic(name);
}
}

/**
* Creates a client-side representation of a JMS Queue.
*
* This method is deprecated. Use {@link org.apache.activemq.artemis.jms.client.ActiveMQSession#createQueue(String)} (String)} as that method will know the proper
* prefix used at the target server.
* *
* @param name the name of the queue
* @return The Queue
*/
@Deprecated
public static Queue createQueue(final String name) {
return ActiveMQDestination.createQueue(name);
if (DEFAULT_ENABLE_1X_PREFIXES) {
return ActiveMQDestination.createQueue(PacketImpl.OLD_QUEUE_PREFIX + name, name);
} else {
return ActiveMQDestination.createQueue(name);
}
}

private ActiveMQJMSClient() {
Expand Down
Expand Up @@ -48,6 +48,7 @@
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSConstants;
import org.apache.activemq.artemis.api.jms.JMSFactoryType;
import org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl;
Expand Down Expand Up @@ -94,7 +95,7 @@ public class ActiveMQConnectionFactory extends JNDIStorable implements Connectio

private boolean ignoreJTA;

private boolean enable1xPrefixes = ActiveMQClient.DEFAULT_ENABLE_1X_PREFIXES;
private boolean enable1xPrefixes = ActiveMQJMSClient.DEFAULT_ENABLE_1X_PREFIXES;

@Override
public void writeExternal(ObjectOutput out) throws IOException {
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.api.jms.JMSFactoryType;
import org.apache.activemq.artemis.jms.server.config.ConnectionFactoryConfiguration;
import org.apache.activemq.artemis.utils.BufferHelper;
Expand Down Expand Up @@ -124,7 +125,7 @@ public class ConnectionFactoryConfigurationImpl implements ConnectionFactoryConf

private int initialMessagePacketSize = ActiveMQClient.DEFAULT_INITIAL_MESSAGE_PACKET_SIZE;

private boolean enable1xPrefixes = ActiveMQClient.DEFAULT_ENABLE_1X_PREFIXES;
private boolean enable1xPrefixes = ActiveMQJMSClient.DEFAULT_ENABLE_1X_PREFIXES;

private boolean enableSharedClientID = ActiveMQClient.DEFAULT_ENABLED_SHARED_CLIENT_ID;

Expand Down Expand Up @@ -640,7 +641,7 @@ public void decode(final ActiveMQBuffer buffer) {

deserializationWhiteList = BufferHelper.readNullableSimpleStringAsString(buffer);

enable1xPrefixes = buffer.readableBytes() > 0 ? buffer.readBoolean() : ActiveMQClient.DEFAULT_ENABLE_1X_PREFIXES;
enable1xPrefixes = buffer.readableBytes() > 0 ? buffer.readBoolean() : ActiveMQJMSClient.DEFAULT_ENABLE_1X_PREFIXES;

enableSharedClientID = buffer.readableBytes() > 0 ? buffer.readBoolean() : ActiveMQClient.DEFAULT_ENABLED_SHARED_CLIENT_ID;

Expand Down
@@ -0,0 +1,28 @@
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient
import org.apache.activemq.artemis.jms.client.ActiveMQQueue
import org.apache.activemq.artemis.jms.client.ActiveMQTopic
import org.apache.activemq.artemis.tests.compatibility.GroovyRun

/*
* 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.
*/

ActiveMQQueue queue = (ActiveMQQueue) ActiveMQJMSClient.createQueue("q1");
GroovyRun.assertEquals("jms.queue.q1", queue.getAddress());
GroovyRun.assertEquals("q1", queue.getQueueName());
ActiveMQTopic topic = (ActiveMQTopic) ActiveMQJMSClient.createTopic("t1");
GroovyRun.assertEquals("jms.topic.t1", topic.getAddress());
GroovyRun.assertEquals("t1", topic.getTopicName());
@@ -0,0 +1,92 @@
/*
* 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.compatibility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import static org.apache.activemq.artemis.tests.compatibility.GroovyRun.SNAPSHOT;
import static org.apache.activemq.artemis.tests.compatibility.GroovyRun.ONE_FIVE;

public class ActiveMQJMSClientCompatibilityTest extends ClasspathBaseTest {

@Test
public void testActiveMQJMSCompatibility_1XPrefix_SNAPSHOT() throws Exception {

Assert.assertFalse(ActiveMQJMSClient.DEFAULT_ENABLE_1X_PREFIXES);
ActiveMQQueue queue = (ActiveMQQueue)ActiveMQJMSClient.createQueue("t1");
// this step is to guarantee the class is not affected when there's no property in place
Assert.assertEquals("t1", queue.getAddress());

ClassLoader loader = getClasspath(SNAPSHOT, true);

System.setProperty(ActiveMQJMSClient.class.getName() + ".enable1xPrefixes", "true");

try {

evaluate(loader, "ActiveMQJMSClientCompatibilityTest/validateClient.groovy");

} finally {
System.clearProperty(ActiveMQJMSClient.class.getName() + ".enable1xPrefixes");
}

}

@Test
public void testActiveMQJMSCompatibility_1XPrefix_SNAPSHOT_with_properties() throws Exception {

Assert.assertFalse(ActiveMQJMSClient.DEFAULT_ENABLE_1X_PREFIXES);
ActiveMQQueue queue = (ActiveMQQueue)ActiveMQJMSClient.createQueue("t1");
// this step is to guarantee the class is not affected when there's no property in place
Assert.assertEquals("t1", queue.getAddress());

File file = serverFolder.newFile(ActiveMQJMSClient.class.getName() + ".properties");

FileOutputStream fileOutputStream = new FileOutputStream(file);
PrintStream stream = new PrintStream(fileOutputStream);
stream.println("enable1xPrefixes=true");
stream.close();

String snapshotPath = System.getProperty(SNAPSHOT);
Assume.assumeNotNull(snapshotPath);

String path = serverFolder.getRoot().getAbsolutePath() + File.pathSeparator + snapshotPath;


ClassLoader loader = defineClassLoader(path);

evaluate(loader, "ActiveMQJMSClientCompatibilityTest/validateClient.groovy");

}

@Test

// The purpose here is just to validate the test itself. Nothing to be fixed here
public void testActiveMQJMSCompatibility_1XPrefix_ONE_FIVE() throws Exception {
ClassLoader loader = getClasspath(ONE_FIVE, false);

evaluate(loader, "ActiveMQJMSClientCompatibilityTest/validateClient.groovy");

}
}

0 comments on commit ca113c1

Please sign in to comment.