001: /*
002: * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.corba.se.impl.ior.iiop;
027:
028: import java.util.List;
029: import java.util.Iterator;
030:
031: import org.omg.CORBA.SystemException;
032:
033: import org.omg.CORBA_2_3.portable.OutputStream;
034: import org.omg.CORBA_2_3.portable.InputStream;
035:
036: import org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS;
037: import org.omg.IOP.TAG_INTERNET_IOP;
038: import org.omg.IOP.TAG_JAVA_CODEBASE;
039:
040: import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry;
041:
042: import com.sun.corba.se.spi.oa.ObjectAdapter;
043: import com.sun.corba.se.spi.oa.ObjectAdapterFactory;
044:
045: import com.sun.corba.se.spi.ior.ObjectId;
046: import com.sun.corba.se.spi.ior.ObjectAdapterId;
047: import com.sun.corba.se.spi.ior.TaggedProfile;
048: import com.sun.corba.se.spi.ior.TaggedProfileTemplate;
049: import com.sun.corba.se.spi.ior.ObjectKey;
050: import com.sun.corba.se.spi.ior.ObjectKeyTemplate;
051: import com.sun.corba.se.spi.ior.TaggedComponent;
052: import com.sun.corba.se.spi.ior.IdentifiableBase;
053: import com.sun.corba.se.spi.ior.IORFactories;
054: import com.sun.corba.se.spi.ior.ObjectKeyFactory;
055:
056: import com.sun.corba.se.spi.ior.iiop.IIOPAddress;
057: import com.sun.corba.se.spi.ior.iiop.IIOPProfile;
058: import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate;
059: import com.sun.corba.se.spi.ior.iiop.IIOPFactories;
060: import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
061: import com.sun.corba.se.spi.ior.iiop.JavaCodebaseComponent;
062:
063: import com.sun.corba.se.spi.orb.ORB;
064: import com.sun.corba.se.spi.orb.ORBVersion;
065:
066: import com.sun.corba.se.spi.logging.CORBALogDomains;
067:
068: import com.sun.corba.se.impl.ior.EncapsulationUtility;
069:
070: import com.sun.corba.se.impl.encoding.EncapsInputStream;
071: import com.sun.corba.se.impl.encoding.EncapsOutputStream;
072:
073: import com.sun.corba.se.impl.util.JDKBridge;
074:
075: import com.sun.corba.se.impl.logging.IORSystemException;
076:
077: /**
078: * @author
079: */
080: public class IIOPProfileImpl extends IdentifiableBase implements
081: IIOPProfile {
082: private ORB orb;
083: private IORSystemException wrapper;
084: private ObjectId oid;
085: private IIOPProfileTemplate proftemp;
086: private ObjectKeyTemplate oktemp;
087:
088: // Cached lookups
089: protected String codebase = null;
090: protected boolean cachedCodebase = false;
091:
092: private boolean checkedIsLocal = false;
093: private boolean cachedIsLocal = false;
094:
095: // initialize-on-demand holder
096: private static class LocalCodeBaseSingletonHolder {
097: public static JavaCodebaseComponent comp;
098:
099: static {
100: String localCodebase = JDKBridge.getLocalCodebase();
101: if (localCodebase == null)
102: comp = null;
103: else
104: comp = IIOPFactories
105: .makeJavaCodebaseComponent(localCodebase);
106: }
107: }
108:
109: private GIOPVersion giopVersion = null;
110:
111: public boolean equals(Object obj) {
112: if (!(obj instanceof IIOPProfileImpl))
113: return false;
114:
115: IIOPProfileImpl other = (IIOPProfileImpl) obj;
116:
117: return oid.equals(other.oid) && proftemp.equals(other.proftemp)
118: && oktemp.equals(other.oktemp);
119: }
120:
121: public int hashCode() {
122: return oid.hashCode() ^ proftemp.hashCode() ^ oktemp.hashCode();
123: }
124:
125: public ObjectId getObjectId() {
126: return oid;
127: }
128:
129: public TaggedProfileTemplate getTaggedProfileTemplate() {
130: return proftemp;
131: }
132:
133: public ObjectKeyTemplate getObjectKeyTemplate() {
134: return oktemp;
135: }
136:
137: private IIOPProfileImpl(ORB orb) {
138: this .orb = orb;
139: wrapper = IORSystemException.get(orb, CORBALogDomains.OA_IOR);
140: }
141:
142: public IIOPProfileImpl(ORB orb, ObjectKeyTemplate oktemp,
143: ObjectId oid, IIOPProfileTemplate proftemp) {
144: this (orb);
145: this .oktemp = oktemp;
146: this .oid = oid;
147: this .proftemp = proftemp;
148: }
149:
150: public IIOPProfileImpl(InputStream is) {
151: this ((ORB) (is.orb()));
152: init(is);
153: }
154:
155: public IIOPProfileImpl(ORB orb, org.omg.IOP.TaggedProfile profile) {
156: this (orb);
157:
158: if (profile == null || profile.tag != TAG_INTERNET_IOP.value
159: || profile.profile_data == null) {
160: throw wrapper.invalidTaggedProfile();
161: }
162:
163: EncapsInputStream istr = new EncapsInputStream((ORB) orb,
164: profile.profile_data, profile.profile_data.length);
165: istr.consumeEndian();
166: init(istr);
167: }
168:
169: private void init(InputStream istr) {
170: // First, read all of the IIOP IOR data
171: GIOPVersion version = new GIOPVersion();
172: version.read(istr);
173: IIOPAddress primary = new IIOPAddressImpl(istr);
174: byte[] key = EncapsulationUtility.readOctets(istr);
175:
176: ObjectKey okey = orb.getObjectKeyFactory().create(key);
177: oktemp = okey.getTemplate();
178: oid = okey.getId();
179:
180: proftemp = IIOPFactories.makeIIOPProfileTemplate(orb, version,
181: primary);
182:
183: // Handle any tagged components (if applicable)
184: if (version.getMinor() > 0)
185: EncapsulationUtility.readIdentifiableSequence(proftemp, orb
186: .getTaggedComponentFactoryFinder(), istr);
187:
188: // If there is no codebase in this IOR and there IS a
189: // java.rmi.server.codebase property set, we need to
190: // update the IOR with the local codebase. Note that
191: // there is only one instance of the local codebase, but it
192: // can be safely shared in multiple IORs since it is immutable.
193: if (uncachedGetCodeBase() == null) {
194: JavaCodebaseComponent jcc = LocalCodeBaseSingletonHolder.comp;
195:
196: if (jcc != null) {
197: if (version.getMinor() > 0)
198: proftemp.add(jcc);
199:
200: codebase = jcc.getURLs();
201: }
202:
203: // Whether codebase is null or not, we have it,
204: // and so getCodebase ned never call uncachedGetCodebase.
205: cachedCodebase = true;
206: }
207: }
208:
209: public void writeContents(OutputStream os) {
210: proftemp.write(oktemp, oid, os);
211: }
212:
213: public int getId() {
214: return proftemp.getId();
215: }
216:
217: public boolean isEquivalent(TaggedProfile prof) {
218: if (!(prof instanceof IIOPProfile))
219: return false;
220:
221: IIOPProfile other = (IIOPProfile) prof;
222:
223: return oid.equals(other.getObjectId())
224: && proftemp.isEquivalent(other
225: .getTaggedProfileTemplate())
226: && oktemp.equals(other.getObjectKeyTemplate());
227: }
228:
229: public ObjectKey getObjectKey() {
230: ObjectKey result = IORFactories.makeObjectKey(oktemp, oid);
231: return result;
232: }
233:
234: public org.omg.IOP.TaggedProfile getIOPProfile() {
235: EncapsOutputStream os = new EncapsOutputStream(orb);
236: os.write_long(getId());
237: write(os);
238: InputStream is = (InputStream) (os.create_input_stream());
239: return org.omg.IOP.TaggedProfileHelper.read(is);
240: }
241:
242: private String uncachedGetCodeBase() {
243: Iterator iter = proftemp.iteratorById(TAG_JAVA_CODEBASE.value);
244:
245: if (iter.hasNext()) {
246: JavaCodebaseComponent jcbc = (JavaCodebaseComponent) (iter
247: .next());
248: return jcbc.getURLs();
249: }
250:
251: return null;
252: }
253:
254: public synchronized String getCodebase() {
255: if (!cachedCodebase) {
256: cachedCodebase = true;
257: codebase = uncachedGetCodeBase();
258: }
259:
260: return codebase;
261: }
262:
263: /**
264: * @return the ORBVersion associated with the object key in the IOR.
265: */
266: public ORBVersion getORBVersion() {
267: return oktemp.getORBVersion();
268: }
269:
270: public synchronized boolean isLocal() {
271: if (!checkedIsLocal) {
272: checkedIsLocal = true;
273: String host = proftemp.getPrimaryAddress().getHost();
274:
275: cachedIsLocal = orb.isLocalHost(host)
276: && orb.isLocalServerId(oktemp.getSubcontractId(),
277: oktemp.getServerId())
278: && orb.getLegacyServerSocketManager()
279: .legacyIsLocalServerPort(
280: proftemp.getPrimaryAddress()
281: .getPort());
282: }
283:
284: return cachedIsLocal;
285: }
286:
287: /** Return the servant for this IOR, if it is local AND if the OA that
288: * implements this objref supports direct access to servants outside of an
289: * invocation.
290: * XXX revisit: do we want this at all? If we do, it might move to the
291: * ObjectKeyTemplate instead.
292: */
293: public java.lang.Object getServant() {
294: if (!isLocal())
295: return null;
296:
297: RequestDispatcherRegistry scr = orb
298: .getRequestDispatcherRegistry();
299: ObjectAdapterFactory oaf = scr.getObjectAdapterFactory(oktemp
300: .getSubcontractId());
301:
302: ObjectAdapterId oaid = oktemp.getObjectAdapterId();
303: ObjectAdapter oa = null;
304:
305: try {
306: oa = oaf.find(oaid);
307: } catch (SystemException exc) {
308: // Could not find the OA, so just return null.
309: // This usually happens when POAs are being deleted,
310: // and the POA always return null for getLocalServant anyway.
311: wrapper.getLocalServantFailure(exc, oaid.toString());
312: return null;
313: }
314:
315: byte[] boid = oid.getId();
316: java.lang.Object servant = oa.getLocalServant(boid);
317: return servant;
318: }
319:
320: /**
321: * Return GIOPVersion for this IOR.
322: * Requests created against this IOR will be of the
323: * return Version.
324: */
325: public synchronized GIOPVersion getGIOPVersion() {
326: return proftemp.getGIOPVersion();
327: }
328:
329: public void makeImmutable() {
330: proftemp.makeImmutable();
331: }
332: }
|