Package org.apache.yoko.rmispec.util

Source Code of org.apache.yoko.rmispec.util.UtilLoader$SecMan

/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You under the Apache License, Version 2.0
*  (the "License"); you may not use this file except in compliance with
*  the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.apache.yoko.rmispec.util;

import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Logger;

public class UtilLoader {
    static final Logger logger = Logger.getLogger(UtilLoader.class.getName());

    // Note: this field must be declared before the static intializer that calls Util.loadClass
    // since that method will call loadClass0 which uses this field... if it is below the static
    // initializer the _secman field will be null
    private static final SecMan _secman = new SecMan();

    static public Class loadClass(String name, String codebase, ClassLoader loader)
            throws ClassNotFoundException {
        Class result = null;

        if (loader != null) {
            try {
                result = loader.loadClass(name);
            } catch (ClassNotFoundException ex) {
                // skip //
            }

            if (result != null)
                return result;
        }

        ClassLoader stackLoader = null;
        Class[] stack = _secman.getClassContext();
        for (int i = 1; i < stack.length; i++) {
            stackLoader = stack[i].getClassLoader();
            if (stackLoader != null)
                break;
        }

        if (stackLoader != null) {
            try {
                result = stackLoader.loadClass(name);
            } catch (ClassNotFoundException ex) {
                // skip //
            }

            if (result != null) {
                return result;
            }
        }

        if (codebase != null && !"".equals(codebase)
                && !Boolean.getBoolean("java.rmi.server.useCodeBaseOnly")) {
            try {
                logger.finer("trying RMIClassLoader");

                URLClassLoader url_loader = new URLClassLoader(
                        new URL[] { new URL(codebase) }, loader);

                result = url_loader.loadClass(name);

                // log.info("SUCESSFUL class download "+name+" from "+codebase,
                // new Throwable("TRACE"));

            } catch (ClassNotFoundException ex) {
                logger.finer("RMIClassLoader says " + ex);

                // log.info("FAILED class download "+name+" from "+codebase,
                // ex);

                // skip //
            } catch (MalformedURLException ex) {
                logger.finer("RMIClassLoader says " + ex);

                logger.finer("FAILED class download " + name + " from "
                        + codebase + " " + ex);

                // skip //
            } catch (RuntimeException ex) {

                logger.finer("FAILED class download " + name + " from "
                        + codebase + " " + ex);

            }

            if (result != null) {
                return result;
            }

        } else {

            codebase = (String)AccessController.doPrivileged(new GetSystemPropertyAction("java.rmi.server.codebase"));

            if (codebase != null) {
                try {
                    result = java.rmi.server.RMIClassLoader.loadClass(codebase,
                            name);
                } catch (ClassNotFoundException ex) {
                    // skip //
                } catch (MalformedURLException ex) {
                    // skip //
                }

                if (result != null) {
                    return result;
                }
            }
        }

        if (loader == null) {
            loader = getContextClassLoader();
        }

        try {
            result = loader.loadClass(name);
        } catch (ClassNotFoundException ex) {
            logger.finer("LocalLoader says " + ex);
        }

        if (result != null) {
            return result;
        }

        throw new ClassNotFoundException(name);
    }

    static ClassLoader getContextClassLoader() {
        return (ClassLoader) AccessController
                .doPrivileged(new PrivilegedAction() {
                    public Object run() {
                        return Thread.currentThread().getContextClassLoader();
                    }
                });
    }


    static class SecMan extends java.rmi.RMISecurityManager {
        public Class[] getClassContext() {
            return super.getClassContext();
        }
    }
}


TOP

Related Classes of org.apache.yoko.rmispec.util.UtilLoader$SecMan

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.