StubFactoryFactoryStaticImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.presentation.rmi;
27
28import javax.rmi.CORBA.Util;
29import javax.rmi.CORBA.Tie ;
30
31import org.omg.CORBA.CompletionStatus;
32
33import com.sun.corba.se.spi.presentation.rmi.PresentationManager;
34
35import com.sun.corba.se.impl.util.PackagePrefixChecker;
36import com.sun.corba.se.impl.util.Utility;
37
38import com.sun.corba.se.spi.logging.CORBALogDomains ;
39
40import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
41
42public class StubFactoryFactoryStaticImpl extends
43    StubFactoryFactoryBase
44{
45    private ORBUtilSystemException wrapper = ORBUtilSystemException.get(
46        CORBALogDomains.RPC_PRESENTATION ) ;
47
48    public PresentationManager.StubFactory createStubFactory(
49        String className, boolean isIDLStub, String remoteCodeBase, Class
50        expectedClass, ClassLoader classLoader)
51    {
52        String stubName = null ;
53
54        if (isIDLStub)
55            stubName = Utility.idlStubName( className ) ;
56        else
57            stubName = Utility.stubNameForCompiler( className ) ;
58
59        ClassLoader expectedTypeClassLoader =
60            (expectedClass == null ? classLoader :
61            expectedClass.getClassLoader());
62
63        // The old code was optimized to try to guess which way to load classes
64        // first.  The real stub class name could either be className or
65        // "org.omg.stub." + className.  We will compute this as follows:
66        // If stubName starts with a "forbidden" package, try the prefixed
67        // version first, otherwise try the non-prefixed version first.
68        // In any case, try both forms if necessary.
69
70        String firstStubName = stubName ;
71        String secondStubName = stubName ;
72
73        if (PackagePrefixChecker.hasOffendingPrefix(stubName))
74            firstStubName = PackagePrefixChecker.packagePrefix() + stubName ;
75        else
76            secondStubName = PackagePrefixChecker.packagePrefix() + stubName ;
77
78        Class clz = null;
79
80        try {
81            clz = Util.loadClass( firstStubName, remoteCodeBase,
82                expectedTypeClassLoader ) ;
83        } catch (ClassNotFoundException e1) {
84            // log only at FINE level
85            wrapper.classNotFound1( CompletionStatus.COMPLETED_MAYBE,
86                e1, firstStubName ) ;
87            try {
88                clz = Util.loadClass( secondStubName, remoteCodeBase,
89                    expectedTypeClassLoader ) ;
90            } catch (ClassNotFoundException e2) {
91                throw wrapper.classNotFound2(
92                    CompletionStatus.COMPLETED_MAYBE, e2, secondStubName ) ;
93            }
94        }
95
96        // XXX Is this step necessary, or should the Util.loadClass
97        // algorithm always produce a valid class if the setup is correct?
98        // Does the OMG standard algorithm need to be changed to include
99        // this step?
100        if ((clz == null) ||
101            ((expectedClass != null) && !expectedClass.isAssignableFrom(clz))) {
102            try {
103                ClassLoader cl = Thread.currentThread().getContextClassLoader();
104                if (cl == null)
105                    cl = ClassLoader.getSystemClassLoader();
106
107                clz = cl.loadClass(className);
108            } catch (Exception exc) {
109                // XXX make this a system exception
110                IllegalStateException ise = new IllegalStateException(
111                    "Could not load class " + stubName ) ;
112                ise.initCause( exc ) ;
113                throw ise ;
114            }
115        }
116
117        return new StubFactoryStaticImpl( clz ) ;
118    }
119
120    public Tie getTie( Class cls )
121    {
122        Class tieClass = null ;
123        String className = Utility.tieName(cls.getName());
124
125        // XXX log exceptions at FINE level
126        try {
127            try {
128                //_REVISIT_ The spec does not specify a loadingContext parameter for
129                //the following call.  Would it be useful to pass one?
130                tieClass = Utility.loadClassForClass(className, Util.getCodebase(cls),
131                    null, cls, cls.getClassLoader());
132                return (Tie) tieClass.newInstance();
133            } catch (Exception err) {
134                tieClass = Utility.loadClassForClass(
135                    PackagePrefixChecker.packagePrefix() + className,
136                    Util.getCodebase(cls), null, cls, cls.getClassLoader());
137                return (Tie) tieClass.newInstance();
138            }
139        } catch (Exception err) {
140            return null;
141        }
142
143    }
144
145    public boolean createsDynamicStubs()
146    {
147        return false ;
148    }
149}
150