KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > referenceable > ObjectRefAddr


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 package org.jboss.mq.referenceable;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.rmi.MarshalledObject JavaDoc;
29
30 import javax.naming.RefAddr JavaDoc;
31
32 /**
33  * This class is used to store a serializable object in a RefAddr object.
34  *
35  * @author Scott M Stark (Scott_Stark@d...)
36  * @author Hiram Chirino (Cojonudo14@hotmail.com)
37  * @created August 16, 2001
38  * @version $Revision: 37459 $
39  */

40 public class ObjectRefAddr extends RefAddr JavaDoc
41 {
42
43    /** The serialVersionUID */
44    private static final long serialVersionUID = 751863774931559945L;
45    
46    private byte[] serialContent;
47
48    /**
49     * ObjectRefAddr constructor comment.
50     *
51     * @param arg1 java.lang.String
52     * @param content Description of Parameter
53     * @exception javax.naming.NamingException Description of Exception
54     */

55    public ObjectRefAddr(String JavaDoc arg1, Object JavaDoc content) throws javax.naming.NamingException JavaDoc
56    {
57       super(arg1);
58
59       try
60       {
61          java.rmi.MarshalledObject JavaDoc mo = new MarshalledObject JavaDoc(content);
62          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
63          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
64          oos.writeObject(mo);
65          serialContent = baos.toByteArray();
66       }
67       catch (java.io.IOException JavaDoc e)
68       {
69          e.printStackTrace();
70          throw new javax.naming.NamingException JavaDoc("Could not create a reference: " + e.getMessage());
71       }
72    }
73
74    /**
75     * getContent method comment.
76     *
77     * @return The Content value
78     */

79    public Object JavaDoc getContent()
80    {
81       return serialContent;
82    }
83
84    /**
85     * getContent method comment.
86     *
87     * @param ref Description of Parameter
88     * @param arg1 Description of Parameter
89     * @return Description of the Returned
90     * Value
91     * @exception javax.naming.NamingException Description of Exception
92     */

93    public static Object JavaDoc extractObjectRefFrom(javax.naming.Reference JavaDoc ref, String JavaDoc arg1)
94       throws javax.naming.NamingException JavaDoc
95    {
96
97       try
98       {
99          byte[] serialContent = (byte[]) ref.get(arg1).getContent();
100          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(serialContent);
101          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
102          java.rmi.MarshalledObject JavaDoc mo = (java.rmi.MarshalledObject JavaDoc) ois.readObject();
103          return mo.get();
104       }
105       catch (Exception JavaDoc e)
106       {
107          throw new javax.naming.NamingException JavaDoc("Invalid reference. Error: " + e.getMessage());
108       }
109
110    }
111 }
112
Popular Tags