2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.serialization;
25
import org.jboss.logging.Logger;
26
import org.jboss.remoting.serialization.impl.java.JavaEncryptionSerializationManager;
27
import org.jboss.remoting.serialization.impl.java.JavaSerializationManager;
28
import org.jboss.remoting.serialization.impl.jboss.JBossEncryptionSerializationManager;
29
import org.jboss.remoting.util.SecurityUtility;
31
import java.io.IOException;
32
import java.security.AccessController;
33
import java.security.PrivilegedActionException;
34
import java.security.PrivilegedExceptionAction;
35
import java.util.HashMap;
39
* This factory is for defining the Object stream implemenations to be used
40
* along with creating those implemenations for use. The main function will
41
* be to return instance of ObjectOutput and ObjectInput. By default, the implementations
42
* will be java.io.ObjectOutputStream and java.io.ObjectInputStream.
44
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
45
* @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
47
public class SerializationStreamFactory implements SerializationStreamFactoryMBean
49
protected static final Logger log = Logger.getLogger(SerializationStreamFactory.class);
50
private static Map managers = new HashMap();
52
public static final String DEFAULT = "default";
53
public static final String JAVA = "java";
54
public static final String JBOSS = "jboss";
55
public static final String JAVA_ENCRYPT = "javaencrypt";
56
public static final String JBOSS_ENCRYPT = "jbossencrypt";
63
String defaultValue = JavaSerializationManager.class.getName();
64
String managerClassName = getSystemProperty("SERIALIZATION", defaultValue);
65
setManagerClassName(DEFAULT, managerClassName);
69
log.error(e.getMessage(), e);
73
setManagerClassName(JAVA, JavaSerializationManager.class.getName());
77
log.error(e.getMessage(), e);
81
setManagerClassName(JBOSS, "org.jboss.remoting.serialization.impl.jboss.JBossSerializationManager");
83
catch(Throwable e) // catching throwable as if jboss serialization not on classpath, will throw NoClassDefFoundError
85
log.debug("Could not load JBoss Serialization. Use Java Serialization default.");
90
setManagerClassName(JAVA_ENCRYPT, JavaEncryptionSerializationManager.class.getName());
92
catch(Throwable e) // catching throwable as if jboss serialization not on classpath, will throw NoClassDefFoundError
94
log.debug("Could not load Java Encrypted Serialization. Use Java Serialization default.");
99
setManagerClassName(JBOSS_ENCRYPT, JBossEncryptionSerializationManager.class.getName());
101
catch(Throwable e) // catching throwable as if jboss serialization not on classpath, will throw NoClassDefFoundError
103
log.debug("Could not load JBoss Encrypted Serialization. Use Java Serialization default.");
109
* The fully qualified classname of the DefaultSerializationManager implementation.
113
public static void setManagerClassName(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException
115
setManagerClassName(DEFAULT, className);
119
* The fully qualified classname of the DefaultSerializationManager implementation.
123
public static void setManagerClassName(String key, String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException
125
loadObjectManagerClass(key, className);
129
public String getManager()
131
return getManager(DEFAULT);
134
public String getManager(String key)
136
SerializationManager manager = (SerializationManager) managers.get(key);
143
return manager.getClass().getName();
147
public void setManager(String manager) throws Exception
149
setManager(DEFAULT, manager);
152
public void setManager(String key, String manager) throws Exception
154
setManagerClassName(key, manager);
158
* Loads the implementation class for ObjectOutput as specified by the object output class name. Will also load
159
* the constructor and cache it to be used when creating the actual implementation instance.
161
private static void loadObjectManagerClass(String key, String managerClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException
163
Class managerClass = ClassLoaderUtility.loadClass(SerializationStreamFactory.class, managerClassName);
164
SerializationManager manager = (SerializationManager) managerClass.newInstance();
166
if(managers.get(key) != null)
168
managers.remove(key);
170
managers.put(key, manager);
174
* @return the SerializationManager instance corresponding to the given key. If key is null,
175
* "java" is assumed. The method never returns null, if there's no SerializationManager
176
* associated with the given key, the method throws exception.
178
* @throws IOException if there's no corresponding SerializationManager instance.
180
public static SerializationManager getManagerInstance(String key) throws IOException
186
SerializationManager manager = (SerializationManager) managers.get(key);
190
throw new IOException("Unknown serialization type: " + key);
195
public static SerializationManager getManagerInstance() throws IOException
197
return getManagerInstance(DEFAULT);
200
static private String getSystemProperty(final String name, final String defaultValue)
202
if (SecurityUtility.skipAccessControl())
203
return System.getProperty(name, defaultValue);
208
value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
210
public Object run() throws Exception
212
return System.getProperty(name, defaultValue);
216
catch (PrivilegedActionException e)
218
throw (RuntimeException) e.getCause();
b'\\ No newline at end of file'