ARTEMIS-162 Fix non serializable JGroups on Configuration clone used on Colocated support

https://issues.apache.org/jira/browse/ARTEMIS-162
This commit is contained in:
Clebert Suconic 2015-08-03 16:49:40 -04:00
parent 39b9fd1ce7
commit 7cfc33790d
3 changed files with 41 additions and 22 deletions

View File

@ -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<CoreQueueConfiguration> queueConfigurations = new ArrayList<CoreQueueConfiguration>();
protected List<BroadcastGroupConfiguration> broadcastGroupConfigurations = new ArrayList<BroadcastGroupConfiguration>();
protected transient List<BroadcastGroupConfiguration> broadcastGroupConfigurations = new ArrayList<BroadcastGroupConfiguration>();
protected Map<String, DiscoveryGroupConfiguration> discoveryGroupConfigurations = new LinkedHashMap<String, DiscoveryGroupConfiguration>();
protected transient Map<String, DiscoveryGroupConfiguration> discoveryGroupConfigurations = new LinkedHashMap<String, DiscoveryGroupConfiguration>();
// 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<Configuration>()
{
@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

View File

@ -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));
}

View File

@ -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