KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > ior > ObjectKeyFactoryImpl


1 /*
2  * @(#)ObjectKeyFactoryImpl.java 1.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.ior;
9
10 import java.io.IOException JavaDoc ;
11
12 import org.omg.CORBA.MARSHAL JavaDoc ;
13 import org.omg.CORBA.OctetSeqHolder JavaDoc ;
14 import org.omg.CORBA_2_3.portable.InputStream JavaDoc ;
15
16 import com.sun.corba.se.spi.ior.ObjectId ;
17 import com.sun.corba.se.spi.ior.ObjectKey ;
18 import com.sun.corba.se.spi.ior.ObjectKeyFactory ;
19 import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
20
21 import com.sun.corba.se.spi.orb.ORB ;
22 import com.sun.corba.se.spi.logging.CORBALogDomains ;
23
24 import com.sun.corba.se.impl.orbutil.ORBConstants ;
25
26 import com.sun.corba.se.impl.ior.JIDLObjectKeyTemplate ;
27 import com.sun.corba.se.impl.ior.POAObjectKeyTemplate ;
28 import com.sun.corba.se.impl.ior.WireObjectKeyTemplate ;
29 import com.sun.corba.se.impl.ior.ObjectIdImpl ;
30 import com.sun.corba.se.impl.ior.ObjectKeyImpl ;
31 import com.sun.corba.se.impl.logging.IORSystemException ;
32
33 import com.sun.corba.se.impl.encoding.EncapsInputStream ;
34
35 /** Based on the magic and scid, return the appropriate
36 * ObjectKeyTemplate. Expects to be called with a valid
37 * magic. If scid is not valid, null should be returned.
38 */

39 interface Handler {
40     ObjectKeyTemplate handle( int magic, int scid,
41     InputStream JavaDoc is, OctetSeqHolder JavaDoc osh ) ;
42 }
43
44 /** Singleton used to manufacture ObjectKey and ObjectKeyTemplate
45  * instances.
46  * @author Ken Cavanaugh
47  */

