~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/serialization/SerializationStreamFactory.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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.
6
 
*
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.
11
 
*
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.
16
 
*
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.
21
 
*/
22
 
 
23
 
package org.jboss.remoting.serialization;
24
 
 
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;
30
 
 
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;
36
 
import java.util.Map;
37
 
 
38
 
/**
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.
43
 
 *
44
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
45
 
 * @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
46
 
 */
47
 
public class SerializationStreamFactory implements SerializationStreamFactoryMBean
48
 
{
49
 
   protected static final Logger log = Logger.getLogger(SerializationStreamFactory.class);
50
 
   private static Map managers = new HashMap();
51
 
 
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";
57
 
 
58
 
 
59
 
   static
60
 
   {
61
 
      try
62
 
      {
63
 
         String defaultValue = JavaSerializationManager.class.getName();
64
 
         String managerClassName = getSystemProperty("SERIALIZATION", defaultValue);
65
 
         setManagerClassName(DEFAULT, managerClassName);
66
 
      }
67
 
      catch(Exception e)
68
 
      {
69
 
         log.error(e.getMessage(), e);
70
 
      }
71
 
      try
72
 
      {
73
 
         setManagerClassName(JAVA, JavaSerializationManager.class.getName());
74
 
      }
75
 
      catch(Exception e)
76
 
      {
77
 
         log.error(e.getMessage(), e);
78
 
      }
79
 
      try
80
 
      {
81
 
         setManagerClassName(JBOSS, "org.jboss.remoting.serialization.impl.jboss.JBossSerializationManager");
82
 
      }
83
 
      catch(Throwable e) // catching throwable as if jboss serialization not on classpath, will throw NoClassDefFoundError
84
 
      {
85
 
         log.debug("Could not load JBoss Serialization.  Use Java Serialization default.");
86
 
         log.trace(e);
87
 
      }
88
 
      try
89
 
      {
90
 
         setManagerClassName(JAVA_ENCRYPT, JavaEncryptionSerializationManager.class.getName());
91
 
      }
92
 
      catch(Throwable e) // catching throwable as if jboss serialization not on classpath, will throw NoClassDefFoundError
93
 
      {
94
 
         log.debug("Could not load Java Encrypted Serialization.  Use Java Serialization default.");
95
 
         log.trace(e);
96
 
      }
97
 
      try
98
 
      {
99
 
         setManagerClassName(JBOSS_ENCRYPT, JBossEncryptionSerializationManager.class.getName());
100
 
      }
101
 
      catch(Throwable e) // catching throwable as if jboss serialization not on classpath, will throw NoClassDefFoundError
102
 
      {
103
 
         log.debug("Could not load JBoss Encrypted Serialization.  Use Java Serialization default.");
104
 
         log.trace(e);
105
 
      }
106
 
   }
107
 
 
108
 
   /**
109
 
    * The fully qualified classname of the DefaultSerializationManager implementation.
110
 
    *
111
 
    * @param className
112
 
    */
113
 
   public static void setManagerClassName(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException
114
 
   {
115
 
      setManagerClassName(DEFAULT, className);
116
 
   }
117
 
 
118
 
   /**
119
 
    * The fully qualified classname of the DefaultSerializationManager implementation.
120
 
    *
121
 
    * @param className
122
 
    */
123
 
   public static void setManagerClassName(String key, String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException
124
 
   {
125
 
      loadObjectManagerClass(key, className);
126
 
   }
127
 
 
128
 
 
129
 
   public String getManager()
130
 
   {
131
 
      return getManager(DEFAULT);
132
 
   }
133
 
 
134
 
   public String getManager(String key)
135
 
   {
136
 
      SerializationManager manager = (SerializationManager) managers.get(key);
137
 
      if(manager == null)
138
 
      {
139
 
         return null;
140
 
      }
141
 
      else
142
 
      {
143
 
         return manager.getClass().getName();
144
 
      }
145
 
   }
146
 
 
147
 
   public void setManager(String manager) throws Exception
148
 
   {
149
 
      setManager(DEFAULT, manager);
150
 
   }
151
 
 
152
 
   public void setManager(String key, String manager) throws Exception
153
 
   {
154
 
      setManagerClassName(key, manager);
155
 
   }
156
 
 
157
 
   /**
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.
160
 
    */
161
 
   private static void loadObjectManagerClass(String key, String managerClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException
162
 
   {
163
 
      Class managerClass = ClassLoaderUtility.loadClass(SerializationStreamFactory.class, managerClassName);
164
 
      SerializationManager manager = (SerializationManager) managerClass.newInstance();
165
 
 
166
 
      if(managers.get(key) != null)
167
 
      {
168
 
         managers.remove(key);
169
 
      }
170
 
      managers.put(key, manager);
171
 
   }
172
 
 
173
 
   /**
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.
177
 
    *
178
 
    * @throws IOException if there's no corresponding SerializationManager instance.
179
 
    */
180
 
   public static SerializationManager getManagerInstance(String key) throws IOException
181
 
   {
182
 
      if(key == null)
183
 
      {
184
 
         key = JAVA;
185
 
      }
186
 
      SerializationManager manager = (SerializationManager) managers.get(key);
187
 
 
188
 
      if (manager == null)
189
 
      {
190
 
         throw new IOException("Unknown serialization type: " + key);
191
 
      }
192
 
      return manager;
193
 
   }
194
 
 
195
 
   public static SerializationManager getManagerInstance() throws IOException
196
 
   {
197
 
      return getManagerInstance(DEFAULT);
198
 
   }
199
 
 
200
 
   static private String getSystemProperty(final String name, final String defaultValue)
201
 
   {
202
 
      if (SecurityUtility.skipAccessControl())
203
 
         return System.getProperty(name, defaultValue);
204
 
         
205
 
      String value = null;
206
 
      try
207
 
      {
208
 
         value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
209
 
         {
210
 
            public Object run() throws Exception
211
 
            {
212
 
               return System.getProperty(name, defaultValue);
213
 
            }
214
 
         });
215
 
      }
216
 
      catch (PrivilegedActionException e)
217
 
      {
218
 
         throw (RuntimeException) e.getCause();
219
 
      }
220
 
      
221
 
      return value;
222
 
   }
223
 
}
 
 
b'\\ No newline at end of file'