KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > ObjectSerializationFactory


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.marshall;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import java.io.IOException JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.ObjectOutputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16
17 /**
18  * Factory class for creating object output and inut streams, switching between JDK defaults and JBoss Serialization classes.
19  *
20  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
21  * @author <a HREF="mailto:clebert.suconic@jboss.org">Clebert Suconic</a>
22  */

23 public class ObjectSerializationFactory
24 {
25    static boolean useJBossSerialization = false;
26    private static Log log = LogFactory.getLog(ObjectSerializationFactory.class);
27    static ObjectStreamFactory factory = new JavaObjectStreamFactory();
28
29    static
30    {
31       String JavaDoc useJBossSerializationStr = System.getProperty("serialization.jboss", "true");
32       useJBossSerialization = Boolean.valueOf(useJBossSerializationStr);
33
34       try
35       {
36          if (useJBossSerialization)
37          {
38             factory = (ObjectStreamFactory) Class.forName("org.jboss.cache.marshall.JBossObjectStreamFactory").newInstance();
39          }
40       }
41       catch (Exception JavaDoc e)
42       {
43          log.error("Unable to load JBossObjectStreamFactory. Perhaps jboss-serialization jar not loaded?", e);
44          log.error("Falling back to java serialization.");
45
46       }
47    }
48
49    public static ObjectOutputStream JavaDoc createObjectOutputStream(OutputStream JavaDoc out) throws IOException JavaDoc
50    {
51       return factory.createObjectOutputStream(out);
52    }
53
54    public static ObjectInputStream JavaDoc createObjectInputStream(byte[] bytes) throws IOException JavaDoc
55    {
56       return factory.createObjectInputStream(bytes);
57    }
58
59    public static boolean useJBossSerialization()
60    {
61       return useJBossSerialization;
62    }
63 }
64
Popular Tags