KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > naming > NamingUtils


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 package com.sun.enterprise.naming;
24
25 import java.rmi.*;
26 import java.io.*;
27 import java.util.logging.*;
28 import com.sun.logging.*;
29 import com.sun.enterprise.util.*;
30
31 /**
32  * This is a utils class for refactoring the following method.
33  */

34
35 public class NamingUtils {
36
37    static Logger _logger=LogDomains.getLogger(LogDomains.JNDI_LOGGER);
38
39    /**
40      * method to make a copy of the object.
41      */

42
43   public static Object JavaDoc makeCopyOfObject(Object JavaDoc obj) {
44
45        
46     if ( obj instanceof Serializable ) {
47         try {
48         // first serialize the object
49
ByteArrayOutputStream bos = new ByteArrayOutputStream();
50         ObjectOutputStream oos = new ObjectOutputStream(bos);
51         oos.writeObject(obj);
52         oos.flush();
53         byte[] data = bos.toByteArray();
54         oos.close();
55         bos.close();
56         
57         // now deserialize it
58
ByteArrayInputStream bis = new ByteArrayInputStream(data);
59         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
60         ObjectInputStream ois = new ObjectInputStreamWithLoader(bis,cl);
61              
62                                
63         return ois.readObject();
64         } catch ( Exception JavaDoc ex ) {
65          
66                 _logger.log(Level.SEVERE,
67                             "enterprise_naming.excep_in_copymutableobj", ex);
68
69                 RuntimeException JavaDoc re =
70                     new RuntimeException JavaDoc("Cant copy Serializable object:");
71                 re.initCause(ex);
72                 throw re;
73         }
74     }
75     else {
76         // XXX no copy ?
77
return obj;
78     }
79     }
80 }
81
Popular Tags