48 public class ObjectKeyFactoryImpl implements ObjectKeyFactory
49 {
50     public static final int MAGIC_BASE = 0xAFABCAFE ;
51
52     // Magic used in our object keys for JDK 1.2, 1.3, RMI-IIOP OP,
53
// J2EE 1.0-1.2.1.
54
public static final int JAVAMAGIC_OLD = MAGIC_BASE ;
55
56     // Magic used only in JDK 1.3.1. No format changes in object keys.
57
public static final int JAVAMAGIC_NEW = MAGIC_BASE + 1 ;
58
59     // New magic used in our object keys for JDK 1.4, J2EE 1.3 and later.
60
// Format changes: all object keys have version string; POA key format
61
// is changed.
62
public static final int JAVAMAGIC_NEWER = MAGIC_BASE + 2 ;
63
64     public static final int MAX_MAGIC = JAVAMAGIC_NEWER ;
65
66     // Beginning in JDK 1.3.1_01, we introduced changes which required
67
// the ability to distinguish between JDK 1.3.1 FCS and the patch
68
// versions. See OldJIDLObjectKeyTemplate.
69
public static final byte JDK1_3_1_01_PATCH_LEVEL = 1;
70
71     private final ORB orb ;
72     private IORSystemException wrapper ;
73
74     public ObjectKeyFactoryImpl( ORB orb )
75     {
76     this.orb = orb ;
77     wrapper = IORSystemException.get( orb,
78         CORBALogDomains.OA_IOR ) ;
79     }
80    
81     // XXX The handlers still need to be made pluggable.
82
//
83
// I think this can be done as follows:
84
// 1. Move the Handler interface into the SPI as ObjectKeyHandler.
85
// 2. Add two methods to ObjectAdapterFactory:
86
// ObjectKeyHandler getHandlerForObjectKey( ) ;
87
// ObjectKeyHandler getHandlerForObjectKeyTemplate( ) ;
88
// 3. Move the implementation of the fullKey handler and the
89
// oktempOnly handler into TOAFactory and POAFactory.
90
// 4. Move the ObjectKey impl classes into the impl/oa packages.
91
// 5. Create an internal interface
92
// interface HandlerFinder {
93
// ObjectKeyHandler get( int scid ) ;
94
// }
95
// and modify create(InputStream,Handler,OctetSeqHolder)
96
// to take a HandlerFinder instead of a Handler.
97
// 6. Modify create( byte[] ) and createTemplate( InputStream )
98
// to create an instance of HandlerFinder: something like:
99
// new HandlerFinder() {
100
// ObjectKeyHandler get( int scid )
101
// {
102
// return orb.getRequestDispatcherRegistry().
103
// getObjectAdapterFactory( scid ).getHandlerForObjectKey() ;
104
// }
105
// and similarly for getHandlerForObjectKeyTemplate.
106

107     /** This handler reads the full object key, both the oktemp
108     * and the ID.
109     */

110     private Handler fullKey = new Handler() {
111     public ObjectKeyTemplate handle( int magic, int scid,
112         InputStream JavaDoc is, OctetSeqHolder JavaDoc osh ) {
113         ObjectKeyTemplate oktemp = null ;
114
115         if ((scid >= ORBConstants.FIRST_POA_SCID) &&
116             (scid <= ORBConstants.MAX_POA_SCID)) {
117             if (magic >= JAVAMAGIC_NEWER)
118             oktemp = new POAObjectKeyTemplate( orb, magic, scid, is, osh ) ;
119             else
120             oktemp = new OldPOAObjectKeyTemplate( orb, magic, scid, is, osh ) ;
121         } else if ((scid >= 0) && (scid < ORBConstants.FIRST_POA_SCID)) {
122             if (magic >= JAVAMAGIC_NEWER)
123             oktemp = new JIDLObjectKeyTemplate( orb, magic, scid, is, osh ) ;
124             else
125             oktemp = new OldJIDLObjectKeyTemplate( orb, magic, scid, is, osh );
126         }
127
128         return oktemp ;
129         }
130     } ;
131
132     /** This handler reads only the oktemp.
133     */

134     private Handler oktempOnly = new Handler() {
135     public ObjectKeyTemplate handle( int magic, int scid,
136         InputStream JavaDoc is, OctetSeqHolder JavaDoc osh ) {
137         ObjectKeyTemplate oktemp = null ;
138
139         if ((scid >= ORBConstants.FIRST_POA_SCID) &&
140             (scid <= ORBConstants.MAX_POA_SCID)) {
141             if (magic >= JAVAMAGIC_NEWER)
142             oktemp = new POAObjectKeyTemplate( orb, magic, scid, is ) ;
143             else
144             oktemp = new OldPOAObjectKeyTemplate( orb, magic, scid, is ) ;
145         } else if ((scid >= 0) && (scid < ORBConstants.FIRST_POA_SCID)) {
146             if (magic >= JAVAMAGIC_NEWER)
147             oktemp = new JIDLObjectKeyTemplate( orb, magic, scid, is ) ;
148             else
149             oktemp = new OldJIDLObjectKeyTemplate( orb, magic, scid, is ) ;
150         }
151
152         return oktemp ;
153         }
154     } ;
155
156     /** Returns true iff magic is in the range of valid magic numbers
157     * for our ORB.
158     */

159     private boolean validMagic( int magic )
160     {
161     return (magic >= MAGIC_BASE) && (magic <= MAX_MAGIC) ;
162     }
163
164     /** Creates an ObjectKeyTemplate from the InputStream. Most of the
165     * decoding is done inside the handler.
166     */

167     private ObjectKeyTemplate create( InputStream JavaDoc is, Handler handler,
168     OctetSeqHolder JavaDoc osh )
169     {
170     ObjectKeyTemplate oktemp = null ;
171     
172     try {
173         is.mark(0) ;
174         int magic = is.read_long() ;
175             
176         if (validMagic( magic )) {
177         int scid = is.read_long() ;
178         oktemp = handler.handle( magic, scid, is, osh ) ;
179         }
180     } catch (MARSHAL JavaDoc mexc) {
181         // XXX log this error
182
// ignore this: error handled below because oktemp == null
183
}
184
185     if (oktemp == null)
186         // If we did not successfully construct a oktemp, reset the
187
// stream so that WireObjectKeyTemplate can correctly construct the
188
// object key.
189
try {
190         is.reset() ;
191         } catch (IOException JavaDoc exc) {
192         // XXX log this error
193
// ignore this
194
}
195
196     return oktemp ;
197     }
198
199     public ObjectKey create( byte[] key )
200     {
201     OctetSeqHolder JavaDoc osh = new OctetSeqHolder JavaDoc() ;
202     EncapsInputStream is = new EncapsInputStream( orb, key, key.length ) ;
203
204     ObjectKeyTemplate oktemp = create( is, fullKey, osh ) ;
205     if (oktemp == null)
206         oktemp = new WireObjectKeyTemplate( is, osh ) ;
207
208     ObjectId oid = new ObjectIdImpl( osh.value ) ;
209     return new ObjectKeyImpl( oktemp, oid ) ;
210     }
211
212     public ObjectKeyTemplate createTemplate( InputStream JavaDoc is )
213     {
214     ObjectKeyTemplate oktemp = create( is, oktempOnly, null ) ;
215     if (oktemp == null)
216         oktemp = new WireObjectKeyTemplate( orb ) ;
217
218     return oktemp ;
219     }
220 }
221
Popular Tags