KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > encoding > CachedCodeBase


1 /*
2  * @(#)CachedCodeBase.java 1.6 04/06/21
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.encoding;
9
10 import java.util.Hashtable JavaDoc;
11 import com.sun.org.omg.CORBA.ValueDefPackage.FullValueDescription;
12 import com.sun.org.omg.SendingContext.CodeBase;
13 import com.sun.org.omg.SendingContext.CodeBaseHelper;
14 import com.sun.org.omg.SendingContext._CodeBaseImplBase;
15 import com.sun.org.omg.SendingContext._CodeBaseStub;
16 import com.sun.corba.se.spi.transport.CorbaConnection;
17
18 /**
19  * Provides the reading side with a per connection cache of
20  * info obtained via calls to the remote CodeBase.
21  *
22  * Previously, most of this was in IIOPConnection.
23  *
24  * Features:
25  * Delays cache creation unless used
26  * Postpones remote calls until necessary
27  * Handles creating obj ref from IOR
28  * Maintains caches for the following maps:
29  * CodeBase IOR to obj ref (global)
30  * RepId to implementation URL(s)
31  * RepId to remote FVD
32  * RepId to superclass type list
33  *
34  * Needs cache management.
35  */

36 // REVISIT: revert to package protected after framework merge.
37
public class CachedCodeBase extends _CodeBaseImplBase
38 {
39     private Hashtable JavaDoc implementations, fvds, bases;
40     private CodeBase delegate;
41     private CorbaConnection conn;
42
43     private static Hashtable JavaDoc iorToCodeBaseObjMap = new Hashtable JavaDoc();
44
45     public CachedCodeBase(CorbaConnection connection) {
46         conn = connection;
47     }
48
49     public com.sun.org.omg.CORBA.Repository get_ir () {
50         return null;
51     }
52         
53     public String JavaDoc implementation (String JavaDoc repId) {
54         String JavaDoc urlResult = null;
55
56         if (implementations == null)
57             implementations = new Hashtable JavaDoc();
58         else
59             urlResult = (String JavaDoc)implementations.get(repId);
60
61         if (urlResult == null && connectedCodeBase()) {
62             urlResult = delegate.implementation(repId);
63
64             if (urlResult != null)
65                 implementations.put(repId, urlResult);
66         }
67
68         return urlResult;
69     }
70
71     public String JavaDoc[] implementations (String JavaDoc[] repIds) {
72         String JavaDoc[] urlResults = new String JavaDoc[repIds.length];
73
74         for (int i = 0; i < urlResults.length; i++)
75             urlResults[i] = implementation(repIds[i]);
76
77         return urlResults;
78     }
79
80     public FullValueDescription meta (String JavaDoc repId) {
81         FullValueDescription result = null;
82
83         if (fvds == null)
84             fvds = new Hashtable JavaDoc();
85         else
86             result = (FullValueDescription)fvds.get(repId);
87
88         if (result == null && connectedCodeBase()) {
89             result = delegate.meta(repId);
90
91             if (result != null)
92                 fvds.put(repId, result);
93         }
94
95         return result;
96     }
97
98     public FullValueDescription[] metas (String JavaDoc[] repIds) {
99         FullValueDescription[] results
100             = new FullValueDescription[repIds.length];
101
102         for (int i = 0; i < results.length; i++)
103             results[i] = meta(repIds[i]);
104
105         return results;
106     }
107
108     public String JavaDoc[] bases (String JavaDoc repId) {
109
110         String JavaDoc[] results = null;
111
112         if (bases == null)
113             bases = new Hashtable JavaDoc();
114         else
115             results = (String JavaDoc[])bases.get(repId);
116
117         if (results == null && connectedCodeBase()) {
118             results = delegate.bases(repId);
119
120             if (results != null)
121                 bases.put(repId, results);
122         }
123
124         return results;
125     }
126
127     // Ensures that we've used the connection's IOR to create
128
// a valid CodeBase delegate. If this returns false, then
129
// it is not valid to access the delegate.
130
private boolean connectedCodeBase() {
131         if (delegate != null)
132             return true;
133
134         // The delegate was null, so see if the connection's
135
// IOR was set. If so, then we just need to connect
136
// it. Otherwise, there is no hope of checking the
137
// remote code base. That could be bug if the
138
// service context processing didn't occur, or it
139
// could be that we're talking to a foreign ORB which
140
// doesn't include this optional service context.
141
if (conn.getCodeBaseIOR() == null) {
142             // REVISIT. Use Merlin logging service to report that
143
// codebase functionality was requested but unavailable.
144
if (conn.getBroker().transportDebugFlag)
145                 conn.dprint("CodeBase unavailable on connection: " + conn);
146
147             return false;
148         }
149
150         synchronized(this) {
151
152             // Recheck the condition to make sure another
153
// thread didn't already do this while we waited
154
if (delegate != null)
155                 return true;
156
157             // Do we have a reference initialized by another connection?
158
delegate = (CodeBase)CachedCodeBase.iorToCodeBaseObjMap.get(conn.getCodeBaseIOR());
159             if (delegate != null)
160                 return true;
161             
162             // Connect the delegate and update the cache
163
delegate = CodeBaseHelper.narrow(getObjectFromIOR());
164             
165             // Save it for the benefit of other connections
166
CachedCodeBase.iorToCodeBaseObjMap.put(conn.getCodeBaseIOR(),
167                                                    delegate);
168         }
169
170         // It's now safe to use the delegate
171
return true;
172     }
173
174     private final org.omg.CORBA.Object JavaDoc getObjectFromIOR() {
175         return CDRInputStream_1_0.internalIORToObject(
176         conn.getCodeBaseIOR(), null /*stubFactory*/, conn.getBroker());
177     }
178 }
179
180 // End of file.
181

182
Popular Tags