diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java index 0f0616b62c..c6dab0e096 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java @@ -23,6 +23,8 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.URI; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -50,6 +52,7 @@ import org.apache.activemq.artemis.core.server.JournalType; import org.apache.activemq.artemis.core.server.group.impl.GroupingHandlerConfiguration; import org.apache.activemq.artemis.core.settings.impl.AddressSettings; import org.apache.activemq.artemis.core.settings.impl.ResourceLimitSettings; +import org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader; public class ConfigurationImpl implements Configuration, Serializable { @@ -114,9 +117,9 @@ public class ConfigurationImpl implements Configuration, Serializable private List queueConfigurations = new ArrayList(); - protected List broadcastGroupConfigurations = new ArrayList(); + protected transient List broadcastGroupConfigurations = new ArrayList(); - protected Map discoveryGroupConfigurations = new LinkedHashMap(); + protected transient Map discoveryGroupConfigurations = new LinkedHashMap(); // Paging related attributes ------------------------------------------------------------ @@ -1586,11 +1589,28 @@ public class ConfigurationImpl implements Configuration, Serializable @Override public Configuration copy() throws Exception { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutputStream os = new ObjectOutputStream(bos); - os.writeObject(this); - ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); - return (Configuration) ois.readObject(); + + return AccessController.doPrivileged(new PrivilegedExceptionAction() + { + @Override + public Configuration run() throws Exception + { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream os = new ObjectOutputStream(bos); + os.writeObject(ConfigurationImpl.this); + ObjectInputStream ois = new ObjectInputStreamWithClassLoader(new ByteArrayInputStream(bos.toByteArray())); + Configuration config = (Configuration) ois.readObject(); + + // this is transient because of possible jgroups integration, we need to copy it manually + config.setBroadcastGroupConfigurations(ConfigurationImpl.this.getBroadcastGroupConfigurations()); + + // this is transient because of possible jgroups integration, we need to copy it manually + config.setDiscoveryGroupConfigurations(ConfigurationImpl.this.getDiscoveryGroupConfigurations()); + + return config; + } + }); + } @Override diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java index dd9f5849d4..b6d036f934 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java @@ -22,17 +22,12 @@ import org.apache.activemq.artemis.core.config.Configuration; import org.apache.activemq.artemis.core.config.ha.LiveOnlyPolicyConfiguration; import org.apache.activemq.artemis.core.journal.impl.JournalConstants; import org.apache.activemq.artemis.core.server.JournalType; -import org.apache.activemq.artemis.tests.util.RandomUtil; import org.apache.activemq.artemis.tests.util.ActiveMQTestBase; +import org.apache.activemq.artemis.tests.util.RandomUtil; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - public class ConfigurationImplTest extends ActiveMQTestBase { protected Configuration conf; @@ -497,14 +492,9 @@ public class ConfigurationImplTest extends ActiveMQTestBase conf.setClusterPassword(s); Assert.assertEquals(s, conf.getClusterPassword()); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(conf); - oos.flush(); + // This will use serialization to perform a deep copy of the object + Configuration conf2 = conf.copy(); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - ObjectInputStream ois = new ObjectInputStream(bais); - Configuration conf2 = (Configuration)ois.readObject(); Assert.assertTrue(conf.equals(conf2)); } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/connection/ConnectionFactoryWithJGroupsSerializationTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/connection/ConnectionFactoryWithJGroupsSerializationTest.java index 4b58ee1ba6..bf507600f7 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/connection/ConnectionFactoryWithJGroupsSerializationTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jms/connection/ConnectionFactoryWithJGroupsSerializationTest.java @@ -16,6 +16,7 @@ */ package org.apache.activemq.artemis.tests.integration.jms.connection; +import javax.jms.Queue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -23,19 +24,19 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import javax.jms.Queue; - import org.apache.activemq.artemis.api.core.BroadcastEndpoint; import org.apache.activemq.artemis.api.core.BroadcastEndpointFactory; import org.apache.activemq.artemis.api.core.ChannelBroadcastEndpointFactory; import org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration; import org.apache.activemq.artemis.api.core.JGroupsFileBroadcastEndpointFactory; import org.apache.activemq.artemis.api.jms.JMSFactoryType; +import org.apache.activemq.artemis.core.config.Configuration; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; import org.apache.activemq.artemis.tests.util.JMSTestBase; import org.jgroups.JChannel; import org.jgroups.conf.PlainConfigurator; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -168,6 +169,14 @@ public class ConnectionFactoryWithJGroupsSerializationTest extends JMSTestBase assertTrue(broadcastEndpoint instanceof JGroupsFileBroadcastEndpointFactory); } + @Test + public void testCopyConfiguration() throws Exception + { + Assert.assertEquals(2, jmsServer.getActiveMQServer().getConfiguration().getDiscoveryGroupConfigurations().size()); + Configuration copiedconfig = jmsServer.getActiveMQServer().getConfiguration().copy(); + Assert.assertEquals(2, copiedconfig.getDiscoveryGroupConfigurations().size()); + } + @Override @After public void tearDown() throws Exception