You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2006/01/27 14:01:25 UTC

svn commit: r372831 [1/2] - /incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/

Author: rickhall
Date: Fri Jan 27 05:01:17 2006
New Revision: 372831

URL: http://svn.apache.org/viewcvs?rev=372831&view=rev
Log:
These changes represent the actual policy implementations for an OSGi R4
framework and, as such, form the core of Felix. To a large degree this code
has not changed too much; it has just been rearranged and refactored where
necessary.

Added:
    incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentClassLoader.java   (with props)
    incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java   (with props)
    incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveException.java   (with props)
    incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveListener.java   (with props)
    incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/SearchPolicyImpl.java
      - copied, changed from r370894, incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicy.java
    incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/URLPolicyImpl.java   (with props)
Removed:
    incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicy.java

Added: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentClassLoader.java
URL: http://svn.apache.org/viewcvs/incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentClassLoader.java?rev=372831&view=auto
==============================================================================
--- incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentClassLoader.java (added)
+++ incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentClassLoader.java Fri Jan 27 05:01:17 2006
@@ -0,0 +1,195 @@
+package org.apache.felix.framework.searchpolicy;
+
+import java.net.URL;
+import java.security.CodeSource;
+import java.security.SecureClassLoader;
+import java.security.cert.Certificate;
+
+import org.apache.felix.moduleloader.*;
+
+public class ContentClassLoader extends SecureClassLoader
+{
+    private ContentLoaderImpl m_contentLoader = null;
+
+    ContentClassLoader(ContentLoaderImpl contentLoader)
+    {
+        m_contentLoader = contentLoader;
+    }
+
+    public IContentLoader getContentLoader()
+    {
+        return m_contentLoader;
+    }
+
+    public Class loadClassLocal(String name)
+        throws ClassNotFoundException
+    {
+        // Ask the search policy for the clas before consulting the module.
+        Class clazz = findClass(name);
+
+        // If not found, then throw an exception.
+        if (clazz == null)
+        {
+            throw new ClassNotFoundException(name);
+        }
+        resolveClass(clazz);
+        return clazz;
+    }
+
+    protected Class loadClass(String name, boolean resolve)
+        throws ClassNotFoundException
+    {
+        // Make sure the class was not already loaded.
+        Class clazz = findLoadedClass(name);
+
+        // Ask the search policy for the clas before consulting the module.
+        clazz = m_contentLoader.getModuleCallback().findClass(name);
+
+        // If still not found, then throw an exception.
+        if (clazz == null)
+        {
+            throw new ClassNotFoundException(name);
+        }
+        // Otherwise resolve the class.
+        if (resolve)
+        {
+            resolveClass(clazz);
+        }
+        return clazz;
+    }
+
+    protected Class findClass(String name) throws ClassNotFoundException
+    {
+        Class clazz = findLoadedClass(name);
+
+        // Search for class in module.
+        if (clazz == null)
+        {
+            String actual = name.replace('.', '/') + ".class";
+            byte[] bytes = null;
+
+            // Check the module class path.
+            for (int i = 0;
+                (bytes == null) &&
+                (i < m_contentLoader.getClassPath().length); i++)
+            {
+                bytes = m_contentLoader.getClassPath()[i].getEntry(actual);
+            }
+
+            if (bytes != null)
+            {
+                // We need to try to define a Package object for the class
+                // before we call defineClass(). Get the package name and
+                // see if we have already created the package.
+                String pkgName = Util.getClassPackage(name);
+                if (pkgName.length() > 0)
+                {
+                    if (getPackage(pkgName) == null)
+                    {
+                        Object[] params = null;
+// TODO: FIX PACKAGE POLICY.
+//                        Object[] params =
+//                            m_mgr.getSearchPolicy().definePackage(m_module, pkgName);
+                        if (params != null)
+                        {
+                            definePackage(
+                                pkgName,
+                                (String) params[0],
+                                (String) params[1],
+                                (String) params[2],
+                                (String) params[3],
+                                (String) params[4],
+                                (String) params[5],
+                                null);
+                        }
+                        else
+                        {
+                            definePackage(pkgName, null, null,
+                                null, null, null, null, null);
+                        }
+                    }
+                }
+
+                // Get the code source URL for this class. For concurrency
+                // purposes, we are performing this call outside of the
+                // synchronized block below since we call out to application
+                // code, which might in turn need to call back into the
+                // module loader code. Because of this, it is better to
+                // not be holding any locks before making the call.
+                URL url = null;
+// TODO: FIX CODE SOURCE URL
+//                URL url = m_mgr.getURLPolicy().createCodeSourceURL(
+//                    m_mgr, m_module);
+
+                // If we have a valid code source URL, then use it to
+                // define the class for security purposes, otherwise
+                // define the class without a code source.
+                if (url != null)
+                {
+                    CodeSource cs = new CodeSource(url, (Certificate[]) null);
+                    clazz = defineClass(name, bytes, 0, bytes.length, cs);
+                }
+                else
+                {
+                    clazz = defineClass(name, bytes, 0, bytes.length);
+                }
+            }
+        }
+
+        if (clazz != null)
+        {
+            return clazz;
+        }
+
+        return null;
+    }
+
+    public URL getResourceLocal(String name)
+    {
+        try
+        {
+            return findResource(name);
+        }
+        catch (Throwable th)
+        {
+            // Ignore and just return null.
+        }
+        return null;
+    }
+
+    public URL getResource(String name)
+    {
+System.out.println("ContentClassLoader.getResource("+name+") for " + m_contentLoader);
+        // Ask the search policy for the clas before consulting the module.
+        try
+        {
+            return m_contentLoader.getModuleCallback().findResource(name);
+        }
+        catch (ResourceNotFoundException ex)
+        {
+// TODO: FIX LOGGING.
+            ex.printStackTrace();
+            return null;
+        }
+    }
+
+    protected URL findResource(String name)
+    {
+System.out.println("ContentClassLoader.findResource("+name+") for " + m_contentLoader);
+        URL url = null;
+
+        // Check the module class path.
+        for (int i = 0;
+            (url == null) &&
+            (i < m_contentLoader.getClassPath().length); i++)
+        {
+            if (m_contentLoader.getClassPath()[i].hasEntry(name))
+            {
+                url = m_contentLoader.getModuleCallback().createURL(i + "/" + name);
+                System.out.println("URL = " + url);
+            }
+        }
+
+        return url;
+    }
+}
\ No newline at end of file

