KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > base > io > J2EEObjectStreamFactoryImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.base.io;
25
26 import java.io.*;
27 import java.security.*;
28 import java.rmi.*;
29 import javax.ejb.*;
30 import javax.naming.*;
31
32 import java.util.logging.*;
33 import com.sun.logging.*;
34
35 import com.sun.ejb.spi.io.J2EEObjectStreamFactory;
36 import com.sun.ejb.spi.io.NonSerializableObjectHandler;
37
38 /**
39  * A Factory class for creating EJBObject input/output Stream
40  *
41  * @author Mahesh Kannan
42  */

43 public class J2EEObjectStreamFactoryImpl
44     implements J2EEObjectStreamFactory
45 {
46
47     private static Logger _ejbLogger =
48        LogDomains.getLogger(LogDomains.EJB_LOGGER);
49
50     public J2EEObjectStreamFactoryImpl() {
51     }
52
53     /**
54      *
55      * Obtain an ObjectOutputStream that allows serialization
56      * of a graph of objects. The objects can be plain Serializable objects
57      * or can be converted into Serializable objects using
58      * the handler
59      *
60      *@throws IOException when the serialziation fails
61      *@return an ObjectOutputStream that can be used to serialize objects
62      */

63     public ObjectOutputStream createObjectOutputStream(
64             final OutputStream os,
65             final boolean replaceObject,
66             final NonSerializableObjectHandler handler)
67         throws IOException
68     {
69         // Need privileged block here because EJBObjectOutputStream
70
// does enableReplaceObject
71
ObjectOutputStream oos = null;
72         try {
73             oos = (ObjectOutputStream)AccessController.doPrivileged(
74                 new PrivilegedExceptionAction() {
75                     public java.lang.Object JavaDoc run()
76                         throws Exception JavaDoc
77                     {
78                         return new EJBObjectOutputStream(
79                             os, replaceObject, handler);
80                     }
81                 });
82         } catch ( PrivilegedActionException ex ) {
83             throw (IOException) ex.getException();
84         }
85         return oos;
86     }
87
88     /**
89      *
90      * Obtain an ObjectInputStream that allows de-serialization
91      * of a graph of objects.
92      *
93      *@throws IOException when the de-serialziation fails
94      *@return an ObjectInputStream that can be used to deserialize objects
95      */

96     public ObjectInputStream createObjectInputStream(
97             final InputStream is,
98             final boolean resolveObject,
99             final ClassLoader JavaDoc loader)
100         throws Exception JavaDoc
101     {
102         ObjectInputStream ois = null;
103         if ( loader != null ) {
104             // Need privileged block here because EJBObjectInputStream
105
// does enableResolveObject
106
try {
107                 ois = (ObjectInputStream)AccessController.doPrivileged(
108                 new PrivilegedExceptionAction() {
109                     public java.lang.Object JavaDoc run()
110                         throws Exception JavaDoc
111                     {
112                         return new EJBObjectInputStream(
113                             is, loader, resolveObject);
114                     }
115                 });
116             } catch ( PrivilegedActionException ex ) {
117                 throw (IOException) ex.getException();
118             }
119         } else {
120             ois = new ObjectInputStream(is);
121         }
122
123         return ois;
124     }
125
126 }
127
Popular Tags