KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > IDLEntityHelper


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop;
19
20 import org.apache.geronimo.interop.*;
21 import org.apache.geronimo.interop.util.*;
22 import java.lang.reflect.*;
23 import java.util.HashMap JavaDoc;
24
25 public class IDLEntityHelper implements ObjectHelper
26 {
27     private static Class JavaDoc[] EMPTY_CLASS_ARRAY = {};
28
29     private static Object JavaDoc[] EMPTY_OBJECT_ARRAY = {};
30
31     private static HashMap JavaDoc _helperMap = new HashMap JavaDoc();
32
33     private Method _id;
34
35     private Method _type;
36
37     private Method _read;
38
39     private Method _write;
40
41     static IDLEntityHelper getInstance(Class JavaDoc theClass)
42     {
43         IDLEntityHelper helper = (IDLEntityHelper)_helperMap.get(theClass);
44         if (helper == null)
45         {
46             synchronized (_helperMap)
47             {
48                 helper = (IDLEntityHelper)_helperMap.get(theClass);
49                 if (helper == null)
50                 {
51                     helper = new IDLEntityHelper(theClass);
52                     _helperMap.put(theClass, helper);
53                 }
54             }
55         }
56         return helper;
57     }
58
59     private IDLEntityHelper(Class JavaDoc theClass)
60     {
61         try
62         {
63             Class JavaDoc helper = ThreadContext.loadClass(theClass.getName() + "Helper", theClass);
64             _id = helper.getDeclaredMethod("id", EMPTY_CLASS_ARRAY);
65             _type = helper.getDeclaredMethod("type", EMPTY_CLASS_ARRAY);
66             _read = helper.getDeclaredMethod("read", new Class JavaDoc[] { org.omg.CORBA.portable.InputStream JavaDoc.class });
67             _write = helper.getDeclaredMethod("write", new Class JavaDoc[] { org.omg.CORBA.portable.OutputStream JavaDoc.class, theClass });
68         }
69         catch (SystemException ex)
70         {
71             throw ex;
72         }
73         catch (Exception JavaDoc ex)
74         {
75             throw new SystemException(ex);
76         }
77     }
78
79     public String JavaDoc id()
80     {
81         try
82         {
83             return (String JavaDoc)_id.invoke(null, EMPTY_OBJECT_ARRAY);
84         }
85         catch (SystemException ex)
86         {
87             throw ex;
88         }
89         catch (Exception JavaDoc ex)
90         {
91             throw new SystemException(ex);
92         }
93     }
94
95     public org.omg.CORBA.TypeCode JavaDoc type()
96     {
97         try
98         {
99             return (org.omg.CORBA.TypeCode JavaDoc)_type.invoke(null, EMPTY_OBJECT_ARRAY);
100         }
101         catch (SystemException ex)
102         {
103             throw ex;
104         }
105         catch (Exception JavaDoc ex)
106         {
107             throw new SystemException(ex);
108         }
109     }
110
111     public Object JavaDoc read(ObjectInputStream input)
112     {
113         try
114         {
115             return _read.invoke(null, new Object JavaDoc[] { input._cdrInput });
116         }
117         catch (SystemException ex)
118         {
119             throw ex;
120         }
121         catch (Exception JavaDoc ex)
122         {
123             throw new SystemException(ex);
124         }
125     }
126
127     public void write(ObjectOutputStream output, Object JavaDoc value)
128     {
129         try
130         {
131             _write.invoke(null, new Object JavaDoc[] { output._cdrOutput, value });
132         }
133         catch (SystemException ex)
134         {
135             throw ex;
136         }
137         catch (Exception JavaDoc ex)
138         {
139             throw new SystemException(ex);
140         }
141     }
142 }
143
Popular Tags