Propchange: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java
URL: http://svn.apache.org/viewcvs/incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java?rev=372831&view=auto
==============================================================================
--- incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java (added)
+++ incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java Fri Jan 27 05:01:17 2006
@@ -0,0 +1,91 @@
+package org.apache.felix.framework.searchpolicy;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.felix.moduleloader.*;
+
+public class ContentLoaderImpl implements IContentLoader
+{
+    private IContent m_content = null;
+    private IContent[] m_classPath = null;
+    private IModuleCallback m_callback = null;
+    private ContentClassLoader m_classLoader = null;
+
+    public ContentLoaderImpl(IContent content, IContent[] classPath)
+    {
+        m_content = content;
+        m_content.open();
+        m_classPath = (classPath == null) ? new IContent[0] : classPath;
+        for (int i = 0; i < m_classPath.length; i++)
+        {
+            // Open all class path contents.
+            m_classPath[i].open();
+        }
+    }
+
+    public IContent getContent()
+    {
+        return m_content;
+    }
+
+    public IContent[] getClassPath()
+    {
+        return m_classPath;
+    }
+
+    public void setModuleCallback(IModuleCallback callback)
+    {
+        m_callback = callback;
+    }
+
+    public IModuleCallback getModuleCallback()
+    {
+        return m_callback;
+    }
+
+    public Class getClass(String name)
+    {
+        if (m_classLoader == null)
+        {
+            m_classLoader = new ContentClassLoader(this);
+        }
+
+        try
+        {
+            return m_classLoader.loadClassLocal(name);
+        }
+        catch (ClassNotFoundException ex)
+        {
+            return null;
+        }
+    }
+
+    public URL getResource(String name)
+    {
+        if (m_classLoader == null)
+        {
+            m_classLoader = new ContentClassLoader(this);
+        }
+
+        return m_classLoader.getResourceLocal(name);
+    }
+
+    public InputStream getResourceAsStream(String name)
+        throws IOException
+    {
+        InputStream is = null;
+        for (int i = 0; (is == null) && (i < m_classPath.length); i++)
+        {
+            is = m_classPath[i].getEntryAsStream(name);
+        }
+        return is;
+    }
+
+// TODO: FIX - REMOVE.
+    public String toString()
+    {
+        return m_callback.toString();
+    }
+}
\ No newline at end of file

Propchange: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveException.java
URL: http://svn.apache.org/viewcvs/incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveException.java?rev=372831&view=auto
==============================================================================
--- incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveException.java (added)
+++ incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveException.java Fri Jan 27 05:01:17 2006
@@ -0,0 +1,60 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.felix.framework.searchpolicy;
+
+import org.apache.felix.moduleloader.IModule;
+import org.apache.felix.moduleloader.IPackage;
+
+/**
+ * <p>
+ * This exception is thrown if a module cannot be resolved. The module
+ * that failed to be resolved is recorded, along with the failed import target
+ * identifier and version number. If the error was a result of a propagation
+ * conflict, then the propagation error flag is set.
+ * </p>
+ * @see org.apache.felix.moduleloader.search.ImportSearchPolicy#validate(org.apache.felix.moduleloader.Module)
+**/
+public class ResolveException extends Exception
+{
+    private IModule m_module = null;
+    private IPackage m_pkg = null;
+
+    /**
+     * Constructs an exception with the specified message, module,
+     * import identifier, import version number, and propagation flag.
+    **/
+    public ResolveException(String msg, IModule module, IPackage pkg)
+    {
+        super(msg);
+        m_module = module;
+        m_pkg = pkg;
+    }
+
+    /**
+     * Returns the module that was being resolved.
+     * @return the module that was being resolved.
+    **/
+    public IModule getModule()
+    {
+        return m_module;
+    }
+
+    public IPackage getPackage()
+    {
+        return m_pkg;
+    }
+}
\ No newline at end of file

Propchange: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveException.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveListener.java
URL: http://svn.apache.org/viewcvs/incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveListener.java?rev=372831&view=auto
==============================================================================
--- incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveListener.java (added)
+++ incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveListener.java Fri Jan 27 05:01:17 2006
@@ -0,0 +1,46 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.felix.framework.searchpolicy;
+
+import java.util.EventListener;
+
+import org.apache.felix.moduleloader.ModuleEvent;
+
+/**
+ * <p>
+ * This is an event listener interface for listening to resolution
+ * events that are generated by the <tt>R4SearchPolicy</tt>. Events
+ * are fired when a module is resolved and when it is unresolved.
+ * </p>
+ * @see org.apache.felix.framework.searchpolicy.SearchPolicyImpl
+**/
+public interface ResolveListener extends EventListener
+{
+    /**
+     * This is an event callback method that indicates that
+     * a module was resolved.
+     * @param event the module event containing the event data.
+    **/
+    public void moduleResolved(ModuleEvent event);
+
+    /**
+     * This is an event callback method that indicates that
+     * a module was unresolved.
+     * @param event the module event containing the event data.
+    **/
+    public void moduleUnresolved(ModuleEvent event);
+}
\ No newline at end of file

Propchange: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/felix/sandbox/rickhall/framework-branch/src/main/java/org/apache/felix/framework/searchpolicy/ResolveListener.java
------------------------------------------------------------------------------
    svn:executable = *