Initial source commit.


git-svn-id: https://svn.apache.org/repos/asf/incubator/oscar/trunk@233031 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/org/apache/osgi/framework/BundleContextImpl.java b/src/org/apache/osgi/framework/BundleContextImpl.java
new file mode 100644
index 0000000..e0114d2
--- /dev/null
+++ b/src/org/apache/osgi/framework/BundleContextImpl.java
@@ -0,0 +1,284 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Dictionary;
+
+import org.apache.osgi.framework.ext.FelixBundleContext;
+import org.osgi.framework.*;
+
+class BundleContextImpl implements FelixBundleContext
+{
+    private Felix m_felix = null;
+    private BundleImpl m_bundle = null;
+
+    protected BundleContextImpl(Felix felix, BundleImpl bundle)
+    {
+        m_felix = felix;
+        m_bundle = bundle;
+    }
+
+    public void addImportPackage() throws BundleException
+    {
+        throw new BundleException("Not implemented yet.");
+    }
+
+    public void removeImportPackage() throws BundleException
+    {
+        throw new BundleException("Not implemented yet.");
+    }
+
+    public void addExportPackage() throws BundleException
+    {
+        throw new BundleException("Not implemented yet.");
+    }
+
+    public void removeExportPackage() throws BundleException
+    {
+        throw new BundleException("Not implemented yet.");
+    }
+
+    public String getProperty(String name)
+    {
+        return m_felix.getProperty(name);
+    }
+
+    public Bundle getBundle()
+    {
+        return m_bundle;
+    }
+
+    public Filter createFilter(String expr)
+        throws InvalidSyntaxException
+    {
+        return new FilterImpl(m_felix.getLogger(), expr);
+    }
+
+    public Bundle installBundle(String location)
+        throws BundleException
+    {
+        return installBundle(location, null);
+    }
+
+    public Bundle installBundle(String location, InputStream is)
+        throws BundleException
+    {
+        return m_felix.installBundle(location, is);
+    }
+
+    public Bundle getBundle(long id)
+    {
+        return m_felix.getBundle(id);
+    }
+
+    public Bundle[] getBundles()
+    {
+        return m_felix.getBundles();
+    }
+
+    public void addBundleListener(BundleListener l)
+    {
+        m_felix.addBundleListener(m_bundle, l);
+    }
+
+    public void removeBundleListener(BundleListener l)
+    {
+        m_felix.removeBundleListener(l);
+    }
+
+    public void addServiceListener(ServiceListener l)
+    {
+        try
+        {
+            addServiceListener(l, null);
+        }
+        catch (InvalidSyntaxException ex)
+        {
+            // This will not happen since the filter is null.
+        }
+    }
+
+    public void addServiceListener(ServiceListener l, String s)
+        throws InvalidSyntaxException
+    {
+        m_felix.addServiceListener(m_bundle, l, s);
+    }
+
+    public void removeServiceListener(ServiceListener l)
+    {
+        m_felix.removeServiceListener(l);
+    }
+
+    public void addFrameworkListener(FrameworkListener l)
+    {
+        m_felix.addFrameworkListener(m_bundle, l);
+    }
+
+    public void removeFrameworkListener(FrameworkListener l)
+    {
+        m_felix.removeFrameworkListener(l);
+    }
+
+    public ServiceRegistration registerService(
+        String clazz, Object svcObj, Dictionary dict)
+    {
+        return registerService(new String[] { clazz }, svcObj, dict);
+    }
+
+    public ServiceRegistration registerService(
+        String[] clazzes, Object svcObj, Dictionary dict)
+    {
+        return m_felix.registerService(m_bundle, clazzes, svcObj, dict);
+    }
+
+    public ServiceReference getServiceReference(String clazz)
+    {
+        try
+        {
+            ServiceReference[] refs = getServiceReferences(clazz, null);
+            return getBestServiceReference(refs);
+        }
+        catch (InvalidSyntaxException ex)
+        {
+            m_felix.getLogger().log(LogWrapper.LOG_ERROR, "BundleContextImpl: " + ex);
+        }
+        return null;
+    }
+
+    private ServiceReference getBestServiceReference(ServiceReference[] refs)
+    {
+        if (refs == null)
+        {
+            return null;
+        }
+
+        if (refs.length == 1)
+        {
+            return refs[0];
+        }
+
+        // Loop through all service references and return
+        // the "best" one according to its rank and ID.
+        ServiceReference bestRef = null;
+        Integer bestRank = null;
+        Long bestId = null;
+        for (int i = 0; i < refs.length; i++)
+        {
+            ServiceReference ref = refs[i];
+
+            // The first time through the loop just
+            // assume that the first reference is best.
+            if (bestRef == null)
+            {
+                bestRef = ref;
+                bestRank = (Integer) bestRef.getProperty("service.ranking");
+                // The spec says no ranking defaults to zero.
+                if (bestRank == null)
+                {
+                    bestRank = new Integer(0);
+                }
+                bestId = (Long) bestRef.getProperty("service.id");
+            }
+
+            // Compare current and best references to see if
+            // the current reference is a better choice.
+            Integer rank = (Integer) ref.getProperty("service.ranking");
+
+            // The spec says no ranking defaults to zero.
+            if (rank == null)
+            {
+                rank = new Integer(0);
+            }
+
+            // If the current reference ranking is greater than the
+            // best ranking, then keep the current reference.
+            if (bestRank.compareTo(rank) < 0)
+            {
+                bestRef = ref;
+                bestRank = rank;
+                bestId = (Long) bestRef.getProperty("service.id");
+            }
+            // If rankings are equal, then compare IDs and
+            // keep the smallest.
+            else if (bestRank.compareTo(rank) == 0)
+            {
+                Long id = (Long) ref.getProperty("service.id");
+                // If either reference has a null ID, then keep
+                // the one with a non-null ID.
+                if ((bestId == null) || (id == null))
+                {
+                    bestRef = (bestId == null) ? ref : bestRef;
+                    // bestRank = bestRank; // No need to update since they are equal.
+                    bestId = (Long) bestRef.getProperty("service.id");
+                }
+                // Otherwise compare IDs.
+                else
+                {
+                    // If the current reference ID is less than the
+                    // best ID, then keep the current reference.
+                    if (bestId.compareTo(id) > 0)
+                    {
+                        bestRef = ref;
+                        // bestRank = bestRank; // No need to update since they are equal.
+                        bestId = (Long) bestRef.getProperty("service.id");
+                    }
+                }
+            }
+        }
+
+        return bestRef;
+    }
+
+    public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException
+    {
+        // TODO: Implement BundleContext.getAllServiceReferences()
+        return null;
+    }
+
+    public ServiceReference[] getServiceReferences(String clazz, String filter)
+        throws InvalidSyntaxException
+    {
+        return m_felix.getServiceReferences(m_bundle, clazz, filter);
+    }
+
+    public Object getService(ServiceReference ref)
+    {
+        if (ref == null)
+        {
+            throw new NullPointerException("Specified service reference cannot be null.");
+        }
+        return m_felix.getService(m_bundle, ref);
+    }
+
+    public boolean ungetService(ServiceReference ref)
+    {
+        if (ref == null)
+        {
+            throw new NullPointerException("Specified service reference cannot be null.");
+        }
+
+        // Unget the specified service.
+        return m_felix.ungetService(m_bundle, ref);
+    }
+
+    public File getDataFile(String s)
+    {
+        return m_felix.getDataFile(m_bundle, s);
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/BundleImpl.java b/src/org/apache/osgi/framework/BundleImpl.java
new file mode 100644
index 0000000..0b1d056
--- /dev/null
+++ b/src/org/apache/osgi/framework/BundleImpl.java
@@ -0,0 +1,185 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Enumeration;
+
+import org.osgi.framework.*;
+
+class BundleImpl implements Bundle
+{
+    private Felix m_felix = null;
+    private BundleInfo m_info = null;
+
+    protected BundleImpl(Felix felix, BundleInfo info)
+    {
+        m_felix = felix;
+        m_info = info;
+    }
+
+    Felix getFelix() // package protected
+    {
+        return m_felix;
+    }
+
+    BundleInfo getInfo() // package protected
+    {
+        return m_info;
+    }
+
+    void setInfo(BundleInfo info) // package protected
+    {
+        m_info = info;
+    }
+
+    public long getBundleId()
+    {
+        return m_info.getBundleId();
+    }
+
+    public Dictionary getHeaders()
+    {
+        return m_felix.getBundleHeaders(this);
+    }
+
+    public String getLocation()
+    {
+        return m_felix.getBundleLocation(this);
+    }
+
+    /**
+     * Returns a URL to a named resource in the bundle.
+     *
+     * @return a URL to named resource, or null if not found.
+    **/
+    public URL getResource(String name)
+    {
+        return m_felix.getBundleResource(this, name);
+    }
+
+    /**
+     * Returns an array of service references corresponding to
+     * the bundle's registered services.
+     *
+     * @return an array of service references or null.
+    **/
+    public ServiceReference[] getRegisteredServices()
+    {
+        return m_felix.getBundleRegisteredServices(this);
+    }
+
+    public ServiceReference[] getServicesInUse()
+    {
+        return m_felix.getBundleServicesInUse(this);
+    }
+
+    public int getState()
+    {
+        return m_info.getState();
+    }
+
+    public boolean hasPermission(Object obj)
+    {
+        return m_felix.bundleHasPermission(this, obj);
+    }
+
+    public void start() throws BundleException
+    {
+        m_felix.startBundle(this, true);
+    }
+
+    public void update() throws BundleException
+    {
+        update(null);
+    }
+
+    public void update(InputStream is) throws BundleException
+    {
+        m_felix.updateBundle(this, is);
+    }
+
+    public void stop() throws BundleException
+    {
+        m_felix.stopBundle(this, true);
+    }
+
+    public void uninstall() throws BundleException
+    {
+        m_felix.uninstallBundle(this);
+    }
+
+    public String toString()
+    {
+        return "[" + getBundleId() +"]";
+    }
+
+    //
+    // PLACE FOLLOWING METHODS INTO PROPER LOCATION ONCE IMPLEMENTED.
+    //
+
+    public Dictionary getHeaders(String locale)
+    {
+        // TODO: Implement Bundle.getHeaders()
+        return null;
+    }
+
+    public String getSymbolicName()
+    {
+        // TODO: Implement Bundle.getSymbolicName()
+        return null;
+    }
+
+    public Class loadClass(String name) throws ClassNotFoundException
+    {
+        // TODO: Implement Bundle.loadClass()
+        return null;
+    }
+
+    public Enumeration getResources(String name) throws IOException
+    {
+        // TODO: Implement Bundle.getResources()
+        return null;
+    }
+
+    public Enumeration getEntryPaths(String path)
+    {
+        // TODO: Implement Bundle.getEntryPaths()
+        return null;
+    }
+
+    public URL getEntry(String name)
+    {
+        // TODO: Implement Bundle.getEntry()
+        return null;
+    }
+
+    public long getLastModified()
+    {
+        // TODO: Implement Bundle.getLastModified()
+        return 0;
+    }
+
+    public Enumeration findEntries(String path, String filePattern, boolean recurse)
+    {
+        // TODO: Implement Bundle.findEntries()
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/BundleInfo.java b/src/org/apache/osgi/framework/BundleInfo.java
new file mode 100644
index 0000000..5d589bb
--- /dev/null
+++ b/src/org/apache/osgi/framework/BundleInfo.java
@@ -0,0 +1,372 @@
+/*
+ *   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.osgi.framework;
+
+import java.util.Map;
+
+import org.apache.osgi.framework.cache.BundleArchive;
+import org.apache.osgi.moduleloader.Module;
+import org.osgi.framework.*;
+
+class BundleInfo
+{
+    private LogWrapper m_logger = null;
+    private BundleArchive m_archive = null;
+    private Module[] m_modules = null;
+    private int m_state = 0;
+    private BundleActivator m_activator = null;
+    private BundleContext m_context = null;
+    // Indicates that the bundle was either updated
+    // or uninstalled and is waiting to be removed or refreshed.
+    private boolean m_removalPending = false;
+
+    // Used for bundle locking.
+    private int m_lockCount = 0;
+    private Thread m_lockThread = null;
+
+    protected BundleInfo(LogWrapper logger, BundleArchive archive, Module module)
+        throws Exception
+    {
+        m_logger = logger;
+        m_archive = archive;
+        m_modules = (module == null) ? new Module[0] : new Module[] { module };
+
+        m_state = Bundle.INSTALLED;
+        m_removalPending = false;
+        m_activator = null;
+        m_context = null;
+    }
+
+    /**
+     *  Returns the bundle archive associated with this bundle.
+     * @return the bundle archive associated with this bundle.
+    **/
+    public BundleArchive getArchive()
+    {
+        return m_archive;
+    }
+
+    /**
+     * Returns an array of all modules associated with the bundle represented by
+     * this <tt>BundleInfo</tt> object. A module in the array corresponds to a
+     * revision of the bundle's JAR file and is ordered from oldest to newest.
+     * Multiple revisions of a bundle JAR file might exist if a bundle is
+     * updated, without refreshing the framework. In this case, exports from
+     * the prior revisions of the bundle JAR file are still offered; the
+     * current revision will be bound to packages from the prior revision,
+     * unless the packages were not offered by the prior revision. There is
+     * no limit on the potential number of bundle JAR file revisions.
+     * @return array of modules corresponding to the bundle JAR file revisions.
+    **/
+    public Module[] getModules()
+    {
+        return m_modules;
+    }
+
+    /**
+     * Determines if the specified module is associated with this bundle.
+     * @param module the module to determine if it is associate with this bundle.
+     * @return <tt>true</tt> if the specified module is in the array of modules
+     *         associated with this bundle, <tt>false</tt> otherwise.
+    **/
+    public boolean hasModule(Module module)
+    {
+        for (int i = 0; i < m_modules.length; i++)
+        {
+            if (m_modules[i] == module)
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns the newest module, which corresponds to the last module
+     * in the module array.
+     * @return the newest module.
+    **/
+    public Module getCurrentModule()
+    {
+        return m_modules[m_modules.length - 1];
+    }
+    
+    /**
+     * Add a module that corresponds to a new bundle JAR file revision for
+     * the bundle associated with this <tt>BundleInfo</tt> object.
+     * @param module the module to add.
+    **/
+    public void addModule(Module module)
+    {
+        Module[] dest = new Module[m_modules.length + 1];
+        System.arraycopy(m_modules, 0, dest, 0, m_modules.length);
+        dest[m_modules.length] = module;
+        m_modules = dest;
+    }
+
+    public long getBundleId()
+    {
+        return m_archive.getId();
+    }
+    
+    public String getLocation()
+    {
+        try
+        {
+            return m_archive.getLocation();
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "Error reading location from bundle archive.",
+                ex);
+            return null;
+        }
+    }
+
+    public int getStartLevel(int defaultLevel)
+    {
+        try
+        {
+            return m_archive.getStartLevel();
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "Error reading start level from bundle archive.",
+                ex);
+            return defaultLevel;
+        }
+    }
+
+    public void setStartLevel(int i)
+    {
+        try
+        {
+            m_archive.setStartLevel(i);
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "Error writing start level to bundle archive.",
+                ex);
+        }
+    }
+
+    public Map getCurrentHeader()
+    {
+        try
+        {
+            // Return the header for the most recent bundle revision only,
+            // since we shouldn't ever need access to older revisions.
+            return m_archive.getManifestHeader(m_archive.getRevisionCount() - 1);
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "Error reading manifest from bundle archive.",
+                ex);
+            return null;
+        }
+    }
+
+    public int getState()
+    {
+        return m_state;
+    }
+
+    public void setState(int i)
+    {
+        m_state = i;
+    }
+
+    public int getPersistentState()
+    {
+        try
+        {
+            return m_archive.getPersistentState();
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "Error reading persistent state from bundle archive.",
+                ex);
+            return Bundle.INSTALLED;
+        }
+    }
+
+    public void setPersistentStateInactive()
+    {
+        try
+        {
+            m_archive.setPersistentState(Bundle.INSTALLED);
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(LogWrapper.LOG_ERROR,
+                "Error writing persistent state to bundle archive.",
+                ex);
+        }
+    }
+
+    public void setPersistentStateActive()
+    {
+        try
+        {
+            m_archive.setPersistentState(Bundle.ACTIVE);
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "Error writing persistent state to bundle archive.",
+                ex);
+        }
+    }
+
+    public void setPersistentStateUninstalled()
+    {
+        try
+        {
+            m_archive.setPersistentState(Bundle.UNINSTALLED);
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "Error writing persistent state to bundle archive.",
+                ex);
+        }
+    }
+
+    public BundleContext getContext()
+    {
+        return m_context;
+    }
+
+    public void setContext(BundleContext context)
+    {
+        m_context = context;
+    }
+
+    public BundleActivator getActivator()
+    {
+        return m_activator;
+    }
+
+    public void setActivator(BundleActivator activator)
+    {
+        m_activator = activator;
+    }
+
+    public boolean isRemovalPending()
+    {
+        return m_removalPending;
+    }
+
+    public void setRemovalPending()
+    {
+        m_removalPending = true;
+    }
+
+    //
+    // Locking related methods.
+    // NOTE: These methods are not synchronized because it is assumed they
+    // will only ever be called when the caller is in a synchronized block.
+    //
+
+    public boolean isLockable()
+    {
+        return (m_lockCount == 0) || (m_lockThread == Thread.currentThread());
+    }
+
+    public void lock()
+    {
+        if ((m_lockCount > 0) && (m_lockThread != Thread.currentThread()))
+        {
+            throw new IllegalStateException("Bundle is locked by another thread.");
+        }
+        m_lockCount++;
+        m_lockThread = Thread.currentThread();
+    }
+
+    public void unlock()
+    {
+        if (m_lockCount == 0)
+        {
+            throw new IllegalStateException("Bundle is not locked.");
+        }
+        if ((m_lockCount > 0) && (m_lockThread != Thread.currentThread()))
+        {
+            throw new IllegalStateException("Bundle is locked by another thread.");
+        }
+        m_lockCount--;
+        if (m_lockCount == 0)
+        {
+            m_lockThread = null;
+        }
+    }
+
+    public void syncLock(BundleInfo info)
+    {
+        m_lockCount = info.m_lockCount;
+        m_lockThread = info.m_lockThread;
+    }
+
+    /**
+     * Converts a module identifier to a bundle identifier. Module IDs
+     * are typically <tt>&lt;bundle-id&gt;.&lt;revision&gt;</tt>; this
+     * method returns only the portion corresponding to the bundle ID.
+    **/
+    protected static long getBundleIdFromModuleId(String id)
+    {
+        try
+        {
+            String bundleId = (id.indexOf('.') >= 0)
+                ? id.substring(0, id.indexOf('.')) : id;
+            return Long.parseLong(bundleId);
+        }
+        catch (NumberFormatException ex)
+        {
+            return -1;
+        }
+    }
+
+    /**
+     * Converts a module identifier to a bundle identifier. Module IDs
+     * are typically <tt>&lt;bundle-id&gt;.&lt;revision&gt;</tt>; this
+     * method returns only the portion corresponding to the revision.
+    **/
+    protected static int getModuleRevisionFromModuleId(String id)
+    {
+        try
+        {
+            String rev = (id.indexOf('.') >= 0)
+                ? id.substring(id.indexOf('.') + 1) : id;
+            return Integer.parseInt(rev);
+        }
+        catch (NumberFormatException ex)
+        {
+            return -1;
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/BundleURLConnection.java b/src/org/apache/osgi/framework/BundleURLConnection.java
new file mode 100644
index 0000000..ca890b2
--- /dev/null
+++ b/src/org/apache/osgi/framework/BundleURLConnection.java
@@ -0,0 +1,158 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.*;
+import java.net.URL;
+import java.net.URLConnection;
+import java.security.Permission;
+
+import org.apache.osgi.moduleloader.*;
+
+class BundleURLConnection extends URLConnection
+{
+    private ModuleManager m_mgr = null;
+    private int contentLength;
+    private long contentTime;
+    private String contentType;
+    private InputStream is;
+
+    public BundleURLConnection(ModuleManager mgr, URL url)
+    {
+        super(url);
+        m_mgr = mgr;
+    }
+
+    public void connect() throws IOException
+    {
+        if (!connected)
+        {
+            // The URL is constructed like this:
+            // bundle://<module-id>/<source-idx>/<resource-path>
+
+            Module module = m_mgr.getModule(url.getHost());
+            if (module == null)
+            {
+                throw new IOException("Unable to find bundle's module.");
+            }
+
+            String resource = url.getFile();
+            if (resource == null)
+            {
+                throw new IOException("Unable to find resource: " + url.toString());
+            }
+            if (resource.startsWith("/"))
+            {
+                resource = resource.substring(1);
+            }
+            int rsIdx = -1;
+            try
+            {
+                rsIdx = Integer.parseInt(resource.substring(0, resource.indexOf("/")));
+            }
+            catch (NumberFormatException ex)
+            {
+                new IOException("Error parsing resource index.");
+            }
+            resource = resource.substring(resource.indexOf("/") + 1);
+
+            // Get the resource bytes from the resource source.
+            byte[] bytes = null;
+            ResourceSource[] resSources = module.getResourceSources();
+            if ((resSources != null) && (rsIdx < resSources.length))
+            {
+                if (resSources[rsIdx].hasResource(resource))
+                {
+                    bytes = resSources[rsIdx].getBytes(resource);
+                }
+            }
+
+            if (bytes == null)
+            {
+                throw new IOException("Unable to find resource: " + url.toString());
+            }
+
+            is = new ByteArrayInputStream(bytes);
+            contentLength = bytes.length;
+            contentTime = 0L;  // TODO: Change this.
+            contentType = URLConnection.guessContentTypeFromName(resource);
+            connected = true;
+        }
+    }
+
+    public InputStream getInputStream()
+        throws IOException
+    {
+        if (!connected)
+        {
+            connect();
+        }
+        return is;
+    }
+
+    public int getContentLength()
+    {
+        if (!connected)
+        {
+            try {
+                connect();
+            } catch(IOException ex) {
+                return -1;
+            }
+        }
+        return contentLength;
+    }
+
+    public long getLastModified()
+    {
+        if (!connected)
+        {
+            try {
+                connect();
+            } catch(IOException ex) {
+                return 0;
+            }
+        }
+        if(contentTime != -1L)
+            return contentTime;
+        else
+            return 0L;
+    }
+
+    public String getContentType()
+    {
+        if (!connected)
+        {
+            try {
+                connect();
+            } catch(IOException ex) {
+                return null;
+            }
+        }
+        return contentType;
+    }
+
+    public Permission getPermission()
+    {
+        // TODO: This should probably return a FilePermission
+        // to access the bundle JAR file, but we don't have the
+        // necessary information here to construct the absolute
+        // path of the JAR file...so it would take some
+        // re-arranging to get this to work.
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/BundleURLStreamHandler.java b/src/org/apache/osgi/framework/BundleURLStreamHandler.java
new file mode 100644
index 0000000..3948c63
--- /dev/null
+++ b/src/org/apache/osgi/framework/BundleURLStreamHandler.java
@@ -0,0 +1,37 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.IOException;
+import java.net.*;
+
+import org.apache.osgi.moduleloader.ModuleManager;
+
+class BundleURLStreamHandler extends URLStreamHandler
+{
+    private ModuleManager m_mgr = null;
+
+    public BundleURLStreamHandler(ModuleManager mgr)
+    {
+        m_mgr = mgr;
+    }
+
+    protected URLConnection openConnection(URL url) throws IOException
+    {
+        return new BundleURLConnection(m_mgr, url);
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/ExportedPackageImpl.java b/src/org/apache/osgi/framework/ExportedPackageImpl.java
new file mode 100644
index 0000000..19cbecc
--- /dev/null
+++ b/src/org/apache/osgi/framework/ExportedPackageImpl.java
@@ -0,0 +1,111 @@
+/*
+ *   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.osgi.framework;
+
+import org.apache.osgi.framework.searchpolicy.R4Version;
+import org.osgi.framework.Bundle;
+import org.osgi.service.packageadmin.ExportedPackage;
+
+class ExportedPackageImpl implements ExportedPackage
+{
+    private Felix m_felix = null;
+    private BundleImpl m_exporter = null;
+    private String m_name = null;
+    private R4Version m_version = null;
+    private String m_toString = null;
+    private String m_versionString = null;
+
+    public ExportedPackageImpl(
+        Felix felix, BundleImpl exporter, String name, R4Version version)
+    {
+        m_felix = felix;
+        m_exporter = exporter;
+        m_name = name;
+        m_version = version;
+    }
+
+    public Bundle getExportingBundle()
+    {
+        // If remove is pending due to a bundle update, then
+        // return null per the spec.
+        if (m_exporter.getInfo().isRemovalPending())
+        {
+            return null;
+        }
+        return m_exporter;
+    }
+
+    /**
+     * Returns the exporting bundle whether the package is state or
+     * not. This is called internally to get access to the exporting
+     * bundle during a refresh operation, which is not possible using
+     * <tt>getExportingBundle</tt> since the specification says that
+     * method must return <tt>null</tt> for stale packages.
+     * @return the exporting bundle for the package.
+    **/
+    protected Bundle getExportingBundleInternal()
+    {
+        return m_exporter;
+    }
+    
+    public Bundle[] getImportingBundles()
+    {
+        // If remove is pending due to a bundle update, then
+        // return null per the spec.
+        if (m_exporter.getInfo().isRemovalPending())
+        {
+            return null;
+        }
+        return m_felix.getImportingBundles(this);
+    }
+
+    public String getName()
+    {
+        return m_name;
+    }
+
+    public String getSpecificationVersion()
+    {
+        if (m_versionString == null)
+        {
+            if (m_version == null)
+            {
+                m_versionString = "0.0.0";
+            }
+            else
+            {
+                m_versionString = m_version.toString();
+            }
+        }
+        return m_versionString;
+    }
+
+    public boolean isRemovalPending()
+    {
+        return m_exporter.getInfo().isRemovalPending();
+    }
+
+    public String toString()
+    {
+        if (m_toString == null)
+        {
+            m_toString = m_name
+                + "; version=" + getSpecificationVersion();
+        }
+        return m_toString;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/FakeURLStreamHandler.java b/src/org/apache/osgi/framework/FakeURLStreamHandler.java
new file mode 100644
index 0000000..363c10f
--- /dev/null
+++ b/src/org/apache/osgi/framework/FakeURLStreamHandler.java
@@ -0,0 +1,39 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.IOException;
+import java.net.*;
+
+/**
+ * This class implements a fake stream handler. This class is necessary in
+ * some cases when assigning <tt>CodeSource</tt>s to classes in
+ * <tt>BundleClassLoader</tt>. In general, the bundle location is an URL
+ * and this URL is used as the code source for the bundle's associated
+ * classes. The OSGi specification does not require that the bundle
+ * location be an URL, though, so in that case we try to generate a
+ * fake URL for the code source of the bundle, which is just the location
+ * string prepended with the "location:" protocol, by default. We need
+ * this fake handler to avoid an unknown protocol exception.
+**/
+class FakeURLStreamHandler extends URLStreamHandler
+{
+    protected URLConnection openConnection(URL url) throws IOException
+    {
+        return null;
+    }
+}
diff --git a/src/org/apache/osgi/framework/Felix.java b/src/org/apache/osgi/framework/Felix.java
new file mode 100644
index 0000000..5fa6092
--- /dev/null
+++ b/src/org/apache/osgi/framework/Felix.java
@@ -0,0 +1,3675 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.*;
+import java.net.*;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.util.*;
+
+import org.apache.osgi.framework.cache.*;
+import org.apache.osgi.framework.searchpolicy.*;
+import org.apache.osgi.framework.util.*;
+import org.apache.osgi.framework.util.Util;
+import org.apache.osgi.moduleloader.*;
+import org.apache.osgi.moduleloader.search.ResolveException;
+import org.apache.osgi.moduleloader.search.ResolveListener;
+import org.osgi.framework.*;
+import org.osgi.service.packageadmin.ExportedPackage;
+
+public class Felix
+{
+    // Logging related member variables.
+    private LogWrapper m_logger = new LogWrapper();
+    // Config properties.
+    private PropertyResolver m_config = new ConfigImpl();
+    // Configuration properties passed into constructor.
+    private MutablePropertyResolver m_configProps = null;
+
+    // MODULE MANAGER.
+    private ModuleManager m_mgr = null;
+
+    // Object used as a lock when calculating which bundles
+    // when performing an operation on one or more bundles.
+    private Object[] m_bundleLock = new Object[0];
+
+    // Maps a bundle location to a bundle location;
+    // used to reserve a location when installing a bundle.
+    private Map m_installRequestMap = null;
+    // This lock must be acquired to modify m_installRequestMap;
+    // to help avoid deadlock this lock as priority 1 and should
+    // be acquired before locks with lower priority.
+    private Object[] m_installRequestLock_Priority1 = new Object[0];
+
+    // Maps a bundle location to a bundle.
+    private HashMap m_installedBundleMap = null;
+    // This lock must be acquired to modify m_installedBundleMap;
+    // to help avoid deadlock this lock as priority 2 and should
+    // be acquired before locks with lower priority.
+    private Object[] m_installedBundleLock_Priority2 = new Object[0];
+
+    // An array of uninstalled bundles before a refresh occurs.
+    private BundleImpl[] m_uninstalledBundles = null;
+    // This lock must be acquired to modify m_uninstalledBundles;
+    // to help avoid deadlock this lock as priority 3 and should
+    // be acquired before locks with lower priority.
+    private Object[] m_uninstalledBundlesLock_Priority3 = new Object[0];
+
+    // Status flag for framework.
+    public static final int INITIAL_STATUS  = -1;
+    public static final int RUNNING_STATUS  = 0;
+    public static final int STARTING_STATUS = 1;
+    public static final int STOPPING_STATUS = 2;
+    private int m_frameworkStatus = INITIAL_STATUS;
+
+    // Framework's active start level.
+    private int m_activeStartLevel =
+        FelixConstants.FRAMEWORK_INACTIVE_STARTLEVEL;
+
+    // Local file system cache.
+    private BundleCache m_cache = null;
+
+    // Next available bundle identifier.
+    private long m_nextId = 1L;
+
+    // List of event listeners.
+    private FelixDispatchQueue m_dispatchQueue = null;
+    // Re-usable event dispatchers.
+    private Dispatcher m_frameworkDispatcher = null;
+    private Dispatcher m_bundleDispatcher = null;
+    private Dispatcher m_serviceDispatcher = null;
+
+    // Service registry.
+    private ServiceRegistry m_registry = null;
+
+    // Reusable admin permission object for all instances
+    // of the BundleImpl.
+    private static AdminPermission m_adminPerm = new AdminPermission();
+
+    /**
+     * <p>
+     * This method starts the framework instance; instances of the framework
+     * are dormant until this method is called. The caller may also provide
+     * <tt>MutablePropertyResolver</tt> implementations that the instance will
+     * use to obtain configuration or framework properties. Configuration
+     * properties are used internally by the framework and its extensions to alter
+     * its default behavior. Framework properties are used by bundles
+     * and are accessible from <tt>BundleContext.getProperty()</tt>.
+     * </p>
+     * <p>
+     * Configuration properties are the sole means to configure the framework's
+     * default behavior; the framework does not refer to any system properties for
+     * configuration information. If a <tt>MutablePropertyResolver</tt> is
+     * supplied to this method for configuration properties, then the framework will
+     * consult the <tt>MutablePropertyResolver</tt> instance for any and all
+     * configuration properties. It is possible to specify a <tt>null</tt>
+     * configuration property resolver, in which case the framework will use its
+     * default behavior in all cases. However, if the
+     * <a href="cache/DefaultBundleCache.html"><tt>DefaulBundleCache</tt></a>
+     * is used, then at a minimum a profile name or profile directory must
+     * be specified.
+     * </p>
+     * <p>
+     * The following configuration properties can be specified:
+     * </p>
+     * <ul>
+     *   <li><tt>felix.cache.class</tt> - The class name to be used when
+     *       creating an instance for the bundle cache; this class must
+     *       implement the <tt>BundleCache</tt> interface and have a default
+     *       constructor. By default, the framework will create an instance of
+     *       <tt>DefaultBundleCache</tt> for the bundle cache.
+     *   </li>
+     *   <li><tt>felix.auto.install.&lt;n&gt;</tt> - Space-delimited list of
+     *       bundles to automatically install into start level <tt>n</tt> when
+     *       the framework is started. Append a specific start level to this
+     *       property name to assign the bundles' start level
+     *       (e.g., <tt>felix.auto.install.2</tt>).
+     *   </li>
+     *   <li><tt>felix.auto.start.&lt;n&gt;</tt> - Space-delimited list of
+     *       bundles to automatically install and start into start level
+     *       <tt>n</tt> when the framework is started. Append a
+     *       specific start level to this property name to assign the
+     *       bundles' start level(e.g., <tt>felix.auto.start.2</tt>).
+     *   </li>
+     *   <li><tt>felix.startlevel.framework</tt> - The initial start level
+     *       of the framework once it starts execution; the default
+     *       value is 1.
+     *   </li>
+     *   <li><tt>felix.startlevel.bundle</tt> - The default start level for
+     *       newly installed bundles; the default value is 1.
+     *   </li>
+     *   <li><tt>felix.embedded.execution</tt> - Flag to indicate whether
+     *       the framework is embedded into a host application; the default value is
+     *       "<tt>false</tt>". If this flag is "<tt>true</tt>" then the framework
+     *       will not called <tt>System.exit()</tt> upon termination.
+     *   </li>
+     *   <li><tt>felix.strict.osgi</tt> - Flag to indicate whether the framework is
+     *       running in strict OSGi mode; the default value is "<tt>true</tt>".
+     *       If this flag is "<tt>false</tt>" it enables a non-OSGi-compliant
+     *       feature by persisting <tt>BundleActivator</tt>s that implement
+     *       <tt>Serializable</tt>. This feature is not recommended since
+     *       it is non-compliant.
+     *   </li>
+     * </ul>
+     * <p>
+     * Besides the above framework configuration properties, it is also
+     * possible to specify properties for the bundle cache. The available
+     * bundle cache properties depend on the cache implementation
+     * being used. For the properties of the default bundle cache, refer to the
+     * <a href="cache/DefaultBundleCache.html"><tt>DefaulBundleCache</tt></a>
+     * API documentation.
+     * </p>
+     * <p>
+     * Framework properties are somewhat misnamed, since they are not used by
+     * the framework, but by bundles via <tt>BundleContext.getProperty()</tt>.
+     * Please refer to bundle documentation of your specific bundle for any
+     * available properties.
+     * </p>
+     * <p>
+     * The <a href="Main.html"><tt>Main</tt></a> class implements some
+     * functionality for default property file handling, which makes it
+     * possible to specify configuration properties and framework properties
+     * in files that are automatically loaded when starting the framework. If you
+     * plan to create your own framework instance, you may be
+     * able to take advantage of the features it provides; refer to its
+     * class documentation for more information.
+     * </p>
+     * 
+     * @param configProps An object for obtaining configuration properties,
+     *        may be <tt>null</tt>.
+     * @param frameworkProps An object for obtaining framework properties,
+     *        may be <tt>null</tt>.
+     * @param activatorList A list of System Bundle activators.
+    **/
+    public synchronized void start(
+        MutablePropertyResolver configProps,
+        List activatorList)
+    {
+        if (m_frameworkStatus != INITIAL_STATUS)
+        {
+            throw new IllegalStateException("Invalid framework status: " + m_frameworkStatus);
+        }
+
+        // The framework is now in its startup sequence.
+        m_frameworkStatus = STARTING_STATUS;
+
+        // Initialize member variables.
+        m_mgr = null;
+        m_configProps = (configProps == null)
+            ? new MutablePropertyResolverImpl(new CaseInsensitiveMap()) : configProps;
+        m_activeStartLevel = FelixConstants.FRAMEWORK_INACTIVE_STARTLEVEL;
+        m_installRequestMap = new HashMap();
+        m_installedBundleMap = new HashMap();
+        m_uninstalledBundles = null;
+        m_cache = null;
+        m_nextId = 1L;
+        m_dispatchQueue = null;
+        m_registry = new ServiceRegistry(m_logger);
+
+        // Add a listener to the service registry; this is
+        // used to distribute service registry events to
+        // service listeners.
+        m_registry.addServiceListener(new ServiceListener() {
+            public void serviceChanged(ServiceEvent event)
+            {
+                fireServiceEvent(event);
+            }
+        });
+
+        // Create default storage system from the specified cache class
+        // or use the default cache if no custom cache was specified.
+        String className = m_config.get(FelixConstants.CACHE_CLASS_PROP);
+        if (className == null)
+        {
+            className = DefaultBundleCache.class.getName();
+        }
+
+        try
+        {
+            Class clazz = Class.forName(className);
+            m_cache = (BundleCache) clazz.newInstance();
+            m_cache.initialize(m_config, m_logger);
+        }
+        catch (Exception ex)
+        {
+            System.err.println("Error creating bundle cache:");
+            ex.printStackTrace();
+
+            // Only shutdown the JVM if the framework is running stand-alone.
+            String embedded = m_config.get(
+                FelixConstants.EMBEDDED_EXECUTION_PROP);
+            boolean isEmbedded = (embedded == null)
+                ? false : embedded.equals("true");
+            if (!isEmbedded)
+            {
+                System.exit(-1);
+            }
+            else
+            {
+                throw new RuntimeException(ex.toString());
+            }
+        }
+
+        // Create search policy for module loader.
+        R4SearchPolicy searchPolicy = new R4SearchPolicy(m_logger);
+
+        // Add a resolver listener to the search policy
+        // so that we will be notified when modules are resolved
+        // in order to update the bundle state.
+        searchPolicy.addResolverListener(new ResolveListener() {
+            public void moduleResolved(ModuleEvent event)
+            {
+                BundleImpl bundle = null;
+                try
+                {
+                    long id = BundleInfo.getBundleIdFromModuleId(
+                        event.getModule().getId());
+                    if (id >= 0)
+                    {
+                        // Update the bundle's state to resolved when the
+                        // current module is resolved; just ignore resolve
+                        // events for older revisions since this only occurs
+                        // when an update is done on an unresolved bundle
+                        // and there was no refresh performed.
+                        bundle = (BundleImpl) getBundle(id);
+
+                        // Lock the bundle first.
+                        try
+                        {
+                            acquireBundleLock(bundle);
+                            if (bundle.getInfo().getCurrentModule() == event.getModule())
+                            {
+                                bundle.getInfo().setState(Bundle.RESOLVED);
+                            }
+                        }
+                        catch (BundleException ex)
+                        {
+                            // This should not happen, but if it does
+                            // there isn't much we can do.
+                        }
+                        finally
+                        {
+                            releaseBundleLock(bundle);
+                        }
+                    }
+                }
+                catch (NumberFormatException ex)
+                {
+                    // Ignore.
+                }
+            }
+
+            public void moduleUnresolved(ModuleEvent event)
+            {
+                // We can ignore this, because the only time it
+                // should happen is when a refresh occurs. The
+                // refresh operation resets the bundle's state
+                // by calling BundleInfo.reset(), thus it is not
+                // necessary for us to reset the bundle's state
+                // here.
+            }
+        });
+
+        m_mgr = new ModuleManager(searchPolicy, new OSGiURLPolicy(this));
+
+        // Initialize dispatch queue.
+        m_dispatchQueue = new FelixDispatchQueue(m_logger);
+
+        // Initialize framework properties.
+        initializeFrameworkProperties();
+
+        // Before we reload any cached bundles, let's create a system
+        // bundle that is responsible for providing specific container
+        // related services.
+        SystemBundle systembundle = null;
+        try
+        {
+            // Create a simple bundle info for the system bundle.
+            BundleInfo info = new BundleInfo(
+                m_logger, new SystemBundleArchive(), null);
+            systembundle = new SystemBundle(this, info, activatorList);
+            systembundle.getInfo().addModule(
+                m_mgr.addModule(
+                    "0", systembundle.getAttributes(),
+                    systembundle.getResourceSources(),
+                    systembundle.getLibrarySources(),
+                    true)); // HACK ALERT! This flag indicates that we will
+                            // use the parent class loader as a resource source.
+            m_installedBundleMap.put(
+                systembundle.getInfo().getLocation(), systembundle);
+
+            // Manually resolve the System Bundle, which will cause its
+            // state to be set to RESOLVED.
+            try
+            {
+                searchPolicy.resolve(systembundle.getInfo().getCurrentModule());
+            }
+            catch (ResolveException ex)
+            {
+                // This should never happen.
+                throw new BundleException(
+                        "Unresolved package in System Bundle:"
+                        + ex.getPackage());
+            }
+
+            // Start the system bundle; this will set its state
+            // to STARTING, we must set its state to ACTIVE after
+            // all bundles are restarted below according to the spec.
+            systembundle.start();
+        }
+        catch (Exception ex)
+        {
+            m_mgr = null;
+            DispatchQueue.shutdown();
+            m_logger.log(LogWrapper.LOG_ERROR, "Unable to start system bundle.", ex);
+            throw new RuntimeException("Unable to start system bundle.");
+        }
+        
+        // Reload and cached bundles.
+        BundleArchive[] archives = null;
+
+        // First get cached bundle identifiers.
+        try
+        {
+            archives = m_cache.getArchives();
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "Unable to list saved bundles: " + ex, ex);
+            archives = null;
+        }
+
+        BundleImpl bundle = null;
+
+        // Now install all cached bundles.
+        for (int i = 0; (archives != null) && (i < archives.length); i++)
+        {
+            // Make sure our id generator is not going to overlap.
+            // TODO: This is not correct since it may lead to re-used
+            // ids, which is not okay according to OSGi.
+            m_nextId = Math.max(m_nextId, archives[i].getId() + 1);
+
+            try
+            {
+                // It is possible that a bundle in the cache was previously
+                // uninstalled, but not completely deleted (perhaps because
+                // of a crash or a locked file), so if we see an archive
+                // with an UNINSTALLED persistent state, then try to remove
+                // it now.
+                if (archives[i].getPersistentState() == Bundle.UNINSTALLED)
+                {
+                    m_cache.remove(archives[i]);
+                }
+                // Otherwise re-install the cached bundle.
+                else
+                {
+                    // Install the cached bundle.
+                    bundle = (BundleImpl) installBundle(
+                        archives[i].getId(), archives[i].getLocation(), null);
+                }
+            }
+            catch (Exception ex)
+            {
+                fireFrameworkEvent(FrameworkEvent.ERROR, bundle, ex);
+                try
+                {
+                    m_logger.log(
+                        LogWrapper.LOG_ERROR,
+                        "Unable to re-install " + archives[i].getLocation(),
+                        ex);
+                }
+                catch (Exception ex2)
+                {
+                    m_logger.log(
+                        LogWrapper.LOG_ERROR,
+                        "Unable to re-install bundle " + archives[i].getId(),
+                        ex);
+                }
+                // TODO: Perhaps we should remove the cached bundle?
+            }
+        }
+
+        // Get the framework's default start level.
+        int startLevel = FelixConstants.FRAMEWORK_DEFAULT_STARTLEVEL;
+        String s = m_config.get(FelixConstants.FRAMEWORK_STARTLEVEL_PROP);
+        if (s != null)
+        {
+            try
+            {
+                startLevel = Integer.parseInt(s);
+            }
+            catch (NumberFormatException ex)
+            {
+                startLevel = FelixConstants.FRAMEWORK_DEFAULT_STARTLEVEL;
+            }
+        }
+
+        // Load bundles from auto-install and auto-start properties;
+        processAutoProperties();
+
+        // This will restart bundles if necessary.
+        setFrameworkStartLevel(startLevel);
+
+        // The framework is now running.
+        m_frameworkStatus = RUNNING_STATUS;
+
+        // Set the system bundle state to ACTIVE.
+        systembundle.getInfo().setState(Bundle.ACTIVE);
+
+        // Fire started event for system bundle.
+        fireBundleEvent(BundleEvent.STARTED, systembundle);
+
+        // Send a framework event to indicate the framework has started.
+        fireFrameworkEvent(FrameworkEvent.STARTED, getBundle(0), null);
+    }
+
+    /**
+     * This method cleanly shuts down the framework, it must be called at the
+     * end of a session in order to shutdown all active bundles.
+    **/
+    public synchronized void shutdown()
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        // Change framework status from running to stopping.
+        // If framework is not running, then just return.
+        if (m_frameworkStatus != RUNNING_STATUS)
+        {
+            return;
+        }
+
+        // The framework is now in its shutdown sequence.
+        m_frameworkStatus = STOPPING_STATUS;
+
+        // Set the start level to zero in order to stop
+        // all bundles in the framework.
+        setFrameworkStartLevel(0);
+
+        // Just like initialize() called the system bundle's start()
+        // method, we must call its stop() method here so that it
+        // can perform any necessary clean up.
+        try
+        {
+            getBundle(0).stop();
+        }
+        catch (Exception ex)
+        {
+            fireFrameworkEvent(FrameworkEvent.ERROR, getBundle(0), ex);
+            m_logger.log(LogWrapper.LOG_ERROR, "Error stopping system bundle.", ex);
+        }
+
+        // Loop through all bundles and update any updated bundles.
+        Bundle[] bundles = getBundles();
+        for (int i = 0; i < bundles.length; i++)
+        {
+            BundleImpl bundle = (BundleImpl) bundles[i];
+            if (bundle.getInfo().isRemovalPending())
+            {
+                try
+                {
+                    purgeBundle(bundle);
+                }
+                catch (Exception ex)
+                {
+                    fireFrameworkEvent(FrameworkEvent.ERROR, bundle, ex);
+                    m_logger.log(LogWrapper.LOG_ERROR, "Unable to purge bundle "
+                        + bundle.getInfo().getLocation(), ex);
+                }
+            }
+        }
+
+        // Remove any uninstalled bundles.
+        for (int i = 0;
+            (m_uninstalledBundles != null) && (i < m_uninstalledBundles.length);
+            i++)
+        {
+            try
+            {
+                garbageCollectBundle(m_uninstalledBundles[i]);
+            }
+            catch (Exception ex)
+            {
+                m_logger.log(
+                    LogWrapper.LOG_ERROR,
+                    "Unable to remove "
+                    + m_uninstalledBundles[i].getInfo().getLocation(), ex);
+            }
+        }
+
+        // Shutdown event dispatching queue.
+        DispatchQueue.shutdown();
+
+        // The framework is no longer in a usable state.
+        m_frameworkStatus = INITIAL_STATUS;
+    }
+
+    public int getStatus()
+    {
+        return m_frameworkStatus;
+    }
+
+    /**
+     * Returns the active start level of the framework; this method
+     * implements functionality for the Start Level service.
+     * @return The active start level of the framework.
+    **/
+    protected int getStartLevel()
+    {
+        return m_activeStartLevel;
+    }
+
+    /**
+     * Implements the functionality of the <tt>setStartLevel()</tt>
+     * method for the StartLevel service, but does not do the security or
+     * parameter check. The security and parameter check are done in the
+     * StartLevel service implementation because this method is called on
+     * a separate thread and the caller's thread would already be gone if
+     * we did the checks in this method.
+     * @param requestedLevel The new start level of the framework.
+    **/
+    protected synchronized void setFrameworkStartLevel(int requestedLevel)
+    {
+        // Determine if we are lowering or raising the
+        // active start level.
+        boolean lowering = (requestedLevel < m_activeStartLevel);
+
+        // Record new start level.
+        m_activeStartLevel = requestedLevel;
+
+        // Get array of all installed bundles.
+        Bundle[] bundles = getBundles();
+
+        // Sort bundle array by start level either ascending or
+        // descending depending on whether the start level is being
+        // lowered or raised.
+        Comparator comparator = null;
+        if (lowering)
+        {
+            // Sort descending to stop highest start level first.
+            comparator = new Comparator() {
+                public int compare(Object o1, Object o2)
+                {
+                    BundleImpl b1 = (BundleImpl) o1;
+                    BundleImpl b2 = (BundleImpl) o2;
+                    if (b1.getInfo().getStartLevel(getInitialBundleStartLevel())
+                        < b2.getInfo().getStartLevel(getInitialBundleStartLevel()))
+                    {
+                        return 1;
+                    }
+                    else if (b1.getInfo().getStartLevel(getInitialBundleStartLevel())
+                        > b2.getInfo().getStartLevel(getInitialBundleStartLevel()))
+                    {
+                        return -1;
+                    }
+                    return 0;
+                }
+            };
+        }
+        else
+        {
+            // Sort ascending to start lowest start level first.
+            comparator = new Comparator() {
+                public int compare(Object o1, Object o2)
+                {
+                    BundleImpl b1 = (BundleImpl) o1;
+                    BundleImpl b2 = (BundleImpl) o2;
+                    if (b1.getInfo().getStartLevel(getInitialBundleStartLevel())
+                        > b2.getInfo().getStartLevel(getInitialBundleStartLevel()))
+                    {
+                        return 1;
+                    }
+                    else if (b1.getInfo().getStartLevel(getInitialBundleStartLevel())
+                        < b2.getInfo().getStartLevel(getInitialBundleStartLevel()))
+                    {
+                        return -1;
+                    }
+                    return 0;
+                }
+            };
+        }
+
+        Arrays.sort(bundles, comparator);
+
+        // Stop or start the bundles according to the start level.
+        for (int i = 0; (bundles != null) && (i < bundles.length); i++)
+        {
+            BundleImpl impl = (BundleImpl) bundles[i];
+
+            // Ignore the system bundle, since its start() and
+            // stop() methods get called explicitly in initialize()
+            // and shutdown(), respectively.
+            if (impl.getInfo().getBundleId() == 0)
+            {
+                continue;
+            }
+
+            // Start the bundle if necessary.
+            if ((impl.getInfo().getPersistentState() == Bundle.ACTIVE) &&
+                (impl.getInfo().getStartLevel(getInitialBundleStartLevel())
+                    <= m_activeStartLevel))
+            {
+                try
+                {
+                    startBundle(impl, false);
+                }
+                catch (Throwable th)
+                {
+                    fireFrameworkEvent(FrameworkEvent.ERROR, impl, th);
+                    m_logger.log(
+                        LogWrapper.LOG_ERROR,
+                        "Error starting " + impl.getInfo().getLocation(), th);
+                }
+            }
+            // Stop the bundle if necessary.
+            else if (impl.getInfo().getStartLevel(getInitialBundleStartLevel())
+                > m_activeStartLevel)
+            {
+                try
+                {
+                    stopBundle(impl, false);
+                }
+                catch (Throwable th)
+                {
+                    fireFrameworkEvent(FrameworkEvent.ERROR, impl, th);
+                    m_logger.log(
+                        LogWrapper.LOG_ERROR,
+                        "Error stopping " + impl.getInfo().getLocation(), th);
+                }
+            }
+        }
+
+        fireFrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, getBundle(0), null);
+    }
+
+    /**
+     * Returns the start level into which newly installed bundles will
+     * be placed by default; this method implements functionality for
+     * the Start Level service.
+     * @return The default start level for newly installed bundles.
+    **/
+    protected int getInitialBundleStartLevel()
+    {
+        String s = m_config.get(FelixConstants.BUNDLE_STARTLEVEL_PROP);
+
+        if (s != null)
+        {
+            try
+            {
+                int i = Integer.parseInt(s);
+                return (i > 0) ? i : FelixConstants.BUNDLE_DEFAULT_STARTLEVEL;
+            }
+            catch (NumberFormatException ex)
+            {
+                // Ignore and return the default value.
+            }
+        }
+        return FelixConstants.BUNDLE_DEFAULT_STARTLEVEL;
+    }
+
+    /**
+     * Sets the default start level into which newly installed bundles
+     * will be placed; this method implements functionality for the Start
+     * Level service.
+     * @param startLevel The new default start level for newly installed
+     *        bundles.
+     * @throws java.lang.IllegalArgumentException If the specified start
+     *         level is not greater than zero.
+     * @throws java.security.SecurityException If the caller does not
+     *         have <tt>AdminPermission</tt>.
+    **/
+    protected void setInitialBundleStartLevel(int startLevel)
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        if (startLevel <= 0)
+        {
+            throw new IllegalArgumentException(
+                "Initial start level must be greater than zero.");
+        }
+
+        m_configProps.put(
+            FelixConstants.BUNDLE_STARTLEVEL_PROP, Integer.toString(startLevel));
+    }
+
+    /**
+     * Returns the start level for the specified bundle; this method
+     * implements functionality for the Start Level service.
+     * @param bundle The bundle to examine.
+     * @return The start level of the specified bundle.
+     * @throws java.lang.IllegalArgumentException If the specified
+     *          bundle has been uninstalled.
+    **/
+    protected int getBundleStartLevel(Bundle bundle)
+    {
+        if (bundle.getState() == Bundle.UNINSTALLED)
+        {
+            throw new IllegalArgumentException("Bundle is uninstalled.");
+        }
+
+        return ((BundleImpl) bundle).getInfo().getStartLevel(getInitialBundleStartLevel());
+    }
+
+    /**
+     * Sets the start level of the specified bundle; this method
+     * implements functionality for the Start Level service.
+     * @param bundle The bundle whose start level is to be modified.
+     * @param startLevel The new start level of the specified bundle.
+     * @throws java.lang.IllegalArgumentException If the specified
+     *          bundle is the system bundle or if the bundle has been
+     *          uninstalled.
+     * @throws java.security.SecurityException If the caller does not
+     *          have <tt>AdminPermission</tt>.
+    **/
+    protected void setBundleStartLevel(Bundle bundle, int startLevel)
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        // Cannot change the system bundle.
+        if (bundle.getBundleId() == 0)
+        {
+            throw new IllegalArgumentException(
+                "Cannot change system bundle start level.");
+        }
+
+        // Acquire bundle lock.
+        try
+        {
+            acquireBundleLock((BundleImpl) bundle);
+        }
+        catch (BundleException ex)
+        {
+            m_logger.log(LogWrapper.LOG_ERROR, "Unable to acquire lock to set start level.", ex);
+            return;
+        }
+        
+        Throwable rethrow = null;
+
+        try
+        {
+            if (bundle.getState() == Bundle.UNINSTALLED)
+            {
+                throw new IllegalArgumentException("Bundle is uninstalled.");
+            }
+
+            if (startLevel >= 1)
+            {
+                BundleImpl impl = (BundleImpl) bundle;
+                impl.getInfo().setStartLevel(startLevel);
+    
+                try
+                {
+                    // Start the bundle if necessary.
+                    if ((impl.getInfo().getPersistentState() == Bundle.ACTIVE) &&
+                        (impl.getInfo().getStartLevel(getInitialBundleStartLevel())
+                            <= m_activeStartLevel))
+                    {
+                        startBundle(impl, false);
+                    }
+                    // Stop the bundle if necessary.
+                    else if (impl.getInfo().getStartLevel(getInitialBundleStartLevel())
+                        > m_activeStartLevel)
+                    {
+                        stopBundle(impl, false);
+                    }
+                }
+                catch (Throwable th)
+                {
+                    rethrow = th;
+                    m_logger.log(LogWrapper.LOG_ERROR, "Error starting/stopping bundle.", th);
+                }
+            }
+            else
+            {
+                m_logger.log(LogWrapper.LOG_WARNING, "Bundle start level must be greater than zero.");
+            }
+        }
+        finally
+        {
+            // Always release bundle lock.
+            releaseBundleLock((BundleImpl) bundle);
+        }
+
+        if (rethrow != null)
+        {
+            fireFrameworkEvent(FrameworkEvent.ERROR, bundle, rethrow);
+        }
+    }
+
+    /**
+     * Returns whether a bundle is persistently started; this is an
+     * method implementation for the Start Level service.
+     * @param bundle The bundle to examine.
+     * @return <tt>true</tt> if the bundle is marked as persistently
+     *          started, <tt>false</tt> otherwise.
+     * @throws java.lang.IllegalArgumentException If the specified
+     *          bundle has been uninstalled.
+    **/
+    protected boolean isBundlePersistentlyStarted(Bundle bundle)
+    {
+        if (bundle.getState() == Bundle.UNINSTALLED)
+        {
+            throw new IllegalArgumentException("Bundle is uninstalled.");
+        }
+
+        return (((BundleImpl) bundle).getInfo().getPersistentState() == Bundle.ACTIVE);
+    }
+
+    //
+    // Implementation of Bundle interface methods.
+    //
+
+    /**
+     * Implementation for Bundle.getHeaders().
+    **/
+    protected Dictionary getBundleHeaders(BundleImpl bundle)
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+        return new MapToDictionary(bundle.getInfo().getCurrentHeader());
+    }
+
+    /**
+     * Implementation for Bundle.getLocation().
+    **/
+    protected String getBundleLocation(BundleImpl bundle)
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+        return bundle.getInfo().getLocation();
+    }
+
+    /**
+     * Implementation for Bundle.getResource().
+    **/
+    protected URL getBundleResource(BundleImpl bundle, String name)
+    {
+        if (bundle.getInfo().getState() == Bundle.UNINSTALLED)
+        {
+            throw new IllegalStateException("The bundle is uninstalled.");
+        }
+        else if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+        return bundle.getInfo().getCurrentModule().getClassLoader().getResource(name);
+    }
+
+    protected ServiceReference[] getBundleRegisteredServices(BundleImpl bundle)
+    {
+        if (bundle.getInfo().getState() == Bundle.UNINSTALLED)
+        {
+            throw new IllegalStateException("The bundle is uninstalled.");
+        }
+
+        // Filter list of registered service references.
+        ServiceReference[] refs = m_registry.getRegisteredServices(bundle);
+        List list = new ArrayList();
+        for (int refIdx = 0; (refs != null) && (refIdx < refs.length); refIdx++)
+        {
+            // Check that the current security context has permission
+            // to get at least one of the service interfaces; the
+            // objectClass property of the service stores its service
+            // interfaces.
+            boolean hasPermission = false;
+            if (System.getSecurityManager() != null)
+            {
+                String[] objectClass = (String[])
+                    refs[refIdx].getProperty(Constants.OBJECTCLASS);
+                if (objectClass == null)
+                {
+                    return null;
+                }
+                for (int ifcIdx = 0;
+                    !hasPermission && (ifcIdx < objectClass.length);
+                    ifcIdx++)
+                {
+                    try
+                    {
+                        ServicePermission perm =
+                            new ServicePermission(
+                                objectClass[ifcIdx], ServicePermission.GET);
+                        AccessController.checkPermission(perm);
+                        hasPermission = true;
+                    }
+                    catch (Exception ex)
+                    {
+                    }
+                }
+            }
+            else
+            {
+                hasPermission = true;
+            }
+
+            if (hasPermission)
+            {
+                list.add(refs[refIdx]);
+            }
+        }
+
+        if (list.size() > 0)
+        {
+            return (ServiceReference[])
+                list.toArray(new ServiceReference[list.size()]);
+        }
+
+        return null;
+    }
+
+    protected ServiceReference[] getBundleServicesInUse(Bundle bundle)
+    {
+        // Filter list of "in use" service references.
+        ServiceReference[] refs = m_registry.getServicesInUse(bundle);
+        List list = new ArrayList();
+        for (int refIdx = 0; (refs != null) && (refIdx < refs.length); refIdx++)
+        {
+            // Check that the current security context has permission
+            // to get at least one of the service interfaces; the
+            // objectClass property of the service stores its service
+            // interfaces.
+            boolean hasPermission = false;
+            if (System.getSecurityManager() != null)
+            {
+                String[] objectClass = (String[])
+                    refs[refIdx].getProperty(Constants.OBJECTCLASS);
+                if (objectClass == null)
+                {
+                    return null;
+                }
+                for (int ifcIdx = 0;
+                    !hasPermission && (ifcIdx < objectClass.length);
+                    ifcIdx++)
+                {
+                    try
+                    {
+                        ServicePermission perm =
+                            new ServicePermission(
+                                objectClass[ifcIdx], ServicePermission.GET);
+                        AccessController.checkPermission(perm);
+                        hasPermission = true;
+                    }
+                    catch (Exception ex)
+                    {
+                    }
+                }
+            }
+            else
+            {
+                hasPermission = true;
+            }
+
+            if (hasPermission)
+            {
+                list.add(refs[refIdx]);
+            }
+        }
+
+        if (list.size() > 0)
+        {
+            return (ServiceReference[])
+                list.toArray(new ServiceReference[list.size()]);
+        }
+
+        return null;
+    }
+
+    protected boolean bundleHasPermission(BundleImpl bundle, Object obj)
+    {
+        if (bundle.getInfo().getState() == Bundle.UNINSTALLED)
+        {
+            throw new IllegalStateException("The bundle is uninstalled.");
+        }
+
+// TODO: IMPLEMENT THIS CORRECTLY.
+        return true;
+    }
+
+    /**
+     * Implementation for Bundle.start().
+    **/
+    protected void startBundle(BundleImpl bundle, boolean record)
+        throws BundleException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        // CONCURRENCY NOTE:
+        // Starting a bundle may actually impact many bundles, since
+        // the bundle being started my need to be resolved, which in
+        // turn may need to resolve other bundles. Despite this fact,
+        // we only acquire the lock for the bundle being started, because
+        // when resolve is called on this bundle, it will eventually
+        // call resolve on the module loader search policy, which does
+        // its own locking on the module manager instance. Since the 
+        // resolve algorithm is locking the module manager instance, it
+        // is not possible for other bundles to be installed or removed,
+        // so we don't have to worry about these possibilities.
+        //
+        // Further, if other bundles are started during this operation,
+        // then either they will resolve first because they got the lock
+        // on the module manager or we will resolve first since we got
+        // the lock on the module manager, so there should be no interference.
+        // If other bundles are stopped or uninstalled, this should pose
+        // no problems, since this does not impact their resolved state.
+        // If a refresh occurs, then the refresh algorithm ulimately has
+        // to acquire the module manager instance lock too before it can
+        // completely purge old modules, so it should also complete either
+        // before or after this bundle is started. At least that's the
+        // theory.
+
+        // Acquire bundle lock.
+        acquireBundleLock(bundle);
+
+        try
+        {
+            _startBundle(bundle, record);
+        }
+        finally
+        {
+            // Release bundle lock.
+            releaseBundleLock(bundle);
+        }
+    }
+
+    private void _startBundle(BundleImpl bundle, boolean record)
+        throws BundleException
+    {
+        // Set and save the bundle's persistent state to active
+        // if we are supposed to record state change.
+        if (record)
+        {
+            bundle.getInfo().setPersistentStateActive();
+        }
+
+        // Try to start the bundle.
+        BundleInfo info = bundle.getInfo();
+
+        // Ignore bundles whose persistent state is not active
+        // or whose start level is greater than the framework's.
+        if ((info.getPersistentState() != Bundle.ACTIVE)
+            || (info.getStartLevel(getInitialBundleStartLevel()) > getStartLevel()))
+        {
+            return;
+        }
+
+        switch (info.getState())
+        {
+            case Bundle.UNINSTALLED:
+                throw new IllegalStateException("Cannot start an uninstalled bundle.");
+            case Bundle.STARTING:
+            case Bundle.STOPPING:
+                throw new BundleException("Starting a bundle that is starting or stopping is currently not supported.");
+            case Bundle.ACTIVE:
+                return;
+            case Bundle.INSTALLED:
+                _resolveBundle(bundle);
+            case Bundle.RESOLVED:
+                info.setState(Bundle.STARTING);
+        }
+
+        try
+        {
+            // Set the bundle's activator.
+            bundle.getInfo().setActivator(createBundleActivator(bundle.getInfo()));
+
+            // Activate the bundle if it has an activator.
+            if (bundle.getInfo().getActivator() != null)
+            {
+                if (info.getContext() == null)
+                {
+                    info.setContext(new BundleContextImpl(this, bundle));
+                }
+
+                if (System.getSecurityManager() != null)
+                {
+//                    m_startStopPrivileged.setAction(StartStopPrivileged.START_ACTION);
+//                    m_startStopPrivileged.setBundle(bundle);
+//                    AccessController.doPrivileged(m_startStopPrivileged);
+                }
+                else
+                {
+                    info.getActivator().start(info.getContext());
+                }
+            }
+
+            info.setState(Bundle.ACTIVE);
+
+            fireBundleEvent(BundleEvent.STARTED, bundle);
+        }
+        catch (Throwable th)
+        {
+            // If there was an error starting the bundle,
+            // then reset its state to RESOLVED.
+            info.setState(Bundle.RESOLVED);
+
+            // Unregister any services offered by this bundle.
+            m_registry.unregisterServices(bundle);
+
+            // Release any services being used by this bundle.
+            m_registry.ungetServices(bundle);
+
+            // Remove any listeners registered by this bundle.
+            removeListeners(bundle);
+
+            // The spec says to expect BundleException or
+            // SecurityException, so rethrow these exceptions.
+            if (th instanceof BundleException)
+            {
+                throw (BundleException) th;
+            }
+            else if (th instanceof SecurityException)
+            {
+                throw (SecurityException) th;
+            }
+            // Convert a privileged action exception to the
+            // nested exception.
+            else if (th instanceof PrivilegedActionException)
+            {
+                th = ((PrivilegedActionException) th).getException();
+            }
+
+            // Rethrow all other exceptions as a BundleException.
+            throw new BundleException("Activator start error.", th);
+        }
+    }
+
+    protected void _resolveBundle(BundleImpl bundle)
+        throws BundleException
+    {
+        // If a security manager is installed, then check for permission
+        // to import the necessary packages.
+        if (System.getSecurityManager() != null)
+        {
+            URL url = null;
+            try
+            {
+                url = new URL(bundle.getInfo().getLocation());
+            }
+            catch (MalformedURLException ex)
+            {
+                throw new BundleException("Cannot resolve, bad URL "
+                    + bundle.getInfo().getLocation());
+            }
+
+//            try
+//            {
+//                AccessController.doPrivileged(new CheckImportsPrivileged(url, bundle));
+//            }
+//            catch (PrivilegedActionException ex)
+//            {
+//                Exception thrown = ((PrivilegedActionException) ex).getException();
+//                if (thrown instanceof AccessControlException)
+//                {
+//                    throw (AccessControlException) thrown;
+//                }
+//                else
+//                {
+//                    throw new BundleException("Problem resolving: " + ex);
+//                }
+//            }
+        }
+
+        // Get the import search policy.
+        R4SearchPolicy search = (R4SearchPolicy) m_mgr.getSearchPolicy();
+
+        Module module = bundle.getInfo().getCurrentModule();
+        try
+        {
+            search.resolve(module);
+        }
+        catch (ResolveException ex)
+        {
+            if (ex.getModule() != null)
+            {
+                throw new BundleException(
+                    "Unresolved package in bundle "
+                    + BundleInfo.getBundleIdFromModuleId(ex.getModule().getId())
+                    + ": " + ex.getPackage());
+            }
+            else
+            {
+                throw new BundleException(ex.getMessage());
+            }
+        }
+
+        bundle.getInfo().setState(Bundle.RESOLVED);
+    }
+
+    protected void updateBundle(BundleImpl bundle, InputStream is)
+        throws BundleException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        // Acquire bundle lock.
+        acquireBundleLock(bundle);
+
+        try
+        {
+            _updateBundle(bundle, is);
+        }
+        finally
+        {
+            // Release bundle lock.
+            releaseBundleLock(bundle);
+        }
+    }
+
+    protected void _updateBundle(BundleImpl bundle, InputStream is)
+        throws BundleException
+    {
+        // We guarantee to close the input stream, so put it in a
+        // finally clause.
+    
+        try
+        {
+            // Variable to indicate whether bundle is active or not.
+            Exception rethrow = null;
+
+            // Cannot update an uninstalled bundle.
+            BundleInfo info = bundle.getInfo();
+            if (info.getState() == Bundle.UNINSTALLED)
+            {
+                throw new IllegalStateException("The bundle is uninstalled.");
+            }
+
+            // First get the update-URL from our header.
+            String updateLocation = (String)
+                info.getCurrentHeader().get(Constants.BUNDLE_UPDATELOCATION);
+
+            // If no update location specified, use original location.
+            if (updateLocation == null)
+            {
+                updateLocation = info.getLocation();
+            }
+
+            // Stop the bundle, but do not change the persistent state.
+            stopBundle(bundle, false);
+
+            try
+            {
+                // Get the URL input stream if necessary.
+                if (is == null)
+                {
+                    // Do it the manual way to have a chance to 
+                    // set request properties such as proxy auth.
+                    URL url = new URL(updateLocation);
+                    URLConnection conn = url.openConnection(); 
+
+                    // Support for http proxy authentication.
+                    String auth = System.getProperty("http.proxyAuth");
+                    if ((auth != null) && (auth.length() > 0))
+                    {
+                        if ("http".equals(url.getProtocol()) ||
+                            "https".equals(url.getProtocol()))
+                        {
+                            String base64 = Util.base64Encode(auth);
+                            conn.setRequestProperty(
+                                "Proxy-Authorization", "Basic " + base64);
+                        }
+                    }
+                    is = conn.getInputStream();
+                }
+
+                // Get the bundle's archive.
+                BundleArchive archive = m_cache.getArchive(info.getBundleId());
+                // Update the bundle; this operation will increase
+                // the revision count for the bundle.
+                m_cache.update(archive, is);
+                // Create a module for the new revision; the revision is
+                // base zero, so subtract one from the revision count to
+                // get the revision of the new update.
+                Module module = createModule(
+                    info.getBundleId(),
+                    archive.getRevisionCount() - 1,
+                    info.getCurrentHeader());
+                // Add module to bundle info.
+                info.addModule(module);
+            }
+            catch (Exception ex)
+            {
+                m_logger.log(LogWrapper.LOG_ERROR, "Unable to update the bundle.", ex);
+                rethrow = ex;
+            }
+
+            info.setState(Bundle.INSTALLED);
+
+            // Mark as needing a refresh.
+            info.setRemovalPending();
+    
+            // Fire updated event if successful.
+            if (rethrow == null)
+            {
+                fireBundleEvent(BundleEvent.UPDATED, bundle);
+            }
+    
+            // Restart bundle, but do not change the persistent state.
+            // This will not start the bundle if it was not previously
+            // active.
+            startBundle(bundle, false);
+    
+            // If update failed, rethrow exception.
+            if (rethrow != null)
+            {
+                throw new BundleException("Update failed.", rethrow);
+            }
+        }
+        finally
+        {
+            try
+            {
+                if (is != null) is.close();
+            }
+            catch (IOException ex)
+            {
+                m_logger.log(LogWrapper.LOG_ERROR, "Unable to close input stream.", ex);
+            }
+        }
+    }
+
+    protected void stopBundle(BundleImpl bundle, boolean record)
+        throws BundleException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        // Acquire bundle lock.
+        acquireBundleLock(bundle);
+
+        try
+        {
+            _stopBundle(bundle, record);
+        }
+        finally
+        {
+            // Always release bundle lock.
+            releaseBundleLock(bundle);
+        }
+    }
+
+    private void _stopBundle(BundleImpl bundle, boolean record)
+        throws BundleException
+    {
+        Throwable rethrow = null;
+    
+        // Set the bundle's persistent state to inactive if necessary.
+        if (record)
+        {
+            bundle.getInfo().setPersistentStateInactive();
+        }
+
+        BundleInfo info = bundle.getInfo();
+        
+        switch (info.getState())
+        {
+            case Bundle.UNINSTALLED:
+                throw new IllegalStateException("Cannot stop an uninstalled bundle.");
+            case Bundle.STARTING:
+            case Bundle.STOPPING:
+                throw new BundleException("Stopping a bundle that is starting or stopping is currently not supported.");
+            case Bundle.INSTALLED:
+            case Bundle.RESOLVED:
+                return;
+            case Bundle.ACTIVE:
+                // Set bundle state..
+                info.setState(Bundle.STOPPING);
+        }
+            
+        try
+        {
+            if (bundle.getInfo().getActivator() != null)
+            {
+                if (System.getSecurityManager() != null)
+                {
+//                    m_startStopPrivileged.setAction(StartStopPrivileged.STOP_ACTION);
+//                    m_startStopPrivileged.setBundle(bundle);
+//                    AccessController.doPrivileged(m_startStopPrivileged);
+                }
+                else
+                {
+                    info.getActivator().stop(info.getContext());
+                }
+            }
+        
+            // Try to save the activator in the cache.
+            // NOTE: This is non-standard OSGi behavior and only
+            // occurs if strictness is disabled.
+            String strict = m_config.get(FelixConstants.STRICT_OSGI_PROP);
+            boolean isStrict = (strict == null) ? true : strict.equals("true");
+            if (!isStrict)
+            {
+                try
+                {
+                    m_cache.getArchive(info.getBundleId())
+                        .setActivator(info.getActivator());
+                }
+                catch (Exception ex)
+                {
+                    // Problem saving activator, so ignore it.
+                    // TODO: Perhaps we should handle this some other way?
+                }
+            }
+        }
+        catch (Throwable th)
+        {
+            m_logger.log(LogWrapper.LOG_ERROR, "Error stopping bundle.", th);
+            rethrow = th;
+        }
+                  
+        // Unregister any services offered by this bundle.
+        m_registry.unregisterServices(bundle);
+        
+        // Release any services being used by this bundle.
+        m_registry.ungetServices(bundle);
+        
+        // The spec says that we must remove all event
+        // listeners for a bundle when it is stopped.
+        removeListeners(bundle);
+        
+        info.setState(Bundle.RESOLVED);
+        fireBundleEvent(BundleEvent.STOPPED, bundle);
+        
+        // Throw activator error if there was one.
+        if (rethrow != null)
+        {
+            // The spec says to expect BundleException or
+            // SecurityException, so rethrow these exceptions.
+            if (rethrow instanceof BundleException)
+            {
+                throw (BundleException) rethrow;
+            }
+            else if (rethrow instanceof SecurityException)
+            {
+                throw (SecurityException) rethrow;
+            }
+            else if (rethrow instanceof PrivilegedActionException)
+            {
+                rethrow = ((PrivilegedActionException) rethrow).getException();
+            }
+    
+            // Rethrow all other exceptions as a BundleException.
+            throw new BundleException("Activator stop error.", rethrow);
+        }
+    }
+
+    protected void uninstallBundle(BundleImpl bundle) throws BundleException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        // Acquire bundle lock.
+        acquireBundleLock(bundle);
+
+        try
+        {
+            _uninstallBundle(bundle);
+        }
+        finally
+        {
+            // Always release bundle lock.
+            releaseBundleLock(bundle);
+        }
+    }
+
+    private void _uninstallBundle(BundleImpl bundle) throws BundleException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        BundleException rethrow = null;
+
+        BundleInfo info = bundle.getInfo();
+        if (info.getState() == Bundle.UNINSTALLED)
+        {
+            throw new IllegalStateException("The bundle is uninstalled.");
+        }
+
+        // The spec says that uninstall should always succeed, so
+        // catch an exception here if stop() doesn't succeed and
+        // rethrow it at the end.
+        try
+        {
+            stopBundle(bundle, true);
+        }
+        catch (BundleException ex)
+        {
+            rethrow = ex;
+        }
+
+        // Remove the bundle from the installed map.
+        BundleImpl target = null;
+        synchronized (m_installedBundleLock_Priority2)
+        {
+            target = (BundleImpl) m_installedBundleMap.remove(info.getLocation());
+        }
+
+        // Finally, put the uninstalled bundle into the
+        // uninstalled list for subsequent refreshing.
+        if (target != null)
+        {
+            // Set the bundle's persistent state to uninstalled.
+            target.getInfo().setPersistentStateUninstalled();
+
+            // Mark bundle for removal.
+            target.getInfo().setRemovalPending();
+
+            // Put bundle in uninstalled bundle array.
+            rememberUninstalledBundle(bundle);
+        }
+        else
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR, "Unable to remove bundle from installed map!");
+        }
+
+        // Set state to uninstalled.
+        info.setState(Bundle.UNINSTALLED);
+
+        // Fire bundle event.
+        fireBundleEvent(BundleEvent.UNINSTALLED, bundle);
+
+        if (rethrow != null)
+        {
+            throw rethrow;
+        }
+    }
+
+    //
+    // Implementation of BundleContext interface methods.
+    //
+
+    /**
+     * Implementation for BundleContext.getProperty(). Returns
+     * environment property associated with the framework.
+     *
+     * @param key The name of the property to retrieve.
+     * @return The value of the specified property or null.
+    **/
+    protected String getProperty(String key)
+    {
+        // First, check the config properties.
+        String val = (String) m_configProps.get(key);
+        // If not found, then try the system properties.
+        return (val == null) ? System.getProperty(key) : val;
+    }
+
+    protected Bundle installBundle(String location, InputStream is)
+        throws BundleException
+    {
+        return installBundle(-1, location, is);
+    }
+
+    private Bundle installBundle(long id, String location, InputStream is)
+        throws BundleException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+    
+        BundleImpl bundle = null;
+
+        // Acquire an install lock.
+        acquireInstallLock(location);
+
+        try
+        {
+            // Check to see if the framework is still running;
+            if ((getStatus() == Felix.STOPPING_STATUS) ||
+                (getStatus() == Felix.INITIAL_STATUS))
+            {
+                throw new BundleException("The framework has been shutdown.");
+            }
+
+            // If bundle location is already installed, then
+            // return it as required by the OSGi specification.
+            bundle = (BundleImpl) getBundle(location);
+            if (bundle != null)
+            {
+                return bundle;
+            }
+
+            // Determine if this is a new or existing bundle.
+            boolean isNew = (id < 0);
+
+            // If the bundle is new we must cache its JAR file.
+            if (isNew)
+            {
+                // First generate an identifier for it.
+                id = getNextId();
+
+                try
+                {
+                    // Get the URL input stream if necessary.
+                    if (is == null)
+                    {
+                        // Do it the manual way to have a chance to 
+                        // set request properties such as proxy auth.
+                        URL url = new URL(location);
+                        URLConnection conn = url.openConnection(); 
+
+                        // Support for http proxy authentication.
+                        String auth = System.getProperty("http.proxyAuth");
+                        if ((auth != null) && (auth.length() > 0))
+                        {
+                            if ("http".equals(url.getProtocol()) ||
+                                "https".equals(url.getProtocol()))
+                            {
+                                String base64 = Util.base64Encode(auth);
+                                conn.setRequestProperty(
+                                    "Proxy-Authorization", "Basic " + base64);
+                            }
+                        }
+                        is = conn.getInputStream();
+                    }
+                    // Add the bundle to the cache.
+                    m_cache.create(id, location, is);
+                }
+                catch (Exception ex)
+                {
+                    throw new BundleException(
+                        "Unable to cache bundle: " + location, ex);
+                }
+                finally
+                {
+                    try
+                    {
+                        if (is != null) is.close();
+                    }
+                    catch (IOException ex)
+                    {
+                        m_logger.log(
+                            LogWrapper.LOG_ERROR,
+                            "Unable to close input stream.", ex);
+                    }
+                }
+            }
+            else
+            {
+                // If the bundle we are installing is not new,
+                // then try to purge old revisions before installing
+                // it; this is done just in case a "refresh"
+                // didn't occur last session...this would only be
+                // due to an error or system crash.
+                try
+                {
+                    if (m_cache.getArchive(id).getRevisionCount() > 1)
+                    {
+                        m_cache.purge(m_cache.getArchive(id));
+                    }
+                }
+                catch (Exception ex)
+                {
+                    ex.printStackTrace();
+                    m_logger.log(
+                        LogWrapper.LOG_ERROR,
+                        "Could not purge bundle.", ex);
+                }
+            }
+
+            try
+            {
+                BundleArchive archive = m_cache.getArchive(id);
+                bundle = new BundleImpl(this, createBundleInfo(archive));
+            }
+            catch (Exception ex)
+            {
+                // If the bundle is new, then remove it from the cache.
+                // TODO: Perhaps it should be removed if it is not new too.
+                if (isNew)
+                {
+                    try
+                    {
+                        m_cache.remove(m_cache.getArchive(id));
+                    }
+                    catch (Exception ex1)
+                    {
+                        m_logger.log(
+                            LogWrapper.LOG_ERROR,
+                            "Could not remove from cache.", ex1);
+                    }
+                }
+                throw new BundleException("Could not create bundle object.", ex);
+            }
+
+            // If the bundle is new, then set its start level; existing
+            // bundles already have their start level set.
+            if (isNew)
+            {
+                // This will persistently set the bundle's start level.
+                bundle.getInfo().setStartLevel(getInitialBundleStartLevel());
+            }
+
+            synchronized (m_installedBundleLock_Priority2)
+            {
+                m_installedBundleMap.put(location, bundle);
+            }
+        }
+        finally
+        {
+            // Always release install lock.
+            releaseInstallLock(location);
+
+            // Always try to close the input stream.
+            try
+            {
+                if (is != null) is.close();
+            }
+            catch (IOException ex)
+            {
+                m_logger.log(
+                    LogWrapper.LOG_ERROR,
+                    "Unable to close input stream.", ex);
+                // Not much else we can do.
+            }
+        }
+    
+        // Fire bundle event.
+        fireBundleEvent(BundleEvent.INSTALLED, bundle);
+    
+        // Return new bundle.
+        return bundle;
+    }
+
+    /**
+     * Retrieves a bundle from its location.
+     *
+     * @param location The location of the bundle to retrieve.
+     * @return The bundle associated with the location or null if there
+     *         is no bundle associated with the location.
+    **/
+    protected Bundle getBundle(String location)
+    {
+        synchronized (m_installedBundleLock_Priority2)
+        {
+            return (Bundle) m_installedBundleMap.get(location);
+        }
+    }
+
+    /**
+     * Implementation for BundleContext.getBundle(). Retrieves a
+     * bundle from its identifier.
+     *
+     * @param id The identifier of the bundle to retrieve.
+     * @return The bundle associated with the identifier or null if there
+     *         is no bundle associated with the identifier.
+    **/
+    protected Bundle getBundle(long id)
+    {
+        synchronized (m_installedBundleLock_Priority2)
+        {
+            BundleImpl bundle = null;
+
+            for (Iterator i = m_installedBundleMap.values().iterator(); i.hasNext(); )
+            {
+                bundle = (BundleImpl) i.next();
+                if (bundle.getInfo().getBundleId() == id)
+                {
+                    return bundle;
+                }
+            }
+        }
+
+        return null;
+    }
+
+    // Private member for method below.
+    private Comparator m_comparator = null;
+
+    /**
+     * Implementation for BundleContext.getBundles(). Retrieves
+     * all installed bundles.
+     *
+     * @return An array containing all installed bundles or null if
+     *         there are no installed bundles.
+    **/
+    protected Bundle[] getBundles()
+    {
+        if (m_comparator == null)
+        {
+            m_comparator = new Comparator() {
+                public int compare(Object o1, Object o2)
+                {
+                    Bundle b1 = (Bundle) o1;
+                    Bundle b2 = (Bundle) o2;
+                    if (b1.getBundleId() > b2.getBundleId())
+                        return 1;
+                    else if (b1.getBundleId() < b2.getBundleId())
+                        return -1;
+                    return 0;
+                }
+            };
+        }
+
+        Bundle[] bundles = null;
+
+        synchronized (m_installedBundleLock_Priority2)
+        {
+            if (m_installedBundleMap.size() == 0)
+            {
+                return null;
+            }
+
+            bundles = new Bundle[m_installedBundleMap.size()];
+            int counter = 0;
+            for (Iterator i = m_installedBundleMap.values().iterator(); i.hasNext(); )
+            {
+                bundles[counter++] = (Bundle) i.next();
+            }
+        }
+
+        Arrays.sort(bundles, m_comparator);
+
+        return bundles;
+    }
+
+    protected void addBundleListener(Bundle bundle, BundleListener l)
+    {
+        // The spec says do nothing if the listener is
+        // already registered.
+        BundleListenerWrapper old = (BundleListenerWrapper)
+            m_dispatchQueue.getListener(BundleListener.class, l);
+        if (old == null)
+        {
+            l = new BundleListenerWrapper(bundle, l);
+            m_dispatchQueue.addListener(BundleListener.class, l);
+        }
+    }
+
+    protected void removeBundleListener(BundleListener l)
+    {
+        m_dispatchQueue.removeListener(BundleListener.class, l);
+    }
+
+    /**
+     * Implementation for BundleContext.addServiceListener().
+     * Adds service listener to the listener list so that is
+     * can listen for <code>ServiceEvent</code>s.
+     *
+     * @param bundle The bundle that registered the listener.
+     * @param l The service listener to add to the listener list.
+     * @param f The filter for the listener; may be null.
+    **/
+    protected void addServiceListener(Bundle bundle, ServiceListener l, String f)
+        throws InvalidSyntaxException
+    {
+        // The spec says if the listener is already registered,
+        // then replace filter.
+        ServiceListenerWrapper old = (ServiceListenerWrapper)
+            m_dispatchQueue.getListener(ServiceListener.class, l);
+        if (old != null)
+        {
+            old.setFilter((f == null) ? null : new FilterImpl(m_logger, f));
+        }
+        else
+        {
+            l = new ServiceListenerWrapper(
+                bundle, l, (f == null) ? null : new FilterImpl(m_logger, f));
+            m_dispatchQueue.addListener(ServiceListener.class, l);
+        }
+    }
+
+    /**
+     * Implementation for BundleContext.removeServiceListener().
+     * Removes service listeners from the listener list.
+     *
+     * @param l The service listener to remove from the listener list.
+    **/
+    protected void removeServiceListener(ServiceListener l)
+    {
+        m_dispatchQueue.removeListener(ServiceListener.class, l);
+    }
+
+    protected void addFrameworkListener(Bundle bundle, FrameworkListener l)
+    {
+        // The spec says do nothing if the listener is
+        // already registered.
+        FrameworkListenerWrapper old = (FrameworkListenerWrapper)
+            m_dispatchQueue.getListener(FrameworkListener.class, l);
+        if (old == null)
+        {
+            l = new FrameworkListenerWrapper(bundle, l);
+            m_dispatchQueue.addListener(FrameworkListener.class, l);
+        }
+    }
+
+    protected void removeFrameworkListener(FrameworkListener l)
+    {
+        m_dispatchQueue.removeListener(FrameworkListener.class, l);
+    }
+
+    /**
+     * Remove all of the specified bundle's event listeners from
+     * the framework.
+     * @param bundle The bundle whose listeners are to be removed.
+    **/
+    private void removeListeners(Bundle bundle)
+    {
+        if (bundle == null)
+        {
+            return;
+        }
+
+        // Remove all listeners associated with the supplied bundle;
+        // it is only possible to know the bundle associated with a
+        // listener if the listener was wrapper by a ListenerWrapper,
+        // so look for those.
+        Object[] listeners = m_dispatchQueue.getListeners();
+        for (int i = listeners.length - 2; i >= 0; i -= 2)
+        {
+            // Check for listener wrappers and then compare the bundle.
+            if (listeners[i + 1] instanceof ListenerWrapper)
+            {
+                ListenerWrapper lw = (ListenerWrapper) listeners[i + 1];
+                if ((lw.getBundle() != null) && (lw.getBundle().equals(bundle)))
+                {
+                    m_dispatchQueue.removeListener(
+                        (Class) listeners[i], (EventListener) listeners[i+1]);
+                }
+            }
+        }
+    }
+
+    /**
+     * Implementation for BundleContext.registerService(). Registers
+     * a service for the specified bundle bundle.
+     *
+     * @param classNames A string array containing the names of the classes
+     *                under which the new service is available.
+     * @param svcObj The service object or <code>ServiceFactory</code>.
+     * @param dict A dictionary of properties that further describe the
+     *             service or null.
+     * @return A <code>ServiceRegistration</code> object or null.
+    **/
+    protected ServiceRegistration registerService(
+        BundleImpl bundle, String[] classNames, Object svcObj, Dictionary dict)
+    {
+        if (classNames == null)
+        {
+            throw new NullPointerException("Service class names cannot be null.");
+        }
+        else if (svcObj == null)
+        {
+            throw new IllegalArgumentException("Service object cannot be null.");
+        }
+
+        // Check for permission to register all passed in interface names.
+        if (System.getSecurityManager() != null)
+        {
+            for (int i = 0; i < classNames.length; i++)
+            {
+                ServicePermission perm = new ServicePermission(
+                    classNames[i], ServicePermission.REGISTER);
+                AccessController.checkPermission(perm);
+            }
+        }
+
+        // Acquire bundle lock.
+        try
+        {
+            acquireBundleLock(bundle);
+        }
+        catch (BundleException ex)
+        {
+            // This would probably only happen when the bundle is uninstalled.
+            throw new IllegalStateException(
+                "Can only register services while bundle is active or activating.");
+        }
+
+        ServiceRegistration reg = null;
+
+        try
+        {
+            BundleInfo info = bundle.getInfo();
+
+            // Can only register services if starting or active.
+            if ((info.getState() & (Bundle.STARTING | Bundle.ACTIVE)) == 0)
+            {
+                throw new IllegalStateException(
+                    "Can only register services while bundle is active or activating.");
+            }
+
+            // Check to make sure that the service object is
+            // an instance of all service classes; ignore if
+            // service object is a service factory.
+            if (!(svcObj instanceof ServiceFactory))
+            {
+                for (int i = 0; i < classNames.length; i++)
+                {
+                    Class clazz = loadClassUsingClass(svcObj.getClass(), classNames[i]);
+                    if (clazz == null)
+                    {
+                        throw new IllegalArgumentException(
+                            "Cannot cast service: " + classNames[i]);
+                    }
+                    else if (!clazz.isAssignableFrom(svcObj.getClass()))
+                    {
+                        throw new IllegalArgumentException(
+                            "Service object is not an instance of \""
+                            + classNames[i] + "\".");
+                    }
+                }
+            }
+
+            reg = m_registry.registerService(bundle, classNames, svcObj, dict);
+        }
+        finally
+        {
+            // Always release bundle lock.
+            releaseBundleLock(bundle);
+        }
+        
+        // NOTE: The service registered event is fired from the service
+        // registry to the framework, where it is then redistributed to
+        // interested service event listeners.
+
+        return reg;
+    }
+
+    /**
+     * <p>
+     * This is a simple utility class that attempts to load the named
+     * class using the class loader of the supplied class or
+     * the class loader of one of its super classes or their implemented
+     * interfaces. This is necessary during service registration to test
+     * whether a given service object implements its declared service
+     * interfaces.
+     * </p>
+     * <p>
+     * To perform this test, the framework must try to load
+     * the classes associated with the declared service interfaces, so
+     * it must choose a class loader. The class loader of the registering
+     * bundle cannot be used, since this disallows third parties to
+     * register service on behalf of another bundle. Consequently, the
+     * class loader of the service object must be used. However, this is
+     * also not sufficient since the class loader of the service object
+     * may not have direct access to the class in question.
+     * </p>
+     * <p>
+     * The service object's class loader may not have direct access to
+     * its service interface if it extends a super class from another
+     * bundle which implements the service interface from an imported
+     * bundle or if it implements an extension of the service interface
+     * from another bundle which imports the base interface from another
+     * bundle. In these cases, the service object's class loader only has
+     * access to the super class's class or the extended service interface,
+     * respectively, but not to the actual service interface.
+     * </p>
+     * <p>
+     * Thus, it is necessary to not only try to load the service interface
+     * class from the service object's class loader, but from the class
+     * loaders of any interfaces it implements and the class loaders of
+     * all super classes.
+     * </p>
+     * @param svcObj the class that is the root of the search.
+     * @param name the name of the class to load.
+     * @return the loaded class or <tt>null</tt> if it could not be
+     *         loaded.
+    **/
+    private static Class loadClassUsingClass(Class clazz, String name)
+    {
+        while (clazz != null)
+        {
+            // Get the class loader of the current class object.
+            ClassLoader loader = clazz.getClassLoader();
+            // A null class loader represents the system class loader.
+            loader = (loader == null) ? ClassLoader.getSystemClassLoader() : loader;
+            try
+            {
+                return loader.loadClass(name);
+            }
+            catch (ClassNotFoundException ex)
+            {
+                // Ignore and try interface class loaders.
+            }
+
+            // Try to see if we can load the class from
+            // one of the class's implemented interface
+            // class loaders.
+            Class[] ifcs = clazz.getInterfaces();
+            for (int i = 0; i < ifcs.length; i++)
+            {
+                clazz = loadClassUsingClass(ifcs[i], name);
+                if (clazz != null)
+                {
+                    return clazz;
+                }
+            }
+
+            // Try to see if we can load the class from
+            // the super class class loader.
+            clazz = clazz.getSuperclass();
+        }
+
+        return null;
+    }
+
+    protected ServiceReference[] getServiceReferences(
+        BundleImpl bundle, String className, String expr)
+        throws InvalidSyntaxException
+    {
+        // Define filter if expression is not null.
+        Filter filter = null;
+        if (expr != null)
+        {
+            filter = new FilterImpl(m_logger, expr);
+        }
+
+        // Ask the service registry for all matching service references.
+        List refList = m_registry.getServiceReferences(className, filter);
+
+        // The returned reference list must be filtered for two cases:
+        // 1) The requesting bundle may not be wired to the same class
+        //    as the providing bundle (i.e, different versions), so filter
+        //    any services for which the requesting bundle might get a
+        //    class cast exception.
+        // 2) Security is enabled and the requesting bundle does not have
+        //    permission access the service.
+        for (int refIdx = 0; (refList != null) && (refIdx < refList.size()); refIdx++)
+        {
+            // Get the current service reference.
+            ServiceReference ref = (ServiceReference) refList.get(refIdx);
+
+            // Get the service's objectClass property.
+            String[] objectClass = (String[]) ref.getProperty(FelixConstants.OBJECTCLASS);
+
+            // Boolean flag.
+            boolean allow = false;
+
+            // Filter the service reference if the requesting bundle
+            // does not have permission.
+            if (System.getSecurityManager() != null)
+            {
+                for (int classIdx = 0;
+                    !allow && (classIdx < objectClass.length);
+                    classIdx++)
+                {
+                    try
+                    {
+                        ServicePermission perm = new ServicePermission(
+                            objectClass[classIdx], ServicePermission.GET);
+                        AccessController.checkPermission(perm);
+                        // The bundle only needs permission for one
+                        // of the service interfaces, so break out
+                        // of the loop when permission is granted.
+                        allow = true;
+                    }
+                    catch (Exception ex)
+                    {
+                        // We do not throw this exception since the bundle
+                        // is not supposed to know about the service at all
+                        // if it does not have permission.
+                        m_logger.log(LogWrapper.LOG_ERROR, ex.getMessage());
+                    }
+                }
+                
+                if (!allow)
+                {
+                    refList.remove(refIdx);
+                    refIdx--;
+                    continue;
+                }
+            }
+
+            // Now check for castability.
+            if (!isServiceAssignable(bundle, ref))
+            {
+                refList.remove(refIdx);
+                refIdx--;
+            }
+        }
+
+        if (refList.size() > 0)
+        {
+            return (ServiceReference[]) refList.toArray(new ServiceReference[refList.size()]);
+        }
+
+        return null;
+    }
+
+    /**
+     * This method determines if the requesting bundle is able to cast
+     * the specified service reference based on class visibility rules
+     * of the underlying modules.
+     * @param requester The bundle requesting the service.
+     * @param ref The service in question.
+     * @return <tt>true</tt> if the requesting bundle is able to case
+     *         the service object to a known type.
+    **/
+    protected boolean isServiceAssignable(BundleImpl requester, ServiceReference ref)
+    {
+        // Boolean flag.
+        boolean allow = true;
+        // Get the service's objectClass property.
+        String[] objectClass = (String[]) ref.getProperty(FelixConstants.OBJECTCLASS);
+
+        // The the service reference is not assignable when the requesting
+        // bundle is wired to a different version of the service object.
+        // NOTE: We are pessimistic here, if any class in the service's
+        // objectClass is not usable by the requesting bundle, then we
+        // disallow the service reference.
+        for (int classIdx = 0; (allow) && (classIdx < objectClass.length); classIdx++)
+        {
+            if (!ref.isAssignableTo(requester, objectClass[classIdx]))
+            {
+                allow = false;
+            }
+        }
+        return allow;
+    }
+
+    protected Object getService(Bundle bundle, ServiceReference ref)
+    {
+        // Check that the bundle has permission to get at least
+        // one of the service interfaces; the objectClass property
+        // of the service stores its service interfaces.
+        String[] objectClass = (String[])
+            ref.getProperty(Constants.OBJECTCLASS);
+        if (objectClass == null)
+        {
+            return null;
+        }
+
+        boolean hasPermission = false;
+        if (System.getSecurityManager() != null)
+        {
+            for (int i = 0;
+                !hasPermission && (i < objectClass.length);
+                i++)
+            {
+                try
+                {
+                    ServicePermission perm =
+                        new ServicePermission(
+                            objectClass[i], ServicePermission.GET);
+                    AccessController.checkPermission(perm);
+                    hasPermission = true;
+                }
+                catch (Exception ex)
+                {
+                }
+            }
+        }
+        else
+        {
+            hasPermission = true;
+        }
+
+        // If the bundle does not permission to access the service,
+        // then return null.
+        if (!hasPermission)
+        {
+            return null;
+        }
+
+        return m_registry.getService(bundle, ref);
+    }
+
+    protected boolean ungetService(Bundle bundle, ServiceReference ref)
+    {
+        return m_registry.ungetService(bundle, ref);
+    }
+
+    protected File getDataFile(BundleImpl bundle, String s)
+    {
+        // The spec says to throw an error if the bundle
+        // is stopped, which I assume means not active,
+        // starting, or stopping.
+        if ((bundle.getInfo().getState() != Bundle.ACTIVE) &&
+            (bundle.getInfo().getState() != Bundle.STARTING) &&
+            (bundle.getInfo().getState() != Bundle.STOPPING))
+        {
+            throw new IllegalStateException("Only active bundles can create files.");
+        }
+        try
+        {
+            return m_cache.getArchive(
+                bundle.getInfo().getBundleId()).getDataFile(s);
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(LogWrapper.LOG_ERROR, ex.getMessage());
+            return null;
+        }
+    }
+
+    //
+    // PackageAdmin related methods.
+    //
+
+    /**
+     * Returns the exported package associated with the specified
+     * package name. This is used by the PackageAdmin service
+     * implementation.
+     *
+     * @param name The name of the exported package to find.
+     * @return The exported package or null if no matching package was found.
+    **/
+    protected ExportedPackage getExportedPackage(String pkgName)
+    {
+        // First, find the bundle exporting the package.
+        BundleImpl bundle = null;
+        R4SearchPolicy search = (R4SearchPolicy) m_mgr.getSearchPolicy();
+        Module[] exporters = search.getInUseExporters(new R4Package(pkgName, null, null));
+        if (exporters != null)
+        {
+            // Since OSGi R4 there may be more than one exporting, so just
+            // take the first one.
+            bundle = (BundleImpl) getBundle(
+                BundleInfo.getBundleIdFromModuleId(exporters[0].getId()));
+        }
+
+        // If we have found the exporting bundle, then return the
+        // exported package interface instance.
+        if (bundle != null)
+        {
+            // We need to find the version of the exported package, but this
+            // is tricky since there may be multiple versions of the package
+            // offered by a given bundle, since multiple revisions of the
+            // bundle JAR file may exist if the bundle was updated without
+            // refreshing the framework. In this case, each revision of the
+            // bundle JAR file is represented as a module in the BundleInfo
+            // module array, which is ordered from oldest to newest. We assume
+            // that the first module found to be exporting the package is the
+            // provider of the package, which makes sense since it must have
+            // been resolved first.
+            Module[] modules = bundle.getInfo().getModules();
+            for (int modIdx = 0; modIdx < modules.length; modIdx++)
+            {
+                R4Package pkg = R4SearchPolicy.getExportPackage(modules[modIdx], pkgName);
+                if (pkg != null)
+                {
+                    return new ExportedPackageImpl(this, bundle, pkgName, pkg.getVersionLow());
+                }
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Returns an array of all actively exported packages from the specified
+     * bundle or if the specified bundle is <tt>null</tt> an array
+     * containing all actively exported packages by all bundles.
+     *
+     * @param b The bundle whose exported packages are to be retrieved
+     *        or <tt>null</tt> if the exported packages of all bundles are
+     *        to be retrieved.
+     * @return An array of exported packages.
+    **/
+    protected ExportedPackage[] getExportedPackages(Bundle b)
+    {
+        List list = new ArrayList();
+
+        // If a bundle is specified, then return its
+        // exported packages.
+        if (b != null)
+        {
+            BundleImpl bundle = (BundleImpl) b;
+            getExportedPackages(bundle, list);
+        }
+        // Otherwise return all exported packages.
+        else
+        {
+            // To create a list of all exported packages, we must look
+            // in the installed and uninstalled sets of bundles. To
+            // ensure a somewhat consistent view, we will gather all
+            // of this information from within the installed bundle
+            // lock.
+            synchronized (m_installedBundleLock_Priority2)
+            {
+                // First get exported packages from uninstalled bundles.
+                synchronized (m_uninstalledBundlesLock_Priority3)
+                {
+                    for (int bundleIdx = 0;
+                        (m_uninstalledBundles != null) && (bundleIdx < m_uninstalledBundles.length);
+                        bundleIdx++)
+                    {
+                        BundleImpl bundle = m_uninstalledBundles[bundleIdx];
+                        getExportedPackages(bundle, list);
+                    }
+                }
+
+                // Now get exported packages from installed bundles.
+                Bundle[] bundles = getBundles();
+                for (int bundleIdx = 0; bundleIdx < bundles.length; bundleIdx++)
+                {
+                    BundleImpl bundle = (BundleImpl) bundles[bundleIdx];
+                    getExportedPackages(bundle, list);
+                }
+            }
+        }
+
+        return (ExportedPackage[]) list.toArray(new ExportedPackage[list.size()]);
+    }
+
+    /**
+     * Adds any current active exported packages from the specified bundle
+     * to the passed in list.
+     * @param bundle The bundle from which to retrieve exported packages.
+     * @param list The list to which the exported packages are added
+    **/
+    private void getExportedPackages(BundleImpl bundle, List list)
+    {
+        R4SearchPolicy policy = (R4SearchPolicy) m_mgr.getSearchPolicy();
+
+        // Since a bundle may have many modules associated with it,
+        // one for each revision in the cache, search each module
+        // for each revision to get all exports.
+        Module[] modules = bundle.getInfo().getModules();
+        for (int modIdx = 0; modIdx < modules.length; modIdx++)
+        {
+            R4Package[] exports = R4SearchPolicy.getExportsAttr(modules[modIdx]);
+            if (exports.length > 0)
+            {
+                for (int expIdx = 0; expIdx < exports.length; expIdx++)
+                {
+                    // See if the target bundle's module is one of the
+                    // "in use" exporters of the package.
+                    Module[] inUseModules = policy.getInUseExporters(exports[expIdx]);
+                    if (R4SearchPolicy.isModuleInArray(inUseModules, modules[modIdx]))
+                    {
+                        list.add(new ExportedPackageImpl(
+                            this, bundle, exports[expIdx].getId(), exports[expIdx].getVersionLow()));
+                    }
+                }
+            }
+        }
+    }
+
+    protected Bundle[] getImportingBundles(ExportedPackage ep)
+    {
+        // Get exporting bundle; we need to use this internal
+        // method because the spec says ep.getExportingBundle()
+        // should return null if the package is stale.
+        BundleImpl exporter = (BundleImpl)
+            ((ExportedPackageImpl) ep).getExportingBundleInternal();
+        BundleInfo exporterInfo = exporter.getInfo();
+
+        // Create list for storing importing bundles.
+        List list = new ArrayList();
+        Bundle[] bundles = getBundles();
+
+        // Check all bundles to see who imports the package.
+        for (int bundleIdx = 0; bundleIdx < bundles.length; bundleIdx++)
+        {
+            BundleImpl importer = (BundleImpl) bundles[bundleIdx];
+
+            // Ignore the bundle if it imports from itself.
+            if (exporter != importer)
+            {
+                // Check the import wires of all modules for all bundles.
+                Module[] modules = importer.getInfo().getModules();
+                for (int modIdx = 0; modIdx < modules.length; modIdx++)
+                {
+                    R4Wire wire = R4SearchPolicy.getWire(modules[modIdx], ep.getName());
+    
+                    // If the resolving module is associated with the
+                    // exporting bundle, then add current bundle to
+                    // import list.
+                    if ((wire != null) && exporterInfo.hasModule(wire.m_module))
+                    {
+                        // Add the bundle to the list of importers.
+                        list.add(bundles[bundleIdx]);
+                        break;
+                    }
+                }
+            }
+        }
+
+        // Return the results.
+        if (list.size() > 0)
+        {
+            return (Bundle[]) list.toArray(new Bundle[list.size()]);
+        }
+
+        return null;
+    }
+
+    protected void refreshPackages(Bundle[] targets)
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+
+        // Acquire locks for all impacted bundles.
+        BundleImpl[] bundles = acquireBundleRefreshLocks(targets);
+
+        // Remove any targeted bundles from the uninstalled bundles
+        // array, since they will be removed from the system after
+        // the refresh.
+        for (int i = 0; (bundles != null) && (i < bundles.length); i++)
+        {
+            forgetUninstalledBundle(bundles[i]);
+        }
+
+        try
+        {
+            // If there are targets, then refresh each one.
+            if (bundles != null)
+            {
+                // At this point the map contains every bundle that has been
+                // updated and/or removed as well as all bundles that import
+                // packages from these bundles.
+                
+                // Create refresh helpers for each bundle.
+                RefreshHelper[] helpers = new RefreshHelper[bundles.length];
+                for (int i = 0; i < bundles.length; i++)
+                {
+                    helpers[i] = new RefreshHelper(bundles[i]);
+                }
+
+                // Stop, purge or remove, and reinitialize all bundles first.
+                for (int i = 0; i < helpers.length; i++)
+                {
+                    helpers[i].stop();
+                    helpers[i].purgeOrRemove();
+                    helpers[i].reinitialize();
+                }
+
+                // Then restart all bundles that were previously running.
+                for (int i = 0; i < helpers.length; i++)
+                {
+                    helpers[i].restart();
+                }
+            }
+        }
+        finally
+        {
+            // Always release all bundle locks.
+            releaseBundleLocks(bundles);
+        }
+
+        fireFrameworkEvent(FrameworkEvent.PACKAGES_REFRESHED, getBundle(0), null);
+    }
+
+    private void populateImportGraph(BundleImpl target, Map map)
+    {
+        // Get the exported packages for the specified bundle.
+        ExportedPackage[] pkgs = getExportedPackages(target);
+
+        for (int pkgIdx = 0; (pkgs != null) && (pkgIdx < pkgs.length); pkgIdx++)
+        {
+            // Get all imports of this package.
+            Bundle[] importers = getImportingBundles(pkgs[pkgIdx]);
+
+            for (int impIdx = 0;
+                (importers != null) && (impIdx < importers.length);
+                impIdx++)
+            {
+                    // Add each importing bundle to map.
+                    map.put(importers[impIdx], importers[impIdx]);
+                    // Now recurse into each bundle to get its importers.
+                    populateImportGraph(
+                        (BundleImpl) importers[impIdx], map);
+            }
+        }
+    }
+
+    //
+    // Miscellaneous private methods.
+    //
+
+    private BundleInfo createBundleInfo(BundleArchive archive)
+        throws Exception
+    {
+        // Get the bundle manifest.
+        Map headerMap = null;
+        try
+        {
+            // Although there should only ever be one revision at this
+            // point, get the header for the current revision to be safe.
+            headerMap = archive.getManifestHeader(archive.getRevisionCount() - 1);
+        }
+        catch (Exception ex)
+        {
+            throw new BundleException("Unable to read JAR manifest.", ex);
+        }
+
+        // We can't do anything without the manifest header.
+        if (headerMap == null)
+        {
+            throw new BundleException("Unable to read JAR manifest header.");
+        }
+
+        // Create the module for the bundle; although there should only
+        // ever be one revision at this point, create the module for
+        // the current revision to be safe.
+        Module module = createModule(
+            archive.getId(), archive.getRevisionCount() - 1, headerMap);
+
+        // Finally, create an return the bundle info.
+        return new BundleInfo(m_logger, archive, module);
+    }
+
+    /**
+     * Creates a module for a given bundle by reading the bundle's
+     * manifest meta-data and converting it to work with the underlying
+     * import/export search policy of the module loader.
+     * @param id The identifier of the bundle for which the module should
+     *        be created.
+     * @param headers The headers map associated with the bundle.
+     * @return The initialized and/or newly created module.
+    **/
+    private Module createModule(long id, int revision, Map headerMap)
+        throws Exception
+    {
+        // Get the manifest version.
+        String version = (String) headerMap.get(FelixConstants.BUNDLE_MANIFESTVERSION);
+        version = (version == null) ? "1" : version;
+        if (!version.equals("1") && !version.equals("2"))
+        {
+            throw new BundleException("Unknown 'Bundle-ManifestVersion' value: " + version);
+        }
+
+        // Create the resource sources for the bundle. The resource sources
+        // are comprised of the bundle's class path values (as JarResourceSources).
+        ResourceSource[] resSources = null;
+        try
+        {
+            // Get bundle class path for the specified revision from cache.
+            String[] classPath = m_cache.getArchive(id).getClassPath(revision);
+
+            // Create resource sources for everything.
+            resSources = new ResourceSource[classPath.length];
+            for (int i = 0; i < classPath.length; i++)
+            {
+                resSources[i] = new JarResourceSource(classPath[i]);
+            }
+        }
+        catch (Exception ex)
+        {
+            ex.printStackTrace();
+            throw new BundleException("Error in class path: " + ex);
+        }
+
+        // Get import packages from bundle manifest.
+        R4Package[] imports = R4Package.parseImportOrExportHeader(
+            (String) headerMap.get(Constants.IMPORT_PACKAGE));
+
+
+        // Check to make sure that R3 bundles have only specified
+        // the 'specification-version' attribute and no directives.
+        if (version.equals("1"))
+        {
+            for (int i = 0; (imports != null) && (i < imports.length); i++)
+            {
+                if (imports[i].getDirectives().length != 0)
+                {
+                    throw new BundleException("R3 imports cannot contain directives.");
+                }
+                // NOTE: This is checking for "version" rather than "specification-version"
+                // because the package class normalizes to "version" to avoid having
+                // future special cases. This could be changed if more strict behavior
+                // is required.
+                if ((imports[i].getVersionHigh() != null) ||
+                    (imports[i].getAttributes().length > 1) ||
+                    ((imports[i].getAttributes().length == 1) &&
+                        (!imports[i].getAttributes()[0].getName().equals(FelixConstants.VERSION_ATTRIBUTE))))
+                {
+                    throw new BundleException(
+                        "Import does not conform to R3 syntax: " + imports[i]);
+                }
+            }
+        }
+
+        // Get export packages from bundle manifest.
+        R4Package[] exports = R4Package.parseImportOrExportHeader(
+            (String) headerMap.get(Constants.EXPORT_PACKAGE));
+
+        // Check to make sure that R3 bundles have only specified
+        // the 'specification-version' attribute and no directives.
+        // In addition, all R3 exports imply imports, so add a
+        // corresponding import for each export.
+        if (version.equals("1"))
+        {
+            for (int i = 0; (exports != null) && (i < exports.length); i++)
+            {
+                if (exports[i].getDirectives().length != 0)
+                {
+                    throw new BundleException("R3 exports cannot contain directives.");
+                }
+                // NOTE: This is checking for "version" rather than "specification-version"
+                // because the package class normalizes to "version" to avoid having
+                // future special cases. This could be changed if more strict behavior
+                // is required.
+                if ((exports[i].getAttributes().length > 1) ||
+                    ((exports[i].getAttributes().length == 1) &&
+                        (!exports[i].getAttributes()[0].getName().equals(FelixConstants.VERSION_ATTRIBUTE))))
+                {
+                    throw new BundleException(
+                        "Export does not conform to R3 syntax: " + imports[i]);
+                }
+            }
+            
+            R4Package[] newImports = new R4Package[imports.length + exports.length];
+            System.arraycopy(imports, 0, newImports, 0, imports.length);
+            System.arraycopy(exports, 0, newImports, imports.length, exports.length);
+            imports = newImports;
+        }
+
+        // For R3 bundles, add a "uses" directive onto each export
+        // that references every other import (which will include
+        // exports, since export implies import); this is
+        // necessary since R3 bundles assumed a single class space,
+        // but R4 allows for multiple class spaces.
+        if (version.equals("1"))
+        {
+            String usesValue = "";
+            for (int i = 0; (imports != null) && (i < imports.length); i++)
+            {
+                usesValue = usesValue
+                    + ((usesValue.length() > 0) ? "," : "")
+                    + imports[i].getId();
+            }
+            R4Directive uses = new R4Directive(
+                FelixConstants.USES_DIRECTIVE, usesValue);
+            for (int i = 0; (exports != null) && (i < exports.length); i++)
+            {
+                exports[i] = new R4Package(
+                    exports[i].getId(),
+                    new R4Directive[] { uses },
+                    exports[i].getAttributes());
+            }
+        }
+
+// TODO: CHECK FOR DUPLICATE IMPORTS/EXPORTS HERE.
+
+        // Get dynamic import packages from bundle manifest.
+        R4Package[] dynamics = R4Package.parseImportOrExportHeader(
+            (String) headerMap.get(Constants.DYNAMICIMPORT_PACKAGE));
+
+        // Check to make sure that R3 bundles have no attributes or
+        // directives.
+        if (version.equals("1"))
+        {
+            for (int i = 0; (dynamics != null) && (i < dynamics.length); i++)
+            {
+                if (dynamics[i].getDirectives().length != 0)
+                {
+                    throw new BundleException("R3 dynamic imports cannot contain directives.");
+                }
+                if (dynamics[i].getAttributes().length != 0)
+                {
+                    throw new BundleException("R3 dynamic imports cannot contain attributes.");
+                }
+            }
+        }
+
+        Object[][] attributes = {
+            new Object[] { R4SearchPolicy.EXPORTS_ATTR, exports },
+            new Object[] { R4SearchPolicy.IMPORTS_ATTR, imports },
+            new Object[] { R4SearchPolicy.DYNAMICIMPORTS_ATTR, dynamics }
+        };
+
+        // Get native library entry names for module library sources.
+        LibraryInfo[] libraries =
+            Util.parseLibraryStrings(
+                Util.parseDelimitedString(
+                    (String) headerMap.get(Constants.BUNDLE_NATIVECODE), ","));
+        LibrarySource[] libSources = {
+            new OSGiLibrarySource(
+                m_logger, m_cache, id, revision,
+                getProperty(Constants.FRAMEWORK_OS_NAME),
+                getProperty(Constants.FRAMEWORK_PROCESSOR),
+                libraries)
+        };
+
+        Module module =
+            m_mgr.addModule(
+                Long.toString(id) + "." + Integer.toString(revision),
+                attributes, resSources, libSources);
+
+        return module;
+    }
+
+    private BundleActivator createBundleActivator(BundleInfo info)
+        throws Exception
+    {
+        // CONCURRENCY NOTE:
+        // This method is called indirectly from startBundle() (via _startBundle()),
+        // which has the exclusion lock, so there is no need to do any locking here.
+    
+        BundleActivator activator = null;
+    
+        String strict = m_config.get(FelixConstants.STRICT_OSGI_PROP);
+        boolean isStrict = (strict == null) ? true : strict.equals("true");
+        if (!isStrict)
+        {
+            try
+            {
+                activator =
+                    m_cache.getArchive(info.getBundleId())
+                        .getActivator(info.getCurrentModule().getClassLoader());
+            }
+            catch (Exception ex)
+            {
+                activator = null;
+            }
+        }
+    
+        // If there was no cached activator, then get the activator
+        // class from the bundle manifest.
+        if (activator == null)
+        {
+            // Get the associated bundle archive.
+            BundleArchive ba = m_cache.getArchive(info.getBundleId());
+            // Get the manifest from the current revision; revision is
+            // base zero so subtract one from the count to get the
+            // current revision.
+            Map headerMap = ba.getManifestHeader(ba.getRevisionCount() - 1);
+            // Get the activator class attribute.
+            String className = (String) headerMap.get(Constants.BUNDLE_ACTIVATOR);
+            // Try to instantiate activator class if present.
+            if (className != null)
+            {
+                className = className.trim();
+                Class clazz = info.getCurrentModule().getClassLoader().loadClass(className);
+                if (clazz == null)
+                {
+                    throw new BundleException("Not found: "
+                        + className);
+                }
+                activator = (BundleActivator) clazz.newInstance();
+            }
+        }
+    
+        return activator;
+    }
+
+    private void purgeBundle(BundleImpl bundle) throws Exception
+    {
+        // Acquire bundle lock.
+        acquireBundleLock(bundle);
+
+        try
+        {
+            BundleInfo info = bundle.getInfo();
+    
+            // In case of a refresh, then we want to physically
+            // remove the bundle's modules from the module manager.
+            // This is necessary for two reasons: 1) because
+            // under Windows we won't be able to delete the bundle
+            // because files might be left open in the resource
+            // sources of its modules and 2) we want to make sure
+            // that no references to old modules exist since they
+            // will all be stale after the refresh. The only other
+            // way to do this is to remove the bundle, but that
+            // would be incorrect, because this is a refresh operation
+            // and should not trigger bundle REMOVE events.
+            Module[] modules = info.getModules();
+            for (int i = 0; i < modules.length; i++)
+            {
+                m_mgr.removeModule(modules[i]);
+            }
+
+            // Purge all bundle revisions, but the current one.
+            m_cache.purge(m_cache.getArchive(info.getBundleId()));
+        }
+        finally
+        {
+            // Always release the bundle lock.
+            releaseBundleLock(bundle);
+        }
+    }
+
+    private void garbageCollectBundle(BundleImpl bundle) throws Exception
+    {
+        // CONCURRENCY NOTE: There is no reason to lock this bundle,
+        // because this method is only called during shutdown or a
+        // refresh operation and these are already guarded by locks.
+
+        // Remove the bundle's associated modules from
+        // the module manager.
+        Module[] modules = bundle.getInfo().getModules();
+        for (int i = 0; i < modules.length; i++)
+        {
+            m_mgr.removeModule(modules[i]);
+        }
+
+        // Remove the bundle from the cache.
+        m_cache.remove(m_cache.getArchive(bundle.getInfo().getBundleId()));
+    }
+
+    //
+    // Event-related methods.
+    //
+
+    /**
+     * Fires bundle events.
+    **/
+    private void fireFrameworkEvent(
+        int type, Bundle bundle, Throwable throwable)
+    {
+        if (m_frameworkDispatcher == null)
+        {
+            m_frameworkDispatcher = new Dispatcher() {
+                public void dispatch(EventListener l, EventObject eventObj)
+                {
+                    ((FrameworkListener) l)
+                        .frameworkEvent((FrameworkEvent) eventObj);
+                }
+            };
+        }
+        FrameworkEvent event = new FrameworkEvent(type, bundle, throwable);
+        m_dispatchQueue.dispatch(
+            m_frameworkDispatcher, FrameworkListener.class, event);
+    }
+
+    /**
+     * Fires bundle events.
+     *
+     * @param type The type of bundle event to fire.
+     * @param bundle The bundle associated with the event.
+    **/
+    private void fireBundleEvent(int type, Bundle bundle)
+    {
+        if (m_bundleDispatcher == null)
+        {
+            m_bundleDispatcher = new Dispatcher() {
+                public void dispatch(EventListener l, EventObject eventObj)
+                {
+                    ((BundleListener) l)
+                        .bundleChanged((BundleEvent) eventObj);
+                }
+            };
+        }
+        BundleEvent event = null;
+        event = new BundleEvent(type, bundle);
+        m_dispatchQueue.dispatch(m_bundleDispatcher,
+            BundleListener.class, event);
+    }
+
+    /**
+     * Fires service events.
+     *
+     * @param type The type of service event to fire.
+     * @param ref The service reference associated with the event.
+    **/
+    private void fireServiceEvent(ServiceEvent event)
+    {
+        if (m_serviceDispatcher == null)
+        {
+            m_serviceDispatcher = new Dispatcher() {
+                public void dispatch(EventListener l, EventObject eventObj)
+                {
+// TODO: Filter service events based on service permissions.
+                    if (l instanceof ListenerWrapper)
+                    {
+                        BundleImpl bundle = (BundleImpl) ((ServiceListenerWrapper) l).getBundle();
+                        if (isServiceAssignable(bundle, ((ServiceEvent) eventObj).getServiceReference()))
+                        {
+                            ((ServiceListener) l)
+                                .serviceChanged((ServiceEvent) eventObj);
+                        }
+                    }
+                    else
+                    {
+                        ((ServiceListener) l)
+                            .serviceChanged((ServiceEvent) eventObj);
+                    }
+                }
+            };
+        }
+        m_dispatchQueue.dispatch(m_serviceDispatcher,
+            ServiceListener.class, event);
+    }
+
+    //
+    // Property related methods.
+    //
+
+    private void initializeFrameworkProperties()
+    {
+        // Standard OSGi properties.
+        m_configProps.put(
+            FelixConstants.FRAMEWORK_VERSION,
+            FelixConstants.FRAMEWORK_VERSION_VALUE);
+        m_configProps.put(
+            FelixConstants.FRAMEWORK_VENDOR,
+            FelixConstants.FRAMEWORK_VENDOR_VALUE);
+        m_configProps.put(
+            FelixConstants.FRAMEWORK_LANGUAGE,
+            System.getProperty("user.language"));
+        m_configProps.put(
+            FelixConstants.FRAMEWORK_OS_VERSION,
+            System.getProperty("os.version"));
+
+        String s = null;
+        s = OSGiLibrarySource.normalizePropertyValue(
+            FelixConstants.FRAMEWORK_OS_NAME,
+            System.getProperty("os.name"));
+        m_configProps.put(FelixConstants.FRAMEWORK_OS_NAME, s);
+        s = OSGiLibrarySource.normalizePropertyValue(
+            FelixConstants.FRAMEWORK_PROCESSOR,
+            System.getProperty("os.arch"));
+        m_configProps.put(FelixConstants.FRAMEWORK_PROCESSOR, s);
+
+        // The framework version property.
+        m_configProps.put(
+            FelixConstants.FELIX_VERSION_PROPERTY,
+            FelixConstants.FELIX_VERSION_VALUE);
+    }
+
+    private void processAutoProperties()
+    {
+        // The auto-install property specifies a space-delimited list of
+        // bundle URLs to be automatically installed into each new profile;
+        // the start level to which the bundles are assigned is specified by
+        // appending a ".n" to the auto-install property name, where "n" is
+        // the desired start level for the list of bundles.
+        String[] keys = m_config.getKeys();
+        for (int i = 0; (keys != null) && (i < keys.length); i++)
+        {
+            if (keys[i].startsWith(FelixConstants.AUTO_INSTALL_PROP))
+            {
+                int startLevel = 1;
+                try
+                {
+                    startLevel = Integer.parseInt(keys[i].substring(keys[i].lastIndexOf('.') + 1));
+                }
+                catch (NumberFormatException ex)
+                {
+                    m_logger.log(LogWrapper.LOG_ERROR, "Invalid property: " + keys[i]);
+                }
+                StringTokenizer st = new StringTokenizer(m_config.get(keys[i]), "\" ",true);
+                if (st.countTokens() > 0)
+                {
+                    String location = null;
+                    do
+                    {
+                        location = nextLocation(st);
+                        if (location != null)
+                        {
+                            try
+                            {
+                                BundleImpl b = (BundleImpl) installBundle(location, null);
+                                b.getInfo().setStartLevel(startLevel);
+                            }
+                            catch (Exception ex)
+                            {
+                                m_logger.log(
+                                    LogWrapper.LOG_ERROR, "Auto-properties install.", ex);
+                            }
+                        }
+                    }
+                    while (location != null);
+                }
+            }
+        }
+
+        // The auto-start property specifies a space-delimited list of
+        // bundle URLs to be automatically installed and started into each
+        // new profile; the start level to which the bundles are assigned
+        // is specified by appending a ".n" to the auto-start property name,
+        // where "n" is the desired start level for the list of bundles.
+        // The following code starts bundles in two passes, first it installs
+        // them, then it starts them.
+        for (int i = 0; (keys != null) && (i < keys.length); i++)
+        {
+            if (keys[i].startsWith(FelixConstants.AUTO_START_PROP))
+            {
+                int startLevel = 1;
+                try
+                {
+                    startLevel = Integer.parseInt(keys[i].substring(keys[i].lastIndexOf('.') + 1));
+                }
+                catch (NumberFormatException ex)
+                {
+                    m_logger.log(LogWrapper.LOG_ERROR, "Invalid property: " + keys[i]);
+                }
+                StringTokenizer st = new StringTokenizer(m_config.get(keys[i]), "\" ",true);
+                if (st.countTokens() > 0)
+                {
+                    String location = null;
+                    do
+                    {
+                        location = nextLocation(st);
+                        if (location != null)
+                        {
+                            try
+                            {
+                                BundleImpl b = (BundleImpl) installBundle(location, null);
+                                b.getInfo().setStartLevel(startLevel);
+                            }
+                            catch (Exception ex)
+                            {
+                                m_logger.log(LogWrapper.LOG_ERROR, "Auto-properties install.", ex);
+                            }
+                        }
+                    }
+                    while (location != null);
+                }
+            }
+        }
+
+        // Now loop through and start the installed bundles.
+        for (int i = 0; (keys != null) && (i < keys.length); i++)
+        {
+            if (keys[i].startsWith(FelixConstants.AUTO_START_PROP))
+            {
+                StringTokenizer st = new StringTokenizer(m_config.get(keys[i]), "\" ",true);
+                if (st.countTokens() > 0)
+                {
+                    String location = null;
+                    do
+                    {
+                        location = nextLocation(st);
+                        if (location != null)
+                        {
+                            // Installing twice just returns the same bundle.
+                            try
+                            {
+                                BundleImpl bundle = (BundleImpl) installBundle(location, null);
+                                if (bundle != null)
+                                {
+                                    startBundle(bundle, true);
+                                }
+                            }
+                            catch (Exception ex)
+                            {
+                                m_logger.log(
+                                    LogWrapper.LOG_ERROR, "Auto-properties start.", ex);
+                            }
+                        }
+                    }
+                    while (location != null);
+                }
+            }
+        }
+    }
+
+    private String nextLocation(StringTokenizer st)
+    {
+        String retVal = null;
+
+        if (st.countTokens() > 0)
+        {
+            String tokenList = "\" ";
+            StringBuffer tokBuf = new StringBuffer(10);
+            String tok = null;
+            boolean inQuote = false;
+            boolean tokStarted = false;
+            boolean exit = false;
+            while ((st.hasMoreTokens()) && (!exit))
+            {
+                tok = st.nextToken(tokenList);
+                if (tok.equals("\""))
+                {
+                    inQuote = ! inQuote;
+                    if (inQuote)
+                    {
+                        tokenList = "\"";
+                    }
+                    else
+                    {
+                        tokenList = "\" ";
+                    }
+
+                }
+                else if (tok.equals(" "))
+                {
+                    if (tokStarted)
+                    {
+                        retVal = tokBuf.toString();
+                        tokStarted=false;
+                        tokBuf = new StringBuffer(10);
+                        exit = true;
+                    }
+                }
+                else
+                {
+                    tokStarted = true;
+                    tokBuf.append(tok.trim());
+                }
+            }
+
+            // Handle case where end of token stream and
+            // still got data
+            if ((!exit) && (tokStarted))
+            {
+                retVal = tokBuf.toString();
+            }
+        }
+
+        return retVal;
+    }
+
+    //
+    // Private utility methods.
+    //
+
+    /**
+     * Generated the next valid bundle identifier.
+    **/
+    private synchronized long getNextId()
+    {
+        return m_nextId++;
+    }
+
+    //
+    // Configuration methods and inner classes.
+    //
+
+    public PropertyResolver getConfig()
+    {
+        return m_config;
+    }
+
+    private class ConfigImpl implements PropertyResolver
+    {
+        public String get(String key)
+        {
+            return (m_configProps == null) ? null : m_configProps.get(key);
+        }
+
+        public String[] getKeys()
+        {
+            return m_configProps.getKeys();
+        }
+    }
+
+    //
+    // Logging methods and inner classes.
+    //
+
+    public LogWrapper getLogger()
+    {
+        return m_logger;
+    }
+
+    /**
+     * Simple class that is used in <tt>refreshPackages()</tt> to embody
+     * the refresh logic in order to keep the code clean. This class is
+     * not static because it needs access to framework event firing methods.
+    **/
+    private class RefreshHelper
+    {
+        private BundleImpl m_bundle = null;
+
+        public RefreshHelper(Bundle bundle)
+        {
+            m_bundle = (BundleImpl) bundle;
+        }
+
+        public void stop()
+        {
+            if (m_bundle.getInfo().getState() == Bundle.ACTIVE)
+            {
+                try
+                {
+                    stopBundle(m_bundle, false);
+                }
+                catch (BundleException ex)
+                {
+                    fireFrameworkEvent(FrameworkEvent.ERROR, m_bundle, ex);
+                }
+            }
+        }
+
+        public void purgeOrRemove()
+        {
+            try
+            {
+                BundleInfo info = m_bundle.getInfo();
+
+                // Remove or purge the bundle depending on its
+                // current state.
+                if (info.getState() == Bundle.UNINSTALLED)
+                {
+                    // This physically removes the bundle from memory
+                    // as well as the bundle cache.
+                    garbageCollectBundle(m_bundle);
+                    m_bundle = null;
+                }
+                else
+                {
+                    // This physically removes all old revisions of the
+                    // bundle from memory and only maintains the newest
+                    // version in the bundle cache.
+                    purgeBundle(m_bundle);
+                }
+            }
+            catch (Exception ex)
+            {
+                fireFrameworkEvent(FrameworkEvent.ERROR, m_bundle, ex);
+            }
+        }
+
+        public void reinitialize()
+        {
+            if (m_bundle != null)
+            {
+                try
+                {
+                    BundleInfo info = m_bundle.getInfo();
+                    BundleInfo newInfo = createBundleInfo(info.getArchive());
+                    newInfo.syncLock(info);
+                    m_bundle.setInfo(newInfo);
+                }
+                catch (Exception ex)
+                {
+                    fireFrameworkEvent(FrameworkEvent.ERROR, m_bundle, ex);
+                }
+            }
+        }
+
+        public void restart()
+        {
+            if (m_bundle != null)
+            {
+                try
+                {
+                    startBundle(m_bundle, false);
+                }
+                catch (BundleException ex)
+                {
+                    fireFrameworkEvent(FrameworkEvent.ERROR, m_bundle, ex);
+                }
+            }
+        }
+    }
+
+    //
+    // Locking related methods.
+    //
+
+    private void rememberUninstalledBundle(BundleImpl bundle)
+    {
+        synchronized (m_uninstalledBundlesLock_Priority3)
+        {
+            // Verify that the bundle is not already in the array.
+            for (int i = 0;
+                (m_uninstalledBundles != null) && (i < m_uninstalledBundles.length);
+                i++)
+            {
+                if (m_uninstalledBundles[i] == bundle)
+                {
+                    return;
+                }
+            }
+
+            if (m_uninstalledBundles != null)
+            {
+                BundleImpl[] newBundles =
+                    new BundleImpl[m_uninstalledBundles.length + 1];
+                System.arraycopy(m_uninstalledBundles, 0,
+                    newBundles, 0, m_uninstalledBundles.length);
+                newBundles[m_uninstalledBundles.length] = bundle;
+                m_uninstalledBundles = newBundles;
+            }
+            else
+            {
+                m_uninstalledBundles = new BundleImpl[] { bundle };
+            }
+        }
+    }
+
+    private void forgetUninstalledBundle(BundleImpl bundle)
+    {
+        synchronized (m_uninstalledBundlesLock_Priority3)
+        {
+            if (m_uninstalledBundles == null)
+            {
+                return;
+            }
+            
+            int idx = -1;
+            for (int i = 0; i < m_uninstalledBundles.length; i++)
+            {
+                if (m_uninstalledBundles[i] == bundle)
+                {
+                    idx = i;
+                    break;
+                }
+            }
+    
+            if (idx >= 0)
+            {
+                // If this is the only bundle, then point to empty list.
+                if ((m_uninstalledBundles.length - 1) == 0)
+                {
+                    m_uninstalledBundles = new BundleImpl[0];
+                }
+                // Otherwise, we need to do some array copying.
+                else
+                {
+                    BundleImpl[] newBundles =
+                        new BundleImpl[m_uninstalledBundles.length - 1];
+                    System.arraycopy(m_uninstalledBundles, 0, newBundles, 0, idx);
+                    if (idx < newBundles.length)
+                    {
+                        System.arraycopy(
+                            m_uninstalledBundles, idx + 1,
+                            newBundles, idx, newBundles.length - idx);
+                    }
+                    m_uninstalledBundles = newBundles;
+                }
+            }
+        }
+    }
+
+    protected void acquireInstallLock(String location)
+        throws BundleException
+    {
+        synchronized (m_installRequestLock_Priority1)
+        {
+            while (m_installRequestMap.get(location) != null)
+            {
+                try
+                {
+                    m_installRequestLock_Priority1.wait();
+                }
+                catch (InterruptedException ex)
+                {
+                    throw new BundleException("Unable to install, thread interrupted.");
+                }
+            }
+            
+            m_installRequestMap.put(location, location);
+        }
+    }
+    
+    protected void releaseInstallLock(String location)
+    {
+        synchronized (m_installRequestLock_Priority1)
+        {
+            m_installRequestMap.remove(location);
+            m_installRequestLock_Priority1.notifyAll();
+        }
+    }
+
+    protected void acquireBundleLock(BundleImpl bundle)
+        throws BundleException
+    {
+        synchronized (m_bundleLock)
+        {
+            while (!bundle.getInfo().isLockable())
+            {
+                try
+                {
+                    m_bundleLock.wait();
+                }
+                catch (InterruptedException ex)
+                {
+                    // Ignore and just keep waiting.
+                }
+            }
+            bundle.getInfo().lock();
+        }
+    }
+    
+    protected void releaseBundleLock(BundleImpl bundle)
+    {
+        synchronized (m_bundleLock)
+        {
+            bundle.getInfo().unlock();
+            m_bundleLock.notifyAll();
+        }
+    }
+
+    protected BundleImpl[] acquireBundleRefreshLocks(Bundle[] targets)
+    {
+        // Hold bundles to be locked.
+        BundleImpl[] bundles = null;
+
+        synchronized (m_bundleLock)
+        {
+            boolean success = false;
+            while (!success)
+            {
+                // If targets is null, then refresh all pending bundles.
+                Bundle[] newTargets = targets;
+                if (newTargets == null)
+                {
+                    List list = new ArrayList();
+
+                    // First add all uninstalled bundles.
+                    synchronized (m_uninstalledBundlesLock_Priority3)
+                    {
+                        for (int i = 0;
+                            (m_uninstalledBundles != null) && (i < m_uninstalledBundles.length);
+                            i++)
+                        {
+                            list.add(m_uninstalledBundles[i]);
+                        }
+                    }
+
+                    // Then add all updated bundles.
+                    synchronized (m_installedBundleLock_Priority2)
+                    {
+                        Iterator iter = m_installedBundleMap.values().iterator();
+                        while (iter.hasNext())
+                        {
+                            BundleImpl bundle = (BundleImpl) iter.next();
+                            if (bundle.getInfo().isRemovalPending())
+                            {
+                                list.add(bundle);
+                            }
+                        }
+                    }
+
+                    // Create an array.
+                    if (list.size() > 0)
+                    {
+                        newTargets = (Bundle[]) list.toArray(new Bundle[list.size()]);
+                    }
+                }
+
+                // If there are targets, then find all dependencies
+                // for each one.
+                if (newTargets != null)
+                {
+                    // Create map of bundles that import the packages
+                    // from the target bundles.
+                    Map map = new HashMap();
+                    for (int targetIdx = 0; targetIdx < newTargets.length; targetIdx++)
+                    {
+                        // Add the current target bundle to the map of
+                        // bundles to be refreshed.
+                        BundleImpl target = (BundleImpl) newTargets[targetIdx];
+                        map.put(target, target);
+                        // Add all importing bundles to map.
+                        populateImportGraph(target, map);
+                    }
+                    
+                    bundles = (BundleImpl[]) map.values().toArray(new BundleImpl[map.size()]);
+                }
+                
+                // Check if all corresponding bundles can be locked
+                boolean lockable = true;
+                if (bundles != null)
+                {
+                    for (int i = 0; lockable && (i < bundles.length); i++)
+                    {
+                        lockable = bundles[i].getInfo().isLockable();
+                    }
+        
+                    // If we can lock all bundles, then lock them.
+                    if (lockable)
+                    {
+                        for (int i = 0; i < bundles.length; i++)
+                        {
+                            bundles[i].getInfo().lock();
+                        }
+                        success = true;
+                    }
+                    // Otherwise, wait and try again.
+                    else
+                    {
+                        try
+                        {
+                            m_bundleLock.wait();
+                        }
+                        catch (InterruptedException ex)
+                        {
+                            // Ignore and just keep waiting.
+                        }
+                    }
+                }
+                else
+                {
+                    // If there were no bundles to lock, then we can just
+                    // exit the lock loop.
+                    success = true;
+                }
+            }
+        }
+
+        return bundles;
+    }
+
+    protected void releaseBundleLocks(BundleImpl[] bundles)
+    {
+        // Always unlock any locked bundles.
+        synchronized (m_bundleLock)
+        {
+            for (int i = 0; (bundles != null) && (i < bundles.length); i++)
+            {
+                bundles[i].getInfo().unlock();
+            }
+            m_bundleLock.notifyAll();
+        }
+    }
+}
diff --git a/src/org/apache/osgi/framework/FilterImpl.java b/src/org/apache/osgi/framework/FilterImpl.java
new file mode 100644
index 0000000..4381d28
--- /dev/null
+++ b/src/org/apache/osgi/framework/FilterImpl.java
@@ -0,0 +1,240 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.CharArrayReader;
+import java.io.IOException;
+import java.util.*;
+
+import org.apache.osgi.framework.util.CaseInsensitiveMap;
+import org.apache.osgi.framework.util.ldap.*;
+import org.osgi.framework.*;
+
+/**
+ * This class implements an RFC 1960-based filter. The syntax of the
+ * filter string is the string representation of LDAP search filters
+ * as defined in RFC 1960. These filters are used to search for services
+ * and to track services using <tt>ServiceTracker</tt> objects.
+**/
+public class FilterImpl implements Filter
+{
+    private LogWrapper m_logger = null;
+    private String m_toString = null;
+    private Evaluator m_evaluator = null;
+    private SimpleMapper m_mapper = null;
+
+// TODO: FilterImpl needs a logger, this is a hack to get FrameworkUtil to compile.
+    public FilterImpl(String expr) throws InvalidSyntaxException
+    {
+        this(null, expr);
+    }
+
+    /**
+     * Construct a filter for a given filter expression string.
+     * @param expr the filter expression string for the filter.
+    **/
+    public FilterImpl(LogWrapper logger, String expr) throws InvalidSyntaxException
+    {
+        m_logger = logger;
+        if (expr == null)
+        {
+            throw new InvalidSyntaxException("Filter cannot be null", null);
+        }
+
+        if (expr != null)
+        {
+            CharArrayReader car = new CharArrayReader(expr.toCharArray());
+            LdapLexer lexer = new LdapLexer(car);
+            Parser parser = new Parser(lexer);
+            try
+            {
+                if (!parser.start())
+                {
+                    throw new InvalidSyntaxException(
+                        "Failed to parse LDAP query.", expr);
+                }
+            }
+            catch (ParseException ex)
+            {
+                throw new InvalidSyntaxException(
+                    ex.getMessage(), expr);
+            }
+            catch (IOException ex)
+            {
+                throw new InvalidSyntaxException(
+                    ex.getMessage(), expr);
+            }
+            m_evaluator = new Evaluator(parser.getProgram());
+            m_mapper = new SimpleMapper();
+        }
+    }
+
+    /**
+     * Compares the <tt>Filter</tt> object to another.
+     * @param o the object to compare this <tt>Filter</tt> against.
+     * @return If the other object is a <tt>Filter</tt> object, it
+     *         returns <tt>this.toString().equals(obj.toString())</tt>;
+     *         <tt>false</tt> otherwise.
+    **/
+    public boolean equals(Object o)
+    {
+        if (o == null)
+        {
+            return false;
+        }
+        else if (o instanceof Filter)
+        {
+            return toString().equals(o.toString());
+        }
+        return false;
+    }
+
+    /**
+     * Returns the hash code for the <tt>Filter</tt> object.
+     * @return The value <tt>this.toString().hashCode()</tt>.
+    **/
+    public int hashCode()
+    {
+        return toString().hashCode();
+    }
+
+    /**
+     * Filter using a <tt>Dictionary</tt> object. The <tt>Filter</tt>
+     * is executed using the <tt>Dictionary</tt> object's keys and values.
+     * @param dict the <tt>Dictionary</tt> object whose keys and values
+     *             are used to determine a match.
+     * @return <tt>true</tt> if the <tt>Dictionary</tt> object's keys
+     *         and values match this filter; <tt>false</tt> otherwise.
+     * @throws IllegalArgumentException if the dictionary contains case
+     *         variants of the same key name.
+    **/
+    public boolean match(Dictionary dict)
+        throws IllegalArgumentException
+    {
+        try
+        {
+            m_mapper.setSource(dict);
+            return m_evaluator.evaluate(m_mapper);
+        }
+        catch (AttributeNotFoundException ex)
+        {
+            m_logger.log(LogWrapper.LOG_DEBUG, "FilterImpl: " + ex);
+        }
+        catch (EvaluationException ex)
+        {
+            m_logger.log(LogWrapper.LOG_ERROR, "FilterImpl: " + toString(), ex);
+        }
+        return false;
+    }
+
+    /**
+     * Filter using a service's properties. The <tt>Filter</tt>
+     * is executed using the properties of the referenced service.
+     * @param ref A reference to the service whose properties
+     *             are used to determine a match.
+     * @return <tt>true</tt> if the service's properties match this
+     *         filter; <tt>false</tt> otherwise.
+    **/
+    public boolean match(ServiceReference ref)
+    {
+        try
+        {
+            m_mapper.setSource(ref);
+            return m_evaluator.evaluate(m_mapper);
+        }
+        catch (AttributeNotFoundException ex)
+        {
+            m_logger.log(LogWrapper.LOG_DEBUG, "FilterImpl: " + ex);
+        }
+        catch (EvaluationException ex)
+        {
+            m_logger.log(LogWrapper.LOG_ERROR, "FilterImpl: " + toString(), ex);
+        }
+        return false;
+    }
+
+    public boolean matchCase(Dictionary dictionary)
+    {
+        // TODO: Implement Filter.matchCase()
+        return false;
+    }
+
+    /**
+     * Returns the <tt>Filter</tt> object's filter string.
+     * @return Filter string.
+    **/
+    public String toString()
+    {
+        if (m_toString == null)
+        {
+            m_toString = m_evaluator.toStringInfix();
+        }
+        return m_toString;
+    }
+
+    static class SimpleMapper implements Mapper
+    {
+        private ServiceReference m_ref = null;
+        private Map m_map = null;
+
+        public void setSource(ServiceReference ref)
+        {
+            m_ref = ref;
+            m_map = null;
+        }
+
+        public void setSource(Dictionary dict)
+        {
+            if (m_map == null)
+            {
+                m_map = new CaseInsensitiveMap();
+            }
+            else
+            {
+                m_map.clear();
+            }
+
+            if (dict != null)
+            {
+                Enumeration keys = dict.keys();
+                while (keys.hasMoreElements())
+                {
+                    Object key = keys.nextElement();
+                    if (m_map.get(key) == null)
+                    {
+                        m_map.put(key, dict.get(key));
+                    }
+                    else
+                    {
+                        throw new IllegalArgumentException(
+                            "Duplicate attribute: " + key.toString());
+                    }
+                }
+            }
+            m_ref = null;
+        }
+
+        public Object lookup(String name)
+        {
+            if (m_map == null)
+            {
+                return m_ref.getProperty(name);
+            }
+            return m_map.get(name);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/FrameworkUtil.java b/src/org/apache/osgi/framework/FrameworkUtil.java
new file mode 100644
index 0000000..c20aea6
--- /dev/null
+++ b/src/org/apache/osgi/framework/FrameworkUtil.java
@@ -0,0 +1,29 @@
+/*
+ *   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.osgi.framework;
+
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+
+public class FrameworkUtil
+{
+	public static Filter createFilter(String filter)
+        throws InvalidSyntaxException
+    {
+		return new FilterImpl(filter);
+	}
+}
diff --git a/src/org/apache/osgi/framework/LogWrapper.java b/src/org/apache/osgi/framework/LogWrapper.java
new file mode 100644
index 0000000..a99341c
--- /dev/null
+++ b/src/org/apache/osgi/framework/LogWrapper.java
@@ -0,0 +1,143 @@
+/*
+ *   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.osgi.framework;
+
+import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * <p>
+ * This class mimics the standard OSGi <tt>LogService</tt> interface. An
+ * instance of this class will be used by the framework for all logging. Currently,
+ * the implementation of this class just sends log messages to standard output,
+ * but in the future it will be modified to use a log service if one is
+ * installed in the framework. To do so, it will need to use reflection to
+ * call the log service methods, since it will not have access to the
+ * <tt>LogService</tt> class.
+ * </p>
+**/
+// TODO: Modify LogWrapper to get LogService service object and invoke with reflection.
+public class LogWrapper
+{
+    public static final int LOG_ERROR = 1;
+    public static final int LOG_WARNING = 2;
+    public static final int LOG_INFO = 3;
+    public static final int LOG_DEBUG = 4;
+
+    private Object m_logObj = null;
+
+    public LogWrapper()
+    {
+    }
+
+    public void log(int level, String msg)
+    {
+        synchronized (this)
+        {
+            if (m_logObj != null)
+            {
+// Will use reflection.
+//                m_logObj.log(level, msg);
+            }
+            else
+            {
+                _log(null, level, msg, null);
+            }
+        }
+    }
+
+    public void log(int level, String msg, Throwable ex)
+    {
+        synchronized (this)
+        {
+            if (m_logObj != null)
+            {
+// Will use reflection.
+//                m_logObj.log(level, msg);
+            }
+            else
+            {
+                _log(null, level, msg, ex);
+            }
+        }
+    }
+
+    public void log(ServiceReference sr, int level, String msg)
+    {
+        synchronized (this)
+        {
+            if (m_logObj != null)
+            {
+// Will use reflection.
+//                m_logObj.log(level, msg);
+            }
+            else
+            {
+                _log(sr, level, msg, null);
+            }
+        }
+    }
+
+    public void log(ServiceReference sr, int level, String msg, Throwable ex)
+    {
+        synchronized (this)
+        {
+            if (m_logObj != null)
+            {
+// Will use reflection.
+//                m_logObj.log(level, msg);
+            }
+            else
+            {
+                _log(sr, level, msg, ex);
+            }
+        }
+    }
+    
+    private void _log(ServiceReference sr, int level, String msg, Throwable ex)
+    {
+        String s = (sr == null) ? null : "SvcRef " + sr;
+        s = (s == null) ? msg : s + " " + msg;
+        s = (ex == null) ? s : s + " (" + ex + ")";
+        switch (level)
+        {
+            case LOG_DEBUG:
+                System.out.println("DEBUG: " + s);
+                break;
+            case LOG_ERROR:
+                System.out.println("ERROR: " + s);
+                if (ex != null)
+                {
+                    if ((ex instanceof BundleException) &&
+                        (((BundleException) ex).getNestedException() != null))
+                    {
+                        ex = ((BundleException) ex).getNestedException();
+                    }
+                    ex.printStackTrace();
+                }
+                break;
+            case LOG_INFO:
+                System.out.println("INFO: " + s);
+                break;
+            case LOG_WARNING:
+                System.out.println("WARNING: " + s);
+                break;
+            default:
+                System.out.println("UNKNOWN[" + level + "]: " + s);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/Main.java b/src/org/apache/osgi/framework/Main.java
new file mode 100644
index 0000000..c17b184
--- /dev/null
+++ b/src/org/apache/osgi/framework/Main.java
@@ -0,0 +1,481 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.*;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Properties;
+
+import org.apache.osgi.framework.cache.DefaultBundleCache;
+import org.apache.osgi.framework.util.CaseInsensitiveMap;
+import org.apache.osgi.framework.util.MutablePropertyResolverImpl;
+
+/**
+ * <p>
+ * This class is the default way to instantiate and execute the framework. It is not
+ * intended to be the only way to instantiate and execute the framework; rather, it is
+ * one example of how to do so. When embedding the framework in a host application,
+ * this class can serve as a simple guide of how to do so. It may even be
+ * worthwhile to reuse some of its property handling capabilities. This class
+ * is completely static and is only intended to start a single instance of
+ * the framework.
+ * </p>
+**/
+public class Main
+{
+    /**
+     * The system property name used to specify an URL to the system
+     * property file.
+    **/
+    public static final String SYSTEM_PROPERTIES_PROP = "felix.system.properties";
+    /**
+     * The default name used for the system properties file.
+    **/
+    public static final String SYSTEM_PROPERTIES_FILE_VALUE = "system.properties";
+    /**
+     * The system property name used to specify an URL to the configuration
+     * property file to be used for the created the framework instance.
+    **/
+    public static final String CONFIG_PROPERTIES_PROP = "felix.config.properties";
+    /**
+     * The default name used for the configuration properties file.
+    **/
+    public static final String CONFIG_PROPERTIES_FILE_VALUE = "config.properties";
+
+    private static Felix m_felix = null;
+
+    /**
+     * <p>
+     * This method performs the main task of constructing an framework instance
+     * and starting its execution. The following functions are performed
+     * when invoked:
+     * </p>
+     * <ol>
+     *   <li><i><b>Read the system properties file.<b></i> This is a file
+     *       containing properties to be pushed into <tt>System.setProperty()</tt>
+     *       before starting the framework. This mechanism is mainly shorthand
+     *       for people starting the framework from the command line to avoid having
+     *       to specify a bunch of <tt>-D</tt> system property definitions.
+     *       The only properties defined in this file that will impact the framework's
+     *       behavior are the those concerning setting HTTP proxies, such as
+     *       <tt>http.proxyHost</tt>, <tt>http.proxyPort</tt>, and
+     *       <tt>http.proxyAuth</tt>.
+     *   </li>
+     *   <li><i><b>Perform system property variable substitution on system
+     *       properties.</b></i> Any system properties in the system property
+     *       file whose value adheres to <tt>${&lt;system-prop-name&gt;}</tt>
+     *       syntax will have their value substituted with the appropriate
+     *       system property value.
+     *   </li>
+     *   <li><i><b>Read the framework's configuration property file.</b></i> This is
+     *       a file containing properties used to configure the framework
+     *       instance and to pass configuration information into
+     *       bundles installed into the framework instance. The configuration
+     *       property file is called <tt>config.properties</tt> by default
+     *       and is located in the same directory as the <tt>felix.jar</tt>
+     *       file, which is typically in the <tt>lib/</tt> directory of the
+     *       Felix installation directory. It is possible to use a different
+     *       location for the property file by specifying the desired URL
+     *       using the <tt>felix.config.properties</tt> system property;
+     *       this should be set using the <tt>-D</tt> syntax when executing
+     *       the JVM. Refer to the
+     *       <a href="Felix.html#start(org.apache.osgi.framework.util.MutablePropertyResolver, org.apache.osgi.framework.util.MutablePropertyResolver, java.util.List)">
+     *       <tt>Felix.start()</tt></a> method documentation for more
+     *       information on the framework configuration options.
+     *   </li>
+     *   <li><i><b>Perform system property variable substitution on configuration
+     *       properties.</b></i> Any configuration properties whose value adheres to
+     *       <tt>${&lt;system-prop-name&gt;}</tt> syntax will have their value
+     *       substituted with the appropriate system property value.
+     *   </li>
+     *   <li><i><b>Ensure the default bundle cache has sufficient information to
+     *       initialize.</b></i> The default implementation of the bundle cache
+     *       requires either a profile name or a profile directory in order to
+     *       start. The configuration properties are checked for at least one
+     *       of the <tt>felix.cache.profile</tt> or <tt>felix.cache.profiledir</tt>
+     *       properties. If neither is found, the user is asked to supply a profile
+     *       name that is added to the configuration property set. See the
+     *       <a href="cache/DefaultBundleCache.html"><tt>DefaultBundleCache</tt></a>
+     *       documentation for more details its configuration options.
+     *   </li>
+     *   <li><i><b>Creates and starts a framework instance.</b></i> A simple
+     *       <a href="util/MutablePropertyResolver.html"><tt>MutablePropertyResolver</tt></a>
+     *       is created for the configuration property file and is passed
+     *       into the framework when it is started.
+     *   </li>
+     * </ol>
+     * <p>
+     * It should be noted that simply starting an instance of the framework is not enough
+     * to create an interactive session with it. It is necessary to install
+     * and start bundles that provide an interactive shell; this is generally
+     * done by specifying an "auto-start" property in the framework configuration
+     * property file. If no interactive shell bundles are installed or if
+     * the configuration property file cannot be found, the framework will appear to
+     * be hung or deadlocked. This is not the case, it is executing correctly,
+     * there is just no way to interact with it. Refer to the
+     * <a href="Felix.html#start(org.apache.osgi.framework.util.MutablePropertyResolver, org.apache.osgi.framework.util.MutablePropertyResolver, java.util.List)">
+     * <tt>Felix.start()</tt></a> method documentation for more information on
+     * framework configuration options.
+     * </p>
+     * @param argv An array of arguments, all of which are ignored.
+     * @throws Exception If an error occurs.
+    **/
+    public static void main(String[] argv) throws Exception
+    {
+        // Load system properties.
+        Main.loadSystemProperties();
+
+        // Read configuration properties.
+        Properties configProps = Main.readConfigProperties();
+
+        // See if the profile name property was specified.
+        String profileName = configProps.getProperty(DefaultBundleCache.CACHE_PROFILE_PROP);
+
+        // See if the profile directory property was specified.
+        String profileDirName = configProps.getProperty(DefaultBundleCache.CACHE_PROFILE_DIR_PROP);
+
+        // Print welcome banner.
+        System.out.println("\nWelcome to Felix.");
+        System.out.println("=================\n");
+
+        // If no profile or profile directory is specified in the
+        // properties, then ask for a profile name.
+        if ((profileName == null) && (profileDirName == null))
+        {
+            System.out.print("Enter profile name: ");
+            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+            try
+            {
+                profileName = in.readLine();
+            }
+            catch (IOException ex)
+            {
+                System.err.println("Could not read input.");
+                System.exit(-1);
+            }
+            System.out.println("");
+            if (profileName.length() != 0)
+            {
+                configProps.setProperty(DefaultBundleCache.CACHE_PROFILE_PROP, profileName);
+            }
+        }
+
+        // A profile directory or name must be specified.
+        if ((profileDirName == null) && (profileName.length() == 0))
+        {
+            System.err.println("You must specify a profile name or directory.");
+            System.exit(-1);
+        }
+
+        try
+        {
+            // Now create an instance of the framework.
+            m_felix = new Felix();
+            m_felix.start(
+                new MutablePropertyResolverImpl(new CaseInsensitiveMap(configProps)),
+                null);
+        }
+        catch (Exception ex)
+        {
+            System.err.println("Could not create framework: " + ex);
+            ex.printStackTrace();
+            System.exit(-1);
+        }
+    }
+
+    /**
+     * <p>
+     * Loads the properties in the system property file associated with the
+     * framework installation into <tt>System.setProperty()</tt>. These properties
+     * are not directly used by the framework in anyway. By default, the system property
+     * file is located in the same directory as the <tt>felix.jar</tt> file and
+     * is called "<tt>system.properties</tt>". This may be changed by setting the
+     * "<tt>felix.system.properties</tt>" system property to an
+     * arbitrary URL.
+     * </p>
+    **/
+    public static void loadSystemProperties()
+    {
+        // The system properties file is either specified by a system
+        // property or it is in the same directory as the Felix JAR file.
+        // Try to load it from one of these places.
+
+        // See if the property URL was specified as a property.
+        URL propURL = null;
+        String custom = System.getProperty(SYSTEM_PROPERTIES_PROP);
+        if (custom != null)
+        {
+            try
+            {
+                propURL = new URL(custom);
+            }
+            catch (MalformedURLException ex)
+            {
+                System.err.print("Main: " + ex);
+                return;
+            }
+        }
+        else
+        {
+            // Determine where felix.jar is located by looking at the
+            // system class path.
+            String jarLoc = null;
+            String classpath = System.getProperty("java.class.path");
+            int index = classpath.toLowerCase().indexOf("felix.jar");
+            int start = classpath.lastIndexOf(File.pathSeparator, index) + 1;
+            if (index > start)
+            {
+                jarLoc = classpath.substring(start, index);
+                if (jarLoc.length() == 0)
+                {
+                    jarLoc = ".";
+                }
+            }
+            else
+            {
+                // Can't figure it out so use the current directory as default.
+                jarLoc = System.getProperty("user.dir");
+            }
+
+            try
+            {
+                propURL = new File(jarLoc, SYSTEM_PROPERTIES_FILE_VALUE).toURL();
+            }
+            catch (MalformedURLException ex)
+            {
+                System.err.print("Main: " + ex);
+                return;
+            }
+        }
+
+        // Read the properties file.
+        Properties props = new Properties();
+        InputStream is = null;
+        try
+        {
+            is = propURL.openConnection().getInputStream();
+            props.load(is);
+            is.close();
+        }
+        catch (FileNotFoundException ex)
+        {
+            // Ignore file not found.
+        }
+        catch (Exception ex)
+        {
+            System.err.println(
+                "Main: Error loading system properties from " + propURL);
+            System.err.println("Main: " + ex);
+            try
+            {
+                if (is != null) is.close();
+            }
+            catch (IOException ex2)
+            {
+                // Nothing we can do.
+            }
+            return;
+        }
+
+        // Perform variable substitution for system properties.
+        for (Enumeration e = props.propertyNames(); e.hasMoreElements(); )
+        {
+            String name = (String) e.nextElement();
+            System.setProperty(name, substVars((String) props.getProperty(name)));
+        }
+    }
+
+    /**
+     * <p>
+     * Reads the configuration properties in the configuration property
+     * file associated with the framework installation; these properties are
+     * only accessible to the framework and are intended for configuration
+     * purposes. By default, the configuration property file is located in
+     * the same directory as the <tt>felix.jar</tt> file and is called
+     * "<tt>config.properties</tt>". This may be changed by setting the
+     * "<tt>felix.config.properties</tt>" system property to an
+     * arbitrary URL.
+     * </p>
+     * @return A <tt>Properties</tt> instance or <tt>null</tt> if there was an error.
+    **/
+    public static Properties readConfigProperties()
+    {
+        // The config properties file is either specified by a system
+        // property or it is in the same directory as the Felix JAR file.
+        // Try to load it from one of these places.
+
+        // See if the property URL was specified as a property.
+        URL propURL = null;
+        String custom = System.getProperty(CONFIG_PROPERTIES_PROP);
+        if (custom != null)
+        {
+            try
+            {
+                propURL = new URL(custom);
+            }
+            catch (MalformedURLException ex)
+            {
+                System.err.print("Main: " + ex);
+                return null;
+            }
+        }
+        else
+        {
+            // Determine where felix.jar is located by looking at the
+            // system class path.
+            String jarLoc = null;
+            String classpath = System.getProperty("java.class.path");
+            int index = classpath.toLowerCase().indexOf("felix.jar");
+            int start = classpath.lastIndexOf(File.pathSeparator, index) + 1;
+            if (index > start)
+            {
+                jarLoc = classpath.substring(start, index);
+                if (jarLoc.length() == 0)
+                {
+                    jarLoc = ".";
+                }
+            }
+            else
+            {
+                // Can't figure it out so use the current directory as default.
+                jarLoc = System.getProperty("user.dir");
+            }
+
+            try
+            {
+                propURL = new File(jarLoc, CONFIG_PROPERTIES_FILE_VALUE).toURL();
+            }
+            catch (MalformedURLException ex)
+            {
+                System.err.print("Main: " + ex);
+                return null;
+            }
+        }
+
+        // Read the properties file.
+        Properties props = new Properties();
+        InputStream is = null;
+        try
+        {
+            is = propURL.openConnection().getInputStream();
+            props.load(is);
+            is.close();
+        }
+        catch (FileNotFoundException ex)
+        {
+            // Ignore file not found.
+        }
+        catch (Exception ex)
+        {
+            System.err.println(
+                "Error loading config properties from " + propURL);
+            System.err.println("Main: " + ex);
+            try
+            {
+                if (is != null) is.close();
+            }
+            catch (IOException ex2)
+            {
+                // Nothing we can do.
+            }
+            return null;
+        }
+
+        // Perform variable substitution for system properties.
+        for (Enumeration e = props.propertyNames(); e.hasMoreElements(); )
+        {
+            String name = (String) e.nextElement();
+            props.setProperty(name, substVars((String) props.getProperty(name)));
+        }
+
+        return props;
+    }
+
+    private static final String DELIM_START = "${";
+    private static final char DELIM_STOP  = '}';
+    private static final int DELIM_START_LEN = 2;
+    private static final int DELIM_STOP_LEN  = 1;
+
+    /**
+     * <p>
+     * This method performs system property variable substitution on the
+     * specified string value. If the specified string contains the
+     * syntax <tt>${&lt;system-prop-name&gt;}</tt>, then the corresponding
+     * system property value is substituted for the marker.
+     * </p>
+     * @param val The string on which to perform system property substitution.
+     * @return The value of the specified string after system property substitution.
+     * @throws IllegalArgumentException If there was a syntax error in the
+     *         system property variable marker syntax.
+    **/
+    public static String substVars(String val)
+        throws IllegalArgumentException
+    {
+        StringBuffer sbuf = new StringBuffer();
+
+        if (val == null)
+        {
+            return val;
+        }
+
+        int i = 0;
+        int j, k;
+
+        while (true)
+        {
+            j = val.indexOf(DELIM_START, i);
+            if (j == -1)
+            {
+                if (i == 0)
+                {
+                    return val;
+                }
+                else
+                {
+                    sbuf.append(val.substring(i, val.length()));
+                    return sbuf.toString();
+                }
+            }
+            else
+            {
+                sbuf.append(val.substring(i, j));
+                k = val.indexOf(DELIM_STOP, j);
+                if (k == -1)
+                {
+                    throw new IllegalArgumentException(
+                    '"' + val +
+                    "\" has no closing brace. Opening brace at position "
+                    + j + '.');
+                }
+                else
+                {
+                    j += DELIM_START_LEN;
+                    String key = val.substring(j, k);
+                    // Try system properties.
+                    String replacement = System.getProperty(key, null);
+                    if (replacement != null)
+                    {
+                        sbuf.append(replacement);
+                    }
+                    i = k + DELIM_STOP_LEN;
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/OSGiLibrarySource.java b/src/org/apache/osgi/framework/OSGiLibrarySource.java
new file mode 100644
index 0000000..9196be3
--- /dev/null
+++ b/src/org/apache/osgi/framework/OSGiLibrarySource.java
@@ -0,0 +1,179 @@
+/*
+ *   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.osgi.framework;
+
+import org.apache.osgi.framework.cache.BundleCache;
+import org.apache.osgi.framework.util.LibraryInfo;
+import org.apache.osgi.moduleloader.LibrarySource;
+import org.osgi.framework.Constants;
+
+public class OSGiLibrarySource implements LibrarySource
+{
+    private LogWrapper m_logger = null;
+    private boolean m_opened = false;
+    private BundleCache m_cache = null;
+    private long m_bundleId = -1;
+    private int m_revision = -1;
+    private String m_os = null;
+    private String m_processor = null;
+    private LibraryInfo[] m_libraries = null;
+
+    public OSGiLibrarySource(
+        LogWrapper logger, BundleCache cache, long bundleId, int revision,
+        String os, String processor, LibraryInfo[] libraries)
+    {
+        m_logger = logger;
+        m_cache = cache;
+        m_bundleId = bundleId;
+        m_revision = revision;
+        m_os = normalizePropertyValue(Constants.FRAMEWORK_OS_NAME, os);
+        m_processor = normalizePropertyValue(Constants.FRAMEWORK_PROCESSOR, processor);
+        m_libraries = libraries;
+    }
+
+    public void open()
+    {
+        m_opened = true;
+    }
+
+    public void close()
+    {
+        m_opened = false;
+    }
+
+    public String getPath(String name) throws IllegalStateException
+    {
+        if (!m_opened)
+        {
+            throw new IllegalStateException("OSGiLibrarySource is not open");
+        }
+
+        if (m_libraries != null)
+        {
+            String libname = System.mapLibraryName(name);
+
+            // Check to see if we have a matching library.
+            // TODO: This "matching" algorithm does not fully
+            // match the spec and should be improved.
+            LibraryInfo library = null;
+            for (int i = 0; (library == null) && (i < m_libraries.length); i++)
+            {
+                boolean osOkay = checkOS(m_libraries[i].getOSNames());
+                boolean procOkay = checkProcessor(m_libraries[i].getProcessors());
+                if (m_libraries[i].getName().endsWith(libname) && osOkay && procOkay)
+                {
+                    library = m_libraries[i];
+                }
+            }
+
+            if (library != null)
+            {
+                try {
+                    return m_cache.getArchive(m_bundleId)
+                        .findLibrary(m_revision, library.getName());
+                } catch (Exception ex) {
+                    m_logger.log(LogWrapper.LOG_ERROR, "OSGiLibrarySource: Error finding library.", ex);
+                }
+            }
+        }
+
+        return null;
+    }
+
+    private boolean checkOS(String[] osnames)
+    {
+        for (int i = 0; (osnames != null) && (i < osnames.length); i++)
+        {
+            String osname =
+                normalizePropertyValue(Constants.FRAMEWORK_OS_NAME, osnames[i]);
+            if (m_os.equals(osname))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private boolean checkProcessor(String[] processors)
+    {
+        for (int i = 0; (processors != null) && (i < processors.length); i++)
+        {
+            String processor =
+                normalizePropertyValue(Constants.FRAMEWORK_PROCESSOR, processors[i]);
+            if (m_processor.equals(processor))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * This is simply a hack to try to create some standardized
+     * property values, since there seems to be many possible
+     * values for each JVM implementation.  Currently, this
+     * focuses on Windows and Linux and will certainly need
+     * to be changed in the future or at least edited.
+    **/
+    public static String normalizePropertyValue(String prop, String value)
+    {
+        prop = prop.toLowerCase();
+        value = value.toLowerCase();
+
+        if (prop.equals(Constants.FRAMEWORK_OS_NAME))
+        {
+            if (value.startsWith("linux"))
+            {
+                return "linux";
+            }
+            else if (value.startsWith("win"))
+            {
+                String os = "win";
+                if (value.indexOf("95") >= 0)
+                {
+                    os = "win95";
+                }
+                else if (value.indexOf("98") >= 0)
+                {
+                    os = "win98";
+                }
+                else if (value.indexOf("NT") >= 0)
+                {
+                    os = "winnt";
+                }
+                else if (value.indexOf("2000") >= 0)
+                {
+                    os = "win2000";
+                }
+                else if (value.indexOf("xp") >= 0)
+                {
+                    os = "winxp";
+                }
+                return os;
+            }
+        }
+        else if (prop.equals(Constants.FRAMEWORK_PROCESSOR))
+        {
+            if (value.endsWith("86"))
+            {
+                return "x86";
+            }
+        }
+
+        return value;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/OSGiURLPolicy.java b/src/org/apache/osgi/framework/OSGiURLPolicy.java
new file mode 100644
index 0000000..b2d50c3
--- /dev/null
+++ b/src/org/apache/osgi/framework/OSGiURLPolicy.java
@@ -0,0 +1,133 @@
+/*
+ *   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.osgi.framework;
+
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedExceptionAction;
+
+import org.apache.osgi.framework.util.FelixConstants;
+import org.apache.osgi.moduleloader.*;
+
+public class OSGiURLPolicy implements URLPolicy
+{
+    private Felix m_felix = null;
+    private BundleURLStreamHandler m_handler = null;
+    private FakeURLStreamHandler m_fakeHandler = null;
+
+    public OSGiURLPolicy(Felix felix)
+    {
+        m_felix = felix;
+    }
+
+    public URL createCodeSourceURL(ModuleManager mgr, Module module)
+    {
+        URL url = null;
+/*
+        BundleImpl bundle = null;
+        try
+        {
+            bundle = (BundleImpl)
+                m_felix.getBundle(BundleInfo.getBundleIdFromModuleId(module.getId()));
+            if (bundle != null)
+            {
+                url = new URL(bundle.getInfo().getLocation());
+            }
+        }
+        catch (NumberFormatException ex)
+        {
+            url = null;
+        }
+        catch (MalformedURLException ex)
+        {
+            if (m_fakeHandler == null)
+            {
+                m_fakeHandler = new FakeURLStreamHandler();
+            }
+            try
+            {
+                url = new URL(null,
+                    FelixConstants.FAKE_URL_PROTOCOL_VALUE
+                    + "//" + bundle.getLocation(), m_fakeHandler);
+            }
+            catch (Exception ex2)
+            {
+                url = null;
+            }
+        }
+*/
+        return url;
+    }
+
+    public URL createResourceURL(ModuleManager mgr, Module module, int rsIdx, String name)
+    {
+        if (m_handler == null)
+        {
+            m_handler = new BundleURLStreamHandler(mgr);
+        }
+
+        // Add a slash if there is one already, otherwise
+        // the is no slash separating the host from the file
+        // in the resulting URL.
+        if (!name.startsWith("/"))
+        {
+            name = "/" + name;
+        }
+
+        try
+        {
+            if (System.getSecurityManager() != null)
+            {
+                return (URL) AccessController.doPrivileged(
+                    new CreateURLPrivileged(module.getId(), rsIdx, name));
+            }
+            else
+            {
+                return new URL(FelixConstants.BUNDLE_URL_PROTOCOL,
+                    module.getId(), -1, "/" + rsIdx + name, m_handler);
+            }
+        }
+        catch (Exception ex)
+        {
+            System.err.println("OSGiURLPolicy: " + ex);
+            return null;
+        }
+    }
+
+    /**
+     * This simple class is used to perform the privileged action of
+     * creating a URL using the "bundle:" protocol stream handler.
+    **/
+    private class CreateURLPrivileged implements PrivilegedExceptionAction
+    {
+        private String m_id = null;
+        private int m_rsIdx = 0;
+        private String m_name = null;
+
+        public CreateURLPrivileged(String id, int rsIdx, String name)
+        {
+            m_id = id;
+            m_rsIdx = rsIdx;
+            m_name = name;
+        }
+
+        public Object run() throws Exception
+        {
+            return new URL("bundle", m_id, -1, "/" + m_rsIdx + m_name, m_handler);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/PackageAdminActivator.java b/src/org/apache/osgi/framework/PackageAdminActivator.java
new file mode 100644
index 0000000..4e07315
--- /dev/null
+++ b/src/org/apache/osgi/framework/PackageAdminActivator.java
@@ -0,0 +1,43 @@
+/*
+ *   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.osgi.framework;
+
+import org.osgi.framework.*;
+
+class PackageAdminActivator implements BundleActivator
+{
+    private Felix m_felix = null;
+    private ServiceRegistration m_reg = null;
+
+    public PackageAdminActivator(Felix felix)
+    {
+        m_felix = felix;
+    }
+
+    public void start(BundleContext context) throws Exception
+    {
+        m_reg = context.registerService(
+            org.osgi.service.packageadmin.PackageAdmin.class.getName(),
+            new PackageAdminImpl(m_felix),
+            null);
+    }
+
+    public void stop(BundleContext context) throws Exception
+    {
+        m_reg.unregister();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/PackageAdminImpl.java b/src/org/apache/osgi/framework/PackageAdminImpl.java
new file mode 100644
index 0000000..5a029c0
--- /dev/null
+++ b/src/org/apache/osgi/framework/PackageAdminImpl.java
@@ -0,0 +1,189 @@
+/*
+ *   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.osgi.framework;
+
+import org.osgi.framework.Bundle;
+import org.osgi.service.packageadmin.*;
+
+class PackageAdminImpl implements PackageAdmin, Runnable
+{
+    private Felix m_felix = null;
+    private Bundle[][] m_reqBundles = null;
+
+    public PackageAdminImpl(Felix felix)
+    {
+        m_felix = felix;
+
+        // Start a thread to perform asynchronous package refreshes.
+        Thread t = new Thread(this, "FelixPackageAdmin");
+        t.setDaemon(true);
+        t.start();
+    }
+
+    /**
+     * Returns the exported package associated with the specified
+     * package name.
+     *
+     * @param name the name of the exported package to find.
+     * @return the exported package or null if no matching package was found.
+    **/
+    public ExportedPackage getExportedPackage(String name)
+    {
+        return m_felix.getExportedPackage(name);
+    }
+
+    /**
+     * Returns the packages exported by the specified bundle.
+     *
+     * @param bundle the bundle whose exported packages are to be returned.
+     * @return an array of packages exported by the bundle or null if the
+     *         bundle does not export any packages.
+    **/
+    public ExportedPackage[] getExportedPackages(Bundle b)
+    {
+        return m_felix.getExportedPackages(b);
+    }
+
+    /**
+     * The OSGi specification states that refreshing packages is
+     * asynchronous; this method simply notifies the package admin
+     * thread to do a refresh.
+     * @param bundles array of bundles to refresh or <tt>null</tt> to refresh
+     *                any bundles in need of refreshing.
+    **/
+    public synchronized void refreshPackages(Bundle[] bundles)
+        throws SecurityException
+    {
+        // Save our request parameters and notify all.
+        if (m_reqBundles == null)
+        {
+            m_reqBundles = new Bundle[][] { bundles };
+        }
+        else
+        {
+            Bundle[][] newReqBundles = new Bundle[m_reqBundles.length + 1][];
+            System.arraycopy(m_reqBundles, 0,
+                newReqBundles, 0, m_reqBundles.length);
+            newReqBundles[m_reqBundles.length] = bundles;
+            m_reqBundles = newReqBundles;
+        }
+        notifyAll();
+    }
+
+    /**
+     * The OSGi specification states that package refreshes happen
+     * asynchronously; this is the run() method for the package
+     * refreshing thread.
+    **/
+    public void run()
+    {
+        // This thread loops forever, thus it should
+        // be a daemon thread.
+        Bundle[] bundles = null;
+        while (true)
+        {
+            synchronized (this)
+            {
+                // Wait for a refresh request.
+                while (m_reqBundles == null)
+                {
+                    try
+                    {
+                        wait();
+                    }
+                    catch (InterruptedException ex)
+                    {
+                    }
+                }
+
+                // Get the bundles parameter for the current
+                // refresh request.
+                if (m_reqBundles != null)
+                {
+                    bundles = m_reqBundles[0];
+                }
+            }
+
+            // Perform refresh.
+            m_felix.refreshPackages(bundles);
+
+            // Remove the first request since it is now completed.
+            synchronized (this)
+            {
+                if (m_reqBundles.length == 1)
+                {
+                    m_reqBundles = null;
+                }
+                else
+                {
+                    Bundle[][] newReqBundles = new Bundle[m_reqBundles.length - 1][];
+                    System.arraycopy(m_reqBundles, 1,
+                        newReqBundles, 0, m_reqBundles.length - 1);
+                    m_reqBundles = newReqBundles;
+                }
+            }
+        }
+    }
+
+    public ExportedPackage[] getExportedPackages(String name)
+    {
+        // TODO: Implement PackageAdmin.getExportedPackages()
+        return null;
+    }
+
+    public boolean resolveBundles(Bundle[] bundles)
+    {
+        // TODO: Implement PackageAdmin.resolveBundles()
+        return false;
+    }
+
+    public RequiredBundle[] getRequiredBundles(String symbolicName)
+    {
+        // TODO: Implement PackageAdmin.getRequiredBundles()
+        return null;
+    }
+
+    public Bundle[] getBundles(String symbolicName, String versionRange)
+    {
+        // TODO: Implement PackageAdmin.getBundles()
+        return null;
+    }
+
+    public Bundle[] getFragments(Bundle bundle)
+    {
+        // TODO: Implement PackageAdmin.getFragments()
+        return null;
+    }
+
+    public Bundle[] getHosts(Bundle bundle)
+    {
+        // TODO: Implement PackageAdmin.getHosts()
+        return null;
+    }
+
+    public Bundle getBundle(Class clazz)
+    {
+        // TODO: Implement PackageAdmin.getBundle()
+        return null;
+    }
+
+    public int getBundleType(Bundle bundle)
+    {
+        // TODO: Implement PackageAdmin.getBundleType()
+        return 0;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/ServiceReferenceImpl.java b/src/org/apache/osgi/framework/ServiceReferenceImpl.java
new file mode 100644
index 0000000..8cf5ae4
--- /dev/null
+++ b/src/org/apache/osgi/framework/ServiceReferenceImpl.java
@@ -0,0 +1,175 @@
+/*
+ *   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.osgi.framework;
+
+import org.apache.osgi.framework.searchpolicy.R4SearchPolicy;
+import org.apache.osgi.framework.searchpolicy.R4Wire;
+import org.apache.osgi.framework.util.FelixConstants;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
+
+class ServiceReferenceImpl implements ServiceReference
+{
+    private ServiceRegistrationImpl m_registration = null;
+    private Bundle m_bundle = null;
+
+    public ServiceReferenceImpl(ServiceRegistrationImpl reg, Bundle bundle)
+    {
+        m_registration = reg;
+        m_bundle = bundle;
+    }
+
+    protected ServiceRegistrationImpl getServiceRegistration()
+    {
+        return m_registration;
+    }
+
+    public Object getProperty(String s)
+    {
+        return m_registration.getProperty(s);
+    }
+
+    public String[] getPropertyKeys()
+    {
+        return m_registration.getPropertyKeys();
+    }
+
+    public Bundle getBundle()
+    {
+        return m_bundle;
+    }
+
+    public Bundle[] getUsingBundles()
+    {
+        return m_registration.getUsingBundles();
+    }
+
+    public boolean equals(Object obj)
+    {
+        try
+        {
+            ServiceReferenceImpl ref = (ServiceReferenceImpl) obj;
+            return ref.m_registration == m_registration;
+        }
+        catch (ClassCastException ex)
+        {
+            // Ignore and return false.
+        }
+        catch (NullPointerException ex)
+        {
+            // Ignore and return false.
+        }
+
+        return false;
+    }
+
+    public int hashCode()
+    {
+        if (m_registration.getReference() != null)
+        {
+            if (m_registration.getReference() != this)
+            {
+                return m_registration.getReference().hashCode();
+            }
+            return super.hashCode();
+        }
+        return 0;
+    }
+
+    public String toString()
+    {
+        String[] ocs = (String[]) getProperty("objectClass");
+        String oc = "[";
+        for(int i = 0; i < ocs.length; i++)
+        {
+            oc = oc + ocs[i];
+            if (i < ocs.length - 1)
+                oc = oc + ", ";
+        }
+        oc = oc + "]";
+        return oc;
+    }
+
+    public boolean isAssignableTo(Bundle requester, String className)
+    {
+        // Always return true if the requester is the same as the provider.
+        if (requester == m_bundle)
+        {
+            return true;
+        }
+
+        // Boolean flag.
+        boolean allow = true;
+        // Get the package.
+        String pkgName =
+            org.apache.osgi.moduleloader.Util.getClassPackage(className);
+        // Get package wiring from service provider and requester.
+        R4Wire requesterWire = R4SearchPolicy.getWire(
+            ((BundleImpl) requester).getInfo().getCurrentModule(), pkgName);
+        R4Wire providerWire = R4SearchPolicy.getWire(
+            ((BundleImpl) m_bundle).getInfo().getCurrentModule(), pkgName);
+
+        // There are three situations that may occur here:
+        //   1. The requester does not have a wire for the package.
+        //   2. The provider does not have a wire for the package.
+        //   3. Both have a wire for the package.
+        // For case 1, we do not filter the service reference since we
+        // assume that the bundle is using reflection or that it won't
+        // use that class at all since it does not import it. For
+        // case 2, we have to try to load the class from the class
+        // loader of the service object and then compare the class
+        // loaders to determine if we should filter the service
+        // refernce. In case 3, we simply compare the exporting
+        // modules from the package wiring to determine if we need
+        // to filter the service reference.
+        
+        // Case 1: Always include service reference.
+        if (requesterWire == null)
+        {
+            // This is an intentional no-op.
+        }
+        // Case 2: Only include service reference if the service
+        // object uses the same class as the requester.
+        else if (providerWire == null)
+        {
+            try
+            {
+                // Load the class from the requesting bundle.
+                Class requestClass =
+                    ((BundleImpl) requester).getInfo().getCurrentModule().getClassLoader()
+                        .loadClass(className);
+                // Get the service registration and ask it to check
+                // if the service object is assignable to the requesting
+                // bundle's class.
+                allow = getServiceRegistration().isClassAccessible(requestClass);
+            }
+            catch (Exception ex)
+            {
+                // This should not happen, filter to be safe.
+                allow = false;
+            }
+        }
+        // Case 3: Include service reference if the wires have the
+        // same source module.
+        else
+        {
+            allow = providerWire.m_module.equals(requesterWire.m_module);
+        }
+
+        return allow;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/ServiceRegistrationImpl.java b/src/org/apache/osgi/framework/ServiceRegistrationImpl.java
new file mode 100644
index 0000000..9ba8b28
--- /dev/null
+++ b/src/org/apache/osgi/framework/ServiceRegistrationImpl.java
@@ -0,0 +1,276 @@
+/*
+ *   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.osgi.framework;
+
+import java.security.AccessController;
+import java.security.PrivilegedExceptionAction;
+import java.util.*;
+
+import org.apache.osgi.framework.util.CaseInsensitiveMap;
+import org.osgi.framework.*;
+
+class ServiceRegistrationImpl implements ServiceRegistration
+{
+    // Service registry.
+    private ServiceRegistry m_registry = null;
+    // Bundle implementing the service.
+    private Bundle m_bundle = null;
+    // Interfaces associated with the service object.
+    private String[] m_classes = null;
+    // Service Id associated with the service object.
+    private Long m_serviceId = null;
+    // Service object.
+    private Object m_svcObj = null;
+    // Service factory interface.
+    private ServiceFactory m_factory = null;
+    // Associated property dictionary.
+    private Map m_propMap = null;
+    // Re-usable service reference.
+    private ServiceReferenceImpl m_ref = null;
+
+    public ServiceRegistrationImpl(
+        ServiceRegistry registry, Bundle bundle,
+        String[] classes, Long serviceId,
+        Object svcObj, Dictionary dict)
+    {
+        m_registry = registry;
+        m_bundle = bundle;
+        m_classes = classes;
+        m_serviceId = serviceId;
+        m_svcObj = svcObj;
+        m_factory = (m_svcObj instanceof ServiceFactory)
+            ? (ServiceFactory) m_svcObj : null;
+
+        initializeProperties(dict);
+
+        // This reference is the "standard" reference for this
+        // service and will always be returned by getReference().
+        // Since all reference to this service are supposed to
+        // be equal, we use the hashcode of this reference for
+        // a references to this service in ServiceReference.
+        m_ref = new ServiceReferenceImpl(this, m_bundle);
+    }
+
+    protected boolean isValid()
+    {
+        return (m_svcObj != null);
+    }
+
+    public ServiceReference getReference()
+    {
+        return m_ref;
+    }
+
+    public void setProperties(Dictionary dict)
+    {
+        // Make sure registration is valid.
+        if (!isValid())
+        {
+            throw new IllegalStateException(
+                "The service registration is no longer valid.");
+        }
+        // Set the properties.
+        initializeProperties(dict);
+        // Tell registry about it.
+        m_registry.servicePropertiesModified(this);
+    }
+
+    public void unregister()
+    {
+        m_registry.unregisterService(m_bundle, this);
+        m_svcObj = null;
+        m_factory = null;
+    }
+
+    //
+    // Utility methods.
+    //
+
+    /**
+     * This method determines if the class loader of the service object
+     * has access to the specified class.
+     * @param clazz the class to test for reachability.
+     * @return <tt>true</tt> if the specified class is reachable from the
+     *         service object's class loader, <tt>false</tt> otherwise.
+    **/
+    protected boolean isClassAccessible(Class clazz)
+    {
+        ClassLoader loader = (m_factory != null)
+            ? m_factory.getClass().getClassLoader()
+            : m_svcObj.getClass().getClassLoader();
+        try
+        {
+            Class target = loader.loadClass(clazz.getName());
+            return (target.getClassLoader() == clazz.getClassLoader());
+        }
+        catch (Exception ex)
+        {
+        }
+        return false;
+    }
+
+    protected Object getProperty(String key)
+    {
+        return m_propMap.get(key);
+    }
+
+    private transient ArrayList m_list = new ArrayList();
+
+    protected String[] getPropertyKeys()
+    {
+        synchronized (m_list)
+        {
+            m_list.clear();
+            Iterator i = m_propMap.entrySet().iterator();
+            while (i.hasNext())
+            {
+                Map.Entry entry = (Map.Entry) i.next();
+                m_list.add(entry.getKey());
+            }
+            return (String[]) m_list.toArray(new String[m_list.size()]);
+        }
+    }
+
+    protected Bundle[] getUsingBundles()
+    {
+        return m_registry.getUsingBundles(m_ref);
+    }
+
+    protected Object getService(Bundle acqBundle)
+    {
+        // If the service object is a service factory, then
+        // let it create the service object.
+        if (m_factory != null)
+        {
+            try
+            {
+                if (System.getSecurityManager() != null)
+                {
+                    return AccessController.doPrivileged(
+                        new ServiceFactoryPrivileged(acqBundle, null));
+                }
+                else
+                {
+                    return getFactoryUnchecked(acqBundle);
+                }
+            }
+            catch (Exception ex)
+            {
+                m_registry.getLogger().log(
+                    LogWrapper.LOG_ERROR, "ServiceRegistrationImpl: Error getting service.", ex);
+                return null;
+            }
+        }
+        else
+        {
+            return m_svcObj;
+        }
+    }
+
+    protected void ungetService(Bundle relBundle, Object svcObj)
+    {
+        // If the service object is a service factory, then
+        // let is release the service object.
+        if (m_factory != null)
+        {
+            try
+            {
+                if (System.getSecurityManager() != null)
+                {
+                    AccessController.doPrivileged(
+                        new ServiceFactoryPrivileged(relBundle, svcObj));
+                }
+                else
+                {
+                    ungetFactoryUnchecked(relBundle, svcObj);
+                }
+            }
+            catch (Exception ex)
+            {
+                m_registry.getLogger().log(
+                    LogWrapper.LOG_ERROR, "ServiceRegistrationImpl: Error ungetting service.", ex);
+            }
+        }
+    }
+
+    private void initializeProperties(Dictionary dict)
+    {
+        // Create a case insensitive map.
+        if (m_propMap == null)
+        {
+            m_propMap = new CaseInsensitiveMap();
+        }
+        else
+        {
+            m_propMap.clear();
+        }
+
+        if (dict != null)
+        {
+            Enumeration keys = dict.keys();
+            while (keys.hasMoreElements())
+            {
+                Object key = keys.nextElement();
+                m_propMap.put(key, dict.get(key));
+            }
+        }
+
+        // Add the framework assigned properties.
+        m_propMap.put(Constants.OBJECTCLASS, m_classes);
+        m_propMap.put(Constants.SERVICE_ID, m_serviceId);
+    }
+
+    private Object getFactoryUnchecked(Bundle bundle)
+    {
+        return m_factory.getService(bundle, this);
+    }
+
+    private void ungetFactoryUnchecked(Bundle bundle, Object svcObj)
+    {
+        m_factory.ungetService(bundle, this, svcObj);
+    }
+
+    /**
+     * This simple class is used to ensure that when a service factory
+     * is called, that no other classes on the call stack interferes
+     * with the permissions of the factory itself.
+    **/
+    private class ServiceFactoryPrivileged implements PrivilegedExceptionAction
+    {
+        private Bundle m_bundle = null;
+        private Object m_svcObj = null;
+
+        public ServiceFactoryPrivileged(Bundle bundle, Object svcObj)
+        {
+            m_bundle = bundle;
+            m_svcObj = svcObj;
+        }
+
+        public Object run() throws Exception
+        {
+            if (m_svcObj == null)
+            {
+                return getFactoryUnchecked(m_bundle);
+            }
+            else
+            {
+                ungetFactoryUnchecked(m_bundle, m_svcObj);
+            }
+            return null;
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/ServiceRegistry.java b/src/org/apache/osgi/framework/ServiceRegistry.java
new file mode 100644
index 0000000..02f206b
--- /dev/null
+++ b/src/org/apache/osgi/framework/ServiceRegistry.java
@@ -0,0 +1,522 @@
+/*
+ *   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.osgi.framework;
+
+import java.util.*;
+
+import org.apache.osgi.framework.util.FelixConstants;
+import org.osgi.framework.*;
+
+public class ServiceRegistry
+{
+    private LogWrapper m_logger = null;
+    private long m_currentServiceId = 1L;
+    // Maps bundle to an array of service registrations.
+    private Map m_serviceRegsMap = new HashMap();
+    // Maps bundle to an array of usage counts.
+    private Map m_inUseMap = new HashMap();
+
+    private ServiceListener m_serviceListener = null;
+
+    public ServiceRegistry(LogWrapper logger)
+    {
+        m_logger = logger;
+    }
+
+    public synchronized ServiceReference[] getRegisteredServices(Bundle bundle)
+    {
+        ServiceRegistration[] regs = (ServiceRegistration[]) m_serviceRegsMap.get(bundle);
+        if (regs != null)
+        {
+            ServiceReference[] refs = new ServiceReference[regs.length];
+            for (int i = 0; i < refs.length; i++)
+            {
+                refs[i] = regs[i].getReference();
+            }
+            return refs;
+        }
+        return null;
+    }
+
+    public ServiceRegistration registerService(
+        Bundle bundle, String[] classNames, Object svcObj, Dictionary dict)
+    {
+        ServiceRegistration reg = null;
+
+        synchronized (this)
+        {
+            // Create the service registration.
+            reg = new ServiceRegistrationImpl(
+                this, bundle, classNames, new Long(m_currentServiceId++), svcObj, dict);
+            // Get the bundles current registered services.
+            ServiceRegistration[] regs = (ServiceRegistration[]) m_serviceRegsMap.get(bundle);
+            m_serviceRegsMap.put(bundle, addServiceRegistration(regs, reg));
+        }
+        fireServiceChanged(new ServiceEvent(ServiceEvent.REGISTERED, reg.getReference()));
+        return reg;
+    }
+
+    public void unregisterService(Bundle bundle, ServiceRegistration reg)
+    {
+        synchronized (this)
+        {
+            ServiceRegistration[] regs = (ServiceRegistration[]) m_serviceRegsMap.get(bundle);
+            m_serviceRegsMap.put(bundle, removeServiceRegistration(regs, reg));
+        }
+        fireServiceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, reg.getReference()));
+    }
+
+    public void unregisterServices(Bundle bundle)
+    {
+        // Simply remove all service registrations for the bundle.
+        ServiceRegistration[] regs = null;
+        synchronized (this)
+        {
+            regs = (ServiceRegistration[]) m_serviceRegsMap.get(bundle);
+            m_serviceRegsMap.remove(bundle);
+        }
+
+        // Fire all events outside of synchronized block.
+        for (int i = 0; (regs != null) && (i < regs.length); i++)
+        {
+            fireServiceChanged(
+                new ServiceEvent(ServiceEvent.UNREGISTERING, regs[i].getReference()));
+        }
+    }
+
+    public synchronized List getServiceReferences(String className, Filter filter)
+        throws InvalidSyntaxException
+    {
+        // Create a filtered list of service references.
+        List list = new ArrayList();
+
+        // Iterator over all service registrations.
+        for (Iterator i = m_serviceRegsMap.entrySet().iterator(); i.hasNext(); )
+        {
+            Map.Entry entry = (Map.Entry) i.next();
+            ServiceRegistration[] regs = (ServiceRegistration[]) entry.getValue();
+
+            for (int regIdx = 0;
+                (regs != null) && (regIdx < regs.length);
+                regIdx++)
+            {
+                // Determine if the registered services matches
+                // the search criteria.
+                boolean matched = false;
+
+                // If className is null, then look at filter only.
+                if ((className == null) &&
+                    ((filter == null) || filter.match(regs[regIdx].getReference())))
+                {
+                    matched = true;
+                }
+                // If className is not null, then first match the
+                // objectClass property before looking at the
+                // filter.
+                else if (className != null)
+                {
+                    String[] objectClass = (String[])
+                        ((ServiceRegistrationImpl) regs[regIdx]).getProperty(FelixConstants.OBJECTCLASS);
+                    for (int classIdx = 0;
+                        classIdx < objectClass.length;
+                        classIdx++)
+                    {
+                        if (objectClass[classIdx].equals(className) &&
+                            ((filter == null) || filter.match(regs[regIdx].getReference())))
+                        {
+                            matched = true;
+                            break;
+                        }
+                    }
+                }
+
+                // Add reference if it was a match.
+                if (matched)
+                {
+                    list.add(regs[regIdx].getReference());
+                }
+            }
+        }
+
+        return list;
+    }
+
+    public synchronized ServiceReference[] getServicesInUse(Bundle bundle)
+    {
+        UsageCount[] usages = (UsageCount[]) m_inUseMap.get(bundle);
+        if (usages != null)
+        {
+            ServiceReference[] refs = new ServiceReference[usages.length];
+            for (int i = 0; i < refs.length; i++)
+            {
+                refs[i] = usages[i].m_ref;
+            }
+            return refs;
+        }
+        return null;
+    }
+
+    public synchronized Object getService(Bundle bundle, ServiceReference ref)
+    {
+        // Get usage counts for specified bundle.
+        UsageCount[] usages = (UsageCount[]) m_inUseMap.get(bundle);
+
+        // Make sure the service registration is still valid.
+        if (!((ServiceReferenceImpl) ref).getServiceRegistration().isValid())
+        {
+            // If the service registration is not valid, then this means
+            // that the service provider unregistered the service. The spec
+            // says that calls to get an unregistered service should always
+            // return null (assumption: even if it is currently cached
+            // by the bundle). So in this case, flush the service reference
+            // from the cache and return null.
+            m_inUseMap.put(bundle, removeUsageCount(usages, ref));
+
+            // It is not necessary to unget the service object from
+            // the providing bundle, since the associated service is
+            // unregistered and hence not in the list of registered services
+            // of the providing bundle. This is precisely why the service
+            // registration was not found above in the first place.
+            return null;
+        }
+
+        // Get the service registration.
+        ServiceRegistrationImpl reg = ((ServiceReferenceImpl) ref).getServiceRegistration();
+
+        // Get the usage count, if any.
+        UsageCount usage = getUsageCount(usages, ref);
+       
+        // If the service object is cached, then increase the usage
+        // count and return the cached service object.
+        Object svcObj = null;
+        if (usage != null)
+        {
+            usage.m_count++;
+            svcObj = usage.m_svcObj;
+        }
+        else
+        {
+            // Get service object from service registration.
+            svcObj = reg.getService(bundle);
+
+            // Cache the service object.
+            if (svcObj != null)
+            {
+                m_inUseMap.put(bundle, addUsageCount(usages, ref, svcObj));
+            }
+        }
+
+        return svcObj;
+    }
+
+    public synchronized boolean ungetService(Bundle bundle, ServiceReference ref)
+    {
+        // Get usage count.
+        UsageCount[] usages = (UsageCount[]) m_inUseMap.get(bundle);
+        UsageCount usage = getUsageCount(usages, ref);
+
+        // If no usage count, then return.
+        if (usage == null)
+        {
+            return false;
+        }
+
+        // Make sure the service registration is still valid.
+        if (!((ServiceReferenceImpl) ref).getServiceRegistration().isValid())
+        {
+            // If the service registration is not valid, then this means
+            // that the service provider unregistered the service. The spec
+            // says that calls to get an unregistered service should always
+            // return null (assumption: even if it is currently cached
+            // by the bundle). So in this case, flush the service reference
+            // from the cache and return null.
+            m_inUseMap.put(bundle, removeUsageCount(usages, ref));
+            return false;
+        }
+
+        // Decrement usage count.
+        usage.m_count--;
+
+        // Remove reference when usage count goes to zero
+        // and unget the service object from the exporting
+        // bundle.
+        if (usage.m_count == 0)
+        {
+            m_inUseMap.put(bundle, removeUsageCount(usages, ref));
+            ServiceRegistrationImpl reg =
+                ((ServiceReferenceImpl) ref).getServiceRegistration();
+            reg.ungetService(bundle, usage.m_svcObj);
+            usage.m_svcObj = null;
+        }
+
+        // Return true if the usage count is greater than zero.
+        return (usage.m_count > 0);
+    }
+
+
+    /**
+     * This is a utility method to release all services being
+     * used by the specified bundle.
+     * @param bundle the bundle whose services are to be released.
+    **/
+    public synchronized void ungetServices(Bundle bundle)
+    {
+        UsageCount[] usages = (UsageCount[]) m_inUseMap.get(bundle);
+        if (usages == null)
+        {
+            return;
+        }
+
+        // Remove each service object from the
+        // service cache.
+        for (int i = 0; i < usages.length; i++)
+        {
+            // Keep ungetting until all usage count is zero.
+            while (ungetService(bundle, usages[i].m_ref))
+            {
+                // Empty loop body.
+            }
+        }
+    }
+
+    public synchronized Bundle[] getUsingBundles(ServiceReference ref)
+    {
+        Bundle[] bundles = null;
+        for (Iterator iter = m_inUseMap.entrySet().iterator(); iter.hasNext(); )
+        {
+            Map.Entry entry = (Map.Entry) iter.next();
+            Bundle bundle = (Bundle) entry.getKey();
+            UsageCount[] usages = (UsageCount[]) entry.getValue();
+            for (int useIdx = 0; useIdx < usages.length; useIdx++)
+            {
+                if (usages[useIdx].m_ref.equals(ref))
+                {
+                    // Add the bundle to the array to be returned.
+                    if (bundles == null)
+                    {
+                        bundles = new Bundle[] { bundle };
+                    }
+                    else
+                    {
+                        Bundle[] nbs = new Bundle[bundles.length + 1];
+                        System.arraycopy(bundles, 0, nbs, 0, bundles.length);
+                        nbs[bundles.length] = bundle;
+                        bundles = nbs;
+                    }
+                }
+            }
+        }
+        return bundles;
+    }
+
+    public void servicePropertiesModified(ServiceRegistration reg)
+    {
+        fireServiceChanged(new ServiceEvent(ServiceEvent.MODIFIED, reg.getReference()));
+    }
+
+    public LogWrapper getLogger()
+    {
+        return m_logger;
+    }
+
+    private static ServiceRegistration[] addServiceRegistration(
+        ServiceRegistration[] regs, ServiceRegistration reg)
+    {
+        if (regs == null)
+        {
+            regs = new ServiceRegistration[] { reg };
+        }
+        else
+        {
+            ServiceRegistration[] newRegs = new ServiceRegistration[regs.length + 1];
+            System.arraycopy(regs, 0, newRegs, 0, regs.length);
+            newRegs[regs.length] = reg;
+            regs = newRegs;
+        }
+        return regs;
+    }
+
+    private static ServiceRegistration[] removeServiceRegistration(
+        ServiceRegistration[] regs, ServiceRegistration reg)
+    {
+        for (int i = 0; (regs != null) && (i < regs.length); i++)
+        {
+            if (regs[i].equals(reg))
+            {
+                // If this is the only usage, then point to empty list.
+                if ((regs.length - 1) == 0)
+                {
+                    regs = new ServiceRegistration[0];
+                }
+                // Otherwise, we need to do some array copying.
+                else
+                {
+                    ServiceRegistration[] newRegs = new ServiceRegistration[regs.length - 1];
+                    System.arraycopy(regs, 0, newRegs, 0, i);
+                    if (i < newRegs.length)
+                    {
+                        System.arraycopy(
+                            regs, i + 1, newRegs, i, newRegs.length - i);
+                    }
+                    regs = newRegs;
+                }
+            }
+        }      
+        return regs;
+    }
+
+    public synchronized void addServiceListener(ServiceListener l)
+    {
+        m_serviceListener = ServiceListenerMulticaster.add(m_serviceListener, l);
+    }
+
+    public synchronized void removeServiceListener(ServiceListener l)
+    {
+        m_serviceListener = ServiceListenerMulticaster.remove(m_serviceListener, l);
+    }
+
+    protected void fireServiceChanged(ServiceEvent event)
+    {
+        // Grab a copy of the listener list.
+        ServiceListener listener = m_serviceListener;
+        // If not null, then dispatch event.
+        if (listener != null)
+        {
+            m_serviceListener.serviceChanged(event);
+        }
+    }
+
+    private static class ServiceListenerMulticaster implements ServiceListener
+    {
+        protected ServiceListener m_a = null, m_b = null;
+
+        protected ServiceListenerMulticaster(ServiceListener a, ServiceListener b)    
+        {        
+            m_a = a;        
+            m_b = b;    
+        }    
+    
+        public void serviceChanged(ServiceEvent e)    
+        {
+            m_a.serviceChanged(e);
+            m_b.serviceChanged(e);
+        }
+    
+        public static ServiceListener add(ServiceListener a, ServiceListener b)
+        {
+            if (a == null)
+            {
+                return b;
+            }
+            else if (b == null)
+            {
+                return a;
+            }
+            else
+            {
+                return new ServiceListenerMulticaster(a, b);
+            }
+        }
+    
+        public static ServiceListener remove(ServiceListener a, ServiceListener b)
+        {
+            if ((a == null) || (a == b))
+            {
+                return null;
+            }
+            else if (a instanceof ServiceListenerMulticaster)
+            {
+                return add(
+                    remove(((ServiceListenerMulticaster) a).m_a, b),
+                    remove(((ServiceListenerMulticaster) a).m_b, b));
+            }
+            else
+            {
+                return a;
+            }
+        }
+    }
+
+    private static UsageCount getUsageCount(UsageCount[] usages, ServiceReference ref)
+    {
+        for (int i = 0; (usages != null) && (i < usages.length); i++)
+        {
+            if (usages[i].m_ref.equals(ref))
+            {
+                return usages[i];
+            }
+        }
+        return null;
+    }
+
+    private static UsageCount[] addUsageCount(UsageCount[] usages, ServiceReference ref, Object svcObj)
+    {
+        UsageCount usage = new UsageCount();
+        usage.m_ref = ref;
+        usage.m_svcObj = svcObj;
+        usage.m_count++;
+
+        if (usages == null)
+        {
+            usages = new UsageCount[] { usage };
+        }
+        else
+        {
+            UsageCount[] newUsages = new UsageCount[usages.length + 1];
+            System.arraycopy(usages, 0, newUsages, 0, usages.length);
+            newUsages[usages.length] = usage;
+            usages = newUsages;
+        }
+        return usages;
+    }
+
+    private static UsageCount[] removeUsageCount(UsageCount[] usages, ServiceReference ref)
+    {
+        for (int i = 0; (usages != null) && (i < usages.length); i++)
+        {
+            if (usages[i].m_ref.equals(ref))
+            {
+                // If this is the only usage, then point to empty list.
+                if ((usages.length - 1) == 0)
+                {
+                    usages = new UsageCount[0];
+                }
+                // Otherwise, we need to do some array copying.
+                else
+                {
+                    UsageCount[] newUsages= new UsageCount[usages.length - 1];
+                    System.arraycopy(usages, 0, newUsages, 0, i);
+                    if (i < newUsages.length)
+                    {
+                        System.arraycopy(
+                            usages, i + 1, newUsages, i, newUsages.length - i);
+                    }
+                    usages = newUsages;
+                }
+            }
+        }
+        
+        return usages;
+    }
+
+    private static class UsageCount
+    {
+        public int m_count = 0;
+        public ServiceReference m_ref = null;
+        public Object m_svcObj = null;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/StartLevelActivator.java b/src/org/apache/osgi/framework/StartLevelActivator.java
new file mode 100644
index 0000000..222b55e
--- /dev/null
+++ b/src/org/apache/osgi/framework/StartLevelActivator.java
@@ -0,0 +1,43 @@
+/*
+ *   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.osgi.framework;
+
+import org.osgi.framework.*;
+
+class StartLevelActivator implements BundleActivator
+{
+    private Felix m_felix = null;
+    private ServiceRegistration m_reg = null;
+
+    public StartLevelActivator(Felix felix)
+    {
+        m_felix = felix;
+    }
+
+    public void start(BundleContext context) throws Exception
+    {
+        m_reg = context.registerService(
+            org.osgi.service.startlevel.StartLevel.class.getName(),
+            new StartLevelImpl(m_felix),
+            null);
+    }
+
+    public void stop(BundleContext context) throws Exception
+    {
+        m_reg.unregister();
+    }
+}
diff --git a/src/org/apache/osgi/framework/StartLevelImpl.java b/src/org/apache/osgi/framework/StartLevelImpl.java
new file mode 100644
index 0000000..831af64
--- /dev/null
+++ b/src/org/apache/osgi/framework/StartLevelImpl.java
@@ -0,0 +1,147 @@
+/*
+ *   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.osgi.framework;
+
+import java.security.AccessController;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.osgi.framework.AdminPermission;
+import org.osgi.framework.Bundle;
+import org.osgi.service.startlevel.StartLevel;
+
+/**
+ * @author rickhall
+ *
+ * To change the template for this generated type comment go to
+ * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
+**/
+public class StartLevelImpl implements StartLevel, Runnable
+{
+    private Felix m_felix = null;
+    private List m_requestList = null;
+    // Reusable admin permission.
+    private static AdminPermission m_adminPerm = new AdminPermission();
+    
+    public StartLevelImpl(Felix felix)
+    {
+        m_felix = felix;
+        m_requestList = new ArrayList();
+
+        // Start a thread to perform asynchronous package refreshes.
+        Thread t = new Thread(this, "FelixStartLevel");
+        t.setDaemon(true);
+        t.start();
+    }
+    
+    /* (non-Javadoc)
+     * @see org.osgi.service.startlevel.StartLevel#getStartLevel()
+    **/
+    public int getStartLevel()
+    {
+        return m_felix.getStartLevel();
+    }
+
+    /* (non-Javadoc)
+     * @see org.osgi.service.startlevel.StartLevel#setStartLevel(int)
+    **/
+    public void setStartLevel(int startlevel)
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(m_adminPerm);
+        }
+        else if (startlevel <= 0)
+        {
+            throw new IllegalArgumentException(
+                "Start level must be greater than zero.");
+        }
+        synchronized (m_requestList)
+        {
+            m_requestList.add(new Integer(startlevel));
+            m_requestList.notifyAll();
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.osgi.service.startlevel.StartLevel#getBundleStartLevel(org.osgi.framework.Bundle)
+    **/
+    public int getBundleStartLevel(Bundle bundle)
+    {
+        return m_felix.getBundleStartLevel(bundle);
+    }
+
+    /* (non-Javadoc)
+     * @see org.osgi.service.startlevel.StartLevel#setBundleStartLevel(org.osgi.framework.Bundle, int)
+    **/
+    public void setBundleStartLevel(Bundle bundle, int startlevel)
+    {
+        m_felix.setBundleStartLevel(bundle, startlevel);
+    }
+
+    /* (non-Javadoc)
+     * @see org.osgi.service.startlevel.StartLevel#getInitialBundleStartLevel()
+    **/
+    public int getInitialBundleStartLevel()
+    {
+        return m_felix.getInitialBundleStartLevel();
+    }
+
+    /* (non-Javadoc)
+     * @see org.osgi.service.startlevel.StartLevel#setInitialBundleStartLevel(int)
+    **/
+    public void setInitialBundleStartLevel(int startlevel)
+    {
+        m_felix.setInitialBundleStartLevel(startlevel);
+    }
+
+    /* (non-Javadoc)
+     * @see org.osgi.service.startlevel.StartLevel#isBundlePersistentlyStarted(org.osgi.framework.Bundle)
+    **/
+    public boolean isBundlePersistentlyStarted(Bundle bundle)
+    {
+        return m_felix.isBundlePersistentlyStarted(bundle);
+    }
+
+    public void run()
+    {
+        int startLevel = 0;
+
+        // This thread loops forever, thus it should
+        // be a daemon thread.
+        while (true)
+        {
+            synchronized (m_requestList)
+            {
+                // Wait for a request.
+                while (m_requestList.size() == 0)
+                {
+                    try {
+                        m_requestList.wait();
+                    } catch (InterruptedException ex) {
+                    }
+                }
+                
+                // Get the requested start level.
+                startLevel = ((Integer) m_requestList.remove(0)).intValue();
+            }
+
+            // Set the new start level.
+            m_felix.setFrameworkStartLevel(startLevel);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/SystemBundle.java b/src/org/apache/osgi/framework/SystemBundle.java
new file mode 100644
index 0000000..d201e7d
--- /dev/null
+++ b/src/org/apache/osgi/framework/SystemBundle.java
@@ -0,0 +1,277 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.InputStream;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.*;
+
+import org.apache.osgi.framework.searchpolicy.*;
+import org.apache.osgi.framework.util.CaseInsensitiveMap;
+import org.apache.osgi.framework.util.FelixConstants;
+import org.apache.osgi.moduleloader.LibrarySource;
+import org.apache.osgi.moduleloader.ResourceSource;
+import org.osgi.framework.*;
+
+
+class SystemBundle extends BundleImpl
+{
+    private List m_activatorList = null;
+    private BundleActivator m_activator = null;
+    private Thread m_shutdownThread = null;
+    private Object[][] m_attributes = null;
+    private ResourceSource[] m_resSources = null;
+    private LibrarySource[] m_libSources = null;
+
+    protected SystemBundle(Felix felix, BundleInfo info, List activatorList)
+        throws BundleException
+    {
+        super(felix, info);
+
+        // Create an activator list if necessary.
+        if (activatorList == null)
+        {
+            activatorList = new ArrayList();
+        }
+
+        // Add the bundle activator for the package admin service.
+        activatorList.add(new PackageAdminActivator(felix));
+
+        // Add the bundle activator for the start level service.
+        activatorList.add(new StartLevelActivator(felix));
+
+        m_activatorList = activatorList;
+
+        // The system bundle exports framework packages as well as
+        // arbitrary user-defined packages from the system class path.
+        // We must construct the system bundle's export metadata.
+
+        // Get system property that specifies which class path
+        // packages should be exported by the system bundle.
+        R4Package[] classPathPkgs = null;
+        try
+        {
+            classPathPkgs = R4Package.parseImportOrExportHeader(
+                getFelix().getConfig().get(FelixConstants.FRAMEWORK_SYSTEMPACKAGES));
+        }
+        catch (Exception ex)
+        {
+            getFelix().getLogger().log(
+                LogWrapper.LOG_ERROR,
+                "Error parsing system bundle export statement.", ex);
+        }
+
+        // Now, create the list of standard framework exports for
+        // the system bundle.
+        R4Package[] exports = new R4Package[classPathPkgs.length + 3];
+
+        exports[0] = new R4Package(
+            "org.osgi.framework",
+            new R4Directive[0],
+            new R4Attribute[] { new R4Attribute("version", "1.2.0", false) });
+
+        exports[1] = new R4Package(
+            "org.osgi.service.packageadmin",
+            new R4Directive[0],
+            new R4Attribute[] { new R4Attribute("version", "1.2.0", false) });
+
+        exports[2] = new R4Package(
+            "org.osgi.service.startlevel",
+            new R4Directive[0],
+            new R4Attribute[] { new R4Attribute("version", "1.0.0", false) });
+
+        // Copy the class path exported packages.
+        System.arraycopy(classPathPkgs, 0, exports, 3, classPathPkgs.length);
+
+        m_attributes = new Object[][] {
+            new Object[] { R4SearchPolicy.EXPORTS_ATTR, exports },
+            new Object[] { R4SearchPolicy.IMPORTS_ATTR, new R4Package[0] }
+        };
+
+        m_resSources = new ResourceSource[0];
+
+        m_libSources = null;
+
+        String exportString = "";
+        for (int i = 0; i < exports.length; i++)
+        {
+            exportString = exportString +
+            exports[i].getId()
+            + "; specification-version=\""
+            + exports[i].getVersionLow().toString() + "\"";
+
+            if (i < (exports.length - 1))
+            {
+                exportString = exportString + ", ";
+                exportString = exportString +
+                    exports[i].getId()
+                    + "; specification-version=\""
+                    + exports[i].getVersionLow().toString() + "\", ";
+            }
+        }
+
+        // Initialize header map as a case insensitive map.
+        Map map = new CaseInsensitiveMap();
+        map.put(FelixConstants.BUNDLE_VERSION, FelixConstants.FELIX_VERSION_VALUE);
+        map.put(FelixConstants.BUNDLE_NAME, "System Bundle");
+        map.put(FelixConstants.BUNDLE_DESCRIPTION,
+            "This bundle is system specific; it implements various system services.");
+        map.put(FelixConstants.EXPORT_PACKAGE, exportString);
+        ((SystemBundleArchive) getInfo().getArchive()).setManifestHeader(map);
+    }
+
+    public Object[][] getAttributes()
+    {
+        return m_attributes;
+    }
+
+    public ResourceSource[] getResourceSources()
+    {
+        return m_resSources;
+    }
+
+    public LibrarySource[] getLibrarySources()
+    {
+        return m_libSources;
+    }
+
+    public synchronized void start() throws BundleException
+    {
+        // The system bundle is only started once and it
+        // is started by the framework.
+        if (getState() == Bundle.ACTIVE)
+        {
+            throw new BundleException("Cannot start the system bundle.");
+        }
+
+        getInfo().setState(Bundle.STARTING);
+
+        try {
+            getInfo().setContext(new BundleContextImpl(getFelix(), this));
+            getActivator().start(getInfo().getContext());
+        } catch (Throwable throwable) {
+throwable.printStackTrace();
+            throw new BundleException(
+                "Unable to start system bundle.", throwable);
+        }
+
+        // Do NOT set the system bundle state to active yet, this
+        // must be done after all other bundles have been restarted.
+        // This will be done after the framework is initialized.
+    }
+
+    public synchronized void stop() throws BundleException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(new AdminPermission());
+        }
+
+        // Spec says stop() on SystemBundle should return immediately and
+        // shutdown framework on another thread.
+        if (getFelix().getStatus() == Felix.RUNNING_STATUS)
+        {
+            // Initial call of stop, so kick off shutdown.
+            m_shutdownThread = new Thread("FelixShutdown") {
+                public void run()
+                {
+                    try
+                    {
+                        getFelix().shutdown();
+                    }
+                    catch (Exception ex)
+                    {
+                        getFelix().getLogger().log(
+                            LogWrapper.LOG_ERROR,
+                            "SystemBundle: Error while shutting down.", ex);
+                    }
+
+                    // Only shutdown the JVM if the framework is running stand-alone.
+                    String embedded = getFelix().getConfig()
+                        .get(FelixConstants.EMBEDDED_EXECUTION_PROP);
+                    boolean isEmbedded = (embedded == null)
+                        ? false : embedded.equals("true");
+                    if (!isEmbedded)
+                    {
+                        if (System.getSecurityManager() != null)
+                        {
+                            AccessController.doPrivileged(new PrivilegedAction() {
+                                public Object run()
+                                {
+                                    System.exit(0);
+                                    return null;
+                                }
+                            });
+                        }
+                        else
+                        {
+                            System.exit(0);
+                        }
+                    }
+                }
+            };
+            getInfo().setState(Bundle.STOPPING);
+            m_shutdownThread.start();
+        }
+        else if ((getFelix().getStatus() == Felix.STOPPING_STATUS) &&
+                 (Thread.currentThread() == m_shutdownThread))
+        {
+            // Callback from shutdown thread, so do our own stop.
+            try
+            {
+                getActivator().stop(getInfo().getContext());
+            }
+            catch (Throwable throwable)
+            {
+                throw new BundleException(
+                        "Unable to stop system bundle.", throwable);
+            }
+        }
+    }
+
+    public synchronized void uninstall() throws BundleException
+    {
+        throw new BundleException("Cannot uninstall the system bundle.");
+    }
+
+    public synchronized void update() throws BundleException
+    {
+        update(null);
+    }
+
+    public synchronized void update(InputStream is) throws BundleException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            AccessController.checkPermission(new AdminPermission());
+        }
+
+        // TODO: This is supposed to stop and then restart the framework.
+        throw new BundleException("System bundle update not implemented yet.");
+    }
+
+    protected BundleActivator getActivator()
+        throws Exception
+    {
+        if (m_activator == null)
+        {
+            m_activator = new SystemBundleActivator(getFelix(), m_activatorList);
+        }
+        return m_activator;
+    }
+}
diff --git a/src/org/apache/osgi/framework/SystemBundleActivator.java b/src/org/apache/osgi/framework/SystemBundleActivator.java
new file mode 100644
index 0000000..0dbd1a7
--- /dev/null
+++ b/src/org/apache/osgi/framework/SystemBundleActivator.java
@@ -0,0 +1,61 @@
+/*
+ *   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.osgi.framework;
+
+import java.util.List;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+class SystemBundleActivator implements BundleActivator
+{
+    private Felix m_felix = null;
+    private List m_activatorList = null;
+    private BundleContext m_context = null;
+
+    SystemBundleActivator(Felix felix, List activatorList)
+    {
+        this.m_felix = felix;
+        this.m_activatorList = activatorList;
+    }
+
+    public void start(BundleContext context) throws Exception
+    {
+        this.m_context = context;
+
+        // Start all activators.
+        if (m_activatorList != null)
+        {
+            for (int i = 0; i < m_activatorList.size(); i++)
+            {
+                ((BundleActivator) m_activatorList.get(i)).start(context);
+            }
+        }
+    }
+
+    public void stop(BundleContext context) throws Exception
+    {
+        if (m_activatorList != null)
+        {
+            // Stop all activators.
+            for (int i = 0; i < m_activatorList.size(); i++)
+            {
+                ((BundleActivator) m_activatorList.get(i)).stop(context);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/SystemBundleArchive.java b/src/org/apache/osgi/framework/SystemBundleArchive.java
new file mode 100644
index 0000000..611d350
--- /dev/null
+++ b/src/org/apache/osgi/framework/SystemBundleArchive.java
@@ -0,0 +1,109 @@
+/*
+ *   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.osgi.framework;
+
+import java.io.File;
+import java.util.Map;
+
+import org.apache.osgi.framework.cache.BundleArchive;
+import org.apache.osgi.framework.util.FelixConstants;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+
+public class SystemBundleArchive implements BundleArchive
+{
+    private Map m_headerMap = null;
+
+    public long getId()
+    {
+        return 0;
+    }
+    
+    public String getLocation()
+        throws Exception
+    {
+        return FelixConstants.SYSTEM_BUNDLE_LOCATION;
+    }
+
+    public int getPersistentState()
+        throws Exception
+    {
+        return Bundle.ACTIVE;
+    }
+
+    public void setPersistentState(int state)
+        throws Exception
+    {
+    }
+
+    public int getStartLevel()
+        throws Exception
+    {
+        return FelixConstants.SYSTEMBUNDLE_DEFAULT_STARTLEVEL;
+    }
+
+    public void setStartLevel(int level)
+        throws Exception
+    {
+    }
+
+    public File getDataFile(String fileName)
+        throws Exception
+    {
+        return null;
+    }
+
+    public BundleActivator getActivator(ClassLoader loader)
+        throws Exception
+    {
+        return null;
+    }
+
+    public void setActivator(Object obj)
+        throws Exception
+    {
+    }
+
+    public int getRevisionCount()
+        throws Exception
+    {
+        return 1;
+    }
+
+    public Map getManifestHeader(int revision)
+        throws Exception
+    {
+        return m_headerMap;
+    }
+    
+    protected void setManifestHeader(Map headerMap)
+    {
+        m_headerMap = headerMap;
+    }
+
+    public String[] getClassPath(int revision)
+        throws Exception
+    {
+        return null;
+    }
+
+    public String findLibrary(int revision, String libName)
+        throws Exception
+    {
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/cache/BundleArchive.java b/src/org/apache/osgi/framework/cache/BundleArchive.java
new file mode 100644
index 0000000..9ad76c1
--- /dev/null
+++ b/src/org/apache/osgi/framework/cache/BundleArchive.java
@@ -0,0 +1,194 @@
+/*
+ *   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.osgi.framework.cache;
+
+import java.io.File;
+import java.util.Map;
+
+import org.osgi.framework.BundleActivator;
+
+/**
+ * <p>
+ * This interface represents an individual cached bundle in the
+ * bundle cache. Felix uses this interface to access all information
+ * about the associated bundle's cached information. Classes that implement
+ * this interface will be related to a specific implementation of the
+ * <tt>BundleCache</tt> interface.
+ * </p>
+ * @see org.apache.osgi.framework.BundleCache
+**/
+public interface BundleArchive
+{
+    /**
+     * <p>
+     * Returns the identifier of the bundle associated with this archive.
+     * </p>
+     * @return the identifier of the bundle associated with this archive.
+    **/
+    public long getId();
+    
+    /**
+     * <p>
+     * Returns the location string of the bundle associated with this archive.
+     * </p>
+     * @return the location string of the bundle associated with this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public String getLocation()
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns the persistent state of the bundle associated with the archive;
+     * this value will be either <tt>Bundle.INSTALLED</tt> or <tt>Bundle.ACTIVE</tt>.
+     * </p>
+     * @return the persistent state of the bundle associated with this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public int getPersistentState()
+        throws Exception;
+
+    /**
+     * <p>
+     * Sets the persistent state of the bundle associated with this archive;
+     * this value will be either <tt>Bundle.INSTALLED</tt> or <tt>Bundle.ACTIVE</tt>.
+     * </p>
+     * @param state the new bundle state to write to the archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public void setPersistentState(int state)
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns the start level of the bundle associated with this archive.
+     * </p>
+     * @return the start level of the bundle associated with this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public int getStartLevel()
+        throws Exception;
+
+    /**
+     * <p>
+     * Sets the start level of the bundle associated with this archive.
+     * </p>
+     * @param level the new bundle start level to write to the archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public void setStartLevel(int level)
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns an appropriate data file for the bundle associated with the
+     * archive using the supplied file name.
+     * </p>
+     * @return a <tt>File</tt> corresponding to the requested data file for
+     *         the bundle associated with this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public File getDataFile(String fileName)
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns the persistent bundle activator of the bundle associated with
+     * this archive; this is a non-standard OSGi method that is only called
+     * when Felix is running in non-strict OSGi mode.
+     * </p>
+     * @param loader the class loader to use when trying to instantiate
+     *        the bundle activator.
+     * @return the persistent bundle activator of the bundle associated with
+     *         this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public BundleActivator getActivator(ClassLoader loader)
+        throws Exception;
+
+    /**
+     * <p>
+     * Sets the persistent bundle activator of the bundle associated with
+     * this archive; this is a non-standard OSGi method that is only called
+     * when Felix is running in non-strict OSGi mode.
+     * </p>
+     * @param obj the new persistent bundle activator to write to the archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public void setActivator(Object obj)
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns the number of revisions of the bundle associated with the
+     * archive. When a bundle is updated, the previous version of the bundle
+     * is maintained along with the new revision until old revisions are
+     * purged. The revision count reflects how many revisions of the bundle
+     * are currently available in the cache.
+     * </p>
+     * @return the number of revisions of the bundle associated with this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public int getRevisionCount()
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns the main attributes of the JAR file manifest header of the
+     * specified revision of the bundle associated with this archive. The
+     * returned map should be case insensitive.
+     * </p>
+     * @param revision the specified revision.
+     * @return the case-insensitive JAR file manifest header of the specified
+     *         revision of the bundle associated with this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public Map getManifestHeader(int revision)
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns an array of <tt>String</tt>s that represent the class path of
+     * the specified revision of the bundle associated with this archive.
+     * Currently, these values are restricted to absolute paths in the file
+     * system, but this may be lifted in the future (perhaps they should be
+     * <tt>ResourceSource</tt>s from the Module Loader.
+     * </p>
+     * @param revision the specified revision.
+     * @return a <tt>String</tt> array of the absolute path names that
+     *         comprise the class path of the specified revision of the
+     *         bundle associated with this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public String[] getClassPath(int revision)
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns the absolute file path for the specified native library of the
+     * specified revision of the bundle associated with this archive.
+     * </p>
+     * @param revision the specified revision.
+     * @param libName the name of the library.
+     * @return a <tt>String</tt> that contains the absolute path name to
+     *         the requested native library of the specified revision of the
+     *         bundle associated with this archive.
+     * @throws java.lang.Exception if any error occurs.
+    **/
+    public String findLibrary(int revision, String libName)
+        throws Exception;
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/cache/BundleCache.java b/src/org/apache/osgi/framework/cache/BundleCache.java
new file mode 100644
index 0000000..87c0ee9
--- /dev/null
+++ b/src/org/apache/osgi/framework/cache/BundleCache.java
@@ -0,0 +1,148 @@
+/*
+ *   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.osgi.framework.cache;
+
+import java.io.InputStream;
+
+import org.apache.osgi.framework.LogWrapper;
+import org.apache.osgi.framework.util.PropertyResolver;
+
+/**
+ * <p>
+ * This interface represents the storage mechanism that Felix uses for
+ * caching bundles. It is possible for multiple implementations of
+ * this interface to exist for different storage technologies, such as the
+ * file system, memory, or a database. Felix includes a default implementation
+ * of this interface that uses the file system. Felix allows you to specify
+ * alternative implementations to use by specifying a class name via the
+ * <tt>felix.cache.class</tt> system property. Bundle cache implemenations
+ * should implement this interface and provide a default constructor.
+ * </p>
+ * @see org.apache.osgi.framework.BundleArchive
+**/
+public interface BundleCache
+{
+    /**
+     * <p>
+     * This method is called before using the BundleCache implementation
+     * to initialize it and to pass it a reference to its associated
+     * configuration property resolver and logger. The <tt>BundleCache</tt>
+     * implementation should not use <tt>System.getProperty()</tt> directly
+     * for configuration properties, it should use the property resolver
+     * instance passed into this method. The property resolver
+     * provides access to properties passed into the Felix instance's
+     * constructor. This approach allows multiple instances of Felix to
+     * exist in memory at the same time, but for
+     * them to be configured differently. For example, an application may
+     * want two instances of Felix, where each instance stores their cache
+     * in a different location in the file system. When using multiple
+     * instances of Felix in memory at the same time, system properties
+     * should be avoided and all properties should be passed in to Felix's
+     * constructor.
+     * </p>
+     * @param cfg the property resolver for obtaining configuration properties.
+     * @param logger the logger to use for reporting errors.
+     * @throws Exception if any error occurs.
+    **/
+    public void initialize(PropertyResolver cfg, LogWrapper logger)
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns all cached bundle archives.
+     * </p>
+     * @return an array of all cached bundle archives.
+     * @throws Exception if any error occurs.
+    **/
+    public BundleArchive[] getArchives()
+        throws Exception;
+
+    /**
+     * <p>
+     * Returns the bundle archive associated with the specified
+     * bundle indentifier.
+     * </p>
+     * @param id the identifier of the bundle archive to retrieve.
+     * @return the bundle archive assocaited with the specified bundle identifier.
+     * @throws Exception if any error occurs.
+    **/
+    public BundleArchive getArchive(long id)
+        throws Exception;
+
+    /**
+     * <p>
+     * Creates a new bundle archive for the specified bundle
+     * identifier using the supplied location string and input stream. The
+     * contents of the bundle JAR file should be read from the supplied
+     * input stream, which will not be <tt>null</tt>. The input stream is
+     * closed by the caller; the implementation is only responsible for
+     * closing streams it opens. If this method completes successfully, then
+     * it means that the initial bundle revision of the specified bundle was
+     * successfully cached.
+     * </p>
+     * @param id the identifier of the bundle associated with the new archive.
+     * @param location the location of the bundle associated with the new archive.
+     * @param is the input stream to the bundle's JAR file.
+     * @return the created bundle archive.
+     * @throws Exception if any error occurs.
+    **/
+    public BundleArchive create(long id, String location, InputStream is)
+        throws Exception;
+
+    /**
+     * <p>
+     * Saves an updated revision of the specified bundle to
+     * the bundle cache using the supplied input stream. The contents of the
+     * updated bundle JAR file should be read from the supplied input stream,
+     * which will not be <tt>null</tt>. The input stream is closed by the
+     * caller; the implementation is only responsible for closing streams
+     * it opens. Updating a bundle in the cache does not replace the current
+     * revision of the bundle, it makes a new revision available. If this
+     * method completes successfully, then it means that the number of
+     * revisions of the specified bundle has increased by one.
+     * </p>
+     * @param ba the bundle archive of the bundle to update.
+     * @param is the input stream to the bundle's updated JAR file.
+     * @throws Exception if any error occurs.
+    **/
+    public void update(BundleArchive ba, InputStream is)
+        throws Exception;
+
+    /**
+     * <p>
+     * Purges all old revisions of the specified bundle from
+     * the cache. If this method completes successfully, then it means that
+     * only the most current revision of the bundle should exist in the cache.
+     * </p>
+     * @param ba the bundle archive of the bundle to purge.
+     * @throws Exception if any error occurs.
+    **/
+    public void purge(BundleArchive ba)
+        throws Exception;
+
+    /**
+     * <p>
+     * Removes the specified bundle from the cache. If this method
+     * completes successfully, there should be no trace of the removed bundle
+     * in the cache.
+     * </p>
+     * @param ba the bundle archive of the bundle to remove.
+     * @throws Exception if any error occurs.
+    **/
+    public void remove(BundleArchive ba)
+        throws Exception;
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/cache/DefaultBundleArchive.java b/src/org/apache/osgi/framework/cache/DefaultBundleArchive.java
new file mode 100644
index 0000000..18d04c0
--- /dev/null
+++ b/src/org/apache/osgi/framework/cache/DefaultBundleArchive.java
@@ -0,0 +1,1471 @@
+/*
+ *   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.osgi.framework.cache;
+
+import java.io.*;
+import java.security.*;
+import java.util.*;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+
+import org.apache.osgi.framework.LogWrapper;
+import org.apache.osgi.framework.util.*;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+
+/**
+ * <p>
+ * This class, combined with <tt>DefaultBundleCache</tt>, implements the
+ * default file system-based bundle cache for Felix.
+ * </p>
+ * @see org.apache.osgi.framework.util.DefaultBundleCache
+**/
+public class DefaultBundleArchive implements BundleArchive
+{
+    private static final transient String BUNDLE_JAR_FILE = "bundle.jar";
+    private static final transient String BUNDLE_LOCATION_FILE = "bundle.location";
+    private static final transient String BUNDLE_STATE_FILE = "bundle.state";
+    private static final transient String BUNDLE_START_LEVEL_FILE = "bundle.startlevel";
+    private static final transient String REFRESH_COUNTER_FILE = "refresh.counter";
+    private static final transient String BUNDLE_ACTIVATOR_FILE = "bundle.activator";
+
+    private static final transient String REVISION_DIRECTORY = "version";
+    private static final transient String EMBEDDED_DIRECTORY = "embedded";
+    private static final transient String LIBRARY_DIRECTORY = "lib";
+    private static final transient String DATA_DIRECTORY = "data";
+
+    private static final transient String ACTIVE_STATE = "active";
+    private static final transient String INSTALLED_STATE = "installed";
+    private static final transient String UNINSTALLED_STATE = "uninstalled";
+
+    private LogWrapper m_logger = null;
+    private long m_id = -1;
+    private File m_dir = null;
+    private String m_location = null;
+    private int m_persistentState = -1;
+    private int m_startLevel = -1;
+    private Map m_currentHeader = null;
+
+    private long m_refreshCount = -1;
+    private int m_revisionCount = -1;
+
+    public DefaultBundleArchive(LogWrapper logger, File dir, long id, String location, InputStream is)    
+        throws Exception
+    {
+        this(logger, dir, id);
+        m_location = location;
+
+        // Try to save and pre-process the bundle JAR.
+        try
+        {
+            initialize(is);
+        }
+        catch (Exception ex)
+        {
+            if (!deleteDirectoryTree(dir))
+            {
+                m_logger.log(
+                    LogWrapper.LOG_ERROR,
+                    "Unable to delete the archive directory: " + id);
+            }
+            throw ex;
+        }
+    }
+
+    public DefaultBundleArchive(LogWrapper logger, File dir, long id)
+    {
+        m_logger = logger;
+        m_dir = dir;
+        m_id = id;
+        if (m_id <= 0)
+        {
+            throw new IllegalArgumentException(
+                "Bundle ID cannot be less than or equal to zero.");
+        }
+    }
+
+    private void initialize(InputStream is)
+        throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.INITIALIZE_ACTION, this, is));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            initializeUnchecked(is);
+        }
+    }
+
+    private void initializeUnchecked(InputStream is)
+        throws Exception
+    {
+        FileWriter fw = null;
+        BufferedWriter bw = null;
+
+        try
+        {
+            // Create archive directory.
+            if (!m_dir.mkdir())
+            {
+                m_logger.log(
+                    LogWrapper.LOG_ERROR,
+                    "DefaultBundleArchive: Unable to create archive directory.");
+                throw new IOException("Unable to create archive directory.");
+            }
+
+            // Save location string.
+            File file = new File(m_dir, BUNDLE_LOCATION_FILE);
+            fw = new FileWriter(file);
+            bw = new BufferedWriter(fw);
+            bw.write(m_location, 0, m_location.length());
+
+            // Create version/revision directory for bundle JAR.
+            // Since this is only called when the bundle JAR is
+            // first saved, the update and revision will always
+            // be "0.0" for the directory name.
+            File revisionDir = new File(m_dir, REVISION_DIRECTORY + "0.0");
+            if (!revisionDir.mkdir())
+            {
+                m_logger.log(
+                    LogWrapper.LOG_ERROR,
+                    "DefaultBundleArchive: Unable to create revision directory.");
+                throw new IOException("Unable to create revision directory.");
+            }
+
+            // Save the bundle jar file.
+            file = new File(revisionDir, BUNDLE_JAR_FILE);
+            copy(is, file);
+
+            // This will always be revision zero.
+            preprocessBundleJar(0, revisionDir);
+
+        }
+        finally
+        {
+            if (is != null) is.close();
+            if (bw != null) bw.close();
+            if (fw != null) fw.close();
+        }
+    }
+
+    public File getDirectory()
+    {
+        return m_dir;
+    }
+
+    public long getId()
+    {
+        return m_id;
+    }
+
+    public String getLocation()
+        throws Exception
+    {
+        if (m_location != null)
+        {
+            return m_location;
+        }
+        else if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                return (String) AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.GET_LOCATION_ACTION, this));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            return getLocationUnchecked();
+        }
+    }
+
+    private String getLocationUnchecked()
+        throws Exception
+    {
+        // Get bundle location file.
+        File locFile = new File(m_dir, BUNDLE_LOCATION_FILE);
+
+        // Read bundle location.
+        FileReader fr = null;
+        BufferedReader br = null;
+        try
+        {
+            fr = new FileReader(locFile);
+            br = new BufferedReader(fr);
+            m_location = br.readLine();
+            return m_location;
+        }
+        finally
+        {
+            if (br != null) br.close();
+            if (fr != null) fr.close();
+        }
+    }
+
+    public int getPersistentState()
+        throws Exception
+    {
+        if (m_persistentState >= 0)
+        {
+            return m_persistentState;
+        }
+        else if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                return ((Integer) AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.GET_PERSISTENT_STATE_ACTION, this))).intValue();
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            return getPersistentStateUnchecked();
+        }
+    }
+
+    private int getPersistentStateUnchecked()
+        throws Exception
+    {
+        // Get bundle state file.
+        File stateFile = new File(m_dir, BUNDLE_STATE_FILE);
+
+        // If the state file doesn't exist, then
+        // assume the bundle was installed.
+        if (!stateFile.exists())
+        {
+            return Bundle.INSTALLED;
+        }
+
+        // Read the bundle state.
+        FileReader fr = null;
+        BufferedReader br= null;
+        try
+        {
+            fr = new FileReader(stateFile);
+            br = new BufferedReader(fr);
+            String s = br.readLine();
+            if (s.equals(ACTIVE_STATE))
+            {
+                m_persistentState = Bundle.ACTIVE;
+            }
+            else if (s.equals(UNINSTALLED_STATE))
+            {
+                m_persistentState = Bundle.UNINSTALLED;
+            }
+            else
+            {
+                m_persistentState = Bundle.INSTALLED;
+            }
+            return m_persistentState;
+        }
+        finally
+        {
+            if (br != null) br.close();
+            if (fr != null) fr.close();
+        }
+    }
+
+    public void setPersistentState(int state)
+        throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.SET_PERSISTENT_STATE_ACTION, this, state));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            setPersistentStateUnchecked(state);
+        }
+    }
+
+    private void setPersistentStateUnchecked(int state)
+        throws Exception
+    {
+        // Get bundle state file.
+        File stateFile = new File(m_dir, BUNDLE_STATE_FILE);
+
+        // Write the bundle state.
+        FileWriter fw = null;
+        BufferedWriter bw= null;
+        try
+        {
+            fw = new FileWriter(stateFile);
+            bw = new BufferedWriter(fw);
+            String s = null;
+            switch (state)
+            {
+                case Bundle.ACTIVE:
+                    s = ACTIVE_STATE;
+                    break;
+                case Bundle.UNINSTALLED:
+                    s = UNINSTALLED_STATE;
+                    break;
+                default:
+                    s = INSTALLED_STATE;
+                    break;
+            }
+            bw.write(s, 0, s.length());
+            m_persistentState = state;
+        }
+        catch (IOException ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "DefaultBundleArchive: Unable to record state: " + ex);
+            throw ex;
+        }
+        finally
+        {
+            if (bw != null) bw.close();
+            if (fw != null) fw.close();
+        }
+    }
+
+    public int getStartLevel()
+        throws Exception
+    {
+        if (m_startLevel >= 0)
+        {
+            return m_startLevel;
+        }
+        else if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                return ((Integer) AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.GET_START_LEVEL_ACTION, this))).intValue();
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            return getStartLevelUnchecked();
+        }
+    }
+
+    private int getStartLevelUnchecked()
+        throws Exception
+    {
+        // Get bundle start level file.
+        File levelFile = new File(m_dir, BUNDLE_START_LEVEL_FILE);
+
+        // If the start level file doesn't exist, then
+        // return an error.
+        if (!levelFile.exists())
+        {
+            return -1;
+        }
+
+        // Read the bundle start level.
+        FileReader fr = null;
+        BufferedReader br= null;
+        try
+        {
+            fr = new FileReader(levelFile);
+            br = new BufferedReader(fr);
+            m_startLevel = Integer.parseInt(br.readLine());
+            return m_startLevel;
+        }
+        finally
+        {
+            if (br != null) br.close();
+            if (fr != null) fr.close();
+        }
+    }
+
+    public void setStartLevel(int level)
+        throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.SET_START_LEVEL_ACTION, this, level));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            setStartLevelUnchecked(level);
+        }
+    }
+
+    private void setStartLevelUnchecked(int level)
+        throws Exception
+    {
+        // Get bundle start level file.
+        File levelFile = new File(m_dir, BUNDLE_START_LEVEL_FILE);
+
+        // Write the bundle start level.
+        FileWriter fw = null;
+        BufferedWriter bw = null;
+        try
+        {
+            fw = new FileWriter(levelFile);
+            bw = new BufferedWriter(fw);
+            String s = Integer.toString(level);
+            bw.write(s, 0, s.length());
+            m_startLevel = level;
+        }
+        catch (IOException ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "DefaultBundleArchive: Unable to record start leel: " + ex);
+            throw ex;
+        }
+        finally
+        {
+            if (bw != null) bw.close();
+            if (fw != null) fw.close();
+        }
+    }
+
+    public File getDataFile(String fileName)
+        throws Exception
+    {
+        // Do some sanity checking.
+        if ((fileName.length() > 0) && (fileName.charAt(0) == File.separatorChar))
+            throw new IllegalArgumentException("The data file path must be relative, not absolute.");
+        else if (fileName.indexOf("..") >= 0)
+            throw new IllegalArgumentException("The data file path cannot contain a reference to the \"..\" directory.");
+
+        // Get bundle data directory.
+        File dataDir = new File(m_dir, DATA_DIRECTORY);
+
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.CREATE_DATA_DIR_ACTION, this, dataDir));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            createDataDirectoryUnchecked(dataDir);
+        }
+
+        // Return the data file.
+        return new File(dataDir, fileName);
+    }
+
+    private void createDataDirectoryUnchecked(File dir)
+        throws Exception
+    {
+        // Create data directory if necessary.
+        if (!dir.exists())
+        {
+            if (!dir.mkdir())
+            {
+                throw new IOException("Unable to create bundle data directory.");
+            }
+        }
+    }
+
+    public BundleActivator getActivator(ClassLoader loader)
+        throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                return (BundleActivator) AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.GET_ACTIVATOR_ACTION, this, loader));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            return getActivatorUnchecked(loader);
+        }
+    }
+
+    private BundleActivator getActivatorUnchecked(ClassLoader loader)
+        throws Exception
+    {
+        // Get bundle activator file.
+        File activatorFile = new File(m_dir, BUNDLE_ACTIVATOR_FILE);
+        // If the activator file doesn't exist, then
+        // assume there isn't one.
+        if (!activatorFile.exists())
+            return null;
+
+        // Deserialize the activator object.
+        InputStream is = null;
+        ObjectInputStreamX ois = null;
+        try
+        {
+            is = new FileInputStream(activatorFile);
+            ois = new ObjectInputStreamX(is, loader);
+            Object o = ois.readObject();
+            return (BundleActivator) o;
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "DefaultBundleArchive: Trying to deserialize - " + ex);
+        }
+        finally
+        {
+            if (ois != null) ois.close();
+            if (is != null) is.close();
+        }
+
+        return null;
+    }
+
+    public void setActivator(Object obj)
+        throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.SET_ACTIVATOR_ACTION, this, obj));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            setActivatorUnchecked(obj);
+        }
+    }
+
+    private void setActivatorUnchecked(Object obj)
+        throws Exception
+    {
+        if (!(obj instanceof Serializable))
+        {
+            return;
+        }
+
+        // Get bundle activator file.
+        File activatorFile = new File(m_dir, BUNDLE_ACTIVATOR_FILE);
+
+        // Serialize the activator object.
+        OutputStream os = null;
+        ObjectOutputStream oos = null;
+        try
+        {
+            os = new FileOutputStream(activatorFile);
+            oos = new ObjectOutputStream(os);
+            oos.writeObject(obj);
+        }
+        catch (IOException ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "DefaultBundleArchive: Unable to serialize activator - " + ex);
+            throw ex;
+        }
+        finally
+        {
+            if (oos != null) oos.close();
+            if (os != null) os.close();
+        }
+    }
+
+    public int getRevisionCount()
+        throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                return ((Integer) AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.GET_REVISION_COUNT_ACTION, this))).intValue();
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            return getRevisionCountUnchecked();
+        }
+    }
+
+    public int getRevisionCountUnchecked()
+    {
+        // We should always have at least one revision
+        // directory, so try to count them if the value
+        // has not been initialized yet.
+        if (m_revisionCount <= 0)
+        {
+            m_revisionCount = 0;
+            File[] children = m_dir.listFiles();
+            for (int i = 0; (children != null) && (i < children.length); i++)
+            {
+                if (children[i].getName().startsWith(REVISION_DIRECTORY))
+                {
+                    m_revisionCount++;
+                }
+            }
+        }
+        return m_revisionCount;
+    }
+
+    public Map getManifestHeader(int revision)
+        throws Exception
+    {
+        // If the request is for the current revision header,
+        // then return the cached copy if it is present.
+        if ((revision == (getRevisionCount() - 1)) && (m_currentHeader != null))
+        {
+            return m_currentHeader;
+        }
+
+        // Get the revision directory.
+        File revisionDir = new File(
+            m_dir, REVISION_DIRECTORY + getRefreshCount() + "." + revision);
+
+        // Get the embedded resource.
+        JarFile jarFile = null;
+
+        try
+        {
+            // Create JarFile object using privileged block.
+            if (System.getSecurityManager() != null)
+            {
+                jarFile = (JarFile) AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.OPEN_BUNDLE_JAR_ACTION, this, revisionDir));
+            }
+            else
+            {
+                jarFile = openBundleJarUnchecked(revisionDir);
+            }
+
+            // Error if no jar file.
+            if (jarFile == null)
+            {
+                throw new IOException("No JAR file found.");
+            }
+
+            // Get manifest.
+            Manifest mf = jarFile.getManifest();
+            // Create a case insensitive map of manifest attributes.
+            Map map = new CaseInsensitiveMap(mf.getMainAttributes());
+            // If the request is for the current revision's header,
+            // then cache it.
+            if (revision == (getRevisionCount() - 1))
+            {
+                m_currentHeader = map;
+            }
+            return map;
+
+        } catch (PrivilegedActionException ex) {
+            throw ((PrivilegedActionException) ex).getException();
+        } finally {
+            if (jarFile != null) jarFile.close();
+        }
+    }
+
+    private JarFile openBundleJarUnchecked(File revisionDir)
+        throws Exception
+    {
+        // Get bundle jar file.
+        File bundleJar = new File(revisionDir, BUNDLE_JAR_FILE);
+        // Get bundle jar file.
+        return new JarFile(bundleJar);
+    }
+
+    public String[] getClassPath(int revision)
+        throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                return (String []) AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.GET_CLASS_PATH_ACTION, this, revision));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            return getClassPathUnchecked(revision);
+        }
+    }
+
+    private String[] getClassPathUnchecked(int revision)
+        throws Exception
+    {
+        // Get the revision directory.
+        File revisionDir = new File(
+            m_dir, REVISION_DIRECTORY + getRefreshCount() + "." + revision);
+
+        // Get the bundle's manifest header.
+        Map map = getManifestHeader(revision);
+        if (map == null)
+        {
+            map = new HashMap();
+        }
+
+        // Find class path meta-data.
+        String classPath = null;
+        Iterator iter = map.entrySet().iterator();
+        while ((classPath == null) && iter.hasNext())
+        {
+            Map.Entry entry = (Map.Entry) iter.next();
+            if (entry.getKey().toString().toLowerCase().equals(
+                FelixConstants.BUNDLE_CLASSPATH.toLowerCase()))
+            {
+                classPath = entry.getValue().toString();
+            }
+        }
+
+        // Parse the class path into strings.
+        String[] classPathStrings = Util.parseDelimitedString(
+            classPath, FelixConstants.CLASS_PATH_SEPARATOR);
+
+        if (classPathStrings == null)
+        {
+            classPathStrings = new String[0];
+        }
+
+        // Now, check for "." in the class path.
+        boolean includeDot = false;
+        for (int i = 0; !includeDot && (i < classPathStrings.length); i++)
+        {
+            if (classPathStrings[i].equals(FelixConstants.CLASS_PATH_DOT))
+            {
+                includeDot = true;
+            }
+        }
+
+        // Include all JARs in the embedded jar directory, since they
+        // were extracted when the bundle was initially saved.
+        File embedDir = new File(revisionDir, EMBEDDED_DIRECTORY);
+        String[] paths = null;
+        if (embedDir.exists())
+        {
+            // The size of the paths array is the number of
+            // embedded JAR files plus one, if we need to include
+            // ".", otherwise it is just the number of JAR files.
+            // If "." is included, then it will be added to the
+            // first place in the path array below.
+            File[] children = embedDir.listFiles();
+            int size = (children == null) ? 0 : children.length;
+            size = (includeDot) ? size + 1 : size;
+            paths = new String[size];
+            for (int i = 0; i < children.length; i++)
+            {
+                // If we are including "." then skip the first slot,
+                // because this is where we will put the bundle JAR file.
+                paths[(includeDot) ? i + 1 : i] = children[i].getPath();
+            }
+        }
+
+        // If there is nothing on the class path, then include
+        // "." by default, as per the spec.
+        if ((paths == null) || (paths.length == 0))
+        {
+            includeDot = true;
+            paths = new String[1];
+        }
+
+        // Put the bundle jar file first, if included.
+        if (includeDot)
+        {
+            paths[0] = revisionDir + File.separator + BUNDLE_JAR_FILE;
+        }
+
+        return paths;
+    }
+
+//  TODO: This will need to consider security.
+    public String findLibrary(int revision, String libName)
+        throws Exception
+    {
+        return findLibraryUnchecked(revision, libName);
+    }
+
+    private String findLibraryUnchecked(int revision, String libName)
+        throws Exception
+    {
+        // Get the revision directory.
+        File revisionDir = new File(
+            m_dir.getAbsoluteFile(),
+            REVISION_DIRECTORY + getRefreshCount() + "." + revision);
+
+        // Get bundle lib directory.
+        File libDir = new File(revisionDir, LIBRARY_DIRECTORY);
+        // Get lib file.
+        File libFile = new File(libDir, File.separatorChar + libName);
+        // Make sure that the library's parent directory exists;
+        // it may be in a sub-directory.
+        libDir = libFile.getParentFile();
+        if (!libDir.exists())
+        {
+            if (!libDir.mkdirs())
+            {
+                throw new IOException("Unable to create library directory.");
+            }
+        }
+        // Extract the library from the JAR file if it does not
+        // already exist.
+        if (!libFile.exists())
+        {
+            JarFile jarFile = null;
+            InputStream is = null;
+
+            try
+            {
+                jarFile = openBundleJarUnchecked(revisionDir);
+                ZipEntry ze = jarFile.getEntry(libName);
+                if (ze == null)
+                {
+                    throw new IOException("No JAR entry: " + libName);
+                }
+                is = new BufferedInputStream(
+                    jarFile.getInputStream(ze), DefaultBundleCache.BUFSIZE);
+                if (is == null)
+                {
+                    throw new IOException("No input stream: " + libName);
+                }
+
+                // Create the file.
+                copy(is, libFile);
+
+            }
+            finally
+            {
+                if (jarFile != null) jarFile.close();
+                if (is != null) is.close();
+            }
+        }
+
+        return libFile.toString();
+    }
+
+    /**
+     * This utility method is used to retrieve the current refresh
+     * counter value for the bundle. This value is used when generating
+     * the bundle JAR directory name where native libraries are extracted.
+     * This is necessary because Sun's JVM requires a one-to-one mapping
+     * between native libraries and class loaders where the native library
+     * is uniquely identified by its absolute path in the file system. This
+     * constraint creates a problem when a bundle is refreshed, because it
+     * gets a new class loader. Using the refresh counter to generate the name
+     * of the bundle JAR directory resolves this problem because each time
+     * bundle is refresh, the native library will have a unique name.
+     * As a result of the unique name, the JVM will then reload the
+     * native library without a problem.
+    **/
+    private long getRefreshCount()
+        throws Exception
+    {
+        // If we have already read the update counter file,
+        // then just return the result.
+        if (m_refreshCount >= 0)
+        {
+            return m_refreshCount;
+        }
+
+        // Get update counter file.
+        File counterFile = new File(m_dir, REFRESH_COUNTER_FILE);
+
+        // If the update counter file doesn't exist, then
+        // assume the counter is at zero.
+        if (!counterFile.exists())
+        {
+            return 0;
+        }
+
+        // Read the bundle update counter.
+        FileReader fr = null;
+        BufferedReader br = null;
+        try
+        {
+            fr = new FileReader(counterFile);
+            br = new BufferedReader(fr);
+            long counter = Long.parseLong(br.readLine());
+            return counter;
+        }
+        finally
+        {
+            if (br != null) br.close();
+            if (fr != null) fr.close();
+        }
+    }
+
+    /**
+     * This utility method is used to retrieve the current refresh
+     * counter value for the bundle. This value is used when generating
+     * the bundle JAR directory name where native libraries are extracted.
+     * This is necessary because Sun's JVM requires a one-to-one mapping
+     * between native libraries and class loaders where the native library
+     * is uniquely identified by its absolute path in the file system. This
+     * constraint creates a problem when a bundle is refreshed, because it
+     * gets a new class loader. Using the refresh counter to generate the name
+     * of the bundle JAR directory resolves this problem because each time
+     * bundle is refresh, the native library will have a unique name.
+     * As a result of the unique name, the JVM will then reload the
+     * native library without a problem.
+    **/
+    private void setRefreshCount(long counter)
+        throws Exception
+    {
+        // Get update counter file.
+        File counterFile = new File(m_dir, REFRESH_COUNTER_FILE);
+
+        // Write the update counter.
+        FileWriter fw = null;
+        BufferedWriter bw = null;
+        try
+        {
+            fw = new FileWriter(counterFile);
+            bw = new BufferedWriter(fw);
+            String s = Long.toString(counter);
+            bw.write(s, 0, s.length());
+            m_refreshCount = counter;
+        }
+        catch (IOException ex)
+        {
+            m_logger.log(
+                LogWrapper.LOG_ERROR,
+                "DefaultBundleArchive: Unable to write counter: " + ex);
+            throw ex;
+        }
+        finally
+        {
+            if (bw != null) bw.close();
+            if (fw != null) fw.close();
+        }
+    }
+
+    //
+    // File-oriented utility methods.
+    //
+
+    protected static boolean deleteDirectoryTree(File target)
+    {
+        if (!target.exists())
+        {
+            return true;
+        }
+
+        if (target.isDirectory())
+        {
+            File[] files = target.listFiles();
+            for (int i = 0; i < files.length; i++)
+            {
+                deleteDirectoryTree(files[i]);
+            }
+        }
+
+        return target.delete();
+    }
+
+    /**
+     * This method copies an input stream to the specified file.
+     * <p>
+     * Security: This method must be called from within a <tt>doPrivileged()</tt>
+     * block since it accesses the disk.
+     * @param is the input stream to copy.
+     * @param outputFile the file to which the input stream should be copied.
+    **/
+    private void copy(InputStream is, File outputFile)
+        throws IOException
+    {
+        OutputStream os = null;
+
+        try
+        {
+            os = new BufferedOutputStream(
+                new FileOutputStream(outputFile), DefaultBundleCache.BUFSIZE);
+            byte[] b = new byte[DefaultBundleCache.BUFSIZE];
+            int len = 0;
+            while ((len = is.read(b)) != -1)
+                os.write(b, 0, len);
+        }
+        finally
+        {
+            if (is != null) is.close();
+            if (os != null) os.close();
+        }
+    }
+
+    /**
+     * This method pre-processes a bundle JAR file making it ready
+     * for use. This entails extracting all embedded JAR files and
+     * all native libraries.
+     * @throws java.lang.Exception if any error occurs while processing JAR file.
+    **/
+    private void preprocessBundleJar(int revision, File revisionDir)
+        throws Exception
+    {
+        //
+        // Create special directories so that we can avoid checking
+        // for their existence all the time.
+        //
+
+        File embedDir = new File(revisionDir, EMBEDDED_DIRECTORY);
+        if (!embedDir.exists())
+        {
+            if (!embedDir.mkdir())
+            {
+                throw new IOException("Could not create embedded JAR directory.");
+            }
+        }
+
+        File libDir = new File(revisionDir, LIBRARY_DIRECTORY);
+        if (!libDir.exists())
+        {
+            if (!libDir.mkdir())
+            {
+                throw new IOException("Unable to create native library directory.");
+            }
+        }
+
+        //
+        // This block extracts all embedded JAR files.
+        //
+
+        try
+        {
+            // Get the bundle's manifest header.
+            Map map = getManifestHeader(revision);
+            if (map == null)
+            {
+                map = new HashMap();
+            }
+
+            // Find class path meta-data.
+            String classPath = null;
+            Iterator iter = map.entrySet().iterator();
+            while ((classPath == null) && iter.hasNext())
+            {
+                Map.Entry entry = (Map.Entry) iter.next();
+                if (entry.getKey().toString().toLowerCase().equals(
+                    FelixConstants.BUNDLE_CLASSPATH.toLowerCase()))
+                {
+                    classPath = entry.getValue().toString();
+                }
+            }
+
+            // Parse the class path into strings.
+            String[] classPathStrings = Util.parseDelimitedString(
+                classPath, FelixConstants.CLASS_PATH_SEPARATOR);
+
+            if (classPathStrings == null)
+            {
+                classPathStrings = new String[0];
+            }
+
+            for (int i = 0; i < classPathStrings.length; i++)
+            {
+                if (!classPathStrings[i].equals(FelixConstants.CLASS_PATH_DOT))
+                {
+                    extractEmbeddedJar(revisionDir, classPathStrings[i]);
+                }
+            }
+
+        }
+        catch (PrivilegedActionException ex)
+        {
+            throw ((PrivilegedActionException) ex).getException();
+        }
+    }
+
+    /**
+     * This method extracts an embedded JAR file from the bundle's
+     * JAR file.
+     * <p>
+     * Security: This method must be called from within a <tt>doPrivileged()</tt>
+     * block since it accesses the disk.
+     * @param id the identifier of the bundle that owns the embedded JAR file.
+     * @param jarPath the path to the embedded JAR file inside the bundle JAR file.
+    **/
+    private void extractEmbeddedJar(File revisionDir, String jarPath)
+        throws Exception
+    {
+        // Remove leading slash if present.
+        jarPath = (jarPath.charAt(0) == '/') ? jarPath.substring(1) : jarPath;
+        // Get only the JAR file name.
+        String jarName = (jarPath.lastIndexOf('/') >= 0)
+            ? jarPath.substring(jarPath.lastIndexOf('/') + 1) : jarPath;
+
+        // If JAR is already extracted, then don't
+        // re-extract it...
+        File embedFile = new File(
+            revisionDir, EMBEDDED_DIRECTORY + File.separatorChar + jarName);
+        if (!embedFile.exists())
+        {
+            JarFile jarFile = null;
+            InputStream is = null;
+
+            try
+            {
+                jarFile = openBundleJarUnchecked(revisionDir);
+                ZipEntry ze = jarFile.getEntry(jarPath);
+                if (ze == null)
+                {
+                    throw new IOException("No JAR entry: " + jarPath);
+                }
+                is = new BufferedInputStream(jarFile.getInputStream(ze), DefaultBundleCache.BUFSIZE);
+                if (is == null)
+                {
+                    throw new IOException("No input stream: " + jarPath);
+                }
+
+                // Create the file.
+                copy(is, embedFile);
+
+            }
+            finally
+            {
+                if (jarFile != null) jarFile.close();
+                if (is != null) is.close();
+            }
+        }
+    }
+
+    // INCREASES THE REVISION COUNT.    
+    protected void update(InputStream is) throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.UPDATE_ACTION, this, is));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            updateUnchecked(is);
+        }
+    }
+
+    // INCREASES THE REVISION COUNT.    
+    private void updateUnchecked(InputStream is) throws Exception
+    {
+        File revisionDir = null;
+
+        try
+        {
+            // Create the new revision directory.
+            int revision = getRevisionCountUnchecked();
+            revisionDir = new File(
+                m_dir, REVISION_DIRECTORY
+                + getRefreshCount() + "." + revision);
+            if (!revisionDir.mkdir())
+            {
+                throw new IOException("Unable to create revision directory.");
+            }
+
+            // Save the new revision bundle jar file.
+            File file = new File(revisionDir, BUNDLE_JAR_FILE);
+            copy(is, file);
+
+            preprocessBundleJar(revision, revisionDir);
+        }
+        catch (Exception ex)
+        {
+            if ((revisionDir != null) && revisionDir.exists())
+            {
+                try
+                {
+                    deleteDirectoryTree(revisionDir);
+                }
+                catch (Exception ex2)
+                {
+                    // There is very little we can do here.
+                    m_logger.log(
+                        LogWrapper.LOG_ERROR,
+                        "Unable to remove partial revision directory.", ex2);
+                }
+            }
+            throw ex;
+        }
+
+        // If everything was successful, then update
+        // the revision count.
+        m_revisionCount++;
+        // Clear the cached revision header, since it is
+        // no longer the current revision.
+        m_currentHeader = null;
+    }
+
+    // DECREASES THE REVISION COUNT.    
+    protected void purge() throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.PURGE_ACTION, this));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            purgeUnchecked();
+        }
+    }
+    
+    // DECREASES THE REVISION COUNT.    
+    private void purgeUnchecked() throws Exception
+    {
+        // Get the current update count.
+        long update = getRefreshCount();
+        // Get the current revision count.
+        int count = getRevisionCountUnchecked();
+
+        File revisionDir = null;
+        for (int i = 0; i < count - 1; i++)
+        {
+            revisionDir = new File(m_dir, REVISION_DIRECTORY + update + "." + i);
+            if (revisionDir.exists())
+            {
+                deleteDirectoryTree(revisionDir);
+            }
+        }
+        // Increment the update count.
+        setRefreshCount(update + 1);
+
+        // Rename the current revision to be the current update.
+        File currentDir = new File(m_dir, REVISION_DIRECTORY + (update + 1) + ".0");
+        revisionDir = new File(m_dir, REVISION_DIRECTORY + update + "." + (count - 1));
+        revisionDir.renameTo(currentDir);
+        
+        // If everything is successful, then set the revision
+        // count to one.
+        m_revisionCount = 1;
+        // Although the cached current header should stay the same
+        // here, clear it for consistency.
+        m_currentHeader = null;
+    }
+
+    protected void remove() throws Exception
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                AccessController.doPrivileged(
+                    new PrivilegedAction(
+                        PrivilegedAction.REMOVE_ACTION, this));
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw ((PrivilegedActionException) ex).getException();
+            }
+        }
+        else
+        {
+            removeUnchecked();
+        }
+    }
+    
+    private void removeUnchecked() throws Exception
+    {
+        deleteDirectoryTree(m_dir);
+    }
+
+    //
+    // Utility class for performing privileged actions.
+    //
+
+    private static class PrivilegedAction implements PrivilegedExceptionAction
+    {
+        private static final int INITIALIZE_ACTION = 0;
+        private static final int UPDATE_ACTION = 1;
+        private static final int PURGE_ACTION = 2;
+        private static final int REMOVE_ACTION = 3;
+        private static final int GET_REVISION_COUNT_ACTION = 4;
+        private static final int GET_LOCATION_ACTION = 5;
+        private static final int GET_PERSISTENT_STATE_ACTION = 6;
+        private static final int SET_PERSISTENT_STATE_ACTION = 7;
+        private static final int GET_START_LEVEL_ACTION = 8;
+        private static final int SET_START_LEVEL_ACTION = 9;
+        private static final int OPEN_BUNDLE_JAR_ACTION = 10;
+        private static final int CREATE_DATA_DIR_ACTION = 11;
+        private static final int GET_CLASS_PATH_ACTION = 12;
+        private static final int GET_ACTIVATOR_ACTION = 13;
+        private static final int SET_ACTIVATOR_ACTION = 14;
+
+        private int m_action = 0;
+        private DefaultBundleArchive m_archive = null;
+        private InputStream m_isArg = null;
+        private int m_intArg = 0;
+        private File m_fileArg = null;
+        private ClassLoader m_loaderArg = null;
+        private Object m_objArg = null;
+
+        public PrivilegedAction(int action, DefaultBundleArchive archive)
+        {
+            m_action = action;
+            m_archive = archive;
+        }
+
+        public PrivilegedAction(int action, DefaultBundleArchive archive, InputStream isArg)
+        {
+            m_action = action;
+            m_archive = archive;
+            m_isArg = isArg;
+        }
+
+        public PrivilegedAction(int action, DefaultBundleArchive archive, int intArg)
+        {
+            m_action = action;
+            m_archive = archive;
+            m_intArg = intArg;
+        }
+
+        public PrivilegedAction(int action, DefaultBundleArchive archive, File fileArg)
+        {
+            m_action = action;
+            m_archive = archive;
+            m_fileArg = fileArg;
+        }
+
+        public PrivilegedAction(int action, DefaultBundleArchive archive, ClassLoader loaderArg)
+        {
+            m_action = action;
+            m_archive = archive;
+            m_loaderArg = loaderArg;
+        }
+
+        public PrivilegedAction(int action, DefaultBundleArchive archive, Object objArg)
+        {
+            m_action = action;
+            m_archive = archive;
+            m_objArg = objArg;
+        }
+
+        public Object run() throws Exception
+        {
+            switch (m_action)
+            {
+                case INITIALIZE_ACTION:
+                    m_archive.initializeUnchecked(m_isArg);
+                    return null;
+                case UPDATE_ACTION:
+                    m_archive.updateUnchecked(m_isArg);
+                    return null;
+                case PURGE_ACTION:
+                    m_archive.purgeUnchecked();
+                    return null;
+                case REMOVE_ACTION:
+                    m_archive.removeUnchecked();
+                    return null;
+                case GET_REVISION_COUNT_ACTION:
+                    return new Integer(m_archive.getRevisionCountUnchecked());
+                case GET_LOCATION_ACTION:
+                    return m_archive.getLocationUnchecked();
+                case GET_PERSISTENT_STATE_ACTION:
+                    return new Integer(m_archive.getPersistentStateUnchecked());
+                case SET_PERSISTENT_STATE_ACTION:
+                    m_archive.setPersistentStateUnchecked(m_intArg);
+                    return null;
+                case GET_START_LEVEL_ACTION:
+                    return new Integer(m_archive.getStartLevelUnchecked());
+                case SET_START_LEVEL_ACTION:
+                    m_archive.setStartLevelUnchecked(m_intArg);
+                    return null;
+                case OPEN_BUNDLE_JAR_ACTION:
+                    return m_archive.openBundleJarUnchecked(m_fileArg);
+                case CREATE_DATA_DIR_ACTION:
+                    m_archive.createDataDirectoryUnchecked(m_fileArg);
+                    return null;
+                case GET_CLASS_PATH_ACTION:
+                    return m_archive.getClassPathUnchecked(m_intArg);
+                case GET_ACTIVATOR_ACTION:
+                    return m_archive.getActivatorUnchecked(m_loaderArg);
+                case SET_ACTIVATOR_ACTION:
+                    m_archive.setActivatorUnchecked(m_objArg);
+                    return null;
+            }
+
+            throw new IllegalArgumentException("Invalid action specified.");
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/cache/DefaultBundleCache.java b/src/org/apache/osgi/framework/cache/DefaultBundleCache.java
new file mode 100644
index 0000000..eb6b0b7
--- /dev/null
+++ b/src/org/apache/osgi/framework/cache/DefaultBundleCache.java
@@ -0,0 +1,262 @@
+/*
+ *   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.osgi.framework.cache;
+
+import java.io.*;
+
+import org.apache.osgi.framework.LogWrapper;
+import org.apache.osgi.framework.util.PropertyResolver;
+
+/**
+ * <p>
+ * This class, combined with <tt>DefaultBundleArchive</tt>, implements the
+ * default file system-based bundle cache for Felix. It is possible to
+ * configure the default behavior of this class by passing properties into
+ * Felix constructor. The configuration properties for this class are:
+ * </p>
+ * <ul>
+ *   <li><tt>felix.cache.bufsize</tt> - Sets the buffer size to be used by
+ *       the cache; the default value is 4096. The integer
+ *       value of this string provides control over the size of the
+ *       internal buffer of the disk cache for performance reasons.
+ *   </li>
+ *   <li><tt>felix.cache.dir</tt> - Sets the directory to be used by the
+ *       cache as its cache directory. The cache directory is where all
+ *       profile directories are stored and a profile directory is where a
+ *       set of installed bundles are stored. By default, the cache
+ *       directory is <tt>.felix</tt> in the user's home directory. If
+ *       this property is specified, then its value will be used as the cache
+ *       directory instead of <tt>.felix</tt>. This directory will be created
+ *       if it does not exist.
+ *   </li>
+ *   <li><tt>felix.cache.profile</tt> - Sets the profile name that will be
+ *       used to create a profile directory inside of the cache directory.
+ *       The created directory will contained all installed bundles associated
+ *       with the profile.
+ *   </li>
+ *   <li><tt>felix.cache.profiledir</tt> - Sets the directory to use as the
+ *       profile directory for the bundle cache; by default the profile
+ *       name is used to create a directory in the <tt>.felix</tt> cache
+ *       directory. If this property is specified, then the cache directory
+ *       and profile name properties are ignored. The specified value of this
+ *       property is used directly as the directory to contain all cached
+ *       bundles. If this property is set, it is not necessary to set the
+ *       cache directory or profile name properties. This directory will be
+ *       created if it does not exist.
+ *   </li>
+ * </ul>
+ * <p>
+ * For specific information on how to configure Felix using system properties,
+ * refer to the Felix usage documentation.
+ * </p>
+ * @see org.apache.osgi.framework.util.DefaultBundleArchive
+**/
+public class DefaultBundleCache implements BundleCache
+{
+    public static final String CACHE_BUFSIZE_PROP = "felix.cache.bufsize";
+    public static final String CACHE_DIR_PROP = "felix.cache.dir";
+    public static final String CACHE_PROFILE_DIR_PROP = "felix.cache.profiledir";
+    public static final String CACHE_PROFILE_PROP = "felix.cache.profile";
+
+    protected static transient int BUFSIZE = 4096;
+    protected static transient final String CACHE_DIR_NAME = ".felix";
+    protected static transient final String BUNDLE_DIR_PREFIX = "bundle";
+
+    private PropertyResolver m_cfg = null;
+    private LogWrapper m_logger = null;
+    private File m_profileDir = null;
+    private BundleArchive[] m_archives = null;
+
+    public DefaultBundleCache()
+    {
+    }
+
+    public void initialize(PropertyResolver cfg, LogWrapper logger) throws Exception
+    {
+        // Save Properties reference.
+        m_cfg = cfg;
+        // Save LogService reference.
+        m_logger = logger;
+
+        // Get buffer size value.
+        try
+        {
+            String sBufSize = m_cfg.get(CACHE_BUFSIZE_PROP);
+            if (sBufSize != null)
+            {
+                BUFSIZE = Integer.parseInt(sBufSize);
+            }
+        }
+        catch (NumberFormatException ne)
+        {
+            // Use the default value.
+        }
+
+        // See if the profile directory is specified.
+        String profileDirStr = m_cfg.get(CACHE_PROFILE_DIR_PROP);
+        if (profileDirStr != null)
+        {
+            m_profileDir = new File(profileDirStr);
+        }
+        else
+        {
+            // Since no profile directory was specified, then the profile
+            // directory will be a directory in the cache directory named
+            // after the profile.
+
+            // First, determine the location of the cache directory; it
+            // can either be specified or in the default location.
+            String cacheDirStr = m_cfg.get(CACHE_DIR_PROP);
+            if (cacheDirStr == null)
+            {
+                // Since no cache directory was specified, put it
+                // ".felix" in the user's home by default.
+                cacheDirStr = System.getProperty("user.home");
+                cacheDirStr = cacheDirStr.endsWith(File.separator)
+                    ? cacheDirStr : cacheDirStr + File.separator;
+                cacheDirStr = cacheDirStr + CACHE_DIR_NAME;
+            }
+
+            // Now, get the profile name.
+            String profileName = m_cfg.get(CACHE_PROFILE_PROP);
+            if (profileName == null)
+            {
+                throw new IllegalArgumentException(
+                    "No profile name or directory has been specified.");
+            }
+            // Profile name cannot contain the File.separator char.
+            else if (profileName.indexOf(File.separator) >= 0)
+            {
+                throw new IllegalArgumentException(
+                    "The profile name cannot contain the file separator character.");
+            }
+
+            m_profileDir = new File(cacheDirStr, profileName);
+        }
+
+        // Create profile directory.
+        if (!m_profileDir.exists())
+        {
+            if (!m_profileDir.mkdirs())
+            {
+                m_logger.log(
+                    LogWrapper.LOG_ERROR,
+                    "Unable to create directory: " + m_profileDir);
+                throw new RuntimeException("Unable to create profile directory.");
+            }
+        }
+
+        // Create the existing bundle archives in the profile directory,
+        // if any exist.
+        File[] children = m_profileDir.listFiles();
+        int count = 0;
+        for (int i = 0; (children != null) && (i < children.length); i++)
+        {
+            // Count the legitimate bundle directories.
+            if (children[i].getName().startsWith(BUNDLE_DIR_PREFIX))
+            {
+                count++;
+            }
+        }
+        m_archives = new BundleArchive[count];
+        count = 0;
+        for (int i = 0; (children != null) && (i < children.length); i++)
+        {
+            // Ignore directories that aren't bundle directories.
+            if (children[i].getName().startsWith(BUNDLE_DIR_PREFIX))
+            {
+                String id = children[i].getName().substring(BUNDLE_DIR_PREFIX.length());
+                m_archives[count] = new DefaultBundleArchive(
+                    m_logger, children[i], Long.parseLong(id));
+                count++;
+            }
+        }
+    }
+
+    public BundleArchive[] getArchives()
+        throws Exception
+    {
+        return m_archives;
+    }
+
+    public BundleArchive getArchive(long id)
+        throws Exception
+    {
+        for (int i = 0; i < m_archives.length; i++)
+        {
+            if (m_archives[i].getId() == id)
+            {
+                return m_archives[i];
+            }
+        }
+        return null;
+    }
+
+    public BundleArchive create(long id, String location, InputStream is)
+        throws Exception
+    {
+        // Define new bundle's directory.
+        File bundleDir = new File(m_profileDir, "bundle" + id);
+
+        try
+        {
+            // Buffer the input stream.
+            is = new BufferedInputStream(is, DefaultBundleCache.BUFSIZE);
+            // Create an archive instance for the new bundle.
+            BundleArchive ba = new DefaultBundleArchive(
+                m_logger, bundleDir, id, location, is);
+            // Add the archive instance to the list of bundle archives.
+            BundleArchive[] bas = new BundleArchive[m_archives.length + 1];
+            System.arraycopy(m_archives, 0, bas, 0, m_archives.length);
+            bas[m_archives.length] = ba;
+            m_archives = bas;
+            return ba;
+        }
+        finally
+        {
+            if (is != null) is.close();
+        }
+    }
+
+    public void update(BundleArchive ba, InputStream is)
+        throws Exception
+    {
+        try
+        {
+            // Buffer the input stream.
+            is = new BufferedInputStream(is, DefaultBundleCache.BUFSIZE);
+            // Do the update.
+            ((DefaultBundleArchive) ba).update(is);
+        }
+        finally
+        {
+            if (is != null) is.close();
+        }
+    }
+
+    public void purge(BundleArchive ba)
+        throws Exception
+    {
+        ((DefaultBundleArchive) ba).purge();
+    }
+
+    public void remove(BundleArchive ba)
+        throws Exception
+    {
+        ((DefaultBundleArchive) ba).remove();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/ext/FelixBundleContext.java b/src/org/apache/osgi/framework/ext/FelixBundleContext.java
new file mode 100644
index 0000000..e1d6cc9
--- /dev/null
+++ b/src/org/apache/osgi/framework/ext/FelixBundleContext.java
@@ -0,0 +1,28 @@
+/*
+ *   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.osgi.framework.ext;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+
+public interface FelixBundleContext extends BundleContext
+{
+    public void addImportPackage() throws BundleException;
+    public void removeImportPackage() throws BundleException;
+    public void addExportPackage() throws BundleException;
+    public void removeExportPackage() throws BundleException;
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/Artifact.java b/src/org/apache/osgi/framework/installer/Artifact.java
new file mode 100644
index 0000000..42f8227
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/Artifact.java
@@ -0,0 +1,30 @@
+/*
+ *   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.osgi.framework.installer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+
+public interface Artifact
+{
+    public StringProperty getSourceName();
+    public StringProperty getDestinationDirectory();
+    public InputStream getInputStream(Status status) throws IOException;
+    public boolean localize();
+    public boolean process(Status status, Map propMap);
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/BooleanProperty.java b/src/org/apache/osgi/framework/installer/BooleanProperty.java
new file mode 100644
index 0000000..f470bce
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/BooleanProperty.java
@@ -0,0 +1,23 @@
+/*
+ *   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.osgi.framework.installer;
+
+public interface BooleanProperty extends Property
+{
+    public boolean getBooleanValue();
+    public void setBooleanValue(boolean b);
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/Install.java b/src/org/apache/osgi/framework/installer/Install.java
new file mode 100644
index 0000000..7e6eae6
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/Install.java
@@ -0,0 +1,431 @@
+/*
+ *   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.osgi.framework.installer;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.io.File;
+import java.util.*;
+
+import javax.swing.*;
+import javax.swing.border.BevelBorder;
+
+import org.apache.osgi.framework.installer.artifact.*;
+import org.apache.osgi.framework.installer.editor.BooleanEditor;
+import org.apache.osgi.framework.installer.editor.FileEditor;
+import org.apache.osgi.framework.installer.property.*;
+import org.apache.osgi.framework.util.FelixConstants;
+
+public class Install extends JFrame
+{
+    private static transient final String PROPERTY_FILE = "property.xml";
+    private static transient final String ARTIFACT_FILE = "artifact.xml";
+
+    public static transient final String JAVA_DIR = "Java directory";
+    public static transient final String INSTALL_DIR = "Install directory";
+
+    private PropertyPanel m_propPanel = null;
+    private JButton m_okayButton = null;
+    private JButton m_cancelButton = null;
+    private JLabel m_statusLabel = null;
+
+    private java.util.List m_propList = null;
+    private java.util.List m_artifactList = null;
+
+    public Install()
+        throws Exception
+    {
+        super("Install");
+
+        // Load properties before resources, because resources
+        // refer to properties.
+        m_propList = loadPropertyList();
+        m_artifactList = loadArtifactList();
+
+        getContentPane().setLayout(new BorderLayout());
+        getContentPane().add(
+            m_propPanel = new PropertyPanel(m_propList), BorderLayout.CENTER);
+        getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
+        pack();
+        setResizable(true);
+        centerWindow(this);
+
+        // Make window closeable.
+        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
+        addWindowListener(new WindowAdapter() {
+            public void windowClosing(WindowEvent event)
+            {
+                doCancel();
+            }
+        });
+    }
+
+    public java.util.List loadPropertyList()
+    {
+        String installDir = System.getProperty("user.home");
+        if (!installDir.endsWith(File.separator))
+        {
+            installDir = installDir + File.separator;
+        }
+
+        Property prop = null;
+
+        // Eventually these should be read from a file.
+        java.util.List list = new ArrayList();
+
+        // Add the shell choice property.
+        prop = new BooleanPropertyImpl("Shell", true);
+        prop.setEditor(new BooleanEditor((BooleanProperty) prop, "Text", "GUI"));
+        list.add(prop);
+
+        // Add the java directory property.
+        prop = new StringPropertyImpl(JAVA_DIR, System.getProperty("java.home"));
+        prop.setEditor(new FileEditor((StringProperty) prop, true));
+        list.add(prop);
+
+        // Add the installation directory property.
+        prop = new StringPropertyImpl(INSTALL_DIR, installDir + "Felix");
+        prop.setEditor(new FileEditor((StringProperty) prop, true));
+        list.add(prop);
+
+        // Add the documentation URL property.
+        prop = new BooleanStringPropertyImpl(
+            "User documentation",
+            true,
+            "http://download.forge.objectweb.org/oscar/oscar-doc-"
+            + FelixConstants.FELIX_VERSION_VALUE + ".jar");
+        list.add(prop);
+
+        // Add the documentation URL property.
+        prop = new BooleanStringPropertyImpl(
+            "API documentation",
+            true,
+            "http://download.forge.objectweb.org/oscar/oscar-api-"
+            + FelixConstants.FELIX_VERSION_VALUE + ".jar");
+        list.add(prop);
+
+        return list;
+    }
+
+    public java.util.List loadArtifactList() throws Exception
+    {
+        // Eventually I will changed these to be read from a file.
+        java.util.List list = new ArrayList();
+        list.add(
+            new ArtifactHolder(
+                (BooleanProperty) getProperty("User documentation"),
+                new URLJarArtifact(
+                    (StringProperty) getProperty("User documentation"))));
+        list.add(
+            new ArtifactHolder(
+                (BooleanProperty) getProperty("API documentation"),
+                new URLJarArtifact(
+                    (StringProperty) getProperty("API documentation"))));
+        list.add(
+            new ArtifactHolder(
+                new ResourceJarArtifact(
+                    new StringPropertyImpl("sourceName", "package.jar"))));
+        list.add(
+            new ArtifactHolder(
+                new ResourceFileArtifact(
+                    new StringPropertyImpl("sourceName", "src.jar"))));
+        list.add(
+            new ArtifactHolder(
+                new ResourceFileArtifact(
+                    new StringPropertyImpl("sourceName", "LICENSE.txt"))));
+        list.add(
+            new ArtifactHolder(
+                (BooleanProperty) getProperty("Shell"),
+                new ResourceFileArtifact(
+                    new StringPropertyImpl("sourceName", "config.properties.text"),
+                    new StringPropertyImpl("destName", "config.properties"),
+                    new StringPropertyImpl("destDir", "lib"))));
+        list.add(
+            new ArtifactHolder(
+                new NotBooleanPropertyImpl((BooleanProperty) getProperty("Shell")),
+                new ResourceFileArtifact(
+                    new StringPropertyImpl("sourceName", "config.properties.gui"),
+                    new StringPropertyImpl("destName", "config.properties"),
+                    new StringPropertyImpl("destDir", "lib"))));
+        list.add(
+            new ArtifactHolder(
+                new ResourceFileArtifact(
+                    new StringPropertyImpl("sourceName", "example.policy"))));
+        list.add(
+            new ArtifactHolder(
+                new ResourceFileArtifact(
+                    new StringPropertyImpl("sourceName", "felix.bat"),
+                    new StringPropertyImpl("destName" , "felix.bat"),
+                    new StringPropertyImpl("destDir", ""),
+                    true)));
+        list.add(
+            new ArtifactHolder(
+                new ResourceFileArtifact(
+                    new StringPropertyImpl("sourceName", "felix.sh"),
+                    new StringPropertyImpl("destName" , "felix.sh"),
+                    new StringPropertyImpl("destDir", ""),
+                    true)));
+
+        return list;
+    }
+
+    private Property getProperty(String name)
+    {
+        for (int i = 0; i < m_propList.size(); i++)
+        {
+            Property prop = (Property) m_propList.get(i);
+            if (prop.getName().equals(name))
+            {
+                return prop;
+            }
+        }
+        return null;
+    }
+
+    protected void doOkay()
+    {
+        m_propPanel.setEnabled(false);
+        m_okayButton.setEnabled(false);
+        m_cancelButton.setEnabled(false);
+        new Thread(new InstallRunnable()).start();
+    }
+
+    protected void doCancel()
+    {
+        System.exit(0);
+    }
+
+    protected JPanel createButtonPanel()
+    {
+        JPanel buttonPanel = new JPanel();
+        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
+
+        // Create and set layout.
+        GridBagLayout grid = new GridBagLayout();
+        GridBagConstraints c = new GridBagConstraints();
+
+        buttonPanel.setLayout(grid);
+
+        // Create labels and fields.
+        c.insets = new Insets(2, 2, 2, 2);
+
+        // Okay button.
+        c.gridx = 0;
+        c.gridy = 0;
+        c.gridwidth = 1;
+        c.gridheight = 1;
+        c.anchor = GridBagConstraints.EAST;
+        grid.setConstraints(m_okayButton = new JButton("OK"), c);
+        buttonPanel.add(m_okayButton);
+        m_okayButton.setDefaultCapable(true);
+        getRootPane().setDefaultButton(m_okayButton);
+
+        // Cancel button.
+        c.gridx = 1;
+        c.gridy = 0;
+        c.gridwidth = 1;
+        c.gridheight = 1;
+        c.anchor = GridBagConstraints.WEST;
+        grid.setConstraints(m_cancelButton = new JButton("Cancel"), c);
+        buttonPanel.add(m_cancelButton);
+
+        // Add action listeners.
+        m_okayButton.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent event)
+            {
+                doOkay();
+            }
+        });
+
+        m_cancelButton.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent event)
+            {
+                doCancel();
+            }
+        });
+
+        // Status label.
+        m_statusLabel = new JLabel("Felix installation");
+        m_statusLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
+
+        // Complete panel.
+        JPanel panel = new JPanel(new BorderLayout());
+        panel.add(buttonPanel, BorderLayout.CENTER);
+        panel.add(m_statusLabel, BorderLayout.SOUTH);
+        return panel;
+    }
+
+    public static void centerWindow(Component window)
+    {
+        Toolkit toolkit = Toolkit.getDefaultToolkit();
+        Dimension dim = toolkit.getScreenSize();
+        int screenWidth = dim.width;
+        int screenHeight = dim.height;
+        int x = (screenWidth - window.getSize().width) / 2;
+        int y = (screenHeight - window.getSize().height) / 2;
+        window.setLocation(x, y);
+    }
+
+    public static void main(String[] argv) throws Exception
+    {
+        String msg = "<html>"
+            + "<center><h1>Felix " + FelixConstants.FELIX_VERSION_VALUE + "</h1></center>"
+            + "You can download example bundles at the Felix shell prompt by<br>"
+            + "using the <b><tt>obr</tt></b> command to access the OSGi Bundle Repository;<br>"
+            + "type <b><tt>obr help</tt></b> at the Felix shell prompt for details."
+            + "</html>";
+        JLabel label = new JLabel(msg);
+        label.setFont(new Font("SansSerif", Font.PLAIN, 11));
+        final JDialog dlg = new JDialog((Frame) null, "Felix Install", true);
+        dlg.getContentPane().setLayout(new BorderLayout(10, 10));
+        dlg.getContentPane().add(label, BorderLayout.CENTER);
+        JPanel panel = new JPanel();
+        JButton button = new JButton("OK");
+        button.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent event)
+            {
+                dlg.hide();
+            }
+        });
+        panel.add(button);
+        dlg.getContentPane().add(panel, BorderLayout.SOUTH);
+        // For spacing purposes...
+        dlg.getContentPane().add(new JPanel(), BorderLayout.NORTH);
+        dlg.getContentPane().add(new JPanel(), BorderLayout.EAST);
+        dlg.getContentPane().add(new JPanel(), BorderLayout.WEST);
+        dlg.pack();
+        centerWindow(dlg);
+        dlg.show();
+
+        Install obj = new Install();
+        obj.setVisible(true);
+    }
+
+    class InstallRunnable implements Runnable
+    {
+        public void run()
+        {
+            Map propMap = new HashMap();
+            for (int i = 0; i < m_propList.size(); i++)
+            {
+                Property prop = (Property) m_propList.get(i);
+                propMap.put(prop.getName(), prop);
+            }
+
+            String installDir = ((StringProperty) propMap.get(INSTALL_DIR)).getStringValue();
+
+            // Make sure the install directory ends with separator char.
+            if (!installDir.endsWith(File.separator))
+            {
+                installDir = installDir + File.separator;
+            }
+
+            // Make sure the install directory exists and
+            // that is actually a directory.
+            File file = new File(installDir);
+            if (!file.exists())
+            {
+                if (!file.mkdirs())
+                {
+                    JOptionPane.showMessageDialog(Install.this,
+                        "Unable to create install directory.",
+                        "Error", JOptionPane.ERROR_MESSAGE);
+                    System.exit(-1);
+                }
+            }
+            else if (!file.isDirectory())
+            {
+                JOptionPane.showMessageDialog(Install.this,
+                    "The selected install location is not a directory.",
+                    "Error", JOptionPane.ERROR_MESSAGE);
+                System.exit(-1);
+            }
+
+            // Status updater runnable.
+            StatusRunnable sr = new StatusRunnable();
+
+            // Loop through and process resources.
+            for (int i = 0; i < m_artifactList.size(); i++)
+            {
+                ArtifactHolder ah = (ArtifactHolder) m_artifactList.get(i);
+                if (ah.isIncluded())
+                {
+                    if (!ah.getArtifact().process(sr, propMap))
+                    {
+                        JOptionPane.showMessageDialog(Install.this,
+                            "An error occurred while processing the resources.",
+                            "Error", JOptionPane.ERROR_MESSAGE);
+                        System.exit(-1);
+                    }
+                }
+            }
+
+            System.exit(0);
+        }
+    }
+
+    class StatusRunnable implements Status, Runnable
+    {
+        private String text = null;
+
+        public void setText(String s)
+        {
+            text = s;
+            try {
+                SwingUtilities.invokeAndWait(this);
+            } catch (Exception ex) {
+                // Ignore.
+            }
+        }
+
+        public void run()
+        {
+            m_statusLabel.setText(text);
+        }
+    }
+
+    // Re-usable static member for ResourceHolder inner class.
+    private static BooleanProperty m_trueProp =
+        new BooleanPropertyImpl("mandatory", true);
+
+    class ArtifactHolder
+    {
+        private BooleanProperty m_isIncluded = null;
+        private Artifact m_artifact = null;
+        
+        public ArtifactHolder(Artifact artifact)
+        {
+            this(m_trueProp, artifact);
+        }
+        
+        public ArtifactHolder(BooleanProperty isIncluded, Artifact artifact)
+        {
+            m_isIncluded = isIncluded;
+            m_artifact = artifact;
+        }
+        
+        public boolean isIncluded()
+        {
+            return m_isIncluded.getBooleanValue();
+        }
+        
+        public Artifact getArtifact()
+        {
+            return m_artifact;
+        }
+    }
+}
diff --git a/src/org/apache/osgi/framework/installer/Property.java b/src/org/apache/osgi/framework/installer/Property.java
new file mode 100644
index 0000000..7c52c46
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/Property.java
@@ -0,0 +1,27 @@
+/*
+ *   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.osgi.framework.installer;
+
+import javax.swing.JComponent;
+
+public interface Property
+{
+    public String getName();
+    public JComponent getEditor();
+    public void setEditor(JComponent comp);
+    public String toString();
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/PropertyPanel.java b/src/org/apache/osgi/framework/installer/PropertyPanel.java
new file mode 100644
index 0000000..46dd4a1
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/PropertyPanel.java
@@ -0,0 +1,87 @@
+/*
+ *   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.osgi.framework.installer;
+
+import java.awt.*;
+import java.util.*;
+import java.util.List;
+
+import javax.swing.*;
+
+public class PropertyPanel extends JPanel
+{
+    private List m_propList = null;
+    private Map m_propToCompMap = null;
+
+    public PropertyPanel(List paramList)
+    {
+        super();
+        m_propList = paramList;
+        m_propToCompMap = new HashMap();
+        layoutComponents();
+    }
+
+    public void setEnabled(boolean b)
+    {
+        for (int i = 0; i < m_propList.size(); i++)
+        {
+            Property prop = (Property) m_propList.get(i);
+            JComponent comp = (JComponent) m_propToCompMap.get(prop.getName());
+            comp.setEnabled(b);
+        }
+    }
+
+    public List getProperties()
+    {
+        return m_propList;
+    }
+
+    protected void layoutComponents()
+    {
+        // Create the field panel for entering query variables.
+        GridBagLayout grid = new GridBagLayout();
+        GridBagConstraints gbc = new GridBagConstraints();
+        gbc.insets = new Insets(2, 2, 2, 2);
+        setLayout(grid);
+
+        for (int i = 0; i < m_propList.size(); i++)
+        {
+            Property prop = (Property) m_propList.get(i);
+            JLabel label = null;
+            JComponent component = null;
+
+            // Add label.
+            gbc.gridx = 0;
+            gbc.gridy = i;
+            gbc.gridheight = 1;
+            gbc.gridwidth = 1;
+            gbc.anchor = GridBagConstraints.EAST;
+            grid.setConstraints(label = new JLabel(prop.getName()), gbc);
+            add(label);
+
+            gbc.gridx = 1;
+            gbc.gridy = i;
+            gbc.gridheight = 1;
+            gbc.gridwidth = 3;
+            gbc.anchor = GridBagConstraints.WEST;
+            grid.setConstraints(component = prop.getEditor(), gbc);
+            add(component);
+
+            m_propToCompMap.put(prop.getName(), component);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/Status.java b/src/org/apache/osgi/framework/installer/Status.java
new file mode 100644
index 0000000..7070422
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/Status.java
@@ -0,0 +1,22 @@
+/*
+ *   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.osgi.framework.installer;
+
+public interface Status
+{
+    public void setText(String s);
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/StringProperty.java b/src/org/apache/osgi/framework/installer/StringProperty.java
new file mode 100644
index 0000000..2597d47
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/StringProperty.java
@@ -0,0 +1,23 @@
+/*
+ *   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.osgi.framework.installer;
+
+public interface StringProperty extends Property
+{
+    public String getStringValue();
+    public void setStringValue(String s);
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/artifact/AbstractArtifact.java b/src/org/apache/osgi/framework/installer/artifact/AbstractArtifact.java
new file mode 100644
index 0000000..c3ee33b
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/artifact/AbstractArtifact.java
@@ -0,0 +1,230 @@
+/*
+ *   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.osgi.framework.installer.artifact;
+
+import java.io.*;
+import java.util.Map;
+
+import org.apache.osgi.framework.installer.*;
+import org.apache.osgi.framework.installer.property.StringPropertyImpl;
+
+public abstract class AbstractArtifact implements Artifact
+{
+    private StringProperty m_sourceName = null;
+    private StringProperty m_destDir = null;
+    private boolean m_localize = false;
+
+    // This following shared buffer assumes that there is
+    // no concurrency when processing resources.
+    protected static byte[] s_buffer = new byte[2048];
+
+    public AbstractArtifact(
+        StringProperty sourceName, StringProperty destDir, boolean localize)
+    {
+        if (destDir == null)
+        {
+            destDir = new StringPropertyImpl("empty", "");
+        }
+        m_sourceName = sourceName;
+        m_destDir = destDir;
+        m_localize = localize;
+    }
+
+    public StringProperty getSourceName()
+    {
+        return m_sourceName;
+    }
+
+    public StringProperty getDestinationDirectory()
+    {
+        return m_destDir;
+    }
+
+    public boolean localize()
+    {
+        return m_localize;
+    }
+
+    protected static void copy(
+        InputStream is, String installDir, String destName, String destDir)
+        throws IOException
+    {
+        if (destDir == null)
+        {
+            destDir = "";
+        }
+
+        // Make sure the target directory exists and
+        // that is actually a directory.
+        File targetDir = new File(installDir, destDir);
+        if (!targetDir.exists())
+        {
+            if (!targetDir.mkdirs())
+            {
+                throw new IOException("Unable to create target directory: "
+                    + targetDir);
+            }
+        }
+        else if (!targetDir.isDirectory())
+        {
+            throw new IOException("Target is not a directory: "
+                + targetDir);
+        }
+
+        BufferedOutputStream bos = new BufferedOutputStream(
+            new FileOutputStream(new File(targetDir, destName)));
+        int count = 0;
+        while ((count = is.read(s_buffer)) > 0)
+        {
+            bos.write(s_buffer, 0, count);
+        }
+        bos.close();
+    }
+
+    protected static void copyAndLocalize(
+        InputStream is, String installDir, String destName,
+        String destDir, Map propMap)
+        throws IOException
+    {
+        if (destDir == null)
+        {
+            destDir = "";
+        }
+
+        // Make sure the target directory exists and
+        // that is actually a directory.
+        File targetDir = new File(installDir, destDir);
+        if (!targetDir.exists())
+        {
+            if (!targetDir.mkdirs())
+            {
+                throw new IOException("Unable to create target directory: "
+                    + targetDir);
+            }
+        }
+        else if (!targetDir.isDirectory())
+        {
+            throw new IOException("Target is not a directory: "
+                + targetDir);
+        }
+
+        BufferedOutputStream bos = new BufferedOutputStream(
+            new FileOutputStream(new File(targetDir, destName)));
+        int i = 0;
+        while ((i = is.read()) > 0)
+        {
+            // Parameters start with "%%", so check to see if
+            // we have a parameter.
+            if ((char)i == '%')
+            {
+                // One of three possibilities, we have a parameter,
+                // we have an end of file, or we don't have a parameter.
+                int i2 = is.read();
+                if ((char) i2 == '%')
+                {
+                    Object obj = readParameter(is);
+
+                    // If the byte sequence was not a parameter afterall,
+                    // then a byte array is returned, otherwise a string
+                    // containing the parameter m_name is returned.
+                    if (obj instanceof byte[])
+                    {
+                        bos.write(i);
+                        bos.write(i2);
+                        bos.write((byte[]) obj);
+                    }
+                    else
+                    {
+                        Property prop = (Property) propMap.get(obj);
+                        String value = (prop == null) ? "" : prop.toString();
+                        bos.write(value.getBytes());
+                    }
+                }
+                else if (i2 == -1)
+                {
+                    bos.write(i);
+                }
+                else
+                {
+                    bos.write(i);
+                    bos.write(i2);
+                }
+            }
+            else
+            {
+                bos.write(i);
+            }
+        }
+        bos.close();
+    }
+
+    protected static Object readParameter(InputStream is)
+        throws IOException
+    {
+        int count = 0;
+        int i = 0;
+        while ((count < s_buffer.length) && ((i = is.read()) > 0))
+        {
+            if ((char) i == '%')
+            {
+                // One of three possibilities, we have the end of
+                // the parameter, we have an end of file, or we
+                // don't have the parameter end.
+                int i2 = is.read();
+                if ((char) i2 == '%')
+                {
+                    return new String(s_buffer, 0, count);
+                }
+                else if (i2 == -1)
+                {
+                    s_buffer[count] = (byte) i;
+                    byte[] b = new byte[count];
+                    for (int j = 0; j < count; j++)
+                        b[j] = s_buffer[j];
+                    return b;
+                }
+                else
+                {
+                    s_buffer[count++] = (byte) i;
+                    s_buffer[count++] = (byte) i2;
+                }
+            }
+            else
+            {
+                s_buffer[count++] = (byte) i;
+            }
+        }
+
+        byte[] b = new byte[count - 1];
+        for (int j = 0; j < (count - 1); j++)
+            b[j] = s_buffer[j];
+
+        return b;
+    }
+
+    public static String getPath(String s, char separator)
+    {
+        return (s.lastIndexOf(separator) < 0)
+            ? "" : s.substring(0, s.lastIndexOf(separator));
+    }
+
+    public static String getPathHead(String s, char separator)
+    {
+        return (s.lastIndexOf(separator) < 0)
+            ? s : s.substring(s.lastIndexOf(separator) + 1);
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/artifact/AbstractFileArtifact.java b/src/org/apache/osgi/framework/installer/artifact/AbstractFileArtifact.java
new file mode 100644
index 0000000..4b46338
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/artifact/AbstractFileArtifact.java
@@ -0,0 +1,103 @@
+/*
+ *   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.osgi.framework.installer.artifact;
+
+import java.io.InputStream;
+import java.util.Map;
+
+import org.apache.osgi.framework.installer.*;
+
+public abstract class AbstractFileArtifact extends AbstractArtifact
+{
+    private StringProperty m_destName = null;
+
+    public AbstractFileArtifact(StringProperty sourceName)
+    {
+        this(sourceName, sourceName);
+    }
+
+    public AbstractFileArtifact(StringProperty sourceName, StringProperty destName)
+    {
+        this(sourceName, destName, null);
+    }
+
+    public AbstractFileArtifact(
+        StringProperty sourceName, StringProperty destName, StringProperty destDir)
+    {
+        this(sourceName, destName, destDir, false);
+    }
+
+    public AbstractFileArtifact(
+        StringProperty sourceName, StringProperty destName,
+        StringProperty destDir, boolean localize)
+    {
+        super(sourceName, destDir, localize);
+        m_destName = destName;
+    }
+
+    public StringProperty getDestinationName()
+    {
+        return m_destName;
+    }
+
+    public boolean process(Status status, Map propMap)
+    {
+        String installDir =
+            ((StringProperty) propMap.get(Install.INSTALL_DIR)).getStringValue();
+
+        try
+        {
+            InputStream is = getInputStream(status);
+
+            if (is == null)
+            {
+                return true;
+            }
+
+            if (localize())
+            {
+                status.setText("Copying and configuring "
+                    + getSourceName().getStringValue());
+                copyAndLocalize(
+                    is,
+                    installDir,
+                    getDestinationName().getStringValue(),
+                    getDestinationDirectory().getStringValue(),
+                    propMap);
+            }
+            else
+            {
+                status.setText("Copying " + getSourceName().getStringValue());
+                copy(
+                    is,
+                    installDir,
+                    getDestinationName().getStringValue(),
+                    getDestinationDirectory().getStringValue());
+            }
+
+            is.close();
+
+        }
+        catch (Exception ex)
+        {
+            System.err.println(ex);
+            return false;
+        }
+
+        return true;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/artifact/AbstractJarArtifact.java b/src/org/apache/osgi/framework/installer/artifact/AbstractJarArtifact.java
new file mode 100644
index 0000000..fef3316
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/artifact/AbstractJarArtifact.java
@@ -0,0 +1,124 @@
+/*
+ *   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.osgi.framework.installer.artifact;
+
+import java.io.*;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+
+import org.apache.osgi.framework.installer.*;
+
+public abstract class AbstractJarArtifact extends AbstractArtifact
+{
+    public AbstractJarArtifact(StringProperty sourceName)
+    {
+        this(sourceName, null);
+    }
+
+    public AbstractJarArtifact(StringProperty sourceName, StringProperty destDir)
+    {
+        this(sourceName, destDir, false);
+    }
+
+    public AbstractJarArtifact(
+        StringProperty sourceName, StringProperty destDir, boolean localize)
+    {
+        super(sourceName, destDir, localize);
+    }
+
+    public boolean process(Status status, Map propMap)
+    {
+        try
+        {
+            InputStream is = getInputStream(status);
+
+            if (is == null)
+            {
+                return true;
+            }
+
+            JarInputStream jis = new JarInputStream(is);
+            status.setText("Extracting...");
+            unjar(jis, propMap);
+            jis.close();
+        }
+        catch (Exception ex)
+        {
+            System.err.println(this);
+            System.err.println(ex);
+            return false;
+        }
+
+        return true;
+    }
+
+    protected void unjar(JarInputStream jis, Map propMap)
+        throws IOException
+    {
+        String installDir =
+            ((StringProperty) propMap.get(Install.INSTALL_DIR)).getStringValue();
+
+        // Loop through JAR entries.
+        for (JarEntry je = jis.getNextJarEntry();
+             je != null;
+             je = jis.getNextJarEntry())
+        {
+            if (je.getName().startsWith("/"))
+            {
+                throw new IOException("JAR resource cannot contain absolute paths.");
+            }
+
+            File target =
+                new File(installDir, getDestinationDirectory().getStringValue());
+            target = new File(target, je.getName());
+
+            // Check to see if the JAR entry is a directory.
+            if (je.isDirectory())
+            {
+                if (!target.exists())
+                {
+                    if (!target.mkdirs())
+                    {
+                        throw new IOException("Unable to create target directory: "
+                            + target);
+                    }
+                }
+                // Just continue since directories do not have content to copy.
+                continue;
+            }
+
+            int lastIndex = je.getName().lastIndexOf('/');
+            String name = (lastIndex >= 0) ?
+                je.getName().substring(lastIndex + 1) : je.getName();
+            String destination = (lastIndex >= 0) ?
+                je.getName().substring(0, lastIndex) : "";
+
+            // JAR files use '/', so convert it to platform separator.
+            destination = destination.replace('/', File.separatorChar);
+
+            if (localize())
+            {
+                copyAndLocalize(jis, installDir, name, destination, propMap);
+            }
+            else
+            {
+                copy(jis, installDir, name, destination);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/artifact/ResourceFileArtifact.java b/src/org/apache/osgi/framework/installer/artifact/ResourceFileArtifact.java
new file mode 100644
index 0000000..599df964
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/artifact/ResourceFileArtifact.java
@@ -0,0 +1,61 @@
+/*
+ *   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.osgi.framework.installer.artifact;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.osgi.framework.installer.Status;
+import org.apache.osgi.framework.installer.StringProperty;
+import org.apache.osgi.framework.installer.resource.ResourceLoader;
+
+public class ResourceFileArtifact extends AbstractFileArtifact
+{
+    public ResourceFileArtifact(StringProperty sourceName)
+    {
+        this(sourceName, sourceName);
+    }
+
+    public ResourceFileArtifact(StringProperty sourceName, StringProperty destName)
+    {
+        this(sourceName, destName, null);
+    }
+
+    public ResourceFileArtifact(
+        StringProperty sourceName, StringProperty destName, StringProperty destDir)
+    {
+        this(sourceName, destName, destDir, false);
+    }
+
+    public ResourceFileArtifact(
+        StringProperty sourceName, StringProperty destName,
+        StringProperty destDir, boolean localize)
+    {
+        super(sourceName, destName, destDir, localize);
+    }
+
+    public InputStream getInputStream(Status status)
+        throws IOException
+    {
+        return ResourceLoader.getResourceAsStream(getSourceName().getStringValue());
+    }
+
+    public String toString()
+    {
+        return "RESOURCE FILE: " + getSourceName().getStringValue();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/artifact/ResourceJarArtifact.java b/src/org/apache/osgi/framework/installer/artifact/ResourceJarArtifact.java
new file mode 100644
index 0000000..59faea3
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/artifact/ResourceJarArtifact.java
@@ -0,0 +1,54 @@
+/*
+ *   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.osgi.framework.installer.artifact;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.osgi.framework.installer.Status;
+import org.apache.osgi.framework.installer.StringProperty;
+import org.apache.osgi.framework.installer.resource.ResourceLoader;
+
+public class ResourceJarArtifact extends AbstractJarArtifact
+{
+    public ResourceJarArtifact(StringProperty sourceName)
+    {
+        this(sourceName, null);
+    }
+
+    public ResourceJarArtifact(StringProperty sourceName, StringProperty destDir)
+    {
+        this(sourceName, destDir, false);
+    }
+
+    public ResourceJarArtifact(
+        StringProperty sourceName, StringProperty destDir, boolean localize)
+    {
+        super(sourceName, destDir, localize);
+    }
+
+    public InputStream getInputStream(Status status)
+        throws IOException
+    {
+        return ResourceLoader.getResourceAsStream(getSourceName().getStringValue());
+    }
+
+    public String toString()
+    {
+        return "RESOURCE JAR: " + getSourceName().getStringValue();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/artifact/URLFileArtifact.java b/src/org/apache/osgi/framework/installer/artifact/URLFileArtifact.java
new file mode 100644
index 0000000..b3729f0
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/artifact/URLFileArtifact.java
@@ -0,0 +1,90 @@
+/*
+ *   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.osgi.framework.installer.artifact;
+
+import java.io.*;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.osgi.framework.installer.Status;
+import org.apache.osgi.framework.installer.StringProperty;
+
+public class URLFileArtifact extends AbstractFileArtifact
+{
+    public URLFileArtifact(StringProperty sourceName, StringProperty destName)
+    {
+        this(sourceName, destName, null);
+    }
+
+    public URLFileArtifact(
+        StringProperty sourceName, StringProperty destName, StringProperty destDir)
+    {
+        this(sourceName, destName, destDir, false);
+    }
+
+    public URLFileArtifact(
+        StringProperty sourceName, StringProperty destName,
+        StringProperty destDir, boolean localize)
+    {
+        super(sourceName, destName, destDir, localize);
+    }
+
+    public InputStream getInputStream(Status status)
+        throws IOException
+    {
+        String fileName = getSourceName().getStringValue();
+        fileName = (fileName.lastIndexOf('/') > 0)
+            ? fileName.substring(fileName.lastIndexOf('/') + 1)
+            : fileName;
+        
+        status.setText("Connecting...");
+
+        File file = File.createTempFile("felix-install.tmp", null);
+        file.deleteOnExit();
+
+        OutputStream os = new FileOutputStream(file);
+        URLConnection conn = new URL(getSourceName().getStringValue()).openConnection();
+        int total = conn.getContentLength();
+        InputStream is = conn.getInputStream();
+
+        int count = 0;
+        for (int len = is.read(s_buffer); len > 0; len = is.read(s_buffer))
+        {
+            count += len;
+            os.write(s_buffer, 0, len);
+            if (total > 0)
+            {
+                status.setText("Downloading " + fileName
+                    + " ( " + count + " bytes of " + total + " ).");
+            }
+            else
+            {
+                status.setText("Downloading " + fileName + " ( " + count + " bytes ).");
+            }
+        }
+
+        os.close();
+        is.close();
+
+        return new FileInputStream(file);
+    }
+
+    public String toString()
+    {
+        return "URL FILE: " + getSourceName().getStringValue();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/artifact/URLJarArtifact.java b/src/org/apache/osgi/framework/installer/artifact/URLJarArtifact.java
new file mode 100644
index 0000000..21e4751
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/artifact/URLJarArtifact.java
@@ -0,0 +1,88 @@
+/*
+ *   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.osgi.framework.installer.artifact;
+
+import java.io.*;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.osgi.framework.installer.Status;
+import org.apache.osgi.framework.installer.StringProperty;
+
+public class URLJarArtifact extends AbstractJarArtifact
+{
+    public URLJarArtifact(StringProperty sourceName)
+    {
+        this(sourceName, null);
+    }
+
+    public URLJarArtifact(StringProperty sourceName, StringProperty destDir)
+    {
+        this(sourceName, destDir, false);
+    }
+
+    public URLJarArtifact(
+        StringProperty sourceName, StringProperty destDir, boolean localize)
+    {
+        super(sourceName, destDir, localize);
+    }
+
+    public InputStream getInputStream(Status status)
+        throws IOException
+    {
+        String fileName = getSourceName().getStringValue();
+        fileName = (fileName.lastIndexOf('/') > 0)
+            ? fileName.substring(fileName.lastIndexOf('/') + 1)
+            : fileName;
+        
+        status.setText("Connecting...");
+
+        File file = File.createTempFile("felix-install.tmp", null);
+        file.deleteOnExit();
+
+        OutputStream os = new FileOutputStream(file);
+        URLConnection conn = new URL(getSourceName().getStringValue()).openConnection();
+        int total = conn.getContentLength();
+        InputStream is = conn.getInputStream();
+
+        int count = 0;
+        for (int len = is.read(s_buffer); len > 0; len = is.read(s_buffer))
+        {
+            count += len;
+            os.write(s_buffer, 0, len);
+            if (total > 0)
+            {
+                status.setText("Downloading " + fileName
+                    + " ( " + count + " bytes of " + total + " ).");
+            }
+            else
+            {
+                status.setText("Downloading " + fileName + " ( " + count + " bytes ).");
+            }
+        }
+
+        os.close();
+        is.close();
+
+        return new FileInputStream(file);
+    }
+
+    public String toString()
+    {
+        return "URL JAR: " + getSourceName().getStringValue();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/editor/BooleanEditor.java b/src/org/apache/osgi/framework/installer/editor/BooleanEditor.java
new file mode 100644
index 0000000..e11a654
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/editor/BooleanEditor.java
@@ -0,0 +1,89 @@
+/*
+ *   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.osgi.framework.installer.editor;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.*;
+
+import org.apache.osgi.framework.installer.BooleanProperty;
+import org.apache.osgi.framework.installer.Property;
+
+public class BooleanEditor extends JPanel
+{
+    private BooleanProperty m_prop = null;
+    private JRadioButton m_trueButton = null;
+    private JRadioButton m_falseButton = null;
+    private String m_trueString = null;
+    private String m_falseString = null;
+
+    public BooleanEditor(BooleanProperty prop)
+    {
+        this(prop, "true", "false");
+    }
+
+    public BooleanEditor(BooleanProperty prop, String trueString, String falseString)
+    {
+        m_prop = prop;
+        m_trueString = trueString;
+        m_falseString = falseString;
+        init();
+    }
+
+    public Property getProperty()
+    {
+        return m_prop;
+    }
+
+    public void setEnabled(boolean b)
+    {
+        m_trueButton.setEnabled(b);
+        m_falseButton.setEnabled(b);
+    }
+
+    protected void init()
+    {
+        add(m_trueButton = new JRadioButton(m_trueString));
+        add(m_falseButton = new JRadioButton(m_falseString));
+        ButtonGroup group = new ButtonGroup();
+        group.add(m_trueButton);
+        group.add(m_falseButton);
+        if (m_prop.getBooleanValue())
+        {
+            m_trueButton.setSelected(true);
+        }
+        else
+        {
+            m_falseButton.setSelected(true);
+        }
+
+        // Add action listeners.
+        m_trueButton.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent event)
+            {
+                m_prop.setBooleanValue(true);
+            }
+        });
+        m_falseButton.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent event)
+            {
+                m_prop.setBooleanValue(false);
+            }
+        });
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/editor/BooleanStringEditor.java b/src/org/apache/osgi/framework/installer/editor/BooleanStringEditor.java
new file mode 100644
index 0000000..f64a96e
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/editor/BooleanStringEditor.java
@@ -0,0 +1,125 @@
+/*
+ *   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.osgi.framework.installer.editor;
+
+import java.awt.*;
+import java.awt.event.*;
+
+import javax.swing.*;
+
+import org.apache.osgi.framework.installer.*;
+
+public class BooleanStringEditor extends JPanel
+{
+    private Property m_prop = null;
+    private JCheckBox m_includeButton = null;
+    private JTextField m_textField = null;
+
+    public BooleanStringEditor(Property prop)
+    {
+        if ((prop instanceof BooleanProperty) && (prop instanceof StringProperty))
+        {
+            m_prop = prop;
+        }
+        else
+        {
+            throw new IllegalArgumentException(
+                "Property must implement both boolean and string property interfaces.");
+        }
+        init();
+    }
+
+    public Property getProperty()
+    {
+        return m_prop;
+    }
+
+    public void setEnabled(boolean b)
+    {
+        m_includeButton.setEnabled(b);
+        m_textField.setEnabled(b && m_includeButton.isSelected());
+    }
+
+    protected void init()
+    {
+        // Set layout.
+        GridBagLayout grid = new GridBagLayout();
+        GridBagConstraints gbc = new GridBagConstraints();
+        gbc.insets = new Insets(0, 2, 0, 2);
+        setLayout(grid);
+
+        // Add button.
+        gbc.gridx = 0;
+        gbc.gridy = 0;
+        gbc.gridheight = 1;
+        gbc.gridwidth = 1;
+        gbc.anchor = GridBagConstraints.WEST;
+        m_includeButton = new JCheckBox("");
+        grid.setConstraints(m_includeButton, gbc);
+        add(m_includeButton);
+
+        // Add field.
+        gbc.gridx = 1;
+        gbc.gridy = 0;
+        gbc.gridheight = 1;
+        gbc.gridwidth = 3;
+        gbc.anchor = GridBagConstraints.WEST;
+        m_textField = new JTextField(30);
+        m_textField.setText(((StringProperty) m_prop).getStringValue());
+        grid.setConstraints(m_textField, gbc);
+        add(m_textField);
+        m_textField.setEnabled(false);
+
+        // Add action listener.
+        m_includeButton.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent event)
+            {
+                if (m_includeButton.isSelected())
+                {
+                    ((BooleanProperty) m_prop).setBooleanValue(true);
+                    m_textField.setEnabled(true);
+                }
+                else
+                {
+                    ((BooleanProperty) m_prop).setBooleanValue(false);
+                    m_textField.setEnabled(false);
+                }
+            }
+        });
+
+        // Add focus listener.
+        m_textField.addFocusListener(new FocusListener() {
+            public void focusGained(FocusEvent event)
+            {
+            }
+            public void focusLost(FocusEvent event)
+            {
+                if (!event.isTemporary())
+                {
+                    ((StringProperty) m_prop).setStringValue(m_textField.getText());
+                }
+            }
+        });
+
+        // Currently, the button is not selected. If the property
+        // is true, then click once to select button.        
+        if (((BooleanProperty) m_prop).getBooleanValue())
+        {
+            m_includeButton.doClick();
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/editor/FileEditor.java b/src/org/apache/osgi/framework/installer/editor/FileEditor.java
new file mode 100644
index 0000000..04a7bde
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/editor/FileEditor.java
@@ -0,0 +1,139 @@
+/*
+ *   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.osgi.framework.installer.editor;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.io.File;
+
+import javax.swing.*;
+
+import org.apache.osgi.framework.installer.Property;
+import org.apache.osgi.framework.installer.StringProperty;
+
+public class FileEditor extends JPanel
+{
+    private StringProperty m_prop = null;
+    private JTextField m_textField = null;
+    private JButton m_browseButton = null;
+    private boolean m_isDirectory = false;
+
+    public FileEditor(StringProperty prop, boolean isDirectory)
+    {
+        super();
+        m_prop = prop;
+        m_isDirectory = isDirectory;
+        init();
+    }
+
+    public Property getProperty()
+    {
+        return m_prop;
+    }
+
+    public void setEnabled(boolean b)
+    {
+        m_textField.setEnabled(b);
+        m_browseButton.setEnabled(b);
+    }
+
+    protected void init()
+    {
+        // Set layout.
+        GridBagLayout grid = new GridBagLayout();
+        GridBagConstraints gbc = new GridBagConstraints();
+        gbc.insets = new Insets(0, 2, 0, 2);
+        setLayout(grid);
+
+        // Add field.
+        gbc.gridx = 0;
+        gbc.gridy = 0;
+        gbc.gridheight = 1;
+        gbc.gridwidth = 2;
+        gbc.anchor = GridBagConstraints.WEST;
+        m_textField = new JTextField(30);
+        m_textField.setText(m_prop.getStringValue());
+        grid.setConstraints(m_textField, gbc);
+        add(m_textField);
+
+        // Add button.
+        gbc.gridx = 2;
+        gbc.gridy = 0;
+        gbc.gridheight = 1;
+        gbc.gridwidth = 1;
+        gbc.anchor = GridBagConstraints.EAST;
+        m_browseButton = new JButton("Browse...");
+        m_browseButton.setMargin(new Insets(1, 1, 1, 1));
+        grid.setConstraints(m_browseButton, gbc);
+        add(m_browseButton);
+
+        // Add focus listener.
+        m_textField.addFocusListener(new FocusListener() {
+            public void focusGained(FocusEvent event)
+            {
+            }
+            public void focusLost(FocusEvent event)
+            {
+                if (!event.isTemporary())
+                {
+                    // Set the new value.
+                    m_prop.setStringValue(normalizeValue(m_textField.getText()));
+
+                }
+            }
+        });
+
+        // Add action listener.
+        m_browseButton.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent event)
+            {
+                JFileChooser fileDlg = new JFileChooser();
+                if (m_isDirectory)
+                {
+                    fileDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+                    fileDlg.setDialogTitle("Please select a directory...");
+                }
+                else
+                {
+                    fileDlg.setFileSelectionMode(JFileChooser.FILES_ONLY);
+                    fileDlg.setDialogTitle("Please select a file...");
+                }
+                fileDlg.setApproveButtonText("Select");
+                if (fileDlg.showOpenDialog(FileEditor.this) ==
+                    JFileChooser.APPROVE_OPTION)
+                {
+                    m_textField.setText(fileDlg.getSelectedFile().getAbsolutePath());
+                    m_prop.setStringValue(normalizeValue(m_textField.getText()));
+                }
+            }
+        });
+    }
+
+    private String normalizeValue(String value)
+    {
+        // Make sure that directories never end with a slash,
+        // for consistency.
+        if (m_isDirectory)
+        {
+            if (value.endsWith(File.separator))
+            {
+                value = value.substring(0, value.length() - 1);
+            }
+        }
+        return value;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/editor/StringEditor.java b/src/org/apache/osgi/framework/installer/editor/StringEditor.java
new file mode 100644
index 0000000..8d0c162
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/editor/StringEditor.java
@@ -0,0 +1,83 @@
+/*
+ *   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.osgi.framework.installer.editor;
+
+import java.awt.*;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
+
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+import org.apache.osgi.framework.installer.Property;
+import org.apache.osgi.framework.installer.StringProperty;
+
+public class StringEditor extends JPanel
+{
+    private StringProperty m_prop = null;
+    private JTextField m_textField = null;
+
+    public StringEditor(StringProperty prop)
+    {
+        m_prop = prop;
+        init();
+    }
+
+    public Property getProperty()
+    {
+        return m_prop;
+    }
+
+    public void setEnabled(boolean b)
+    {
+        m_textField.setEnabled(b);
+    }
+
+    protected void init()
+    {
+        // Set layout.
+        GridBagLayout grid = new GridBagLayout();
+        GridBagConstraints gbc = new GridBagConstraints();
+        gbc.insets = new Insets(0, 2, 0, 2);
+        setLayout(grid);
+
+        // Add field.
+        gbc.gridx = 0;
+        gbc.gridy = 0;
+        gbc.gridheight = 1;
+        gbc.gridwidth = 2;
+        gbc.anchor = GridBagConstraints.WEST;
+        m_textField = new JTextField(20);
+        m_textField.setText(m_prop.getStringValue());
+        grid.setConstraints(m_textField, gbc);
+        add(m_textField);
+
+        // Add focus listener.
+        m_textField.addFocusListener(new FocusListener() {
+            public void focusGained(FocusEvent event)
+            {
+            }
+            public void focusLost(FocusEvent event)
+            {
+                if (!event.isTemporary())
+                {
+                    m_prop.setStringValue(m_textField.getText());
+                }
+            }
+        });
+    }
+}
diff --git a/src/org/apache/osgi/framework/installer/manifest.mf b/src/org/apache/osgi/framework/installer/manifest.mf
new file mode 100644
index 0000000..f25e0aa
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/manifest.mf
@@ -0,0 +1 @@
+Main-Class: org.apache.osgi.framework.installer.Install
diff --git a/src/org/apache/osgi/framework/installer/property/BooleanPropertyImpl.java b/src/org/apache/osgi/framework/installer/property/BooleanPropertyImpl.java
new file mode 100644
index 0000000..cde63c1
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/property/BooleanPropertyImpl.java
@@ -0,0 +1,69 @@
+/*
+ *   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.osgi.framework.installer.property;
+
+import javax.swing.JComponent;
+
+import org.apache.osgi.framework.installer.BooleanProperty;
+import org.apache.osgi.framework.installer.editor.BooleanEditor;
+
+public class BooleanPropertyImpl implements BooleanProperty
+{
+    private String m_name = null;
+    private boolean m_value = false;
+    private JComponent m_editor = null;
+
+    public BooleanPropertyImpl(String name, boolean value)
+    {
+        m_name = name;
+        m_value = value;
+    }
+
+    public String getName()
+    {
+        return m_name;
+    }
+
+    public boolean getBooleanValue()
+    {
+        return m_value;
+    }
+
+    public void setBooleanValue(boolean b)
+    {
+        m_value = b;
+    }
+
+    public JComponent getEditor()
+    {
+        if (m_editor == null)
+        {
+            m_editor = new BooleanEditor(this);
+        }
+        return m_editor;
+    }
+
+    public void setEditor(JComponent comp)
+    {
+        m_editor = comp;
+    }
+
+    public String toString()
+    {
+        return (m_value) ? Boolean.TRUE.toString() : Boolean.FALSE.toString();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/property/BooleanStringPropertyImpl.java b/src/org/apache/osgi/framework/installer/property/BooleanStringPropertyImpl.java
new file mode 100644
index 0000000..da8ed1d
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/property/BooleanStringPropertyImpl.java
@@ -0,0 +1,82 @@
+/*
+ *   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.osgi.framework.installer.property;
+
+import javax.swing.JComponent;
+
+import org.apache.osgi.framework.installer.BooleanProperty;
+import org.apache.osgi.framework.installer.StringProperty;
+import org.apache.osgi.framework.installer.editor.BooleanStringEditor;
+
+public class BooleanStringPropertyImpl implements BooleanProperty, StringProperty
+{
+    private String m_name = null;
+    private boolean m_boolean = false;
+    private String m_string = "";
+    private JComponent m_editor = null;
+
+    public BooleanStringPropertyImpl(String name, boolean b, String s)
+    {
+        m_name = name;
+        m_boolean = b;
+        m_string = s;
+    }
+
+    public String getName()
+    {
+        return m_name;
+    }
+
+    public boolean getBooleanValue()
+    {
+        return m_boolean;
+    }
+
+    public void setBooleanValue(boolean b)
+    {
+        m_boolean = b;
+    }
+
+    public String getStringValue()
+    {
+        return m_string;
+    }
+
+    public void setStringValue(String s)
+    {
+        m_string = s;
+    }
+
+    public JComponent getEditor()
+    {
+        if (m_editor == null)
+        {
+            m_editor = new BooleanStringEditor(this);
+        }
+        return m_editor;
+    }
+
+    public void setEditor(JComponent comp)
+    {
+        m_editor = comp;
+    }
+
+    public String toString()
+    {
+        return m_string;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/property/NotBooleanPropertyImpl.java b/src/org/apache/osgi/framework/installer/property/NotBooleanPropertyImpl.java
new file mode 100644
index 0000000..e242552
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/property/NotBooleanPropertyImpl.java
@@ -0,0 +1,67 @@
+/*
+ *   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.osgi.framework.installer.property;
+
+import javax.swing.JComponent;
+
+import org.apache.osgi.framework.installer.BooleanProperty;
+import org.apache.osgi.framework.installer.editor.BooleanEditor;
+
+public class NotBooleanPropertyImpl implements BooleanProperty
+{
+    private BooleanProperty m_prop = null;
+    private JComponent m_editor = null;
+
+    public NotBooleanPropertyImpl(BooleanProperty prop)
+    {
+        m_prop = prop;
+    }
+
+    public String getName()
+    {
+        return "NOT " + m_prop.getName();
+    }
+
+    public boolean getBooleanValue()
+    {
+        return !m_prop.getBooleanValue();
+    }
+
+    public void setBooleanValue(boolean b)
+    {
+        m_prop.setBooleanValue(!b);
+    }
+
+    public JComponent getEditor()
+    {
+        if (m_editor == null)
+        {
+            m_editor = new BooleanEditor(this);
+        }
+        return m_editor;
+    }
+
+    public void setEditor(JComponent comp)
+    {
+        m_editor = comp;
+    }
+
+    public String toString()
+    {
+        return (getBooleanValue()) ? Boolean.TRUE.toString() : Boolean.FALSE.toString();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/property/StringPropertyImpl.java b/src/org/apache/osgi/framework/installer/property/StringPropertyImpl.java
new file mode 100644
index 0000000..47f8db1
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/property/StringPropertyImpl.java
@@ -0,0 +1,69 @@
+/*
+ *   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.osgi.framework.installer.property;
+
+import javax.swing.JComponent;
+
+import org.apache.osgi.framework.installer.StringProperty;
+import org.apache.osgi.framework.installer.editor.StringEditor;
+
+public class StringPropertyImpl implements StringProperty
+{
+    private String m_name = null;
+    private String m_value = "";
+    private JComponent m_editor = null;
+
+    public StringPropertyImpl(String name, String value)
+    {
+        m_name = name;
+        m_value = value;
+    }
+
+    public String getName()
+    {
+        return m_name;
+    }
+
+    public String getStringValue()
+    {
+        return m_value;
+    }
+
+    public void setStringValue(String s)
+    {
+        m_value = s;
+    }
+
+    public JComponent getEditor()
+    {
+        if (m_editor == null)
+        {
+            m_editor = new StringEditor(this);
+        }
+        return m_editor;
+    }
+
+    public void setEditor(JComponent comp)
+    {
+        m_editor = comp;
+    }
+
+    public String toString()
+    {
+        return m_value;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/installer/resource/ResourceLoader.java b/src/org/apache/osgi/framework/installer/resource/ResourceLoader.java
new file mode 100644
index 0000000..ae7d675
--- /dev/null
+++ b/src/org/apache/osgi/framework/installer/resource/ResourceLoader.java
@@ -0,0 +1,33 @@
+/*
+ *   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.osgi.framework.installer.resource;
+
+import java.io.InputStream;
+import java.net.URL;
+
+public class ResourceLoader
+{
+    public static URL getResource(String name)
+    {
+        return ResourceLoader.class.getResource(name);
+    }
+
+    public static InputStream getResourceAsStream(String name)
+    {
+        return ResourceLoader.class.getResourceAsStream(name);
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/manifest.mf b/src/org/apache/osgi/framework/manifest.mf
new file mode 100644
index 0000000..d9208d9
--- /dev/null
+++ b/src/org/apache/osgi/framework/manifest.mf
@@ -0,0 +1,2 @@
+Main-Class: org.apache.osgi.framework.Main
+Class-Path: osgi.jar moduleloader.jar
diff --git a/src/org/apache/osgi/framework/searchpolicy/R4Attribute.java b/src/org/apache/osgi/framework/searchpolicy/R4Attribute.java
new file mode 100644
index 0000000..a3161b9
--- /dev/null
+++ b/src/org/apache/osgi/framework/searchpolicy/R4Attribute.java
@@ -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.osgi.framework.searchpolicy;
+
+public class R4Attribute
+{
+    private String m_name = "";
+    private String m_value = "";
+    private boolean m_isMandatory = false;
+    
+    public R4Attribute(String name, String value, boolean isMandatory)
+    {
+        m_name = name;
+        m_value = value;
+        m_isMandatory = isMandatory;
+    }
+
+    public String getName()
+    {
+        return m_name;
+    }
+
+    public String getValue()
+    {
+        return m_value;
+    }
+
+    public boolean isMandatory()
+    {
+        return m_isMandatory;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/searchpolicy/R4Directive.java b/src/org/apache/osgi/framework/searchpolicy/R4Directive.java
new file mode 100644
index 0000000..12de924
--- /dev/null
+++ b/src/org/apache/osgi/framework/searchpolicy/R4Directive.java
@@ -0,0 +1,39 @@
+/*
+ *   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.osgi.framework.searchpolicy;
+
+public class R4Directive
+{
+    private String m_name = "";
+    private String m_value = "";
+    
+    public R4Directive(String name, String value)
+    {
+        m_name = name;
+        m_value = value;
+    }
+
+    public String getName()
+    {
+        return m_name;
+    }
+
+    public String getValue()
+    {
+        return m_value;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/searchpolicy/R4Package.java b/src/org/apache/osgi/framework/searchpolicy/R4Package.java
new file mode 100755
index 0000000..380c5f0
--- /dev/null
+++ b/src/org/apache/osgi/framework/searchpolicy/R4Package.java
@@ -0,0 +1,634 @@
+/*
+ *   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.osgi.framework.searchpolicy;
+
+import java.util.*;
+
+import org.apache.osgi.framework.util.FelixConstants;
+import org.apache.osgi.framework.util.Util;
+
+public class R4Package
+{
+    private String m_id = "";
+    private R4Directive[] m_directives = null;
+    private R4Attribute[] m_attrs = null;
+    private R4Version m_versionLow = null;
+    private R4Version m_versionHigh = null;
+    private String[] m_uses = null;
+    private boolean m_isOptional = false;
+    private String[][] m_includeFilter = null;
+    private String[][] m_excludeFilter = null;
+
+    protected R4Package(R4Package pkg)
+    {
+        m_id = pkg.m_id;
+        m_directives = pkg.m_directives;
+        m_attrs = pkg.m_attrs;
+        m_versionLow = pkg.m_versionLow;
+        m_versionHigh = pkg.m_versionHigh;
+        m_uses = pkg.m_uses;
+        m_isOptional = pkg.m_isOptional;
+        m_includeFilter = pkg.m_includeFilter;
+        m_excludeFilter = pkg.m_excludeFilter;
+    }
+
+    public R4Package(String id, R4Directive[] directives, R4Attribute[] attrs)
+    {
+        m_id = id;
+        m_directives = (directives == null) ? new R4Directive[0] : directives;
+        m_attrs = (attrs == null) ? new R4Attribute[0] : attrs;
+
+        // Find all directives: uses, mandatory, resolution, include, and exclude.
+        String mandatory = "", uses = "";
+        for (int i = 0; i < m_directives.length; i++)
+        {
+            if (m_directives[i].getName().equals(FelixConstants.USES_DIRECTIVE))
+            {
+                uses = m_directives[i].getValue();
+            }
+            else if (m_directives[i].getName().equals(FelixConstants.MANDATORY_DIRECTIVE))
+            {
+                mandatory = m_directives[i].getValue();
+            }
+            else if (m_directives[i].getName().equals(FelixConstants.RESOLUTION_DIRECTIVE))
+            {
+                m_isOptional = m_directives[i].getValue().equals(FelixConstants.RESOLUTION_OPTIONAL);
+            }
+            else if (m_directives[i].getName().equals(FelixConstants.INCLUDE_DIRECTIVE))
+            {
+                String[] ss = Util.parseDelimitedString(m_directives[i].getValue(), ",");
+                m_includeFilter = new String[ss.length][];
+                for (int filterIdx = 0; filterIdx < ss.length; filterIdx++)
+                {
+                    m_includeFilter[filterIdx] = parseSubstring(ss[filterIdx]);
+                }
+            }
+            else if (m_directives[i].getName().equals(FelixConstants.EXCLUDE_DIRECTIVE))
+            {
+                String[] ss = Util.parseDelimitedString(m_directives[i].getValue(), ",");
+                m_excludeFilter = new String[ss.length][];
+                for (int filterIdx = 0; filterIdx < ss.length; filterIdx++)
+                {
+                    m_excludeFilter[filterIdx] = parseSubstring(ss[filterIdx]);
+                }
+            }
+        }
+
+        // Parse these uses directive.
+        StringTokenizer tok = new StringTokenizer(uses, ",");
+        m_uses = new String[tok.countTokens()];
+        for (int i = 0; i < m_uses.length; i++)
+        {
+            m_uses[i] = tok.nextToken().trim();
+        }
+
+        // Parse mandatory directive and mark specified
+        // attributes as mandatory.
+        tok = new StringTokenizer(mandatory, ",");
+        while (tok.hasMoreTokens())
+        {
+            // Get attribute name.
+            String attrName = tok.nextToken().trim();
+            // Find attribute and mark it as mandatory.
+            boolean found = false;
+            for (int i = 0; (!found) && (i < m_attrs.length); i++)
+            {
+                if (m_attrs[i].getName().equals(attrName))
+                {
+                    m_attrs[i] = new R4Attribute(
+                        m_attrs[i].getName(), m_attrs[i].getValue(), true);
+                    found = true;
+                }
+            }
+            // If a specified mandatory attribute was not found,
+            // then error.
+            if (!found)
+            {
+                throw new IllegalArgumentException(
+                    "Mandatory attribute '" + attrName + "' does not exist.");
+            }
+        }
+
+        // Find and parse version attribute, if present.
+        String versionInterval = "0.0.0";
+        for (int i = 0; i < m_attrs.length; i++)
+        {
+            if (m_attrs[i].getName().equals(FelixConstants.VERSION_ATTRIBUTE) ||
+                m_attrs[i].getName().equals(FelixConstants.PACKAGE_SPECIFICATION_VERSION))
+            {
+                // Normalize version attribute name.
+                m_attrs[i] = new R4Attribute(
+                    FelixConstants.VERSION_ATTRIBUTE, m_attrs[i].getValue(),
+                    m_attrs[i].isMandatory());
+                versionInterval = m_attrs[i].getValue();
+                break;
+            }
+        }
+        
+        R4Version[] versions = parseVersionInterval(versionInterval);
+        m_versionLow = versions[0];
+        if (versions.length == 2)
+        {
+            m_versionHigh = versions[1];
+        }
+    }
+
+    public String getId()
+    {
+        return m_id;
+    }
+
+    public R4Directive[] getDirectives()
+    {
+        return m_directives;
+    }
+
+    public R4Attribute[] getAttributes()
+    {
+        return m_attrs;
+    }
+
+    public R4Version getVersionLow()
+    {
+        return m_versionLow;
+    }
+
+    public R4Version getVersionHigh()
+    {
+        return m_versionHigh;
+    }
+
+    public String[] getUses()
+    {
+        return m_uses;
+    }
+
+    public boolean isOptional()
+    {
+        return m_isOptional;
+    }
+
+    public boolean isIncluded(String name)
+    {
+        if ((m_includeFilter == null) && (m_excludeFilter == null))
+        {
+            return true;
+        }
+
+        // Get the class name portion of the target class.
+        String className = org.apache.osgi.moduleloader.Util.getClassName(name);
+
+        // If there are no include filters then all classes are included
+        // by default, otherwise try to find one match.
+        boolean included = (m_includeFilter == null);
+        for (int i = 0;
+            (!included) && (m_includeFilter != null) && (i < m_includeFilter.length);
+            i++)
+        {
+            included = checkSubstring(m_includeFilter[i], className);
+        }
+
+        // If there are no exclude filters then no classes are excluded
+        // by default, otherwise try to find one match.
+        boolean excluded = false;
+        for (int i = 0;
+            (!excluded) && (m_excludeFilter != null) && (i < m_excludeFilter.length);
+            i++)
+        {
+            excluded = checkSubstring(m_excludeFilter[i], className);
+        }
+        return included && !excluded;
+    }
+
+    // PREVIOUSLY PART OF COMPATIBILITY POLICY.
+    public boolean doesSatisfy(R4Package pkg)
+    {
+        // For packages to be compatible, they must have the
+        // same name.
+        if (!m_id.equals(pkg.m_id))
+        {
+            return false;
+        }
+        
+        return isVersionInRange(m_versionLow, pkg.m_versionLow, pkg.m_versionHigh)
+            && doAttributesMatch(pkg);
+    }
+
+    // PREVIOUSLY PART OF COMPATIBILITY POLICY.
+    public static boolean isVersionInRange(R4Version version, R4Version low, R4Version high)
+    {
+        // We might not have an upper end to the range.
+        if (high == null)
+        {
+            return (version.compareTo(low) >= 0);
+        }
+        else if (low.isInclusive() && high.isInclusive())
+        {
+            return (version.compareTo(low) >= 0) && (version.compareTo(high) <= 0);
+        }
+        else if (high.isInclusive())
+        {
+            return (version.compareTo(low) > 0) && (version.compareTo(high) <= 0);
+        }
+        else if (low.isInclusive())
+        {
+            return (version.compareTo(low) >= 0) && (version.compareTo(high) < 0);
+        }
+
+        return (version.compareTo(low) > 0) && (version.compareTo(high) < 0);
+    }
+
+    private boolean doAttributesMatch(R4Package pkg)
+    {
+        // Cycle through all attributes of the specified package
+        // and make sure their values match the attribute values
+        // of this package.
+        for (int attrIdx = 0; attrIdx < pkg.m_attrs.length; attrIdx++)
+        {
+            // Get current attribute from specified package.
+            R4Attribute attr = pkg.m_attrs[attrIdx];
+
+            // Ignore version attribute, since it is a special case that
+            // has already been compared using isVersionInRange() before
+            // the call to this method was made.
+            if (attr.getName().equals(FelixConstants.VERSION_ATTRIBUTE))
+            {
+                continue;
+            }
+
+            // Check if this package has the same attribute.
+            boolean found = false;
+            for (int thisAttrIdx = 0;
+                (!found) && (thisAttrIdx < m_attrs.length);
+                thisAttrIdx++)
+            {
+                // Get current attribute for this package.
+                R4Attribute thisAttr = m_attrs[thisAttrIdx];
+                // Check if the attribute names are equal.
+                if (attr.getName().equals(thisAttr.getName()))
+                {
+                    // If the values are not equal, then return false immediately.
+                    // We should not compare version values here, since they are
+                    // a special case and have already been compared by a call to
+                    // isVersionInRange() before getting here; however, it is
+                    // possible for version to be mandatory, so make sure it is
+                    // present below.
+                    if (!attr.getValue().equals(thisAttr.getValue()))
+                    {
+                        return false;
+                    }
+                    found = true;
+                }
+            }
+            // If the attribute was not found, then return false.
+            if (!found)
+            {
+                return false;
+            }
+        }
+
+        // Now, cycle through all attributes of this package and verify that
+        // all mandatory attributes are present in the speceified package.
+        for (int thisAttrIdx = 0; thisAttrIdx < m_attrs.length; thisAttrIdx++)
+        {
+            // Get current attribute for this package.
+            R4Attribute thisAttr = m_attrs[thisAttrIdx];
+            
+            // If the attribute is mandatory, then make sure
+            // the specified package has the attribute.
+            if (thisAttr.isMandatory())
+            {
+                boolean found = false;
+                for (int attrIdx = 0;
+                    (!found) && (attrIdx < pkg.m_attrs.length);
+                    attrIdx++)
+                {
+                    // Get current attribute from specified package.
+                    R4Attribute attr = pkg.m_attrs[attrIdx];
+        
+                    // Check if the attribute names are equal
+                    // and set found flag.
+                    if (thisAttr.getName().equals(attr.getName()))
+                    {
+                        found = true;
+                    }
+                }
+                // If not found, then return false.
+                if (!found)
+                {
+                    return false;
+                }
+            }
+        }
+
+        return true;
+    }
+
+    public String toString()
+    {
+        String msg = getId();
+        for (int i = 0; (m_directives != null) && (i < m_directives.length); i++)
+        {
+            msg = msg + " [" + m_directives[i].getName() + ":="+ m_directives[i].getValue() + "]";
+        }
+        for (int i = 0; (m_attrs != null) && (i < m_attrs.length); i++)
+        {
+            msg = msg + " [" + m_attrs[i].getName() + "="+ m_attrs[i].getValue() + "]";
+        }
+        return msg;
+    }
+
+    // Like this: pkg1; pkg2; dir1:=dirval1; dir2:=dirval2; attr1=attrval1; attr2=attrval2,
+    //            pkg1; pkg2; dir1:=dirval1; dir2:=dirval2; attr1=attrval1; attr2=attrval2
+    public static R4Package[] parseImportOrExportHeader(String s)
+    {
+        R4Package[] pkgs = null;
+        if (s != null)
+        {
+            if (s.length() == 0)
+            {
+                throw new IllegalArgumentException(
+                    "The import and export headers cannot be an empty string.");
+            }
+            String[] ss = Util.parseDelimitedString(
+                s, FelixConstants.CLASS_PATH_SEPARATOR);
+            pkgs = parsePackageStrings(ss);
+        }
+        return (pkgs == null) ? new R4Package[0] : pkgs;
+    }
+
+    // Like this: pkg1; pkg2; dir1:=dirval1; dir2:=dirval2; attr1=attrval1; attr2=attrval2
+    public static R4Package[] parsePackageStrings(String[] ss)
+        throws IllegalArgumentException
+    {
+        if (ss == null)
+        {
+            return null;
+        }
+
+        List completeList = new ArrayList();
+        for (int ssIdx = 0; ssIdx < ss.length; ssIdx++)
+        {
+            // Break string into semi-colon delimited pieces.
+            String[] pieces = Util.parseDelimitedString(
+                ss[ssIdx], FelixConstants.PACKAGE_SEPARATOR);
+
+            // Count the number of different packages; packages
+            // will not have an '=' in their string. This assumes
+            // that packages come first, before directives and
+            // attributes.
+            int pkgCount = 0;
+            for (int pieceIdx = 0; pieceIdx < pieces.length; pieceIdx++)
+            {
+                if (pieces[pieceIdx].indexOf('=') >= 0)
+                {
+                    break;
+                }
+                pkgCount++;
+            }
+
+            // Error if no packages were specified.
+            if (pkgCount == 0)
+            {
+                throw new IllegalArgumentException(
+                    "No packages specified on import: " + ss[ssIdx]);
+            }
+
+            // Parse the directives/attributes.
+            R4Directive[] dirs = new R4Directive[pieces.length - pkgCount];
+            R4Attribute[] attrs = new R4Attribute[pieces.length - pkgCount];
+            int dirCount = 0, attrCount = 0;
+            int idx = -1;
+            String sep = null;
+            for (int pieceIdx = pkgCount; pieceIdx < pieces.length; pieceIdx++)
+            {
+                // Check if it is a directive.
+                if ((idx = pieces[pieceIdx].indexOf(FelixConstants.DIRECTIVE_SEPARATOR)) >= 0)
+                {
+                    sep = FelixConstants.DIRECTIVE_SEPARATOR;
+                }
+                // Check if it is an attribute.
+                else if ((idx = pieces[pieceIdx].indexOf(FelixConstants.ATTRIBUTE_SEPARATOR)) >= 0)
+                {
+                    sep = FelixConstants.ATTRIBUTE_SEPARATOR;
+                }
+                // It is an error.
+                else
+                {
+                    throw new IllegalArgumentException(
+                        "Not a directive/attribute: " + ss[ssIdx]);
+                }
+
+                String key = pieces[pieceIdx].substring(0, idx).trim();
+                String value = pieces[pieceIdx].substring(idx + sep.length()).trim();
+
+                // Remove quotes, if value is quoted.
+                if (value.startsWith("\"") && value.endsWith("\""))
+                {
+                    value = value.substring(1, value.length() - 1);
+                }
+
+                // Save the directive/attribute in the appropriate array.
+                if (sep.equals(FelixConstants.DIRECTIVE_SEPARATOR))
+                {
+                    dirs[dirCount++] = new R4Directive(key, value);
+                }
+                else
+                {
+                    attrs[attrCount++] = new R4Attribute(key, value, false);
+                }
+            }
+
+            // Shrink directive array.
+            R4Directive[] dirsFinal = new R4Directive[dirCount];
+            System.arraycopy(dirs, 0, dirsFinal, 0, dirCount);
+            // Shrink attribute array.
+            R4Attribute[] attrsFinal = new R4Attribute[attrCount];
+            System.arraycopy(attrs, 0, attrsFinal, 0, attrCount);
+
+            // Create package attributes for each package and
+            // set directives/attributes. Add each package to
+            // completel list of packages.
+            R4Package[] pkgs = new R4Package[pkgCount];
+            for (int pkgIdx = 0; pkgIdx < pkgCount; pkgIdx++)
+            {
+                pkgs[pkgIdx] = new R4Package(pieces[pkgIdx], dirsFinal, attrsFinal);
+                completeList.add(pkgs[pkgIdx]);
+            }
+        }
+    
+        R4Package[] ips = (R4Package[])
+            completeList.toArray(new R4Package[completeList.size()]);
+        return ips;
+    }
+
+    public static R4Version[] parseVersionInterval(String interval)
+    {
+        // Check if the version is an interval.
+        if (interval.indexOf(',') >= 0)
+        {
+            String s = interval.substring(1, interval.length() - 1);
+            String vlo = s.substring(0, s.indexOf(','));
+            String vhi = s.substring(s.indexOf(',') + 1, s.length());
+            return new R4Version[] {
+                new R4Version(vlo, (interval.charAt(0) == '[')),
+                new R4Version(vhi, (interval.charAt(interval.length() - 1) == ']'))
+            };
+        }
+        else
+        {
+            return new R4Version[] { new R4Version(interval, true) };
+        }
+    }
+
+    //
+    // The following substring-related code was lifted and modified
+    // from the LDAP parser code.
+    //
+
+    private static String[] parseSubstring(String target)
+    {
+        List pieces = new ArrayList();
+        StringBuffer ss = new StringBuffer();
+        // int kind = SIMPLE; // assume until proven otherwise
+        boolean wasStar = false; // indicates last piece was a star
+        boolean leftstar = false; // track if the initial piece is a star
+        boolean rightstar = false; // track if the final piece is a star
+
+        int idx = 0;
+
+        // We assume (sub)strings can contain leading and trailing blanks
+loop:   for (;;)
+        {
+            if (idx >= target.length())
+            {
+                if (wasStar)
+                {
+                    // insert last piece as "" to handle trailing star
+                    rightstar = true;
+                }
+                else
+                {
+                    pieces.add(ss.toString());
+                    // accumulate the last piece
+                    // note that in the case of
+                    // (cn=); this might be
+                    // the string "" (!=null)
+                }
+                ss.setLength(0);
+                break loop;
+            }
+
+            char c = target.charAt(idx++);
+            if (c == '*')
+            {
+                if (wasStar)
+                {
+                    // encountered two successive stars;
+                    // I assume this is illegal
+                    throw new IllegalArgumentException("Invalid filter string: " + target);
+                }
+                if (ss.length() > 0)
+                {
+                    pieces.add(ss.toString()); // accumulate the pieces
+                    // between '*' occurrences
+                }
+                ss.setLength(0);
+                // if this is a leading star, then track it
+                if (pieces.size() == 0)
+                {
+                    leftstar = true;
+                }
+                ss.setLength(0);
+                wasStar = true;
+            }
+            else
+            {
+                wasStar = false;
+                ss.append(c);
+            }
+        }
+        if (leftstar || rightstar || pieces.size() > 1)
+        {
+            // insert leading and/or trailing "" to anchor ends
+            if (rightstar)
+            {
+                pieces.add("");
+            }
+            if (leftstar)
+            {
+                pieces.add(0, "");
+            }
+        }
+        return (String[]) pieces.toArray(new String[pieces.size()]);
+    }
+
+    private static boolean checkSubstring(String[] pieces, String s)
+    {
+        // Walk the pieces to match the string
+        // There are implicit stars between each piece,
+        // and the first and last pieces might be "" to anchor the match.
+        // assert (pieces.length > 1)
+        // minimal case is <string>*<string>
+
+        boolean result = false;
+        int len = pieces.length;
+
+loop:   for (int i = 0; i < len; i++)
+        {
+            String piece = (String) pieces[i];
+            int index = 0;
+            if (i == len - 1)
+            {
+                // this is the last piece
+                if (s.endsWith(piece))
+                {
+                    result = true;
+                }
+                else
+                {
+                    result = false;
+                }
+                break loop;
+            }
+            // initial non-star; assert index == 0
+            else if (i == 0)
+            {
+                if (!s.startsWith(piece))
+                {
+                    result = false;
+                    break loop;
+                }
+            }
+            // assert i > 0 && i < len-1
+            else
+            {
+                // Sure wish stringbuffer supported e.g. indexOf
+                index = s.indexOf(piece, index);
+                if (index < 0)
+                {
+                    result = false;
+                    break loop;
+                }
+            }
+            // start beyond the matching piece
+            index += piece.length();
+        }
+
+        return result;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/searchpolicy/R4SearchPolicy.java b/src/org/apache/osgi/framework/searchpolicy/R4SearchPolicy.java
new file mode 100755
index 0000000..5855c06
--- /dev/null
+++ b/src/org/apache/osgi/framework/searchpolicy/R4SearchPolicy.java
@@ -0,0 +1,1638 @@
+/*
+ *   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.osgi.framework.searchpolicy;
+
+import java.net.URL;
+import java.util.*;
+
+import org.apache.osgi.framework.LogWrapper;
+import org.apache.osgi.moduleloader.*;
+import org.apache.osgi.moduleloader.search.ResolveException;
+import org.apache.osgi.moduleloader.search.ResolveListener;
+
+public class R4SearchPolicy implements SearchPolicy, ModuleListener
+{
+    // Array of R4Package.
+    public static final String EXPORTS_ATTR = "exports";
+    // Array of R4Package.
+    public static final String IMPORTS_ATTR = "imports";
+    // Array of R4Package.
+    public static final String DYNAMICIMPORTS_ATTR = "dynamicimports";
+    // Array of R4Wire.
+    public static final String WIRING_ATTR = "wiring";
+    // Boolean.
+    public static final String RESOLVED_ATTR = "resolved";
+
+    private LogWrapper m_logger = null;
+    private ModuleManager m_mgr = null;
+    private Map m_availPkgMap = new HashMap();
+    private Map m_inUsePkgMap = new HashMap();
+
+    // Listener-related instance variables.
+    private static final ResolveListener[] m_emptyListeners = new ResolveListener[0];
+    private ResolveListener[] m_listeners = m_emptyListeners;
+
+    // Reusable empty arrays.
+    public static final Module[] m_emptyModules = new Module[0];
+    public static final R4Package[] m_emptyPackages = new R4Package[0];
+    public static final R4Wire[] m_emptyWires = new R4Wire[0];
+
+    // Re-usable security manager for accessing class context.
+    private static SecurityManagerX m_sm = new SecurityManagerX();
+
+    public R4SearchPolicy(LogWrapper logger)
+    {
+        m_logger = logger;
+    }
+
+    public void setModuleManager(ModuleManager mgr)
+        throws IllegalStateException
+    {
+        if (m_mgr == null)
+        {
+            m_mgr = mgr;
+            m_mgr.addModuleListener(this);
+        }
+        else
+        {
+            throw new IllegalStateException("Module manager is already initialized");
+        }
+    }
+
+    public Object[] definePackage(Module module, String pkgName)
+    {
+        R4Package pkg = R4SearchPolicy.getExportPackage(module, pkgName);
+        if (pkg != null)
+        {
+            return new Object[]  {
+                pkgName, // Spec title.
+                pkg.getVersionLow().toString(), // Spec version.
+                "", // Spec vendor.
+                "", // Impl title.
+                "", // Impl version.
+                ""  // Impl vendor.
+            };
+        }
+        return null;
+    }
+
+    public Class findClassBeforeModule(ClassLoader parent, Module module, String name)
+        throws ClassNotFoundException
+    {
+        // First, try to resolve the originating module.
+        try
+        {
+            resolve(module);
+        }
+        catch (ResolveException ex)
+        {
+            throw new ClassNotFoundException(name);
+        }
+
+        // Get the package of the target class.
+        String pkgName = Util.getClassPackage(name);
+
+        // Load all "java.*" classes from parent class loader;
+        // these packages cannot be provided by other bundles.
+        if (pkgName.startsWith("java."))
+        {
+            return (parent == null) ? null : parent.loadClass(name);
+        }
+
+        // We delegate to the module's wires to find the class.
+        R4Wire[] wires = getWiringAttr(module);
+        for (int i = 0; i < wires.length; i++)
+        {
+            // Only check when the package of the class is
+            // the same as the import package.
+            if (wires[i].m_pkg.getId().equals(pkgName))
+            {
+                // Before delegating to the module class loader to satisfy
+                // the class load, we must check the include/exclude filters
+                // from the target package to make sure that the class is
+                // actually visible. If the exporting module is the same as
+                // the requesting module, then filtering is not performed
+                // since a module has complete access to itself.
+// TODO: Determine if it is possible to modify Module Loader somehow
+// so that this check is done within the target module itself; it
+// doesn't totally make sense to do this check in the importing module.
+                if (wires[i].m_module != module)
+                {
+                    if (!wires[i].m_pkg.isIncluded(name))
+                    {
+                        throw new ClassNotFoundException(name);
+                    }
+                }
+
+                // Since the class is included, delegate to the exporting module.
+                try
+                {
+                    Class clazz = wires[i].m_module.getClassLoader().loadClassFromModule(name);
+                    if (clazz != null)
+                    {
+                        return clazz;
+                    }
+                }
+                catch (Throwable th)
+                {
+                    // Not much we can do here.
+                }
+                throw new ClassNotFoundException(name);
+            }
+        }
+
+        return null;
+    }
+
+    public Class findClassAfterModule(ClassLoader parent, Module module, String name)
+        throws ClassNotFoundException
+    {
+        // At this point, the module's imports were searched and so was the
+        // the module's own resources. Now we make an attempt to load the
+        // class via a dynamic import, if possible.
+        String pkgName = Util.getClassPackage(name);
+        Module candidate = attemptDynamicImport(module, pkgName);
+        // If the dynamic import was successful, then this initial
+        // time we must directly return the result from dynamically
+        // selected candidate's class loader, but for subsequent
+        // requests for classes in the associated package will be
+        // processed as part of normal static imports.
+        if (candidate != null)
+        {
+            return candidate.getClassLoader().loadClass(name);
+        }
+
+        // At this point, the class could not be found by the bundle's static
+        // or dynamic imports, nor its own resources. Before we throw
+        // an exception, we will try to determine if the instigator of the
+        // class load was a class from a bundle or not. This is necessary
+        // because the specification mandates that classes on the class path
+        // should be hidden (except for java.*), but it does allow for these
+        // classes to be exposed by the system bundle as an export. However,
+        // in some situations classes on the class path make the faulty
+        // assumption that they can access everything on the class path from
+        // every other class loader that they come in contact with. This is
+        // not true if the class loader in question is from a bundle. Thus,
+        // this code tries to detect that situation. If the class
+        // instigating the class load was NOT from a bundle, then we will
+        // make the assumption that the caller actually wanted to use the
+        // parent class loader and we will delegate to it. If the class was
+        // from a bundle, then we will enforce strict class loading rules
+        // for the bundle and throw a class not found exception.
+
+        // Get the class context to see the classes on the stack.
+        Class[] classes = m_sm.getClassContext();
+        // Start from 1 to skip inner class.
+        for (int i = 1; i < classes.length; i++)
+        {
+            // Find the first class on the call stack that is neither
+            // a class loader or Class itself, because we want to ignore
+            // the calls to ClassLoader.loadClass() and Class.forName().
+            if (!ClassLoader.class.isAssignableFrom(classes[i]) &&
+                !Class.class.isAssignableFrom(classes[i]))
+            {
+                // If the instigating class was not from a bundle, then
+                // delegate to the parent class loader. Otherwise, break
+                // out of loop and throw an exception.
+                if (!ModuleClassLoader.class.isInstance(classes[i].getClassLoader()))
+                {
+                    return parent.loadClass(name);
+                }
+                break;
+            }
+        }
+        
+        throw new ClassNotFoundException(name);
+    }
+
+    public URL findResource(ClassLoader parent, Module module, String name)
+        throws ResourceNotFoundException
+    {
+        // First, try to resolve the originating module.
+        try
+        {
+            resolve(module);
+        }
+        catch (ResolveException ex)
+        {
+            return null;
+        }
+
+        // Get the package of the target resource.
+        String pkgName = Util.getResourcePackage(name);
+
+        // Load all "java.*" resources from parent class loader;
+        // these packages cannot be provided by other bundles.
+        if (pkgName.startsWith("java."))
+        {
+            return (parent == null) ? null : parent.getResource(name);
+        }
+
+        // We delegate to the module's wires to find the resource.
+        R4Wire[] wires = getWiringAttr(module);
+        for (int i = 0; i < wires.length; i++)
+        {
+            // Only check when the package of the resource is
+            // the same as the import package.
+            if (wires[i].m_pkg.getId().equals(pkgName))
+            {
+                try
+                {
+                    URL url = wires[i].m_module.getClassLoader().getResourceFromModule(name);
+                    if (url != null)
+                    {
+                        return url;
+                    }
+                }
+                catch (Throwable th)
+                {
+                    // Not much we can do here.
+                }
+                throw new ResourceNotFoundException(name);
+            }
+        }
+
+        // Check dynamic imports.
+// TODO: Dynamic imports should be searched after local sources.
+        Module candidate = attemptDynamicImport(module, pkgName);
+        // This initial time we must directly return the result from
+        // the candidate's class loaders, but since the candidate was
+        // added to the module's wiring attribute, subsequent class
+        // loads from the same package will be handled in the normal
+        // fashion for statically imported packaes.
+        return (candidate == null)
+            ? null : candidate.getClassLoader().getResource(name);
+    }
+
+    private Module attemptDynamicImport(Module module, String pkgName)
+    {
+        Module candidate = null;
+
+        // There is an overriding assumption here that a package is
+        // never split across bundles. If a package can be split
+        // across bundles, then this will fail.
+
+        try
+        {
+            // Check the dynamic import specs for a match of
+            // the target package.
+            R4Package[] dynamics = getDynamicImportsAttr(module);
+            R4Package pkgMatch = null;
+            for (int i = 0; (pkgMatch == null) && (i < dynamics.length); i++)
+            {
+                // Star matches everything.
+                if (dynamics[i].getId().equals("*"))
+                {
+                    // Create a package instance without wildcard.
+                    pkgMatch = new R4Package(
+                        pkgName,
+                        dynamics[i].getDirectives(),
+                        dynamics[i].getAttributes());
+                }
+                // Packages ending in ".*" must match starting strings.
+                else if (dynamics[i].getId().endsWith(".*"))
+                {
+                    if (pkgName.regionMatches(
+                        0, dynamics[i].getId(), 0, dynamics[i].getId().length() - 2))
+                    {
+                        // Create a package instance without wildcard.
+                        pkgMatch = new R4Package(
+                            pkgName,
+                            dynamics[i].getDirectives(),
+                            dynamics[i].getAttributes());
+                    }
+                }
+                // Or we can have a precise match.
+                else
+                {
+                    if (pkgName.equals(dynamics[i].getId()))
+                    {
+                        pkgMatch = dynamics[i];
+                    }
+                }
+            }
+
+            // If the target package does not match any dynamically imported
+            // packages or if the module is already wired for the target package,
+            // then just return null. The module may be already wired to the target
+            // package if the class being searched for does not actually exist.
+            if ((pkgMatch == null) || (getWire(module, pkgMatch.getId()) != null))
+            {
+                return null;
+            }
+
+            // At this point, the target package has matched a dynamically
+            // imported package spec. Now we must try to find a candidate
+            // exporter for target package and add it to the module's set
+            // of wires.
+
+            // Lock module manager instance to ensure that nothing changes.
+            synchronized (m_mgr)
+            {
+                // Try to add a new entry to the module's import attribute.
+                // Select the first candidate that successfully resolves.
+
+                // First check already resolved exports for a match.
+                Module[] candidates = getCompatibleExporters(
+                    (Module[]) m_inUsePkgMap.get(pkgMatch.getId()), pkgMatch);
+                // If there is an "in use" candidate, just take the first one.
+                if (candidates.length > 0)
+                {
+                    candidate = candidates[0];
+                }
+
+                // If there were no "in use" candidates, then try "available"
+                // candidates.
+                if (candidate == null)
+                {
+                    candidates = getCompatibleExporters(
+                        (Module[]) m_availPkgMap.get(pkgMatch.getId()), pkgMatch);
+                    for (int candIdx = 0;
+                        (candidate == null) && (candIdx < candidates.length);
+                        candIdx++)
+                    {
+                        try
+                        {
+                            resolve(module);
+                            candidate = candidates[candIdx];
+                        }
+                        catch (ResolveException ex)
+                        {
+                        }
+                    }
+                }
+
+                // If we found a candidate, then add it to the module's
+                // wiring attribute.
+                if (candidate != null)
+                {
+                    R4Wire[] wires = getWiringAttr(module);
+                    R4Wire[] newWires = new R4Wire[wires.length + 1];
+                    System.arraycopy(wires, 0, newWires, 0, wires.length);
+                    // Find the candidate's export package object and
+                    // use that for creating the wire; this is necessary
+                    // since it contains "uses" dependency information.
+                    newWires[wires.length] = new R4Wire(
+                        getExportPackage(candidate, pkgMatch.getId()), candidate);
+                    module.setAttribute(WIRING_ATTR, newWires);
+m_logger.log(LogWrapper.LOG_DEBUG, "WIRE: [" + module + "] " + newWires[wires.length]);
+                }
+            }
+        }
+        catch (Exception ex)
+        {
+            m_logger.log(LogWrapper.LOG_ERROR, "Unable to dynamically import package.", ex);
+        }
+
+        return candidate;
+    }
+
+    public Module[] getAvailableExporters(R4Package pkg)
+    {
+        // Synchronized on the module manager to make sure that no
+        // modules are added, removed, or resolved.
+        synchronized (m_mgr)
+        {
+            return getCompatibleExporters((Module[]) m_availPkgMap.get(pkg.getId()), pkg);
+        }
+    }
+
+    public Module[] getInUseExporters(R4Package pkg)
+    {
+        // Synchronized on the module manager to make sure that no
+        // modules are added, removed, or resolved.
+        synchronized (m_mgr)
+        {
+            return getCompatibleExporters((Module[]) m_inUsePkgMap.get(pkg.getId()), pkg);
+        }
+    }
+
+    public void resolve(Module rootModule)
+        throws ResolveException
+    {
+        // If the module is already resolved, then we can just return.
+        if (getResolvedAttr(rootModule).booleanValue())
+        {
+            return;
+        }
+
+        // This variable maps an unresolved module to a list of resolver
+        // nodes, where there is one resolver node for each import that
+        // must be resolved. A resolver node contains the potential
+        // candidates to resolve the import and the current selected
+        // candidate index.
+        Map resolverMap = new HashMap();
+
+        // This map will be used to hold the final wires for all
+        // resolved modules, which can then be used to fire resolved
+        // events outside of the synchronized block.
+        Map resolvedModuleWireMap = null;
+    
+        // Synchronize on the module manager, because we don't want
+        // any modules being added or removed while we are in the
+        // middle of this operation.
+        synchronized (m_mgr)
+        {
+            // The first step is to populate the resolver map. This
+            // will use the target module to populate the resolver map
+            // with all potential modules that need to be resolved as a
+            // result of resolving the target module. The key of the
+            // map is a potential module to be resolved and the value is
+            // a list of resolver nodes, one for each of the module's
+            // imports, where each resolver node contains the potential
+            // candidates for resolving the import. Not all modules in
+            // this map will be resolved, only the target module and
+            // any candidates selected to resolve its imports and the
+            // transitive imports this implies.
+            populateResolverMap(resolverMap, rootModule);
+
+//dumpResolverMap();
+
+            // The next step is to use the resolver map to determine if
+            // the class space for the root module is consistent. This
+            // is an iterative process that transitively walks the "uses"
+            // relationships of all currently selected potential candidates
+            // for resolving import packages checking for conflicts. If a
+            // conflict is found, it "increments" the configuration of
+            // currently selected potential candidates and tests them again.
+            // If this method returns, then it has found a consistent set
+            // of candidates; otherwise, a resolve exception is thrown if
+            // it exhausts all possible combinations and could not find a
+            // consistent class space.
+            findConsistentClassSpace(resolverMap, rootModule);
+
+            // The final step is to create the wires for the root module and
+            // transitively all modules that are to be resolved from the
+            // selected candidates for resolving the root module's imports.
+            // When this call returns, each module's wiring and resolved
+            // attributes are set. The resulting wiring map is used below
+            // to fire resolved events outside of the synchronized block.
+            // The resolved module wire map maps a module to its array of
+            // wires.
+            resolvedModuleWireMap = createWires(resolverMap, rootModule);
+            
+//dumpAvailablePackages();
+//dumpUsedPackages();
+
+        } // End of synchronized block on module manager.
+
+        // Fire resolved events for all resolved modules;
+        // the resolved modules array will only be set if the resolve
+        // was successful after the root module was resolved.
+        if (resolvedModuleWireMap != null)
+        {
+            Iterator iter = resolvedModuleWireMap.entrySet().iterator();
+            while (iter.hasNext())
+            {
+                fireModuleResolved((Module) ((Map.Entry) iter.next()).getKey());
+            }
+        }
+    }
+
+    private void populateResolverMap(Map resolverMap, Module module)
+        throws ResolveException
+    {
+        // Detect cycles.
+        if (resolverMap.get(module) != null)
+        {
+            return;
+        }
+
+        // Map to hold the bundle's import packages
+        // and their respective resolving candidates.
+        List nodeList = new ArrayList();
+
+        // Even though the node list is currently emptry, we
+        // record it in the resolver map early so we can use
+        // it to detect cycles.
+        resolverMap.put(module, nodeList);
+
+        // Loop through each import and calculate its resolving
+        // set of candidates.
+        R4Package[] imports = getImportsAttr(module);
+        for (int impIdx = 0; impIdx < imports.length; impIdx++)
+        {
+            // Get the candidates from the "in use" and "available"
+            // package maps. Candidates "in use" have higher priority
+            // than "available" ones, so put the "in use" candidates
+            // at the front of the list of candidates.
+            Module[] inuse = getCompatibleExporters(
+                (Module[]) m_inUsePkgMap.get(
+                    imports[impIdx].getId()), imports[impIdx]);
+            Module[] available = getCompatibleExporters(
+                (Module[]) m_availPkgMap.get(
+                    imports[impIdx].getId()), imports[impIdx]);
+            Module[] candidates = new Module[inuse.length + available.length];
+            System.arraycopy(inuse, 0, candidates, 0, inuse.length);
+            System.arraycopy(available, 0, candidates, inuse.length, available.length);
+
+            // If we have candidates, then we need to recursively populate
+            // the resolver map with each of them.
+            ResolveException rethrow = null;
+            if (candidates.length > 0)
+            {
+                for (int candIdx = 0; candIdx < candidates.length; candIdx++)
+                {
+                    try
+                    {
+                        // Only populate the resolver map with modules that
+                        // are not already resolved.
+                        if (!getResolvedAttr(candidates[candIdx]).booleanValue())
+                        {
+                            populateResolverMap(resolverMap, candidates[candIdx]);
+                        }
+                    }
+                    catch (ResolveException ex)
+                    {
+                        // If we received a resolve exception, then the
+                        // current candidate is not resolvable for some
+                        // reason and should be removed from the list of
+                        // candidates. For now, just null it.
+                        candidates[candIdx] = null;
+                        rethrow = ex;
+                    }
+                }
+
+                // Remove any nulled candidates to create the final list
+                // of available candidates.
+                candidates = shrinkModuleArray(candidates);
+            }
+
+            // If no candidates exist at this point, then throw a
+            // resolve exception unless the import is optional.
+            if ((candidates.length == 0) && !imports[impIdx].isOptional())
+            {
+                // If we have received an exception while trying to populate
+                // the resolver map, rethrow that exception since it might
+                // be useful. NOTE: This is not necessarily the "only"
+                // correct exception, since it is possible that multiple
+                // candidates were not resolvable, but it is better than
+                // nothing.
+                if (rethrow != null)
+                {
+                    throw rethrow;
+                }
+                else
+                {
+                    throw new ResolveException(
+                            "Unable to resolve.", module, imports[impIdx]);
+                }
+            }
+            else if (candidates.length > 0)
+            {
+                nodeList.add(
+                    new ResolverNode(module, imports[impIdx], candidates));
+            }
+        }
+    }
+
+    /**
+     * <p>
+     * This method searches the resolver solution space for a consistent
+     * set of modules to resolve all transitive imports that must be resolved
+     * as a result of resolving the root module. A consistent set of modules
+     * is one where the "uses" relationships of the exported packages for
+     * the selected provider modules do not conflict with each other. A
+     * conflict can occur when the constraints on two or more modules result
+     * in multiple providers for the same package in the same class space.
+     * </p>
+     * @param resolverMap a map containing all potential modules that may need
+     *        to be resolved and the candidates to resolve them.
+     * @param rootModule the module that is the root of the resolve operation.
+     * @throws ResolveException if no consistent set of candidates can be
+     *         found to resolve the root module.
+    **/
+    private void findConsistentClassSpace(Map resolverMap, Module rootModule)
+        throws ResolveException
+    {
+        List resolverList = null;
+
+        // Test the current set of candidates to determine if they
+        // are consistent. Keep looping until we find a consistent
+        // set or an exception is thrown.
+        Map cycleMap = new HashMap();
+        while (!isClassSpaceConsistent(resolverMap, rootModule, cycleMap))
+        {
+m_logger.log(
+    LogWrapper.LOG_DEBUG,
+    "Constraint violation detected, will try to repair.");
+
+            // The incrementCandidateConfiguration() method requires a
+            // ordered access to the resolver map, so we will create
+            // a reusable list once right here.
+            if (resolverList == null)
+            {
+                resolverList = new ArrayList();
+                for (Iterator iter = resolverMap.entrySet().iterator();
+                    iter.hasNext(); )
+                {
+                    resolverList.add((List) ((Map.Entry) iter.next()).getValue());
+                }
+            }
+
+            // Increment the candidate configuration so we can test again.
+            incrementCandidateConfiguration(resolverList);
+
+            // Clear the cycle map.
+            cycleMap.clear();
+        }
+    }
+
+    private boolean isClassSpaceConsistent(
+        Map resolverMap, Module rootModule, Map cycleMap)
+    {
+        // We do not need to verify that already resolved modules
+        // have consistent class spaces because they should be
+        // consistent by definition. Also, if the root module is
+        // part of a cycle, then just assume it is true.
+        if (getResolvedAttr(rootModule).booleanValue() ||
+            (cycleMap.get(rootModule) != null))
+        {
+            return true;
+        }
+
+        // Add to cycle map for future reference.
+        cycleMap.put(rootModule, rootModule);
+
+        // Create an implicit "uses" constraint for every exported package
+        // of the root module that is not also imported; uses constraints
+        // for exported packages that are also imported will be taken
+        // care of as part of normal import package processing.
+        R4Package[] exports = (R4Package[]) getExportsAttr(rootModule);
+        Map usesMap = new HashMap();
+        for (int i = 0; i < exports.length; i++)
+        {
+            // Ignore exports that are also imported, since they
+            // will be taken care of when verifying import constraints.
+            if (getImportPackage(rootModule, exports[i].getId()) == null)
+            {
+                usesMap.put(exports[i].getId(), rootModule);
+            }
+        }
+
+        // Loop through the current candidates for the module's imports
+        // (available in the resolver node list of the resolver map) and
+        // calculate the uses constraints for each of the currently
+        // selected candidates for resolving the imports. Compare each
+        // candidate's constraints to the existing constraints to check
+        // for conflicts.
+        List nodeList = (List) resolverMap.get(rootModule);
+        for (int nodeIdx = 0; nodeIdx < nodeList.size(); nodeIdx++)
+        {
+            // Verify that the current candidate does not violate
+            // any "uses" constraints of existing candidates by
+            // calculating the candidate's transitive "uses" constraints
+            // for the provided package and testing whether they
+            // overlap with existing constraints.
+
+            // First, get the resolver node.
+            ResolverNode node = (ResolverNode) nodeList.get(nodeIdx);
+
+            // Verify that the current candidate itself has a consistent
+            // class space.
+            if (!isClassSpaceConsistent(
+                resolverMap, node.m_candidates[node.m_idx], cycleMap))
+            {
+                return false;
+            }
+
+            // Get the exported package from the current candidate that
+            // will be used to resolve the root module's import.
+            R4Package candidatePkg = getExportPackage(
+                node.m_candidates[node.m_idx], node.m_pkg.getId());
+
+            // Calculate the "uses" dependencies implied by the candidate's
+            // exported package with respect to the currently selected
+            // candidates in the resolver map.
+            Map candUsesMap = calculateUsesDependencies(
+                resolverMap,
+                node.m_candidates[node.m_idx],
+                candidatePkg,
+                new HashMap());
+//System.out.println("MODULE " + rootModule + " USES    " + usesMap);
+//System.out.println("CANDIDATE " + node.m_candidates[node.m_idx] + " USES " + candUsesMap);
+
+            // Iterate through the root module's current set of transitive
+            // "uses" constraints and compare them with the candidate's
+            // transitive set of constraints.
+            Iterator usesIter = candUsesMap.entrySet().iterator();
+            while (usesIter.hasNext())
+            {
+                // If the candidate's uses constraints overlap with
+                // the existing uses constraints, but refer to a
+                // different provider, then the class space is not
+                // consistent; thus, return false.
+                Map.Entry entry = (Map.Entry) usesIter.next();
+                if ((usesMap.get(entry.getKey()) != null) &&
+                    (usesMap.get(entry.getKey()) != entry.getValue()))
+                {
+                    return false;
+                }
+            }
+
+            // Since the current candidate's uses constraints did not
+            // conflict with existing constraints, merge all constraints
+            // and keep testing the remaining candidates for the other
+            // imports of the root module.
+            usesMap.putAll(candUsesMap);
+        }
+
+        return true;
+    }
+
+    private Map calculateUsesDependencies(
+        Map resolverMap, Module module, R4Package exportPkg, Map usesMap)
+    {
+// TODO: CAN THIS BE OPTIMIZED?
+// TODO: IS THIS CYCLE CHECK CORRECT??
+// TODO: WHAT HAPPENS THERE ARE OVERLAPS WHEN CALCULATING USES??
+//       MAKE AN EXAMPLE WHERE TWO DEPENDENCIES PROVIDE SAME PACKAGE.
+        // Make sure we are not in a cycle.
+        if (usesMap.get(exportPkg.getId()) != null)
+        {
+            return usesMap;
+        }
+
+        // The target package at least uses itself,
+        // so add it to the uses map.
+        usesMap.put(exportPkg.getId(), module);
+
+        // Get the "uses" constraints for the target export
+        // package and calculate the transitive uses constraints
+        // of any used packages.
+        String[] uses = exportPkg.getUses();
+        List nodeList = (List) resolverMap.get(module);
+
+        // We need to walk the transitive closure of "uses" relationships
+        // for the current export package to calculate the entire set of
+        // "uses" constraints.
+        for (int usesIdx = 0; usesIdx < uses.length; usesIdx++)
+        {
+            // There are two possibilities at this point: 1) we are dealing
+            // with an already resolved bundle or 2) we are dealing with a
+            // bundle that has not yet been resolved. In case 1, there will
+            // be no resolver node in the resolver map, so we just need to
+            // examine the bundle directly to determine its exact constraints.
+            // In case 2, there will be a resolver node in the resolver map,
+            // so we will use that to determine the potential constraints of
+            // potential candidate for resolving the import.
+
+            // This is case 1, described in the comment above.
+            if (nodeList == null)
+            {
+                // Get the actual exporter from the wire or if there
+                // is no wire, then get the export is from the module
+                // itself.
+                R4Wire wire = getWire(module, uses[usesIdx]);
+                if (wire != null)
+                {
+                    usesMap = calculateUsesDependencies(
+                        resolverMap, wire.m_module, wire.m_pkg, usesMap);
+                }
+                else
+                {
+                    exportPkg = getExportPackage(module, uses[usesIdx]);
+                    if (exportPkg != null)
+                    {
+                        usesMap = calculateUsesDependencies(
+                            resolverMap, module, exportPkg, usesMap);
+                    }
+                }
+            }
+            // This is case 2, described in the comment above.
+            else
+            {
+                // First, get the resolver node for the "used" package.
+                ResolverNode node = null;
+                for (int nodeIdx = 0;
+                    (node == null) && (nodeIdx < nodeList.size());
+                    nodeIdx++)
+                {
+                    node = (ResolverNode) nodeList.get(nodeIdx);
+                    if (!node.m_pkg.getId().equals(uses[usesIdx]))
+                    {
+                        node = null;
+                    }
+                }
+    
+                // If there is a resolver node for the "used" package,
+                // then this means that the module imports the package
+                // and we need to recursively add the constraints of
+                // the potential exporting module.
+                if (node != null)
+                {
+                    usesMap = calculateUsesDependencies(
+                        resolverMap,
+                        node.m_candidates[node.m_idx],
+                        getExportPackage(node.m_candidates[node.m_idx], node.m_pkg.getId()),
+                        usesMap);
+                }
+                // If there was no resolver node for the "used" package,
+                // then this means that the module exports the package
+                // and we need to recursively add the constraints of this
+                // other exported package of this module.
+                else if (getExportPackage(module, uses[usesIdx]) != null)
+                {
+                    usesMap = calculateUsesDependencies(
+                        resolverMap,
+                        module,
+                        getExportPackage(module, uses[usesIdx]),
+                        usesMap);
+                }
+            }
+        }
+
+        return usesMap;
+    }
+
+    /**
+     * <p>
+     * This method <i>increments</i> the current candidate configuration
+     * in the specified resolver list, which contains resolver node lists
+     * for all of the candidates for all of the imports that need to be
+     * resolved. This method performs its function by treating the current
+     * candidate index variable in each resolver node as part of a big
+     * counter. In other words, it increments the least significant index.
+     * If the index overflows it sets it back to zero and carries the
+     * overflow to the next significant index and so on. Using this approach
+     * it checks every possible combination for a solution.
+     * </p>
+     * <p>
+     * This method is inefficient and a better approach is necessary. For
+     * example, it does not take into account which imports are actually
+     * being used, it just increments starting at the beginning of the list.
+     * This means that it could be modifying candidates that are not relevant
+     * to the current configuration and re-testing even though nothing has
+     * really changed. It needs to be smarter.
+     * </p>
+     * @param resolverList an ordered list of resolver node lists for all
+     *        the candidates of the potential imports that need to be
+     *        resolved.
+     * @throws ResolveException if the increment overflows the entire list,
+     *         signifying no consistent configurations exist.
+    **/
+    private void incrementCandidateConfiguration(List resolverList)
+        throws ResolveException
+    {
+        for (int i = 0; i < resolverList.size(); i++)
+        {
+            List nodeList = (List) resolverList.get(i);
+            for (int j = 0; j < nodeList.size(); j++)
+            {
+                ResolverNode node = (ResolverNode) nodeList.get(j);
+                // See if we can increment the node, without overflowing
+                // the candidate array bounds.
+                if ((node.m_idx + 1) < node.m_candidates.length)
+                {
+                    node.m_idx++;
+                    return;
+                }
+                // If the index will overflow the candidate array bounds,
+                // then set the index back to zero and try to increment
+                // the next candidate.
+                else
+                {
+                    node.m_idx = 0;
+                }
+            }
+        }
+        throw new ResolveException(
+            "Unable to resolve due to constraint violation.", null, null);
+    }
+
+    private Map createWires(Map resolverMap, Module rootModule)
+    {
+        Map resolvedModuleWireMap =
+            populateWireMap(resolverMap, rootModule, new HashMap());
+        Iterator iter = resolvedModuleWireMap.entrySet().iterator();
+        while (iter.hasNext())
+        {
+            Map.Entry entry = (Map.Entry) iter.next();
+            Module module = (Module) entry.getKey();
+            R4Wire[] wires = (R4Wire[]) entry.getValue();
+
+            // Set the module's resolved and wiring attribute.
+            module.setAttribute(RESOLVED_ATTR, Boolean.TRUE);
+            // Only add wires attribute if some exist; export
+            // only modules may not have wires.
+            if (wires.length > 0)
+            {
+                module.setAttribute(WIRING_ATTR, wires);
+            }
+
+            // Remove the wire's exporting module from the "available"
+            // package map and put it into the "in use" package map;
+            // these steps may be a no-op.
+            for (int wireIdx = 0;
+                (wires != null) && (wireIdx < wires.length);
+                wireIdx++)
+            {
+m_logger.log(LogWrapper.LOG_DEBUG, "WIRE: [" + module + "] " + wires[wireIdx]);
+                // First remove the wire module from "available" package map.
+                Module[] modules = (Module[]) m_availPkgMap.get(wires[wireIdx].m_pkg.getId());
+                modules = removeModuleFromArray(modules, wires[wireIdx].m_module);
+                m_availPkgMap.put(wires[wireIdx].m_pkg.getId(), modules);
+
+                // Also remove any exported packages from the "available"
+                // package map that are from the module associated with
+                // the current wires where the exported packages were not
+                // actually exported; an export may not be exported if
+                // the module also imports the same package and was wired
+                // to a different module. If the exported package is not
+                // actually exported, then we just want to remove it
+                // completely, since it cannot be used.
+                if (wires[wireIdx].m_module != module)
+                {
+                    modules = (Module[]) m_availPkgMap.get(wires[wireIdx].m_pkg.getId());
+                    modules = removeModuleFromArray(modules, module);
+                    m_availPkgMap.put(wires[wireIdx].m_pkg.getId(), modules);
+                }
+
+                // Add the module of the wire to the "in use" package map.
+                modules = (Module[]) m_inUsePkgMap.get(wires[wireIdx].m_pkg.getId());
+                modules = addModuleToArray(modules, wires[wireIdx].m_module);
+                m_inUsePkgMap.put(wires[wireIdx].m_pkg.getId(), modules);
+            }
+        }
+        return resolvedModuleWireMap;
+    }
+
+    private Map populateWireMap(Map resolverMap, Module module, Map wireMap)
+    {
+        // If the module is already resolved or it is part of
+        // a cycle, then just return the wire map.
+        if (getResolvedAttr(module).booleanValue() ||
+            (wireMap.get(module) != null))
+        {
+            return wireMap;
+        }
+
+        List nodeList = (List) resolverMap.get(module);
+        R4Wire[] wires = new R4Wire[nodeList.size()];
+
+        // Put the module in the wireMap with an empty wire array;
+        // we do this early so we can use it to detect cycles.
+        wireMap.put(module, wires);
+
+        // Loop through each resolver node and create a wire
+        // for the selected candidate for the associated import.
+        for (int nodeIdx = 0; nodeIdx < nodeList.size(); nodeIdx++)
+        {
+            // Get the import's associated resolver node.
+            ResolverNode node = (ResolverNode) nodeList.get(nodeIdx);
+
+            // Add the candidate to the list of wires.
+            R4Package exportPkg =
+                getExportPackage(node.m_candidates[node.m_idx], node.m_pkg.getId());
+            wires[nodeIdx] = new R4Wire(exportPkg, node.m_candidates[node.m_idx]);
+
+            // Create the wires for the selected candidate module.
+            wireMap = populateWireMap(resolverMap, node.m_candidates[node.m_idx], wireMap);
+        }
+
+        return wireMap;
+    }
+
+// TODO: REMOVE THESE DEBUG METHODS.
+    private void dumpResolverMap(Map resolverMap)
+    {
+        Iterator iter = resolverMap.entrySet().iterator();
+        while (iter.hasNext())
+        {
+            Map.Entry entry = (Map.Entry) iter.next();
+            ResolverNode node = (ResolverNode) entry.getValue();
+            System.out.println("MODULE " + node.m_module + " IMPORT " + node.m_pkg);
+            for (int i = 0; i < node.m_candidates.length; i++)
+            {
+                System.out.println("--> " + node.m_candidates[i]);
+            }
+        }
+    }
+
+    private void dumpAvailablePackages()
+    {
+        synchronized (m_mgr)
+        {
+            System.out.println("AVAILABLE PACKAGES:");
+            for (Iterator i = m_availPkgMap.entrySet().iterator(); i.hasNext(); )
+            {
+                Map.Entry entry = (Map.Entry) i.next();
+                System.out.println("  " + entry.getKey());
+                Module[] modules = (Module[]) entry.getValue();
+                for (int j = 0; j < modules.length; j++)
+                {
+                    System.out.println("    " + modules[j]);
+                }
+            }
+        }
+    }
+
+    private void dumpUsedPackages()
+    {
+        synchronized (m_mgr)
+        {
+            System.out.println("USED PACKAGES:");
+            for (Iterator i = m_inUsePkgMap.entrySet().iterator(); i.hasNext(); )
+            {
+                Map.Entry entry = (Map.Entry) i.next();
+                System.out.println("  " + entry.getKey());
+                Module[] modules = (Module[]) entry.getValue();
+                for (int j = 0; j < modules.length; j++)
+                {
+                    System.out.println("    " + modules[j]);
+                }
+            }
+        }
+    }
+
+    /**
+     * This method returns a list of modules that have an export
+     * that is compatible with the given import identifier and version.
+     * @param pkgMap a map of export packages to exporting modules.
+     * @param target the target import package.
+     * @return an array of modules that have compatible exports or <tt>null</tt>
+     *         if none are found.
+    **/
+    protected Module[] getCompatibleExporters(Module[] modules, R4Package target)
+    {
+        // Create list of compatible exporters.
+        Module[] candidates = null;
+        for (int modIdx = 0; (modules != null) && (modIdx < modules.length); modIdx++)
+        {
+            // Get the modules export package for the target package.
+            R4Package exportPkg = getExportPackage(modules[modIdx], target.getId());
+            // If compatible, then add the candidate to the list.
+            if ((exportPkg != null) && (exportPkg.doesSatisfy(target)))
+            {
+                candidates = addModuleToArray(candidates, modules[modIdx]);
+            }
+        }
+
+        if (candidates == null)
+        {
+            return m_emptyModules;
+        }
+
+        return candidates;
+    }
+
+    public void moduleAdded(ModuleEvent event)
+    {
+        // When a module is added to the system, we need to initialize
+        // its resolved and wiring attributes and add its exports to
+        // the map of available exports.
+
+        // Synchronize on the module manager, since we don't want any
+        // bundles to be installed or removed.
+        synchronized (m_mgr)
+        {
+            // Add wiring attribute.
+            event.getModule().setAttribute(WIRING_ATTR, null);
+            // Add resolved attribute.
+            event.getModule().setAttribute(RESOLVED_ATTR, Boolean.FALSE);
+            // Add exports to available package map.
+            R4Package[] exports = getExportsAttr(event.getModule());
+            for (int i = 0; i < exports.length; i++)
+            {
+                Module[] modules = (Module[]) m_availPkgMap.get(exports[i].getId());
+
+                // We want to add the module into the list of available
+                // exporters in sorted order (descending version and
+                // ascending bundle identifier). Insert using a simple
+                // binary search algorithm.
+                if (modules == null)
+                {
+                    modules = new Module[] { event.getModule() };
+                }
+                else
+                {
+                    int top = 0, bottom = modules.length - 1, middle = 0;
+                    R4Version middleVersion = null;
+                    while (top <= bottom)
+                    {
+                        middle = (bottom - top) / 2 + top;
+                        middleVersion = getExportPackage(
+                            modules[middle], exports[i].getId()).getVersionLow();
+                        // Sort in reverse version order.
+                        int cmp = middleVersion.compareTo(exports[i].getVersionLow());
+                        if (cmp < 0)
+                        {
+                            bottom = middle - 1;
+                        }
+                        else if (cmp == 0)
+                        {
+                            // Sort further by ascending bundle ID.
+                            long middleId = getBundleIdFromModuleId(modules[middle].getId());
+                            long exportId = getBundleIdFromModuleId(event.getModule().getId());
+                            if (middleId < exportId)
+                            {
+                                top = middle + 1;
+                            }
+                            else
+                            {
+                                bottom = middle - 1;
+                            }
+                        }
+                        else
+                        {
+                            top = middle + 1;
+                        }
+                    }
+
+                    Module[] newMods = new Module[modules.length + 1];
+                    System.arraycopy(modules, 0, newMods, 0, top);
+                    System.arraycopy(modules, top, newMods, top + 1, modules.length - top);
+                    newMods[top] = event.getModule();
+                    modules = newMods;
+                }
+
+                m_availPkgMap.put(exports[i].getId(), modules);
+            }
+        }
+    }
+
+    public void moduleReset(ModuleEvent event)
+    {
+        moduleRemoved(event);
+    }
+
+    public void moduleRemoved(ModuleEvent event)
+    {
+        // When a module is removed from the system, we need remove
+        // its exports from the "in use" and "available" package maps.
+
+        // Synchronize on the module manager, since we don't want any
+        // bundles to be installed or removed.
+        synchronized (m_mgr)
+        {
+            // Remove exports from package maps.
+            R4Package[] pkgs = getExportsAttr(event.getModule());
+            for (int i = 0; i < pkgs.length; i++)
+            {
+                // Remove from "available" package map.
+                Module[] modules = (Module[]) m_availPkgMap.get(pkgs[i].getId());
+                if (modules != null)
+                {
+                    modules = removeModuleFromArray(modules, event.getModule());
+                    m_availPkgMap.put(pkgs[i].getId(), modules);
+                }
+                // Remove from "in use" package map.
+                modules = (Module[]) m_inUsePkgMap.get(pkgs[i].getId());
+                if (modules != null)
+                {
+                    modules = removeModuleFromArray(modules, event.getModule());
+                    m_inUsePkgMap.put(pkgs[i].getId(), modules);
+                }
+            }
+        }
+    }
+
+    // This is duplicated from BundleInfo and probably shouldn't be,
+    // but its functionality is needed by the moduleAdded() callback.
+    protected static long getBundleIdFromModuleId(String id)
+    {
+        try
+        {
+            String bundleId = (id.indexOf('.') >= 0)
+                ? id.substring(0, id.indexOf('.')) : id;
+            return Long.parseLong(bundleId);
+        }
+        catch (NumberFormatException ex)
+        {
+            return -1;
+        }
+    }
+
+    //
+    // Event handling methods for validation events.
+    //
+
+    /**
+     * Adds a resolver listener to the search policy. Resolver
+     * listeners are notified when a module is resolve and/or unresolved
+     * by the search policy.
+     * @param l the resolver listener to add.
+    **/
+    public void addResolverListener(ResolveListener l)
+    {
+        // Verify listener.
+        if (l == null)
+        {
+            throw new IllegalArgumentException("Listener is null");
+        }
+
+        // Use the m_noListeners object as a lock.
+        synchronized (m_emptyListeners)
+        {
+            // If we have no listeners, then just add the new listener.
+            if (m_listeners == m_emptyListeners)
+            {
+                m_listeners = new ResolveListener[] { l };
+            }
+            // Otherwise, we need to do some array copying.
+            // Notice, the old array is always valid, so if
+            // the dispatch thread is in the middle of a dispatch,
+            // then it has a reference to the old listener array
+            // and is not affected by the new value.
+            else
+            {
+                ResolveListener[] newList = new ResolveListener[m_listeners.length + 1];
+                System.arraycopy(m_listeners, 0, newList, 0, m_listeners.length);
+                newList[m_listeners.length] = l;
+                m_listeners = newList;
+            }
+        }
+    }
+
+    /**
+     * Removes a resolver listener to this search policy.
+     * @param l the resolver listener to remove.
+    **/
+    public void removeResolverListener(ResolveListener l)
+    {
+        // Verify listener.
+        if (l == null)
+        {
+            throw new IllegalArgumentException("Listener is null");
+        }
+
+        // Use the m_emptyListeners object as a lock.
+        synchronized (m_emptyListeners)
+        {
+            // Try to find the instance in our list.
+            int idx = -1;
+            for (int i = 0; i < m_listeners.length; i++)
+            {
+                if (m_listeners[i].equals(l))
+                {
+                    idx = i;
+                    break;
+                }
+            }
+
+            // If we have the instance, then remove it.
+            if (idx >= 0)
+            {
+                // If this is the last listener, then point to empty list.
+                if (m_listeners.length == 1)
+                {
+                    m_listeners = m_emptyListeners;
+                }
+                // Otherwise, we need to do some array copying.
+                // Notice, the old array is always valid, so if
+                // the dispatch thread is in the middle of a dispatch,
+                // then it has a reference to the old listener array
+                // and is not affected by the new value.
+                else
+                {
+                    ResolveListener[] newList = new ResolveListener[m_listeners.length - 1];
+                    System.arraycopy(m_listeners, 0, newList, 0, idx);
+                    if (idx < newList.length)
+                    {
+                        System.arraycopy(m_listeners, idx + 1, newList, idx,
+                            newList.length - idx);
+                    }
+                    m_listeners = newList;
+                }
+            }
+        }
+    }
+
+    /**
+     * Fires a validation event for the specified module.
+     * @param module the module that was resolved.
+    **/
+    protected void fireModuleResolved(Module module)
+    {
+        // Event holder.
+        ModuleEvent event = null;
+
+        // Get a copy of the listener array, which is guaranteed
+        // to not be null.
+        ResolveListener[] listeners = m_listeners;
+
+        // Loop through listeners and fire events.
+        for (int i = 0; i < listeners.length; i++)
+        {
+            // Lazily create event.
+            if (event == null)
+            {
+                event = new ModuleEvent(m_mgr, module);
+            }
+            listeners[i].moduleResolved(event);
+        }
+    }
+
+    /**
+     * Fires an unresolved event for the specified module.
+     * @param module the module that was unresolved.
+    **/
+    protected void fireModuleUnresolved(Module module)
+    {
+        // Event holder.
+        ModuleEvent event = null;
+
+        // Get a copy of the listener array, which is guaranteed
+        // to not be null.
+        ResolveListener[] listeners = m_listeners;
+
+        // Loop through listeners and fire events.
+        for (int i = 0; i < listeners.length; i++)
+        {
+            // Lazily create event.
+            if (event == null)
+            {
+                event = new ModuleEvent(m_mgr, module);
+            }
+            listeners[i].moduleUnresolved(event);
+        }
+    }
+
+    //
+    // Static utility methods.
+    //
+
+    public static Boolean getResolvedAttr(Module m)
+    {
+        Boolean b =
+            (Boolean) m.getAttribute(RESOLVED_ATTR);
+        if (b == null)
+        {
+            b = Boolean.FALSE;
+        }
+        return b;
+    }
+
+    public static R4Package[] getExportsAttr(Module m)
+    {
+        R4Package[] attr =
+            (R4Package[]) m.getAttribute(EXPORTS_ATTR);
+        return (attr == null) ? m_emptyPackages : attr;
+    }
+
+    public static R4Package getExportPackage(Module m, String id)
+    {
+        R4Package[] pkgs = getExportsAttr(m);
+        for (int i = 0; (pkgs != null) && (i < pkgs.length); i++)
+        {
+            if (pkgs[i].getId().equals(id))
+            {
+                return pkgs[i];
+            }
+        }
+        return null;
+    }
+
+    public static R4Package[] getImportsAttr(Module m)
+    {
+        R4Package[] attr =
+            (R4Package[]) m.getAttribute(IMPORTS_ATTR);
+        return (attr == null) ? m_emptyPackages: attr;
+    }
+
+    public static R4Package getImportPackage(Module m, String id)
+    {
+        R4Package[] pkgs = getImportsAttr(m);
+        for (int i = 0; (pkgs != null) && (i < pkgs.length); i++)
+        {
+            if (pkgs[i].getId().equals(id))
+            {
+                return pkgs[i];
+            }
+        }
+        return null;
+    }
+
+    public static R4Package[] getDynamicImportsAttr(Module m)
+    {
+        R4Package[] attr =
+            (R4Package[]) m.getAttribute(DYNAMICIMPORTS_ATTR);
+        return (attr == null) ? m_emptyPackages: attr;
+    }
+
+    public static R4Package getDynamicImportPackage(Module m, String id)
+    {
+        R4Package[] pkgs = getDynamicImportsAttr(m);
+        for (int i = 0; (pkgs != null) && (i < pkgs.length); i++)
+        {
+            if (pkgs[i].getId().equals(id))
+            {
+                return pkgs[i];
+            }
+        }
+        return null;
+    }
+
+    public static R4Wire[] getWiringAttr(Module m)
+    {
+        R4Wire[] attr =
+            (R4Wire[]) m.getAttribute(WIRING_ATTR);
+        if (attr == null)
+        {
+            attr = m_emptyWires;
+        }
+        return attr;
+    }
+
+    public static R4Wire getWire(Module m, String id)
+    {
+        R4Wire[] wires = getWiringAttr(m);
+        for (int i = 0; (wires != null) && (i < wires.length); i++)
+        {
+            if (wires[i].m_pkg.getId().equals(id))
+            {
+                return wires[i];
+            }
+        }
+        return null;
+    }
+
+    public static boolean isModuleInArray(Module[] modules, Module m)
+    {
+        // Verify that the module is not already in the array.
+        for (int i = 0; (modules != null) && (i < modules.length); i++)
+        {
+            if (modules[i] == m)
+            {
+                return true;
+            }
+        }
+        
+        return false;
+    }
+
+    public static Module[] addModuleToArray(Module[] modules, Module m)
+    {
+        // Verify that the module is not already in the array.
+        for (int i = 0; (modules != null) && (i < modules.length); i++)
+        {
+            if (modules[i] == m)
+            {
+                return modules;
+            }
+        }
+
+        if (modules != null)
+        {
+            Module[] newModules = new Module[modules.length + 1];
+            System.arraycopy(modules, 0, newModules, 0, modules.length);
+            newModules[modules.length] = m;
+            modules = newModules;
+        }
+        else
+        {
+            modules = new Module[] { m };
+        }
+
+        return modules;
+    }
+
+    public static Module[] removeModuleFromArray(Module[] modules, Module m)
+    {
+        if (modules == null)
+        {
+            return m_emptyModules;
+        }
+
+        int idx = -1;
+        for (int i = 0; i < modules.length; i++)
+        {
+            if (modules[i] == m)
+            {
+                idx = i;
+                break;
+            }
+        }
+
+        if (idx >= 0)
+        {
+            // If this is the module, then point to empty list.
+            if ((modules.length - 1) == 0)
+            {
+                modules = m_emptyModules;
+            }
+            // Otherwise, we need to do some array copying.
+            else
+            {
+                Module[] newModules= new Module[modules.length - 1];
+                System.arraycopy(modules, 0, newModules, 0, idx);
+                if (idx < newModules.length)
+                {
+                    System.arraycopy(
+                        modules, idx + 1, newModules, idx, newModules.length - idx);
+                }
+                modules = newModules;
+            }
+        }
+        return modules;
+    }
+
+// TODO: INVESTIGATE GENERIC ARRAY GROWING/SHRINKING.
+    private static R4Wire[] shrinkWireArray(R4Wire[] wires)
+    {
+        if (wires == null)
+        {
+            return m_emptyWires;
+        }
+
+        int count = 0;
+        for (int i = 0; i < wires.length; i++)
+        {
+            if (wires[i] == null)
+            {
+                count++;
+            }
+        }
+
+        if (count > 0)
+        {
+            R4Wire[] newWires = new R4Wire[wires.length - count];
+            count = 0;
+            for (int i = 0; i < wires.length; i++)
+            {
+                if (wires[i] != null)
+                {
+                    newWires[count++] = wires[i];
+                }
+            }
+            wires = newWires;
+        }
+
+        return wires;
+    }
+
+    private static Module[] shrinkModuleArray(Module[] modules)
+    {
+        if (modules == null)
+        {
+            return m_emptyModules;
+        }
+
+        int count = 0;
+        for (int i = 0; i < modules.length; i++)
+        {
+            if (modules[i] == null)
+            {
+                count++;
+            }
+        }
+
+        if (count > 0)
+        {
+            Module[] newModules = new Module[modules.length - count];
+            count = 0;
+            for (int i = 0; i < modules.length; i++)
+            {
+                if (modules[i] != null)
+                {
+                    newModules[count++] = modules[i];
+                }
+            }
+            modules = newModules;
+        }
+
+        return modules;
+    }
+
+    private static class ResolverNode
+    {
+        public Module m_module = null;
+        public R4Package m_pkg = null;
+        public Module[] m_candidates = null;
+        public int m_idx = 0;
+        public boolean m_visited = false;
+        public ResolverNode(Module module, R4Package pkg, Module[] candidates)
+        {
+            m_module = module;
+            m_pkg = pkg;
+            m_candidates = candidates;
+            if (getResolvedAttr(m_module).booleanValue())
+            {
+                m_visited = true;
+            }
+        }
+    }
+
+    // Utility class to get the class context from the security manager.
+    private static class SecurityManagerX extends SecurityManager
+    {
+        public Class[] getClassContext()
+        {
+            return super.getClassContext();
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/searchpolicy/R4Version.java b/src/org/apache/osgi/framework/searchpolicy/R4Version.java
new file mode 100644
index 0000000..e7f11fc
--- /dev/null
+++ b/src/org/apache/osgi/framework/searchpolicy/R4Version.java
@@ -0,0 +1,190 @@
+/*
+ *   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.osgi.framework.searchpolicy;
+
+import java.util.StringTokenizer;
+
+public class R4Version implements Comparable
+{
+    private int m_major = 0;
+    private int m_minor = 0;
+    private int m_micro = 0;
+    private String m_qualifier = "";
+    private boolean m_isInclusive = true;
+
+    private static final String SEPARATOR = ".";
+
+    public R4Version(String versionString)
+    {
+        this(versionString, true);
+    }
+
+    public R4Version(String versionString, boolean isInclusive)
+    {
+        if (versionString == null)
+        {
+            versionString = "0.0.0";
+        }
+        Object[] objs = parseVersion(versionString);
+        m_major = ((Integer) objs[0]).intValue();
+        m_minor = ((Integer) objs[1]).intValue();
+        m_micro = ((Integer) objs[2]).intValue();
+        m_qualifier = (String) objs[3];
+        m_isInclusive = isInclusive;
+    }
+
+    private static Object[] parseVersion(String versionString)
+    {
+        String s = versionString.trim();
+        Object[] objs = new Object[4];
+        objs[0] = objs[1] = objs[2] = new Integer(0);
+        objs[3] = "";
+        StringTokenizer tok = new StringTokenizer(s, SEPARATOR);
+        try
+        {
+            objs[0] = Integer.valueOf(tok.nextToken());
+            if (tok.hasMoreTokens())
+            {
+                objs[1] = Integer.valueOf(tok.nextToken());
+                if (tok.hasMoreTokens())
+                {
+                    objs[2] = Integer.valueOf(tok.nextToken());
+                    if (tok.hasMoreTokens())
+                    {
+                        objs[3] = tok.nextToken();
+                    }
+                }
+            }
+        }
+        catch (NumberFormatException ex)
+        {
+            throw new IllegalArgumentException("Invalid version: " + versionString);
+        }
+
+        if ((((Integer) objs[0]).intValue() < 0) ||
+            (((Integer) objs[0]).intValue() < 0) ||
+            (((Integer) objs[0]).intValue() < 0))
+        {
+            throw new IllegalArgumentException("Invalid version: " + versionString);
+        }
+
+        return objs;
+    }
+
+    public boolean equals(Object object)
+    {
+        if (!(object instanceof R4Version))
+        {
+            return false;
+        }
+        R4Version v = (R4Version) object;
+        return
+            (v.getMajorComponent() == m_major) &&
+            (v.getMinorComponent() == m_minor) &&
+            (v.getMicroComponent() == m_micro) &&
+            (v.getQualifierComponent().equals(m_qualifier));
+    }
+
+    public int getMajorComponent()
+    {
+        return m_major;
+    }
+
+    public int getMinorComponent()
+    {
+        return m_minor;
+    }
+
+    public int getMicroComponent()
+    {
+        return m_micro;
+    }
+
+    public String getQualifierComponent()
+    {
+        return m_qualifier;
+    }
+
+    public boolean isInclusive()
+    {
+        return m_isInclusive;
+    }
+
+    public int compareTo(Object o)
+    {
+        if (!(o instanceof R4Version))
+            throw new ClassCastException();
+
+        if (equals(o))
+            return 0;
+
+        if (isGreaterThan((R4Version) o))
+            return 1;
+
+        return -1;
+    }
+
+    public boolean isGreaterThan(R4Version v)
+    {
+        if (v == null)
+        {
+            return false;
+        }
+
+        if (m_major > v.getMajorComponent())
+        {
+            return true;
+        }
+        if (m_major < v.getMajorComponent())
+        {
+            return false;
+        }
+        if (m_minor > v.getMinorComponent())
+        {
+            return true;
+        }
+        if (m_minor < v.getMinorComponent())
+        {
+            return false;
+        }
+        if (m_micro > v.getMicroComponent())
+        {
+            return true;
+        }
+        if (m_micro < v.getMicroComponent())
+        {
+            return false;
+        }
+        if (m_qualifier.compareTo(v.getQualifierComponent()) > 0)
+        {
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public String toString()
+    {
+        if (m_qualifier.length() == 0)
+        {
+            return m_major + "." + m_minor + "." + m_micro; 
+        }
+        return m_major + "." + m_minor + "." + m_micro + "." + m_qualifier; 
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/searchpolicy/R4Wire.java b/src/org/apache/osgi/framework/searchpolicy/R4Wire.java
new file mode 100755
index 0000000..52ab909
--- /dev/null
+++ b/src/org/apache/osgi/framework/searchpolicy/R4Wire.java
@@ -0,0 +1,36 @@
+/*
+ *   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.osgi.framework.searchpolicy;
+
+import org.apache.osgi.moduleloader.Module;
+
+public class R4Wire
+{
+    public R4Package m_pkg = null;
+    public Module m_module = null;
+    
+    public R4Wire(R4Package pkg, Module module)
+    {
+        m_pkg = pkg;
+        m_module = module;
+    }
+    
+    public String toString()
+    {
+        return m_pkg.getId() + " -> " + m_module;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/BundleListenerWrapper.java b/src/org/apache/osgi/framework/util/BundleListenerWrapper.java
new file mode 100644
index 0000000..033bac6
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/BundleListenerWrapper.java
@@ -0,0 +1,65 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.osgi.framework.*;
+
+public class BundleListenerWrapper extends ListenerWrapper implements BundleListener
+{
+    public BundleListenerWrapper(Bundle bundle, BundleListener l)
+    {
+        super(bundle,
+            (l instanceof SynchronousBundleListener)
+                ? SynchronousBundleListener.class : BundleListener.class,
+            l);
+    }
+
+    public void bundleChanged(final BundleEvent event)
+    {
+        // A bundle listener is either synchronous or asynchronous.
+        // If the bundle listener is synchronous, then deliver the
+        // event to bundles with a state of STARTING, STOPPING, or
+        // ACTIVE. If the listener is asynchronous, then deliver the
+        // event only to bundles that are STARTING or ACTIVE.
+        if (((getListenerClass() == SynchronousBundleListener.class) &&
+            ((getBundle().getState() == Bundle.STARTING) ||
+            (getBundle().getState() == Bundle.STOPPING) ||
+            (getBundle().getState() == Bundle.ACTIVE)))
+            ||
+            ((getBundle().getState() == Bundle.STARTING) ||
+            (getBundle().getState() == Bundle.ACTIVE)))
+        {
+            if (System.getSecurityManager() != null)
+            {
+                AccessController.doPrivileged(new PrivilegedAction() {
+                    public Object run()
+                    {
+                        ((BundleListener) getListener()).bundleChanged(event);
+                        return null;
+                    }
+                });
+            }
+            else
+            {
+                ((BundleListener) getListener()).bundleChanged(event);
+            }
+        }
+    }
+}
diff --git a/src/org/apache/osgi/framework/util/CaseInsensitiveMap.java b/src/org/apache/osgi/framework/util/CaseInsensitiveMap.java
new file mode 100644
index 0000000..6d805aa
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/CaseInsensitiveMap.java
@@ -0,0 +1,50 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.*;
+
+/**
+ * Simple utility class that creates a case-insensitive map by
+ * extending <tt>TreeMap</tt> and to use a case-insensitive
+ * comparator. Any keys put into this map will be converted to
+ * a <tt>String</tt> using the <tt>toString()</tt> method,
+ * since it is intended to compare strings.
+**/
+public class CaseInsensitiveMap extends TreeMap
+{
+    public CaseInsensitiveMap()
+    {
+        super(new Comparator() {
+            public int compare(Object o1, Object o2)
+            {
+                return o1.toString().compareToIgnoreCase(o2.toString());
+            }
+        });
+    }
+    
+    public CaseInsensitiveMap(Map map)
+    {
+        this();
+        putAll(map);
+    }
+    
+    public Object put(Object key, Object value)
+    {
+        return super.put(key.toString(), value);
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/DispatchQueue.java b/src/org/apache/osgi/framework/util/DispatchQueue.java
new file mode 100644
index 0000000..9d70a9c
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/DispatchQueue.java
@@ -0,0 +1,420 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.*;
+
+import org.apache.osgi.framework.LogWrapper;
+
+/**
+ * This class implements an event dispatching queue to simplify delivering
+ * events to a list of event listener. To use this class, simply create an
+ * instance and use it to keep track of your event listeners, much like
+ * <tt>javax.swing.event.EventListenerList</tt>. To dispatch an event,
+ * simply create an instance of a <tt>Dispatcher</tt> and pass the instance
+ * to <tt>DispatchQueue.dispatch()</tt> method, for example:
+ * <pre>
+ *  Dispatcher d = new Dispatcher() {
+ *      public void dispatch(EventListener l, Object eventObj)
+ *      {
+ *          ((FooListener) l).fooXXX((FooEvent) eventObj);
+ *      }
+ *  };
+ *  FooEvent event = new FooEvent(this);
+ *  dispatchQueue.dispatch(d, FooListener.class, event);
+ * </pre>
+ * In the above code substitute a specific listener and event for the
+ * <tt>Foo</tt> listener and event. Since <tt>Dispatcher</tt>s are
+ * reusable, it is probably a good idea to create one for each type of
+ * event to be delivered and just reuse them everytime to avoid unnecessary
+ * memory allocation.
+ * <p>
+ * Currently, the <tt>DispatchQueue</tt> creates an internal thread with
+ * which all events are delivered; this means that events are never delivered
+ * using the caller's thread.
+**/
+public class DispatchQueue
+{
+    // Representation of an empty listener list.
+    private static final Object[] m_emptyList = new Object[0];
+
+    // The event listeners for a particular queue instance.
+    private Object[] m_listeners = m_emptyList;
+
+    // A single thread is used to deliver events for all dispatchers.
+    private static Thread m_thread = null;
+    private static String m_threadLock = "thread lock";
+    private static boolean m_stopping = false;
+    private static boolean m_stopped = false;
+
+    // List of dispatch requests.
+    private static final ArrayList m_requestList = new ArrayList();
+    // Cached dispatch requests to avoid memory allocation.
+    private static final ArrayList m_requestCache = new ArrayList();
+    // The logger for dispatch queue.
+    private static LogWrapper m_logger = null;
+
+    /**
+     * Constructs a dispatch queue and starts a dispather thread if
+     * necessary.
+    **/
+    public DispatchQueue(LogWrapper logger)
+    {
+        synchronized (m_threadLock)
+        {
+            // Start event dispatching thread if necessary.
+            if (m_thread == null)
+            {
+                m_logger = logger;
+                m_thread = new Thread(new Runnable() {
+                    public void run()
+                    {
+                        DispatchQueue.run();
+                    }
+                }, "FelixDispatchQueue");
+                m_thread.start();
+            }
+        }
+    }
+
+    /**
+     * Terminates the dispatching thread for a graceful shutdown
+     * of the dispatching queue; the caller will block until the
+     * dispatching thread has completed all pending dispatches.
+     * Since there is only one thread per all instances of
+     * <tt>DispatchQueue</tt>, this method should only be called
+     * prior to exiting the JVM.
+    **/
+    public static void shutdown()
+    {
+        synchronized (m_threadLock)
+        {
+            // Return if already stopped.
+            if (m_stopped)
+            {
+                return;
+            }
+
+            // Signal dispatch thread.
+            m_stopping = true;
+            synchronized (m_requestList)
+            {
+                m_requestList.notify();
+            }
+
+            // Wait for dispatch thread to stop.
+            while (!m_stopped)
+            {
+                try {
+                    m_threadLock.wait();
+                } catch (InterruptedException ex) {
+                }
+            }
+        }
+    }
+
+    public static LogWrapper getLogger()
+    {
+        return m_logger;
+    }
+
+    /**
+     * Returns a pointer to the array of event listeners. The array stores pairs
+     * of associated <tt>Class</tt> and <tt>EventListener</tt> objects; a pair
+     * corresponds to the arguments passed in to the <tt>addListener()</tt> method.
+     * Even numbered array elements are the class object and odd numbered elements
+     * are the corresponding event listener instance.
+     *
+     * @return guaranteed to return a non-null object array.
+    **/
+    public Object[] getListeners()
+    {
+        return m_listeners;
+    }
+
+    /**
+     * Returns the listener if it is already in the dispatch queue.
+     * @param clazz the class of the listener to find.
+     * @param l the listener instance to find.
+     * @return the listener instance or <tt>null</tt> if the listener was
+     *         not found.
+    **/
+    public EventListener getListener(Class clazz, EventListener l)
+    {
+        // Verify listener.
+        if (l == null)
+        {
+            throw new IllegalArgumentException("Listener is null");
+        }
+        else if (!clazz.isInstance(l))
+        {
+            throw new IllegalArgumentException(
+                "Listener not of type " + clazz.getName());
+        }
+
+        // Lock the object.
+        synchronized (this)
+        {
+            // Try to find the instance in our list.
+            for (int i = 0; i < m_listeners.length; i += 2)
+            {
+                if ((m_listeners[i] == clazz) &&
+                    (m_listeners[i + 1].equals(l)))
+                {
+                    return (EventListener) m_listeners[i + 1];
+                }
+            }
+        }
+        
+        return null;
+    }
+
+    /**
+     * Adds a listener to the dispatch queue's listener list; the listener
+     * is then able to receive events.
+     *
+     * @param clazz the class object associated with the event listener type.
+     * @param l the instance of the event listener to add.
+    **/
+    public void addListener(Class clazz, EventListener l)
+    {
+        // Verify listener.
+        if (l == null)
+        {
+            throw new IllegalArgumentException("Listener is null");
+        }
+        else if (!clazz.isInstance(l))
+        {
+            throw new IllegalArgumentException(
+                "Listener not of type " + clazz.getName());
+        }
+
+        // Lock the object.
+        synchronized (this)
+        {
+            // If we have no listeners, then just add the new listener.
+            if (m_listeners == m_emptyList)
+            {
+                m_listeners = new Object[] { clazz, l };
+            }
+            // Otherwise, we need to do some array copying.
+            // Notice, the old array is always valid, so if
+            // the dispatch thread is in the middle of a dispatch,
+            // then it has a reference to the old listener array
+            // and is not affected by the new value.
+            else
+            {
+                Object[] newList = new Object[m_listeners.length + 2];
+                System.arraycopy(m_listeners, 0, newList, 0, m_listeners.length);
+                newList[m_listeners.length] = clazz;
+                newList[m_listeners.length + 1] = l;
+                m_listeners = newList;
+            }
+        }
+    }
+
+    /**
+     * Removes a listener from the dispatch queue's listener list; the listener
+     * is no longer able to receive events.
+     *
+     * @param clazz the class object associated with the event listener type.
+     * @param l the instance of the event listener to remove.
+    **/
+    public void removeListener(Class clazz, EventListener l)
+    {
+        // Verify listener.
+        if (l == null)
+        {
+            throw new IllegalArgumentException("Listener is null");
+        }
+        else if (!clazz.isInstance(l))
+        {
+            throw new IllegalArgumentException(
+                "Listener not of type " + clazz.getName());
+        }
+
+        // Lock the object.
+        synchronized (this)
+        {
+            // Try to find the instance in our list.
+            int idx = -1;
+            for (int i = 0; i < m_listeners.length; i += 2)
+            {
+                if ((m_listeners[i] == clazz) &&
+                    (m_listeners[i + 1].equals(l)))
+                {
+                    idx = i;
+                    break;
+                }
+            }
+
+            // If we have the instance, then remove it.
+            if (idx >= 0)
+            {
+                // If this is the last listener, then point to empty list.
+                if ((m_listeners.length - 2) == 0)
+                {
+                    m_listeners = m_emptyList;
+                }
+                // Otherwise, we need to do some array copying.
+                // Notice, the old array is always valid, so if
+                // the dispatch thread is in the middle of a dispatch,
+                // then it has a reference to the old listener array
+                // and is not affected by the new value.
+                else
+                {
+                    Object[] newList  = new Object[m_listeners.length - 2];
+                    System.arraycopy(m_listeners, 0, newList, 0, idx);
+                    if (idx < newList.length)
+                    {
+                        System.arraycopy(m_listeners, idx + 2, newList, idx,
+                            newList.length - idx);
+                    }
+                    m_listeners = newList;
+                }
+            }
+        }
+    }
+
+    /**
+     * Dispatches an event to a set of event listeners using a specified
+     * dispatcher object.
+     *
+     * @param d the dispatcher used to actually dispatch the event; this
+     *          varies according to the type of event listener.
+     * @param clazz the class associated with the target event listener type;
+     *              only event listeners of this type will receive the event.
+     * @param eventObj the actual event object to dispatch.
+    **/
+    public void dispatch(Dispatcher d, Class clazz, EventObject eventObj)
+    {
+        dispatch(m_listeners, d, clazz, eventObj);
+    }
+
+    protected void dispatch(
+        Object[] listeners, Dispatcher d, Class clazz, EventObject eventObj)
+    {
+        // If dispatch thread is stopped, then ignore dispatch request.
+        if (m_stopped)
+        {
+            return;
+        }
+
+        // First get a dispatch request from the cache or
+        // create one if necessary.
+        DispatchRequest dr = null;
+        synchronized (m_requestCache)
+        {
+            if (m_requestCache.size() > 0)
+                dr = (DispatchRequest) m_requestCache.remove(0);
+            else
+                dr = new DispatchRequest();
+        }
+
+        // Initialize dispatch request.
+        dr.m_listeners = listeners;
+        dr.m_dispatcher = d;
+        dr.m_clazz = clazz;
+        dr.m_eventObj = eventObj;
+
+        // Lock the request list.
+        synchronized (m_requestList)
+        {
+            // Add our request to the list.
+            m_requestList.add(dr);
+            // Notify the dispatch thread that there is
+            // work to do.
+            m_requestList.notify();
+        }
+    }
+
+    private static void run()
+    {
+        DispatchRequest dr = null;
+        while (true)
+        {
+            // Lock the request list so we can try to get a
+            // dispatch request from it.
+            synchronized (m_requestList)
+            {
+                // Wait while there are no requests to dispatch. If the
+                // dispatcher thread is supposed to stop, then let the
+                // dispatcher thread exit the loop and stop.
+                while ((m_requestList.size() == 0) && !m_stopping)
+                {
+                    // Wait until some signals us for work.
+                    try {
+                        m_requestList.wait();
+                    } catch (InterruptedException ex) {
+                        m_logger.log(LogWrapper.LOG_ERROR, "DispatchQueue: Thread interrupted.", ex);
+                    }
+                }
+
+                // If there are no events to dispatch and shutdown
+                // has been called then exit, otherwise dispatch event.
+                if ((m_requestList.size() == 0) && (m_stopping))
+                {
+                    synchronized (m_threadLock)
+                    {
+                        m_stopped = true;
+                        m_threadLock.notifyAll();
+                    }
+                    return;
+                }
+
+                // Get the dispatch request.
+                dr = (DispatchRequest) m_requestList.remove(0);
+            }
+
+            // Deliver event outside of synchronized block
+            // so that we don't block other requests from being
+            // queued during event processing.
+
+            // Try to dispatch to the listeners.
+            if (dr.m_listeners.length > 0)
+            {
+                // Notify appropriate listeners.
+                for (int i = dr.m_listeners.length - 2; i >= 0; i -= 2)
+                {
+                    if (dr.m_listeners[i] == dr.m_clazz)
+                    {
+                        try {
+                            dr.m_dispatcher.dispatch(
+                                (EventListener) dr.m_listeners[i + 1], dr.m_eventObj);
+                        } catch (Throwable th) {
+                            m_logger.log(LogWrapper.LOG_ERROR, "DispatchQueue: Error during dispatch.", th);
+                        }
+                    }
+                }
+            }
+
+            // Put dispatch request in cache.
+            synchronized (m_requestCache)
+            {
+                m_requestCache.add(dr);
+            }
+        }
+    }
+
+    private static class DispatchRequest
+    {
+        public Object[] m_listeners = null;
+        public Dispatcher m_dispatcher = null;
+        public Class m_clazz = null;
+        public EventObject m_eventObj = null;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/Dispatcher.java b/src/org/apache/osgi/framework/util/Dispatcher.java
new file mode 100644
index 0000000..a3c0192
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/Dispatcher.java
@@ -0,0 +1,51 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.EventListener;
+import java.util.EventObject;
+
+/**
+ * This interface is used by <tt>DispatchQueue</tt> to dispatch events.
+ * Generally speaking, each type of event to dispatch will have an instance
+ * of a <tt>Dispatcher</tt> so that the dispatch queue can dispatch to
+ * the appropriate listener method for the specific listener type,
+ * for example:
+ * <pre>
+ *  Dispatcher d = new Dispatcher() {
+ *      public void dispatch(EventListener l, EventObject eventObj)
+ *      {
+ *          ((FooListener) l).fooXXX((FooEvent) eventObj);
+ *      }
+ *  };
+ *  FooEvent event = new FooEvent(this);
+ *  dispatchQueue.dispatch(d, FooListener.class, event);
+ * </pre>
+ * In the above code substitute a specific listener and event for the
+ * <tt>Foo</tt> listener and event. <tt>Dispatcher</tt>s can be reused, so
+ * it is a good idea to cache them to avoid unnecessary memory allocations.
+**/
+public interface Dispatcher
+{
+    /**
+     * Dispatch an event to a specified event listener.
+     *
+     * @param l the event listener to receive the event.
+     * @param eventObj the event to dispatch.
+    **/
+    public void dispatch(EventListener l, EventObject eventObj);
+}
diff --git a/src/org/apache/osgi/framework/util/FelixConstants.java b/src/org/apache/osgi/framework/util/FelixConstants.java
new file mode 100644
index 0000000..a98e4e3
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/FelixConstants.java
@@ -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.osgi.framework.util;
+
+public interface FelixConstants extends org.osgi.framework.Constants
+{
+    // Framework constants and values.
+    public static final String FRAMEWORK_VERSION_VALUE = "4.0";
+    public static final String FRAMEWORK_VENDOR_VALUE = "Apache";
+
+    // Framework constants and values.
+    public static final String FELIX_VERSION_PROPERTY = "felix.version";
+    public static final String FELIX_VERSION_VALUE = "2.0.0.alpha8";
+
+    // Miscellaneous manifest constants.
+    public static final String DIRECTIVE_SEPARATOR = ":=";
+    public static final String ATTRIBUTE_SEPARATOR = "=";
+    public static final String CLASS_PATH_SEPARATOR = ",";
+    public static final String CLASS_PATH_DOT = ".";
+    public static final String PACKAGE_SEPARATOR = ";";
+    public static final String VERSION_SEGMENT_SEPARATOR = ".";
+    public static final int VERSION_SEGMENT_COUNT = 3;
+
+    // Miscellaneous OSGi constants.
+    public static final String BUNDLE_URL_PROTOCOL = "bundle";
+
+    // Miscellaneous framework configuration property names.
+    public static final String AUTO_INSTALL_PROP = "felix.auto.install";
+    public static final String AUTO_START_PROP = "felix.auto.start";
+    public static final String EMBEDDED_EXECUTION_PROP = "felix.embedded.execution";
+    public static final String STRICT_OSGI_PROP = "felix.strict.osgi";
+    public static final String CACHE_CLASS_PROP = "felix.cache.class";
+    public static final String FRAMEWORK_STARTLEVEL_PROP
+        = "felix.startlevel.framework";
+    public static final String BUNDLE_STARTLEVEL_PROP
+        = "felix.startlevel.bundle";
+
+    // Start level-related constants.
+    public static final int FRAMEWORK_INACTIVE_STARTLEVEL = 0;
+    public static final int FRAMEWORK_DEFAULT_STARTLEVEL = 1;
+    public static final int SYSTEMBUNDLE_DEFAULT_STARTLEVEL = 0;
+    public static final int BUNDLE_DEFAULT_STARTLEVEL = 1;
+
+    // Miscellaneous properties values.
+    public static final String FAKE_URL_PROTOCOL_VALUE = "location:";
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/FelixDispatchQueue.java b/src/org/apache/osgi/framework/util/FelixDispatchQueue.java
new file mode 100644
index 0000000..bed03a4
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/FelixDispatchQueue.java
@@ -0,0 +1,147 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.EventListener;
+import java.util.EventObject;
+
+import org.apache.osgi.framework.LogWrapper;
+import org.osgi.framework.*;
+
+/**
+ * This is a subclass of <tt>DispatchQueue</tt> that specifically adds support
+ * for <tt>SynchronousBundleListener</tt>s; the OSGi specification
+ * says that synchronous bundle listeners should receive their events
+ * immediately, i.e., they should not be delivered on a separate thread
+ * like is the case with the <tt>DispatchQueue</tt>. To achieve this
+ * functionality, this class overrides the dispatch method to automatically
+ * fire any bundle events to synchronous bundle listeners using the
+ * calling thread. In order to ensure that synchronous bundle listeners
+ * do not receive an event twice, it wraps the passed in <tt>Dipatcher</tt>
+ * instance so that it filters synchronous bundle listeners.
+**/
+public class FelixDispatchQueue extends DispatchQueue
+{
+    public FelixDispatchQueue(LogWrapper logger)
+    {
+        super(logger);
+    }
+
+    /**
+     * Dispatches an event to a set of event listeners using a specified
+     * dispatcher object. This overrides the definition of the super class
+     * to deliver events to <tt>ServiceListener</tt>s and
+     * <tt>SynchronousBundleListener</tt>s using
+     * the calling thread instead of the event dispatching thread. All
+     * other events are still delivered asynchronously.
+     *
+     * @param dispatcher the dispatcher used to actually dispatch the event; this
+     *          varies according to the type of event listener.
+     * @param clazz the class associated with the target event listener type;
+     *              only event listeners of this type will receive the event.
+     * @param eventObj the actual event object to dispatch.
+    **/
+    public void dispatch(Dispatcher dispatcher, Class clazz, EventObject eventObj)
+    {
+        Object[] listeners = getListeners();
+
+        // If this is an event for service listeners, then dispatch it
+        // immediately since service events are never asynchronous.
+        if ((clazz == ServiceListener.class) && (listeners.length > 0))
+        {
+            // Notify appropriate listeners.
+            for (int i = listeners.length - 2; i >= 0; i -= 2)
+            {
+                // If the original listener is a synchronous bundle listener
+                // or a service listener, then dispatch event immediately
+                // per the specification.
+                ListenerWrapper lw = (ListenerWrapper) listeners[i + 1];
+                if (lw.getListenerClass() == ServiceListener.class)
+                {
+                    try {
+                        dispatcher.dispatch(
+                            (EventListener) lw, eventObj);
+                    } catch (Throwable th) {
+                        getLogger().log(
+                            LogWrapper.LOG_ERROR,
+                            "FelixDispatchQueue: Error during dispatch.", th);
+                    }
+                }
+            }
+        }
+        // Dispatch bundle events to synchronous bundle listeners immediately,
+        // but deliver to standard bundle listeners asynchronously.
+        else if ((clazz == BundleListener.class) && (listeners.length > 0))
+        {
+            // Notify appropriate listeners.
+            for (int i = listeners.length - 2; i >= 0; i -= 2)
+            {
+                // If the original listener is a synchronous bundle listener,
+                // then dispatch event immediately per the specification.
+                ListenerWrapper lw = (ListenerWrapper) listeners[i + 1];
+                if (lw.getListenerClass() == SynchronousBundleListener.class)
+                {
+                    try {
+                        dispatcher.dispatch(
+                            (EventListener) lw, eventObj);
+                    } catch (Throwable th) {
+                        getLogger().log(
+                            LogWrapper.LOG_ERROR,
+                            "FelixDispatchQueue: Error during dispatch.", th);
+                    }
+                }
+            }
+
+            // Wrap the dispatcher so that it ignores synchronous
+            // bundle listeners since they have already been dispatched.
+            IgnoreSynchronousDispatcher ignoreDispatcher = new IgnoreSynchronousDispatcher();
+            ignoreDispatcher.setDispatcher(dispatcher);
+
+            // Dispatch the bundle listener asynchronously.
+            dispatch(listeners, ignoreDispatcher, clazz, eventObj);
+        }
+        // All other events are dispatched asynchronously.
+        else
+        {
+            dispatch(listeners, dispatcher, clazz, eventObj);
+        }
+    }
+
+    private static class IgnoreSynchronousDispatcher implements Dispatcher
+    {
+        private Dispatcher m_dispatcher = null;
+
+        public void setDispatcher(Dispatcher dispatcher)
+        {
+            m_dispatcher = dispatcher;
+        }
+
+        public void dispatch(EventListener l, EventObject eventObj)
+        {
+            if (l instanceof ListenerWrapper)
+            {
+                ListenerWrapper lw = (ListenerWrapper) l;
+                // Do not dispatch events to synchronous listeners,
+                // since they are dispatched immediately above.
+                if (!(lw.getListenerClass() == SynchronousBundleListener.class))
+                {
+                    m_dispatcher.dispatch(l, eventObj);
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/FrameworkListenerWrapper.java b/src/org/apache/osgi/framework/util/FrameworkListenerWrapper.java
new file mode 100644
index 0000000..630c78c
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/FrameworkListenerWrapper.java
@@ -0,0 +1,55 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.osgi.framework.*;
+
+public class FrameworkListenerWrapper extends ListenerWrapper implements FrameworkListener
+{
+    public FrameworkListenerWrapper(Bundle bundle, FrameworkListener l)
+    {
+        super(bundle, FrameworkListener.class, l);
+    }
+
+    public void frameworkEvent(final FrameworkEvent event)
+    {
+        // The spec says only active bundles receive asynchronous events,
+        // but we will include starting bundles too otherwise
+        // it is impossible to see everything.
+        if ((getBundle().getState() == Bundle.STARTING) ||
+            (getBundle().getState() == Bundle.ACTIVE))
+        {
+            if (System.getSecurityManager() != null)
+            {
+                AccessController.doPrivileged(new PrivilegedAction() {
+                    public Object run()
+                    {
+                        ((FrameworkListener) getListener()).frameworkEvent(event);
+                        return null;
+                    }
+                });
+            }
+            else
+            {
+                ((FrameworkListener) getListener()).frameworkEvent(event);
+            }
+        }
+    }
+}
diff --git a/src/org/apache/osgi/framework/util/IteratorToEnumeration.java b/src/org/apache/osgi/framework/util/IteratorToEnumeration.java
new file mode 100644
index 0000000..c457827
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/IteratorToEnumeration.java
@@ -0,0 +1,44 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+
+public class IteratorToEnumeration implements Enumeration
+{
+    private Iterator m_iter = null;
+
+    public IteratorToEnumeration(Iterator iter)
+    {
+        m_iter = iter;
+    }
+
+    public boolean hasMoreElements()
+    {
+        if (m_iter == null)
+            return false;
+        return m_iter.hasNext();
+    }
+
+    public Object nextElement()
+    {
+        if (m_iter == null)
+            return null;
+        return m_iter.next();
+    }
+}
diff --git a/src/org/apache/osgi/framework/util/LibraryInfo.java b/src/org/apache/osgi/framework/util/LibraryInfo.java
new file mode 100644
index 0000000..f444ef9
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/LibraryInfo.java
@@ -0,0 +1,179 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.*;
+
+import org.osgi.framework.Constants;
+
+public class LibraryInfo
+{
+    private String m_name = null;
+    private String[] m_osnames = null;
+    private String[] m_osversions = null;
+    private String[] m_processors = null;
+    private String[] m_languages = null;
+
+    public LibraryInfo(String name, String[] osnames, String[] osversions,
+        String[] processors, String[] languages)
+    {
+        m_name = name;
+        m_osnames = osnames;
+        m_osversions = osversions;
+        m_processors = processors;
+        m_languages = languages;
+    }
+
+    public LibraryInfo(LibraryInfo library)
+    {
+        m_name = library.m_name;
+        m_osnames = library.m_osnames;
+        m_osversions = library.m_osversions;
+        m_processors = library.m_processors;
+        m_languages = library.m_languages;
+    }
+
+    public String getName()
+    {
+        return m_name;
+    }
+
+    public String[] getOSNames()
+    {
+        return m_osnames;
+    }
+
+    public String[] getOSVersions()
+    {
+        return m_osversions;
+    }
+
+    public String[] getProcessors()
+    {
+        return m_processors;
+    }
+
+    public static LibraryInfo[] parse(String s)
+    {
+        try
+        {
+            if ((s == null) || (s.length() == 0))
+            {
+                return null;
+            }
+
+            // The tokens are separated by semicolons and may include
+            // any number of libraries (whose name starts with a "/")
+            // along with one set of associated properties.
+            StringTokenizer st = new StringTokenizer(s, ";");
+            String[] libs = new String[st.countTokens()];
+            List osNameList = new ArrayList();
+            List osVersionList = new ArrayList();
+            List processorList = new ArrayList();
+            List languageList = new ArrayList();
+            int libCount = 0;
+            while (st.hasMoreTokens())
+            {
+                String token = st.nextToken().trim();
+                if (token.indexOf('=') < 0)
+                {
+                    // Remove the slash, if necessary.
+                    libs[libCount] = (token.charAt(0) == '/')
+                        ? token.substring(1)
+                        : token;
+                    libCount++;
+                }
+                else
+                {
+                    // Check for valid native library properties; defined as
+                    // a property name, an equal sign, and a value.
+                    StringTokenizer stProp = new StringTokenizer(token, "=");
+                    if (stProp.countTokens() != 2)
+                    {
+                        throw new IllegalArgumentException(
+                            "Bundle manifest native library entry malformed: " + token);
+                    }
+                    String property = stProp.nextToken().trim().toLowerCase();
+                    String value = stProp.nextToken().trim();
+                    
+                    // Values may be quoted, so remove quotes if present.
+                    if (value.charAt(0) == '"')
+                    {
+                        // This should always be true, otherwise the
+                        // value wouldn't be properly quoted, but we
+                        // will check for safety.
+                        if (value.charAt(value.length() - 1) == '"')
+                        {
+                            value = value.substring(1, value.length() - 1);
+                        }
+                        else
+                        {
+                            value = value.substring(1);
+                        }
+                    }
+                    // Add the value to its corresponding property list.
+                    if (property.equals(Constants.BUNDLE_NATIVECODE_OSNAME))
+                    {
+                        osNameList.add(value);
+                    }
+                    else if (property.equals(Constants.BUNDLE_NATIVECODE_OSVERSION))
+                    {
+                        osVersionList.add(value);
+                    }
+                    else if (property.equals(Constants.BUNDLE_NATIVECODE_PROCESSOR))
+                    {
+                        processorList.add(value);
+                    }
+                    else if (property.equals(Constants.BUNDLE_NATIVECODE_LANGUAGE))
+                    {
+                        languageList.add(value);
+                    }
+                }
+            }
+
+            if (libCount == 0)
+            {
+                return null;
+            }
+
+            LibraryInfo[] libraries = new LibraryInfo[libCount];
+            for (int i = 0; i < libCount; i++)
+            {
+                libraries[i] =
+                    new LibraryInfo(
+                        libs[i],
+                        (String[]) osNameList.toArray(new String[0]),
+                        (String[]) osVersionList.toArray(new String[0]),
+                        (String[]) processorList.toArray(new String[0]),
+                        (String[]) languageList.toArray(new String[0]));
+            }
+
+            return libraries;
+
+        }
+        catch (RuntimeException ex)
+        {
+            ex.printStackTrace();
+            throw ex;
+        }
+    }
+
+    public String toString()
+    {
+        return m_name;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ListenerWrapper.java b/src/org/apache/osgi/framework/util/ListenerWrapper.java
new file mode 100644
index 0000000..0265e8d
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ListenerWrapper.java
@@ -0,0 +1,66 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.EventListener;
+
+import org.osgi.framework.Bundle;
+
+public class ListenerWrapper
+{
+    // The bundle associated with the listener.
+    private Bundle m_bundle = null;
+    // Listener class.
+    private Class m_class = null;
+    // The original listener.
+    private EventListener m_listener = null;
+
+    public ListenerWrapper(Bundle bundle, Class clazz, EventListener l)
+    {
+        m_bundle = bundle;
+        m_class = clazz;
+        m_listener = l;
+    }
+
+    public Bundle getBundle()
+    {
+        return m_bundle;
+    }
+
+    protected Class getListenerClass()
+    {
+        return m_class;
+    }
+
+    protected EventListener getListener()
+    {
+        return m_listener;
+    }
+
+    public boolean equals(Object obj)
+    {
+        if (obj instanceof ListenerWrapper)
+        {
+            return (((ListenerWrapper) obj).m_listener == m_listener);
+        }
+        else if (obj instanceof EventListener)
+        {
+            return (obj == m_listener);
+        }
+        return false;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/MapToDictionary.java b/src/org/apache/osgi/framework/util/MapToDictionary.java
new file mode 100644
index 0000000..0bdd510
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/MapToDictionary.java
@@ -0,0 +1,92 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.*;
+
+
+/**
+ * This is a simple class that implements a <tt>Dictionary</tt>
+ * from a <tt>Map</tt>. The resulting dictionary is immutatable.
+**/
+public class MapToDictionary extends Dictionary
+{
+    /**
+     * Map source.
+    **/
+    private Map m_map = null;
+
+    public MapToDictionary(Map map)
+    {
+        m_map = map;
+    }
+
+    public Enumeration elements()
+    {
+        if (m_map == null)
+        {
+            return null;
+        }
+        return new IteratorToEnumeration(m_map.values().iterator());
+    }
+
+    public Object get(Object key)
+    {
+        if (m_map == null)
+        {
+            return null;
+        }
+        return m_map.get(key);
+    }
+
+    public boolean isEmpty()
+    {
+        if (m_map == null)
+        {
+            return true;
+        }
+        return m_map.isEmpty();
+    }
+
+    public Enumeration keys()
+    {
+        if (m_map == null)
+        {
+            return null;
+        }
+        return new IteratorToEnumeration(m_map.keySet().iterator());
+    }
+
+    public Object put(Object key, Object value)
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    public Object remove(Object key)
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    public int size()
+    {
+        if (m_map == null)
+        {
+            return 0;
+        }
+        return m_map.size();
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/MutablePropertyResolver.java b/src/org/apache/osgi/framework/util/MutablePropertyResolver.java
new file mode 100644
index 0000000..f5aaf84
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/MutablePropertyResolver.java
@@ -0,0 +1,22 @@
+/*
+ *   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.osgi.framework.util;
+
+public interface MutablePropertyResolver extends PropertyResolver
+{
+    public String put(String key, String value);
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/MutablePropertyResolverImpl.java b/src/org/apache/osgi/framework/util/MutablePropertyResolverImpl.java
new file mode 100644
index 0000000..3be8c37
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/MutablePropertyResolverImpl.java
@@ -0,0 +1,44 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.util.Map;
+
+public class MutablePropertyResolverImpl implements MutablePropertyResolver
+{
+    private Map m_props = null;
+    
+    public MutablePropertyResolverImpl(Map props)
+    {
+        m_props = props;
+    }
+
+    public synchronized String put(String key, String value)
+    {
+        return (String) m_props.put(key, value);
+    }
+
+    public synchronized String get(String key)
+    {
+        return (String) m_props.get(key);
+    }
+
+    public synchronized String[] getKeys()
+    {
+        return (String[]) m_props.keySet().toArray(new String[0]);
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ObjectInputStreamX.java b/src/org/apache/osgi/framework/util/ObjectInputStreamX.java
new file mode 100644
index 0000000..e7b99be
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ObjectInputStreamX.java
@@ -0,0 +1,53 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.io.*;
+
+/**
+ * The ObjectInputStreamX class is a simple extension to the ObjectInputStream
+ * class.  The purpose of ObjectInputStreamX is to allow objects to be deserialized
+ * when their classes are not in the CLASSPATH (e.g., in a JAR file).
+ */
+public class ObjectInputStreamX extends ObjectInputStream
+{
+    private ClassLoader m_loader = null;
+
+    /**
+     * Construct an ObjectInputStreamX for the specified InputStream and the specified
+     * ClassLoader.
+     * @param in the input stream to read.
+     * @param loader the class loader used to resolve classes.
+     */
+    public ObjectInputStreamX(InputStream in, ClassLoader loader)
+        throws IOException, StreamCorruptedException
+    {
+        super(in);
+        this.m_loader = loader;
+    }
+
+    /**
+     * By overloading this method, the ObjectInputStreamX can resolve a class
+     * from the class loader that was passed into it at construction time.
+     */
+    protected Class resolveClass(ObjectStreamClass v)
+        throws IOException, ClassNotFoundException
+    {
+        Class clazz = m_loader.loadClass(v.getName());
+        return clazz;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/PropertyResolver.java b/src/org/apache/osgi/framework/util/PropertyResolver.java
new file mode 100644
index 0000000..cd8f994
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/PropertyResolver.java
@@ -0,0 +1,23 @@
+/*
+ *   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.osgi.framework.util;
+
+public interface PropertyResolver
+{
+    public String get(String key);
+    public String[] getKeys();
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ServiceListenerWrapper.java b/src/org/apache/osgi/framework/util/ServiceListenerWrapper.java
new file mode 100644
index 0000000..0eba299
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ServiceListenerWrapper.java
@@ -0,0 +1,114 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.security.*;
+
+import org.osgi.framework.*;
+
+public class ServiceListenerWrapper extends ListenerWrapper implements ServiceListener
+{
+    // LDAP query filter.
+    private Filter m_filter = null;
+    // Remember the security context.
+    private AccessControlContext m_acc = null;
+
+    public ServiceListenerWrapper(Bundle bundle, ServiceListener l, Filter filter)
+    {
+        super(bundle, ServiceListener.class, l);
+        m_filter = filter;
+
+        // Remember security context for filtering
+        // events based on security.
+        if (System.getSecurityManager() != null)
+        {
+            m_acc = AccessController.getContext();
+        }
+    }
+
+    public void setFilter(Filter filter)
+    {
+        m_filter = filter;
+    }
+    
+    public void serviceChanged(final ServiceEvent event)
+    {
+        // Service events should be delivered to STARTING,
+        // STOPPING, and ACTIVE bundles.
+        if ((getBundle().getState() != Bundle.STARTING) &&
+            (getBundle().getState() != Bundle.STOPPING) &&
+            (getBundle().getState() != Bundle.ACTIVE))
+        {
+            return;
+        }
+
+        // Check that the bundle has permission to get at least
+        // one of the service interfaces; the objectClass property
+        // of the service stores its service interfaces.
+        ServiceReference ref = event.getServiceReference();
+        String[] objectClass = (String[]) ref.getProperty(Constants.OBJECTCLASS);
+
+        // On the safe side, if there is no objectClass property
+        // then ignore event altogether.
+        if (objectClass != null)
+        {
+            boolean hasPermission = false;
+            if (m_acc != null)
+            {
+                for (int i = 0;
+                    !hasPermission && (i < objectClass.length);
+                    i++)
+                {
+                    try {
+                        ServicePermission perm =
+                            new ServicePermission(
+                                objectClass[i], ServicePermission.GET);
+                        m_acc.checkPermission(perm);
+                        hasPermission = true;
+                    } catch (Exception ex) {
+                    }
+                }
+            }
+            else
+            {
+                hasPermission = true;
+            }
+
+            if (hasPermission)
+            {
+                // Dispatch according to the filter.
+                if ((m_filter == null) || m_filter.match(event.getServiceReference()))
+                {
+                    if (System.getSecurityManager() != null)
+                    {
+                        AccessController.doPrivileged(new PrivilegedAction() {
+                            public Object run()
+                            {
+                                ((ServiceListener) getListener()).serviceChanged(event);
+                                return null;
+                            }
+                        });
+                    }
+                    else
+                    {
+                        ((ServiceListener) getListener()).serviceChanged(event);
+                    }
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/Util.java b/src/org/apache/osgi/framework/util/Util.java
new file mode 100644
index 0000000..89591ab
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/Util.java
@@ -0,0 +1,240 @@
+/*
+ *   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.osgi.framework.util;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class Util
+{
+    /**
+     * Parses delimited string and returns an array containing the tokens. This
+     * parser obeys quotes, so the delimiter character will be ignored if it is
+     * inside of a quote. This method assumes that the quote character is not
+     * included in the set of delimiter characters.
+     * @param value the delimited string to parse.
+     * @param delim the characters delimiting the tokens.
+     * @return an array of string tokens or null if there were no tokens.
+    **/
+    public static String[] parseDelimitedString(String value, String delim)
+    {
+        if (value == null)
+        {
+           value = "";
+        }
+
+        List list = new ArrayList();
+
+        int CHAR = 1;
+        int DELIMITER = 2;
+        int STARTQUOTE = 4;
+        int ENDQUOTE = 8;
+
+        StringBuffer sb = new StringBuffer();
+
+        int expecting = (CHAR | DELIMITER | STARTQUOTE);
+        
+        for (int i = 0; i < value.length(); i++)
+        {
+            char c = value.charAt(i);
+
+            boolean isDelimiter = (delim.indexOf(c) >= 0);
+            boolean isQuote = (c == '"');
+
+            if (isDelimiter && ((expecting & DELIMITER) > 0))
+            {
+                list.add(sb.toString().trim());
+                sb.delete(0, sb.length());
+                expecting = (CHAR | DELIMITER | STARTQUOTE);
+            }
+            else if (isQuote && ((expecting & STARTQUOTE) > 0))
+            {
+                sb.append(c);
+                expecting = CHAR | ENDQUOTE;
+            }
+            else if (isQuote && ((expecting & ENDQUOTE) > 0))
+            {
+                sb.append(c);
+                expecting = (CHAR | STARTQUOTE | DELIMITER);
+            }
+            else if ((expecting & CHAR) > 0)
+            {
+                sb.append(c);
+            }
+            else
+            {
+                throw new IllegalArgumentException("Invalid delimited string: " + value);
+            }
+        }
+
+        if (sb.length() > 0)
+        {
+            list.add(sb.toString().trim());
+        }
+
+        return (String[]) list.toArray(new String[list.size()]);
+    }
+
+    /**
+     * Parses native code manifest headers.
+     * @param libStrs an array of native library manifest header
+     *        strings from the bundle manifest.
+     * @return an array of <tt>LibraryInfo</tt> objects for the
+     *         passed in strings.
+    **/
+    public static LibraryInfo[] parseLibraryStrings(String[] libStrs)
+        throws IllegalArgumentException
+    {
+        if (libStrs == null)
+        {
+            return null;
+        }
+
+        List libList = new ArrayList();
+
+        for (int i = 0; i < libStrs.length; i++)
+        {
+            LibraryInfo[] libs = LibraryInfo.parse(libStrs[i]);
+            for (int libIdx = 0;
+                (libs != null) && (libIdx < libs.length);
+                libIdx++)
+            {
+                libList.add(libs[libIdx]);
+            }
+        }
+
+        if (libList.size() == 0)
+        {
+            return null;
+        }
+
+        return (LibraryInfo[]) libList.toArray(new LibraryInfo[libList.size()]);
+    }
+
+    private static final byte encTab[] = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
+        0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,
+        0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
+        0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
+        0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31,
+        0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f };
+
+    private static final byte decTab[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1,
+        -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1,
+        -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
+        18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
+        30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
+        48, 49, 50, 51, -1, -1, -1, -1, -1 };
+
+    public static String base64Encode(String s) throws IOException
+    {
+        return encode(s.getBytes(), 0);
+    }
+
+    /**
+     * Encode a raw byte array to a Base64 String.
+     * 
+     * @param in Byte array to encode.
+     * @param len Length of Base64 lines. 0 means no line breaks.
+    **/
+    public static String encode(byte[] in, int len) throws IOException
+    {
+        ByteArrayOutputStream baos = null;
+        ByteArrayInputStream bais = null;
+        try
+        {
+            baos = new ByteArrayOutputStream();
+            bais = new ByteArrayInputStream(in);
+            encode(bais, baos, len);
+            // ASCII byte array to String
+            return (new String(baos.toByteArray()));
+        }
+        finally
+        {
+            if (baos != null)
+            {
+                baos.close();
+            }
+            if (bais != null)
+            {
+                bais.close();
+            }
+        }
+    }
+
+    public static void encode(InputStream in, OutputStream out, int len)
+        throws IOException
+    {
+
+        // Check that length is a multiple of 4 bytes
+        if (len % 4 != 0)
+        {
+            throw new IllegalArgumentException("Length must be a multiple of 4");
+        }
+
+        // Read input stream until end of file
+        int bits = 0;
+        int nbits = 0;
+        int nbytes = 0;
+        int b;
+
+        while ((b = in.read()) != -1)
+        {
+            bits = (bits << 8) | b;
+            nbits += 8;
+            while (nbits >= 6)
+            {
+                nbits -= 6;
+                out.write(encTab[0x3f & (bits >> nbits)]);
+                nbytes++;
+                // New line
+                if (len != 0 && nbytes >= len)
+                {
+                    out.write(0x0d);
+                    out.write(0x0a);
+                    nbytes -= len;
+                }
+            }
+        }
+
+        switch (nbits)
+        {
+            case 2:
+                out.write(encTab[0x3f & (bits << 4)]);
+                out.write(0x3d); // 0x3d = '='
+                out.write(0x3d);
+                break;
+            case 4:
+                out.write(encTab[0x3f & (bits << 2)]);
+                out.write(0x3d);
+                break;
+        }
+
+        if (len != 0)
+        {
+            if (nbytes != 0)
+            {
+                out.write(0x0d);
+                out.write(0x0a);
+            }
+            out.write(0x0d);
+            out.write(0x0a);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ldap/AttributeNotFoundException.java b/src/org/apache/osgi/framework/util/ldap/AttributeNotFoundException.java
new file mode 100644
index 0000000..69df531
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/AttributeNotFoundException.java
@@ -0,0 +1,25 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+public class AttributeNotFoundException extends EvaluationException
+{
+    public AttributeNotFoundException(String msg)
+    {
+        super(msg);
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ldap/Driver.java b/src/org/apache/osgi/framework/util/ldap/Driver.java
new file mode 100644
index 0000000..f341255
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/Driver.java
@@ -0,0 +1,150 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+import java.io.*;
+import java.util.*;
+
+public class Driver {
+
+    public static void main(String[] argv)
+    {
+    Mapper mapper = new DriverMapper();
+
+    if(argv== null || argv.length == 0) {
+        System.err.println("usage: Driver <ldap spec file>");
+        return;
+    }
+    LdapLexer lexer = new LdapLexer();
+    FileReader fr = null;
+    char[] line = null;
+    Evaluator engine = new Evaluator();
+
+    Parser parser = new Parser();
+//	parser.setDebug(System.out);
+
+    try {
+        File spec = new File(argv[0]);
+        fr = new FileReader(spec);
+
+        // The basic operation of the driver is:
+        // 1. read a line from the file
+        // 2. parse that line
+        // 3. print the resulting program
+        // 4. repeat 1 until eof
+
+        for(;;) {
+        line = getLine(fr);
+        if(line == null) break;
+        System.out.println("Driver: filter: "+new String(line));
+        CharArrayReader car = new CharArrayReader(line);
+        lexer.setReader(car);
+        parser.reset(lexer);
+        boolean status = false;
+        try {
+            status = parser.start();
+            if(!status) {
+            System.err.println("parse failed");
+            printErrorLocation(line,lexer.charno());
+            }
+        } catch (ParseException pe) {
+            System.err.println(pe.toString());
+            printErrorLocation(line,lexer.charno());
+        }
+        if(status) {
+            try {
+            engine.reset(parser.getProgram());
+//            System.out.println("Driver: program: "+engine.toString());
+            System.out.println("Driver: program: "+engine.toStringInfix());
+            System.out.println("Eval = " + engine.evaluate(mapper));
+            } catch (EvaluationException ee) {
+            System.err.print("Driver: ");
+            printEvaluationStack(engine.getOperands());
+            System.err.println(ee.toString());
+            }
+        }
+        }
+    } catch (Exception e) {
+        System.err.println(e.toString());
+        printErrorLocation(line,lexer.charno());
+        e.printStackTrace();
+    }
+    }
+
+    // Get a line of input at a time and return a char[] buffer
+    // containing the line
+
+    static char[] getLine(Reader reader) throws IOException
+    {
+    StringBuffer buf = new StringBuffer();
+    for(;;) {
+        int c = reader.read();
+        if(c == '\r') continue;
+        if(c < 0) {
+        if(buf.length() == 0) return null; // no more lines
+        break;
+        }
+        if(c == '\n') break;
+        buf.append((char)c);
+    }
+
+    char[] cbuf = new char[buf.length()];
+    buf.getChars(0,buf.length(),cbuf,0);
+    return cbuf;
+    }
+
+
+    static void printErrorLocation(char[] line, int charno)
+    {
+    System.err.print("|");
+    if(line != null) System.err.print(new String(line));
+    System.err.println("|");
+    for(int i=0;i<charno;i++) System.err.print(" ");
+    System.err.println("^");
+    }
+
+    // Report the final contents of the evaluation stack
+    static void printEvaluationStack(Stack stack)
+    {
+    System.err.print("Stack:");
+    // recast operands as Vector to make interior access easier
+    Vector operands = stack;
+    int len = operands.size();
+    for(int i=0;i<len;i++) System.err.print(" "+operands.elementAt(i));
+    System.err.println();
+    }
+
+}
+
+class DriverMapper implements Mapper {
+
+    Hashtable hash = new Hashtable();
+
+    public DriverMapper()
+    {
+        hash.put("cn","Babs Jensen");
+        hash.put("objectClass","Person");
+        hash.put("sn","Jensen");
+        hash.put("o","university of michigan");
+        hash.put("foo","bar");
+    }
+
+    public Object lookup(String key)
+    {
+        return hash.get(key);
+    }
+}
diff --git a/src/org/apache/osgi/framework/util/ldap/EvaluationException.java b/src/org/apache/osgi/framework/util/ldap/EvaluationException.java
new file mode 100644
index 0000000..c5e1fc9
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/EvaluationException.java
@@ -0,0 +1,43 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+public class EvaluationException extends Exception
+{
+    private Class m_unsupportedType = null;
+
+    public EvaluationException(String msg)
+    {
+        super(msg);
+    }
+    
+    public EvaluationException(String msg, Class clazz)
+    {
+        super(msg);
+        m_unsupportedType = clazz;
+    }
+    
+    public boolean isUnsupportedType()
+    {
+        return (m_unsupportedType != null);
+    }
+    
+    public Class getUnsupportedType()
+    {
+        return m_unsupportedType;
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ldap/Evaluator.java b/src/org/apache/osgi/framework/util/ldap/Evaluator.java
new file mode 100644
index 0000000..6b0ef58
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/Evaluator.java
@@ -0,0 +1,186 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+import java.util.Stack;
+import java.util.Vector;
+
+public class Evaluator {
+
+    Object[] program = null;
+    Stack operands = new Stack();
+    Mapper mapper = null;
+
+    public Evaluator()
+    {
+        reset();
+    }
+
+    public Evaluator(Object[] prog)
+    {
+        reset(prog);
+    }
+
+    public void reset()
+    {
+        program = null;
+        mapper = null;
+        operands.clear();
+    }
+
+    public void reset(Object[] prog)
+    {
+        reset();
+        setProgram(prog);
+    }
+
+    public void setProgram(Object[] prog)
+    {
+        program = prog;
+    }
+
+    public void setMapper(Mapper mapper)
+    {
+        this.mapper = mapper;
+    }
+
+    public Stack getOperands()
+    {
+        return operands;
+    }
+
+    public boolean evaluate(Mapper mapper) throws EvaluationException
+    {
+        try
+        {
+            // The following code is a little complicated because it
+            // is trying to deal with evaluating a given filter expression
+            // when it contains an attribute that does not exist in the
+            // supplied mapper. In such a situation the code below
+            // catches the "attribute not found" exception and inserts
+            // an instance of Unknown, which is used as a marker for
+            // non-existent attributes. The Unknown instance forces the
+            // operator to throw an "unsupported type" exception, which
+            // the code below converts into a FALSE and this has the effect
+            // of evaluating the subexpression that contained the
+            // non-existent attribute to FALSE. The rest of the filter
+            // expression evaluates normally. Any other exceptions are
+            // rethrown.
+            setMapper(mapper);
+            for (int i = 0; i < program.length; i++)
+            {
+                try
+                {
+                    Operator op = (Operator) program[i];
+                    op.execute(operands, mapper);
+//                    printAction(op); // for debug output
+                }
+                catch (AttributeNotFoundException ex)
+                {
+                    operands.push(new Unknown());
+                }
+                catch (EvaluationException ex)
+                {
+                    // If the exception is for an unsupported type of
+                    // type Unknown, then just push FALSE onto the
+                    // operand stack because this type will only appear
+                    // if an attribute was not found.
+                    if (ex.isUnsupportedType() &&
+                        (ex.getUnsupportedType() == Unknown.class))
+                    {
+                        operands.push(Boolean.FALSE);
+                    }
+                    // Otherwise, rethrow the exception.
+                    else
+                    {
+                        throw ex;
+                    }
+                }
+            }
+
+            if (operands.empty())
+            {
+                throw new EvaluationException(
+                    "Evaluation.evalute: final stack is empty");
+            }
+
+            Object result = operands.pop();
+
+            if (!operands.empty())
+            {
+                throw new EvaluationException(
+                    "Evaluation.evalute: final stack has more than one result");
+            }
+
+            if (!(result instanceof Boolean))
+            {
+                throw new EvaluationException(
+                    "Evaluation.evalute: final result is not Boolean");
+            }
+
+            return ((Boolean) result).booleanValue();
+        }
+        finally
+        {
+            // Clear the operands just in case an exception was thrown,
+            // otherwise stuff will be left in the stack.
+            operands.clear();
+        }
+    }
+
+    // For debugging; Dump the operator and stack
+    void printAction(Operator op)
+    {
+        System.err.println("Operator:"+op.toString());
+        System.err.print("Stack After:");
+        // recast operands as Vector to make interior access easier
+        Vector v = operands;
+        int len = v.size();
+        for (int i = 0; i < len; i++)
+            System.err.print(" " + v.elementAt(i));
+        System.err.println();
+    }
+
+    public String toString()
+    {
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < program.length; i++)
+        {
+            buf.append((i==0) ? "{" : ";");
+                buf.append(((Operator) program[i]).toString());
+        }
+        buf.append("}");
+        return buf.toString();
+    }
+
+    public String toStringInfix()
+    {
+        // First, we "evaluate" the program
+        // but for the purpose of re-constructing
+        // a parsetree.
+        operands.clear();
+        for (int i = 0; i < program.length; i++)
+        {
+            ((Operator) program[i]).buildTree(operands);
+        }
+        StringBuffer b = new StringBuffer();
+        Object result = operands.pop();
+        ((Operator)result).toStringInfix(b);
+        operands.clear();
+        return b.toString();
+    }
+}
diff --git a/src/org/apache/osgi/framework/util/ldap/LdapLexer.java b/src/org/apache/osgi/framework/util/ldap/LdapLexer.java
new file mode 100644
index 0000000..1617c4d
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/LdapLexer.java
@@ -0,0 +1,98 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+import java.io.IOException;
+import java.io.Reader;
+
+public class LdapLexer {
+
+    static final int EOF = -1;
+    static final int NOCHAR = 0; // signal no peeked char; different from EOF
+
+    public static final String WHITESPACE = " \t\n\r";
+
+    Reader reader = null;
+
+    int nextChar = NOCHAR; // last peeked character
+
+    public LdapLexer() {}
+
+    public LdapLexer(Reader r)
+    {
+    setReader(r);
+    charno = 1;
+    }
+
+    public void setReader(Reader r)
+    {
+    reader = r;
+    }
+
+    /*
+    The procedures get(),peek(),skipwhitespace(),getnw(), and peeknw()
+    provide the essential LdapLexer interface.
+    */
+
+    public int get() throws IOException // any next char
+    {
+    if(nextChar == NOCHAR) return readChar();
+    int c = nextChar;
+    nextChar = NOCHAR;
+    return c;
+    }
+
+    public int peek() throws IOException
+    {
+    if(nextChar == NOCHAR) {
+        nextChar = readChar();
+    }
+    return nextChar;
+    }
+
+    void skipwhitespace() throws IOException
+    {
+    while(WHITESPACE.indexOf(peek()) >= 0) get();
+    }
+
+    public int getnw() throws IOException // next non-whitespace char
+    {					   // (note: not essential but useful)
+    skipwhitespace();
+    return get();
+    }
+
+    public int peeknw() throws IOException // next non-whitespace char
+    {					   // (note: not essential but useful)
+    skipwhitespace();
+    return peek();
+    }
+
+    // Following is for error reporting
+
+    // Pass all character reads thru this so we can track char count
+
+    int charno; // 1-based
+
+    public int charno() {return charno;}
+
+    int readChar() throws IOException
+    {
+    charno++;
+    return reader.read();
+    }
+
+}
diff --git a/src/org/apache/osgi/framework/util/ldap/Mapper.java b/src/org/apache/osgi/framework/util/ldap/Mapper.java
new file mode 100644
index 0000000..bf9af65
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/Mapper.java
@@ -0,0 +1,22 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+public interface Mapper
+{
+    public Object lookup(String key);
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ldap/Operator.java b/src/org/apache/osgi/framework/util/ldap/Operator.java
new file mode 100644
index 0000000..b733238
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/Operator.java
@@ -0,0 +1,34 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+import java.util.Stack;
+
+public abstract class Operator
+{
+    public abstract void execute(Stack operands, Mapper mapper)
+        throws EvaluationException;
+
+    public abstract String toString();
+
+    public abstract void buildTree(Stack operands); // re-build the parsetree
+    public abstract void toStringInfix(StringBuffer b); // convert to canonical string
+
+    // Place to store the reconstructed parsetree
+    // Vector -> ArrayList is using jdk1.2 or later
+    public Operator[] children = null;
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ldap/ParseException.java b/src/org/apache/osgi/framework/util/ldap/ParseException.java
new file mode 100644
index 0000000..cdc482b
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/ParseException.java
@@ -0,0 +1,22 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+public class ParseException extends Exception {
+    public ParseException() {super();}
+    public ParseException(String msg) {super(msg);}
+}
diff --git a/src/org/apache/osgi/framework/util/ldap/Parser.java b/src/org/apache/osgi/framework/util/ldap/Parser.java
new file mode 100644
index 0000000..d14cfe3
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/Parser.java
@@ -0,0 +1,1696 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.*;
+
+public class Parser
+{
+    //
+    // Parser contants.
+    //
+
+    // End of file.
+    public static final int EOF = -1;
+
+    // Special characters in parse
+    public static final char LPAREN = '(';
+    public static final char RPAREN = ')';
+    public static final char STAR = '*';
+
+    // Define the list of legal leading and trailing
+    // characters in an attribute name.
+    public static final String ATTRIBUTECHARS0 =
+        ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
+    // Define the list of legal internal characters in an attribute name.
+    public static final String ATTRIBUTECHARS1 =
+        ".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_ ";
+
+    // Define an enum for substring procedure
+    public static final int SIMPLE = 0;
+    public static final int PRESENT = 1;
+    public static final int SUBSTRING = 2;
+
+    // different from =|>|<|~
+    public static final int NOOP = 0;
+
+    // Comparison operators.
+    public static final int EQUAL = 0;
+    public static final int GREATER_EQUAL = 1;
+    public static final int LESS_EQUAL = 2;
+    public static final int APPROX = 3;
+
+    // Criteria in % to accept something as approximate.
+    public static final int APPROX_CRITERIA = 10;
+
+    // Flag indicating presense of BigInteger/Decimal.
+    private static boolean m_hasBigNumbers = false;
+
+    static 
+    {
+        try
+        {
+            Class.forName("java.math.BigDecimal");
+            m_hasBigNumbers = true;
+        }
+        catch (Exception ex)
+        {
+            // Ignore.
+        }
+    }
+    //
+    // Instance variables.
+    //
+
+    private LdapLexer lexer = null;
+    private List program;
+
+    public Parser()
+    {
+        reset();
+    }
+
+    public Parser(LdapLexer l)
+    {
+        reset(l);
+    }
+
+    public void reset()
+    {
+        lexer = null;
+        if (program == null)
+        {
+            program = new ArrayList();
+        }
+        program.clear();
+    }
+
+    public void reset(LdapLexer l)
+    {
+        reset();
+        lexer = l;
+    }
+
+    public Object[] getProgram()
+    {
+        return program.toArray(new Object[program.size()]);
+    }
+
+    // Define the recursive descent procedures
+
+    /*
+    <start>::= <filter> <EOF>
+    */
+    public boolean start() throws ParseException, IOException
+    {
+        boolean ok = filter();
+        if (!ok)
+        {
+            return ok;
+        }
+        int ch = lexer.get();
+        if (ch != EOF)
+        {
+            throw new ParseException(
+                "expected <EOF>; found '" + ((char) ch) + "'");
+        }
+        return ok;
+    }
+
+    /*
+    <filter> ::= '(' <filtercomp> ')'
+     */
+    boolean filter() throws ParseException, IOException
+    {
+        debug("filter");
+        if (lexer.peeknw() != LPAREN)
+        {
+            return false;
+        }
+        lexer.get();
+        if (!filtercomp())
+        {
+            throw new ParseException("expected filtercomp");
+        }
+        if (lexer.getnw() != RPAREN)
+        {
+            throw new ParseException("expected )");
+        }
+        return true;
+    }
+
+    /*
+    <filtercomp> ::= <and> | <or> | <not> | <item>
+    <and> ::= '&' <filterlist>
+    <or> ::= '|' <filterlist>
+    <not> ::= '!' <filter>
+    */
+    boolean filtercomp() throws ParseException, IOException
+    {
+        debug("filtercomp");
+        int c = lexer.peeknw();
+        switch (c)
+        {
+            case '&' :
+            case '|' :
+                lexer.get();
+                int cnt = filterlist();
+                if (cnt == 0)
+                {
+                    return false;
+                }
+                // Code: [And|Or](cnt)
+                program.add(
+                    c == '&'
+                        ? (Operator) new AndOperator(cnt)
+                        : (Operator) new OrOperator(cnt));
+                return true;
+            case '!' :
+                lexer.get();
+                if (!filter())
+                {
+                    return false;
+                }
+                // Code: Not()
+                program.add(new NotOperator());
+                return true;
+            case EOF :
+                return false;
+            default :
+                // check for key
+                if (ATTRIBUTECHARS0.indexOf(c) <= 0)
+                {
+                    return false;
+                }
+                boolean b = item();
+                return b;
+        }
+    }
+
+    /*
+    <filterlist> ::= <filter> | <filter> <filterlist>
+    */
+    int filterlist() throws ParseException, IOException
+    {
+        debug("filterlist");
+        int cnt = 0;
+        if (filter())
+        {
+            do
+            {
+                cnt++;
+            }
+            while (filter());
+        }
+        return (cnt);
+    }
+
+    /*
+    <item> ::= <simple> | <present> | <substring>
+    <simple> ::= <attr> <filtertype> <value>
+    <filtertype> ::= <equal> | <approx> | <greater> | <less>
+    <present> ::= <attr> '=*'
+    <substring> ::= <attr> '=' <initial> <any> <final>
+    */
+    boolean item() throws ParseException, IOException
+    {
+        debug("item");
+
+        StringBuffer attr = new StringBuffer();
+        if (!attribute(attr))
+        {
+            return false;
+        }
+
+        lexer.skipwhitespace(); // assume allowable before equal operator
+        // note: I treat the =* case as = followed by a special substring
+        int op = equalop();
+        if (op == NOOP)
+        {
+            String oplist = "=|~=|>=|<=";
+            throw new ParseException("expected " + oplist);
+        }
+        ArrayList pieces = new ArrayList();
+        int kind = substring(pieces);
+        // Get some of the illegal cases out of the way
+        if (op != '=' && kind != SIMPLE)
+        {
+            // We assume that only the = operator can work
+            // with right sides containing stars.  If not correct
+            // then this code must change.
+            throw new ParseException("expected value|substring|*");
+        }
+
+        switch (kind)
+        {
+            case SIMPLE :
+                // Code: Push(attr); Constant(pieces.get(0)); <operator>();
+                program.add(new PushOperator(attr.toString()));
+                program.add(new ConstOperator(pieces.get(0)));
+                switch (op)
+                {
+                    case '<' :
+                        program.add(new LessEqualOperator());
+                        break;
+                    case '>' :
+                        program.add(new GreaterEqualOperator());
+                        break;
+                    case '~' :
+                        program.add(new ApproxOperator());
+                        break;
+                    case '=' :
+                    default :
+                        program.add(new EqualOperator());
+                }
+                break;
+            case PRESENT :
+                // Code: Present(attr);
+                program.add(new PresentOperator(attr.toString()));
+                break;
+            case SUBSTRING :
+                generateSubStringCode(attr.toString(), pieces);
+                break;
+            default :
+                throw new ParseException("expected value|substring|*");
+        }
+        return true;
+    }
+
+    // Generating code for substring right side is mildly
+    // complicated.
+
+    void generateSubStringCode(String attr, ArrayList pieces)
+    {
+        // Code: Push(attr)
+        program.add(new PushOperator(attr.toString()));
+
+        // Convert the pieces arraylist to a String[]
+        String[] list =
+            (String[]) pieces.toArray(new String[pieces.size()]);
+
+        // Code: SubString(list)
+        program.add(new SubStringOperator(list));
+    }
+
+    /*
+    <attr> is a string representing an attributte,
+    or key, in the properties
+    objects of the registered services. Attribute names are not case
+    sensitive; that is cn and CN both refer to the same attribute.
+    Attribute names may have embedded spaces, but not leading or
+    trailing spaces.
+    */
+    boolean attribute(StringBuffer buf) throws ParseException, IOException
+    {
+        debug("attribute");
+        lexer.skipwhitespace();
+        buf.setLength(0);
+        int c = lexer.peek(); // need to make sure there
+        // is at least one KEYCHAR
+        if (c == EOF)
+        {
+            return false;
+        }
+        if (ATTRIBUTECHARS0.indexOf(c) < 0)
+        {
+            return false;
+        }
+
+        do
+        {
+            buf.append((char) lexer.get());
+        }
+        while (ATTRIBUTECHARS1.indexOf(lexer.peek()) >= 0);
+
+        // The above may have accumulated trailing blanks that must be removed
+        int i = buf.length() - 1;
+        while (i > 0 && buf.charAt(i) == ' ')
+        {
+            i--;
+        }
+        buf.setLength(i + 1);
+        return true;
+    }
+
+    /*
+       <equal> ::= '='
+       <approx> ::= '~='
+       <greater> ::= '>='
+       <less> ::= '<='
+       <present> ::= <attr> '=*'
+    */
+    int equalop() throws ParseException, IOException
+    {
+        debug("equalop");
+        lexer.skipwhitespace();
+        int op = lexer.peek();
+        switch (op)
+        {
+            case '=' :
+                lexer.get();
+                break;
+            case '~' :
+            case '<' :
+            case '>' :
+                // skip main operator char
+                int c = lexer.get();
+                // make sure that the next char is '='
+                c = lexer.get();
+                if (c != '=')
+                {
+                    throw new ParseException("expected ~=|>=|<=");
+                }
+                break;
+            default :
+                op = NOOP;
+        }
+        return op;
+    }
+
+    /*
+    <substring> ::= <attr> '=' <initial> <any> <final>
+    <initial> ::= NULL | <value>
+    <any> ::= '*' <starval>
+    <starval> ::= NULL | <value> '*' <starval>
+    <final> ::= NULL | <value>
+    <value> ::= ...
+    */
+    /*
+    This procedure handles all cases on right side of an item
+    */
+    int substring(ArrayList pieces) throws ParseException, IOException
+    {
+        debug("substring");
+
+        pieces.clear();
+        StringBuffer ss = new StringBuffer();
+        //        int kind = SIMPLE; // assume until proven otherwise
+        boolean wasStar = false; // indicates last piece was a star
+        boolean leftstar = false; // track if the initial piece is a star
+        boolean rightstar = false; // track if the final piece is a star
+
+        // We assume (sub)strings can contain leading and trailing blanks
+loop:   for (;;)
+        {
+            int c = lexer.peek();
+            switch (c)
+            {
+                case RPAREN :
+                    if (wasStar)
+                    {
+                        // insert last piece as "" to handle trailing star
+                        rightstar = true;
+                    }
+                    else
+                    {
+                        pieces.add(ss.toString());
+                        // accumulate the last piece
+                        // note that in the case of
+                        // (cn=); this might be
+                        // the string "" (!=null)
+                    }
+                    ss.setLength(0);
+                    break loop;
+                case '\\' :
+                    wasStar = false;
+                    lexer.get();
+                    c = lexer.get();
+                    if (c != EOF)
+                    {
+                        throw new ParseException("unexpected EOF");
+                    }
+                    ss.append((char) c);
+                    break;
+                case EOF :
+                    if (pieces.size() > 0)
+                    {
+                        throw new ParseException("expected ')'");
+                    }
+                    else
+                    {
+                        throw new ParseException("expected value|substring");
+                    }
+                case '*' :
+                    if (wasStar)
+                    {
+                        // encountered two successive stars;
+                        // I assume this is illegal
+                        throw new ParseException("unexpected '**'");
+                    }
+                    lexer.get();
+                    if (ss.length() > 0)
+                    {
+                        pieces.add(ss.toString()); // accumulate the pieces
+                        // between '*' occurrences
+                    }
+                    ss.setLength(0);
+                    // if this is a leading star, then track it
+                    if (pieces.size() == 0)
+                    {
+                        leftstar = true;
+                    }
+                    ss.setLength(0);
+                    wasStar = true;
+                    break;
+                default :
+                    wasStar = false;
+                    ss.append((char) lexer.get());
+            }
+        }
+        if (pieces.size() == 0)
+        {
+            return PRESENT;
+        }
+        if (leftstar || rightstar || pieces.size() > 1)
+        {
+            // insert leading and/or trailing "" to anchor ends
+            if (rightstar)
+            {
+                pieces.add("");
+            }
+            if (leftstar)
+            {
+                pieces.add(0, "");
+            }
+            return SUBSTRING;
+        }
+        // assert !leftstar && !rightstar && pieces.size == 1
+        return SIMPLE;
+    }
+
+    // Debug stuff
+
+    static boolean debug = false;
+
+    PrintStream dbgout = null;
+
+    public void setDebug(PrintStream out)
+    {
+        debug = true;
+        dbgout = out;
+    }
+
+    void debug(String proc)
+    {
+        if (!debug || dbgout == null)
+        {
+            return;
+        }
+        dbgout.println("parsing " + proc + ":" + lexer.charno());
+        dbgout.flush();
+    }
+
+    // Exclusive inner classes
+
+    private static class AndOperator extends Operator
+    {
+        private int operandCount;
+
+        public AndOperator(int opcnt)
+        {
+            operandCount = opcnt;
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            // Determine result using short-circuit evaluation.
+            boolean result = true;
+            for (int i = 0; i < operandCount; i++)
+            {
+                if (operands.empty())
+                {
+                    fewOperands("AND");
+                }
+
+                // For short-circuited evaluation, once the AND
+                // becomes false, we can ignore the remaining
+                // expressions, but we must still pop them off.
+                if (!result)
+                {
+                    operands.pop();
+                }
+                else
+                {
+                    result = ((Boolean) operands.pop()).booleanValue();
+                }
+            }
+            operands.push(new Boolean(result));
+        }
+
+        public String toString()
+        {
+            return "&(" + operandCount + ")";
+        }
+
+        public void buildTree(Stack operands)
+        {
+            children = new Operator[operandCount];
+            // need to preserve stack order
+            for (int i = 0; i < operandCount; i++)
+            {
+                children[(operandCount - 1) - i] =
+                    (Operator) operands.pop();
+            }
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(&");
+            for (int i = 0; i < children.length; i++)
+            {
+                Operator o = (Operator) children[i];
+                o.toStringInfix(b);
+            }
+            b.append(")");
+        }
+    }
+
+    private static class OrOperator extends Operator
+    {
+        private int operandCount;
+
+        public OrOperator(int opcnt)
+        {
+            operandCount = opcnt;
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            // Determine result using short-circuit evaluation.
+            boolean result = false;
+            for (int i = 0; i < operandCount; i++)
+            {
+                if (operands.empty())
+                {
+                    fewOperands("OR");
+                }
+
+                // For short-circuited evaluation, once the OR
+                // becomes true, we can ignore the remaining
+                // expressions, but we must still pop them off.
+                if (result)
+                {
+                    operands.pop();
+                }
+                else
+                {
+                    result = ((Boolean) operands.pop()).booleanValue();
+                }
+            }
+            operands.push(new Boolean(result));
+        }
+
+        public String toString()
+        {
+            return "|(" + operandCount + ")";
+        }
+
+        public void buildTree(Stack operands)
+        {
+            children = new Operator[operandCount];
+            // need to preserve stack order
+            for (int i = 0; i < operandCount; i++)
+            {
+                children[(operandCount - 1) - i] =
+                    (Operator) operands.pop();
+            }
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(|");
+            for (int i = 0; i < children.length; i++)
+            {
+                Operator o = (Operator) children[i];
+                o.toStringInfix(b);
+            }
+            b.append(")");
+        }
+    }
+
+    private static class NotOperator extends Operator
+    {
+        public NotOperator()
+        {
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            if (operands.empty())
+            {
+                fewOperands("NOT");
+            }
+            boolean result = !((Boolean) operands.pop()).booleanValue();
+            operands.push(new Boolean(result));
+        }
+
+        public String toString()
+        {
+            return "!()";
+        }
+
+        public void buildTree(Stack operands)
+        {
+            children = new Operator[1];
+            children[0] = (Operator) operands.pop();
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(!");
+            for (int i = 0; i < children.length; i++)
+            {
+                Operator o = (Operator) children[i];
+                o.toStringInfix(b);
+            }
+            b.append(")");
+        }
+    }
+
+    private static class EqualOperator extends Operator
+    {
+        public EqualOperator()
+        {
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            if (operands.empty())
+            {
+                fewOperands("=");
+            }
+
+            // We cheat and use the knowledge that top (right) operand
+            // will always be a string because of the way code was generated
+            String rhs = (String) operands.pop();
+            if (operands.empty())
+            {
+                fewOperands("=");
+            }
+
+            Object lhs = operands.pop();
+
+            operands.push(new Boolean(compare(lhs, rhs, EQUAL)));
+        }
+
+        public String toString()
+        {
+            return "=()";
+        }
+
+        public void buildTree(Stack operands)
+        {
+            children = new Operator[2];
+            // need to preserve stack order
+            for (int i = 0; i < 2; i++)
+            {
+                Operator o = (Operator) operands.pop();
+                children[1 - i] = o;
+            }
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(");
+            for (int i = 0; i < children.length; i++)
+            {
+                Operator o = (Operator) children[i];
+                if (i > 0)
+                {
+                    b.append("=");
+                }
+                o.toStringInfix(b);
+            }
+            b.append(")");
+        }
+    }
+
+    private static class GreaterEqualOperator extends Operator
+    {
+        public GreaterEqualOperator()
+        {
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            if (operands.empty())
+            {
+                fewOperands(">=");
+            }
+            // We cheat and use the knowledge that top (right) operand
+            // will always be a string because of the way code was generated
+            String rhs = (String) operands.pop();
+            if (operands.empty())
+            {
+                fewOperands(">=");
+            }
+            Object lhs = operands.pop();
+
+            operands.push(new Boolean(compare(lhs, rhs, GREATER_EQUAL)));
+        }
+
+        public String toString()
+        {
+            return ">=()";
+        }
+
+        public void buildTree(Stack operands)
+        {
+            children = new Operator[2];
+            // need to preserve stack order
+            for (int i = 0; i < 2; i++)
+            {
+                children[1 - i] = (Operator) operands.pop();
+            }
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(");
+            for (int i = 0; i < children.length; i++)
+            {
+                Operator o = (Operator) children[i];
+                if (i > 0)
+                {
+                    b.append(">=");
+                }
+                o.toStringInfix(b);
+            }
+            b.append(")");
+        }
+    }
+
+    private static class LessEqualOperator extends Operator
+    {
+        public LessEqualOperator()
+        {
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            if (operands.empty())
+            {
+                fewOperands("<=");
+            }
+            // We cheat and use the knowledge that top (right) operand
+            // will always be a string because of the way code was generated
+            String rhs = (String) operands.pop();
+            if (operands.empty())
+            {
+                fewOperands("<=");
+            }
+            Object lhs = (Object) operands.pop();
+            operands.push(new Boolean(compare(lhs, rhs, LESS_EQUAL)));
+        }
+
+        public String toString()
+        {
+            return "<=()";
+        }
+
+        public void buildTree(Stack operands)
+        {
+            children = new Operator[2];
+            // need to preserve stack order
+            for (int i = 0; i < 2; i++)
+            {
+                children[1 - i] = (Operator) operands.pop();
+            }
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(");
+            for (int i = 0; i < children.length; i++)
+            {
+                Operator o = (Operator) children[i];
+                if (i > 0)
+                {
+                    b.append("<=");
+                }
+                o.toStringInfix(b);
+            }
+            b.append(")");
+        }
+    }
+
+    private static class ApproxOperator extends Operator
+    {
+        public ApproxOperator()
+        {
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            if (operands.empty())
+            {
+                fewOperands("~=");
+            }
+            // We cheat and use the knowledge that top (right) operand
+            // will always be a string because of the way code was generated
+            String rhs = (String) operands.pop();
+            if (operands.empty())
+            {
+                fewOperands("~=");
+            }
+            Object lhs = operands.pop();
+            operands.push(new Boolean(compare(lhs, rhs, APPROX)));
+        }
+
+        public String toString()
+        {
+            return "~=()";
+        }
+
+        public void buildTree(Stack operands)
+        {
+            children = new Operator[2];
+            // need to preserve stack order
+            for (int i = 0; i < 2; i++)
+            {
+                children[1 - i] = (Operator) operands.pop();
+            }
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(");
+            for (int i = 0; i < children.length; i++)
+            {
+                Operator o = (Operator) children[i];
+                if (i > 0)
+                {
+                    b.append("~=");
+                }
+                o.toStringInfix(b);
+            }
+            b.append(")");
+        }
+    }
+
+    private static class PresentOperator extends Operator
+    {
+        String attribute;
+
+        public PresentOperator(String attribute)
+        {
+            this.attribute = attribute;
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            Object value = mapper.lookup(attribute);
+            operands.push(new Boolean(value != null));
+        }
+
+        public String toString()
+        {
+            return attribute + "=*";
+        }
+
+        public void buildTree(Stack operands)
+        {
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(");
+            b.append(attribute + "=*");
+            b.append(")");
+        }
+    }
+
+    private static class PushOperator extends Operator
+    {
+        String attribute;
+
+        public PushOperator(String attribute)
+        {
+            this.attribute = attribute;
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            // find and push the value of a given attribute
+            Object value = mapper.lookup(attribute);
+            if (value == null)
+            {
+                throw new AttributeNotFoundException(
+                    "attribute " + attribute + " not found");
+            }
+            operands.push(value);
+        }
+
+        public String toString()
+        {
+            return "push(" + attribute + ")";
+        }
+
+        public String toStringInfix()
+        {
+            return attribute;
+        }
+
+        public void buildTree(Stack operands)
+        {
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append(attribute);
+        }
+    }
+
+    private static class ConstOperator extends Operator
+    {
+        Object val;
+
+        public ConstOperator(Object val)
+        {
+            this.val = val;
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            operands.push(val);
+        }
+
+        public String toString()
+        {
+            return "const(" + val + ")";
+        }
+
+        public String toStringInfix()
+        {
+            return val.toString();
+        }
+
+        public void buildTree(Stack operands)
+        {
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append(val.toString());
+        }
+    }
+
+    private static class SubStringOperator extends Operator
+        implements OperatorConstants
+    {
+        String[] pieces;
+
+        public SubStringOperator(String[] pieces)
+        {
+            this.pieces = pieces;
+        }
+
+        public void execute(Stack operands, Mapper mapper)
+            throws EvaluationException
+        {
+            if (operands.empty())
+            {
+                fewOperands("SUBSTRING");
+            }
+
+            Object op = operands.pop();
+
+            // The operand can either be a string or an array of strings.
+            if (op instanceof String)
+            {
+                operands.push(check((String) op));
+            }
+            else if (op instanceof String[])
+            {
+                // If one element of the array matches, then push true.
+                String[] ops = (String[]) op;
+                boolean result = false;
+                for (int i = 0; !result && (i < ops.length); i++)
+                {
+                    if (check(ops[i]) == Boolean.TRUE)
+                    {
+                        result = true;
+                    }
+                }
+
+                operands.push((result) ? Boolean.TRUE : Boolean.FALSE);
+            }
+            else
+            {
+                unsupportedType("SUBSTRING", op.getClass());
+            }
+        }
+
+        private Boolean check(String s)
+        {
+            // Walk the pieces to match the string
+            // There are implicit stars between each piece,
+            // and the first and last pieces might be "" to anchor the match.
+            // assert (pieces.length > 1)
+            // minimal case is <string>*<string>
+
+            Boolean result = Boolean.FALSE;
+            int len = pieces.length;
+
+            loop : for (int i = 0; i < len; i++)
+            {
+                String piece = (String) pieces[i];
+                int index = 0;
+                if (i == len - 1)
+                {
+                    // this is the last piece
+                    if (s.endsWith(piece))
+                    {
+                        result = Boolean.TRUE;
+                    }
+                    else
+                    {
+                        result = Boolean.FALSE;
+                    }
+                    break loop;
+                }
+                // initial non-star; assert index == 0
+                else if (i == 0)
+                {
+                    if (!s.startsWith(piece))
+                    {
+                        result = Boolean.FALSE;
+                        break loop;
+                    }
+                }
+                // assert i > 0 && i < len-1
+                else
+                {
+                    // Sure wish stringbuffer supported e.g. indexOf
+                    index = s.indexOf(piece, index);
+                    if (index < 0)
+                    {
+                        result = Boolean.FALSE;
+                        break loop;
+                    }
+                }
+                // start beyond the matching piece
+                index += piece.length();
+            }
+
+            return result;
+        }
+
+        public String toString()
+        {
+            StringBuffer b = new StringBuffer();
+            b.append("substring(");
+            for (int i = 0; i < pieces.length; i++)
+            {
+                String piece = pieces[i];
+                if (i > 0)
+                {
+                    b.append("*");
+                }
+                b.append(escape(piece));
+            }
+            b.append(")");
+            return b.toString();
+        }
+
+        public String escape(String s)
+        {
+            int len = s.length();
+            StringBuffer buf = new StringBuffer(len);
+            for (int i = 0; i < len; i++)
+            {
+                char c = s.charAt(i);
+                if (c == ')' || c == '*')
+                    buf.append('\\');
+                buf.append(c);
+            }
+            return buf.toString();
+        }
+
+        public void buildTree(Stack operands)
+        {
+            children = new Operator[1];
+            children[0] = (Operator) operands.pop();
+            operands.push(this);
+        }
+
+        public void toStringInfix(StringBuffer b)
+        {
+            b.append("(");
+            children[0].toStringInfix(b); // dump attribute
+            b.append("=");
+            for (int i = 0; i < pieces.length; i++)
+            {
+                String piece = (String) pieces[i];
+                if (i > 0)
+				{
+                    b.append("*");
+                }
+                b.append(piece);
+            }
+            b.append(")");
+        }
+    }
+
+    // Utility classes and Interfaces
+
+    private interface OperatorConstants
+    {
+        static final int SSINIT = 0;
+        static final int SSFINAL = 1;
+        static final int SSMIDDLE = 2;
+        static final int SSANY = 3;
+    }
+
+    /**
+     * Compare two operands in an expression with respect  
+     * to the following operators =, <=, >= and ~=
+     * 
+     * Example: value=100
+     *
+     * @param lhs an object that implements comparable or an array of
+     *            objects that implement comparable.
+     * @param rhs a string representing the right operand.
+     * @param operator an integer that represents the operator.
+     * @return <tt>true</tt> or <tt>false</tt> according to the evaluation.
+     * @throws EvaluationException if it is not possible to do the comparison.
+    **/
+    public static boolean compare(Object lhs, String rhs, int operator)
+        throws EvaluationException
+    {
+        // Determine class of LHS.
+        Class lhsClass = null;
+
+        // If LHS is an array, then call compare() on each element
+        // of the array until a match is found.
+        if (lhs.getClass().isArray())
+        {
+            // First, if this is an array of primitives, then convert
+            // the entire array to an array of the associated
+            // primitive wrapper class instances.
+            if (lhs.getClass().getComponentType().isPrimitive())
+            {
+                lhs = convertPrimitiveArray(lhs);
+            }
+
+            // Now call compare on each element of array.
+            Object[] array = (Object[]) lhs;
+            for (int i = 0; i < array.length; i++)
+            {
+                if (compare(array[i], rhs, operator))
+                {
+                    return true;
+                }
+            }
+        }
+        // If LHS is a vector, then call compare() on each element
+        // of the vector until a match is found.
+        else if (lhs instanceof Vector)
+        {
+            for (Enumeration e = ((Vector) lhs).elements(); e.hasMoreElements();)
+            {
+                if (compare(e.nextElement(), rhs, operator))
+                {
+                    return true;
+                }
+            }
+        }
+        else
+        {
+            // Get the class of LHS.
+            lhsClass = lhs.getClass();
+
+            // At this point we are expecting the LHS to be a comparable,
+            // but Boolean is a special case since it is the only primitive
+            // wrapper class that does not implement comparable; deal with
+            // Boolean separately.
+            if (lhsClass == Boolean.class)
+            {
+                return compareBoolean(lhs, rhs, operator);
+            }
+        
+            // If LHS is not a Boolean, then verify it is a comparable
+            // and perform comparison.
+            if (!(Comparable.class.isAssignableFrom(lhsClass)))
+            {
+                String opName = null;
+                switch (operator)
+                {
+                    case EQUAL :
+                        opName = "=";
+                    case GREATER_EQUAL :
+                        opName = ">=";
+                    case LESS_EQUAL :
+                        opName = "<=";
+                    case APPROX:
+                        opName = "~=";
+                    default:
+                        opName = "UNKNOWN OP";
+                }
+
+                unsupportedType(opName, lhsClass);
+            }
+
+            // We will try to create a comparable object from the
+            // RHS string.
+            Comparable rhsComparable = null;
+            try
+            {
+                // We are expecting to be able to construct a comparable
+                // instance from the RHS string by passing it into the
+                // constructor of the corresponing comparable class. The
+                // Character class is a special case, since its constructor
+                // does not take a string, so handle it separately.
+                if (lhsClass == Character.class)
+                {
+                    rhsComparable = new Character(rhs.charAt(0));
+                }
+                else
+                {
+                    rhsComparable = (Comparable) lhsClass
+                        .getConstructor(new Class[] { String.class })
+                            .newInstance(new Object[] { rhs });
+                }
+            }
+            catch (Exception ex)
+            {
+                String msg = (ex.getCause() == null)
+                    ? ex.toString() : ex.getCause().toString();
+                throw new EvaluationException(
+                    "Could not instantiate class "
+                        + lhsClass.getName()
+                        + " with constructor String parameter "
+                        + rhs + " " + msg);
+            }
+
+            Comparable lhsComparable = (Comparable) lhs;
+
+            switch (operator)
+            {
+                case EQUAL :
+                    return (lhsComparable.compareTo(rhsComparable) == 0);
+                case GREATER_EQUAL :
+                    return (lhsComparable.compareTo(rhsComparable) >= 0);
+                case LESS_EQUAL :
+                    return (lhsComparable.compareTo(rhsComparable) <= 0);
+                case APPROX:
+                    return compareToApprox(lhsComparable, rhsComparable);
+                default:
+                    throw new EvaluationException("Unknown comparison operator..."
+                        + operator);
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * This is an ugly utility method to convert an array of primitives
+     * to an array of primitive wrapper objects. This method simplifies
+     * processing LDAP filters since the special case of primitive arrays
+     * can be ignored.
+     * @param array
+     * @return
+    **/
+    private static Object[] convertPrimitiveArray(Object array)
+    {
+        Class clazz = array.getClass().getComponentType();
+
+        if (clazz == Boolean.TYPE)
+        {
+            boolean[] src = (boolean[]) array;
+            array = new Boolean[src.length];
+            for (int i = 0; i < src.length; i++)
+            {
+                ((Object[]) array)[i] = new Boolean(src[i]);
+            }
+        }
+        else if (clazz == Character.TYPE)
+        {
+            char[] src = (char[]) array;
+            array = new Character[src.length];
+            for (int i = 0; i < src.length; i++)
+            {
+                ((Object[]) array)[i] = new Character(src[i]);
+            }
+        }
+        else if (clazz == Byte.TYPE)
+        {
+            byte[] src = (byte[]) array;
+            array = new Byte[src.length];
+            for (int i = 0; i < src.length; i++)
+            {
+                ((Object[]) array)[i] = new Byte(src[i]);
+            }
+        }
+        else if (clazz == Short.TYPE)
+        {
+            byte[] src = (byte[]) array;
+            array = new Byte[src.length];
+            for (int i = 0; i < src.length; i++)
+            {
+                ((Object[]) array)[i] = new Byte(src[i]);
+            }
+        }
+        else if (clazz == Integer.TYPE)
+        {
+            int[] src = (int[]) array;
+            array = new Integer[src.length];
+            for (int i = 0; i < src.length; i++)
+            {
+                ((Object[]) array)[i] = new Integer(src[i]);
+            }
+        }
+        else if (clazz == Long.TYPE)
+        {
+            long[] src = (long[]) array;
+            array = new Long[src.length];
+            for (int i = 0; i < src.length; i++)
+            {
+                ((Object[]) array)[i] = new Long(src[i]);
+            }
+        }
+        else if (clazz == Float.TYPE)
+        {
+            float[] src = (float[]) array;
+            array = new Float[src.length];
+            for (int i = 0; i < src.length; i++)
+            {
+                ((Object[]) array)[i] = new Float(src[i]);
+            }
+        }
+        else if (clazz == Double.TYPE)
+        {
+            double[] src = (double[]) array;
+            array = new Double[src.length];
+            for (int i = 0; i < src.length; i++)
+            {
+                ((Object[]) array)[i] = new Double(src[i]);
+            }
+        }
+
+        return (Object[]) array;
+    }
+
+    private static boolean compareBoolean(Object lhs, String rhs, int operator)
+        throws EvaluationException
+    {
+        Boolean rhsBoolean = new Boolean(rhs);
+        if (lhs.getClass().isArray())
+        {
+            Object[] objs = (Object[]) lhs;
+            for (int i = 0; i < objs.length; i++)
+            {
+                switch (operator)
+                {
+                    case EQUAL :
+                    case GREATER_EQUAL :
+                    case LESS_EQUAL :
+                    case APPROX:
+                        if (objs[i].equals(rhsBoolean))
+                        {
+                            return true;
+                        }
+                        break;
+                    default:
+                        throw new EvaluationException(
+                            "Unknown comparison operator: " + operator);   
+                }
+            }
+            return false;
+        }
+        else
+        {
+            switch (operator)
+            {
+                case EQUAL :
+                case GREATER_EQUAL :
+                case LESS_EQUAL :
+                case APPROX:
+                    return (lhs.equals(rhsBoolean));
+                default:
+                    throw new EvaluationException("Unknown comparison operator..."
+                        + operator);
+            }
+        }
+    }
+
+    /**
+     * Test if two objects are approximate. The two objects that are passed must
+     * have the same type.
+     * 
+     * Approximate for numerical values involves a difference of less than APPROX_CRITERIA
+     * Approximate for string values is calculated by using the Levenshtein distance
+     * between strings. Less than APPROX_CRITERIA of difference is considered as approximate.
+     * 
+     * Supported types only include the following subclasses of Number:
+     * - Byte
+     * - Double
+     * - Float
+     * - Int
+     * - Long
+     * - Short 
+     * - BigInteger
+     * - BigDecimal
+     * As subclasses of Number must provide methods to convert the represented numeric value 
+     * to byte, double, float, int, long, and short. (see API)
+     * 
+     * @param obj1
+     * @param obj2
+     * @return true if they are approximate
+     * @throws EvaluationException if it the two objects cannot be approximated
+    **/
+    private static boolean compareToApprox(Object obj1, Object obj2) throws EvaluationException
+    {
+        if (obj1 instanceof Byte)
+        {
+            byte value1 = ((Byte)obj1).byteValue();
+            byte value2 = ((Byte)obj2).byteValue();
+            return (value2 >= (value1-((Math.abs(value1)*(byte)APPROX_CRITERIA)/(byte)100)) 
+                && value2 <= (value1+((Math.abs(value1)*(byte)APPROX_CRITERIA)/(byte)100)));
+        }
+        else if (obj1 instanceof Character)
+        {
+            char value1 = ((Character)obj1).charValue();
+            char value2 = ((Character)obj2).charValue();
+            return (value2 >= (value1-((Math.abs(value1)*(char)APPROX_CRITERIA)/(char)100)) 
+                && value2 <= (value1+((Math.abs(value1)*(char)APPROX_CRITERIA)/(char)100)));
+        }
+        else if (obj1 instanceof Double)
+        {
+            double value1 = ((Double)obj1).doubleValue();
+            double value2 = ((Double)obj2).doubleValue();
+            return (value2 >= (value1-((Math.abs(value1)*(double)APPROX_CRITERIA)/(double)100)) 
+                && value2 <= (value1+((Math.abs(value1)*(double)APPROX_CRITERIA)/(double)100)));
+        }
+        else if (obj1 instanceof Float)
+        {
+            float value1 = ((Float)obj1).floatValue();
+            float value2 = ((Float)obj2).floatValue();
+            return (value2 >= (value1-((Math.abs(value1)*(float)APPROX_CRITERIA)/(float)100)) 
+                && value2 <= (value1+((Math.abs(value1)*(float)APPROX_CRITERIA)/(float)100)));
+        }
+        else if (obj1 instanceof Integer)
+        {
+            int value1 = ((Integer)obj1).intValue();
+            int value2 = ((Integer)obj2).intValue();
+            return (value2 >= (value1-((Math.abs(value1)*(int)APPROX_CRITERIA)/(int)100)) 
+                && value2 <= (value1+((Math.abs(value1)*(int)APPROX_CRITERIA)/(int)100)));
+        }
+        else if (obj1 instanceof Long)
+        {
+            long value1 = ((Long)obj1).longValue();
+            long value2 = ((Long)obj2).longValue();
+            return (value2 >= (value1-((Math.abs(value1)*(long)APPROX_CRITERIA)/(long)100)) 
+                && value2 <= (value1+((Math.abs(value1)*(long)APPROX_CRITERIA)/(long)100)));
+        }
+        else if (obj1 instanceof Short)
+        {
+            short value1 = ((Short)obj1).shortValue();
+            short value2 = ((Short)obj2).shortValue();
+            return (value2 >= (value1-((Math.abs(value1)*(short)APPROX_CRITERIA)/(short)100)) 
+                && value2 <= (value1+((Math.abs(value1)*(short)APPROX_CRITERIA)/(short)100)));
+        }
+        else if (obj1 instanceof String)
+        {
+            int distance = getDistance(obj1.toString(),obj2.toString());
+            int size = ((String)obj1).length();
+            return (distance <= ((size*APPROX_CRITERIA)/100));
+        }
+        else if (m_hasBigNumbers && (obj1 instanceof BigInteger))
+        {
+            BigInteger value1 = (BigInteger)obj1;
+            BigInteger value2 = (BigInteger)obj2;
+            BigInteger delta = value1.abs().multiply(
+                BigInteger.valueOf(APPROX_CRITERIA)
+                    .divide(BigInteger.valueOf(100)));
+            BigInteger low = value1.subtract(delta);
+            BigInteger high = value1.add(delta);
+            return (value2.compareTo(low) >= 0) && (value2.compareTo(high) <= 0);
+        }
+        else if (m_hasBigNumbers && (obj1 instanceof BigDecimal))
+        {
+            BigDecimal value1 = (BigDecimal)obj1;
+            BigDecimal value2 = (BigDecimal)obj2;
+            BigDecimal delta = value1.abs().multiply(
+                BigDecimal.valueOf(APPROX_CRITERIA)
+                    .divide(BigDecimal.valueOf(100), BigDecimal.ROUND_HALF_DOWN));
+            BigDecimal low = value1.subtract(delta);
+            BigDecimal high = value1.add(delta);
+            return (value2.compareTo(low) >= 0) && (value2.compareTo(high) <= 0);
+        }
+        throw new EvaluationException(
+            "Approximate operator not supported for type "
+            + obj1.getClass().getName());
+    }
+
+    /**
+     * Calculate the Levenshtein distance (LD) between two strings.
+     * The Levenshteing distance is a measure of the similarity between 
+     * two strings, which we will refer to as the source string (s) and 
+     * the target string (t). The distance is the number of deletions, 
+     * insertions, or substitutions required to transform s into t.
+     * 
+     * Algorithm from: http://www.merriampark.com/ld.htm
+     * 
+     * @param s the first string
+     * @param t the second string
+     * @return
+     */
+    private static int getDistance(String s, String t)
+    {
+        int d[][]; // matrix
+        int n; // length of s
+        int m; // length of t
+        int i; // iterates through s
+        int j; // iterates through t
+        char s_i; // ith character of s
+        char t_j; // jth character of t
+        int cost; // cost
+
+        // Step 1
+        n = s.length();
+        m = t.length();
+        if (n == 0)
+        {
+            return m;
+        }
+        if (m == 0)
+        {
+            return n;
+        }
+        d = new int[n + 1][m + 1];
+
+        // Step 2
+        for (i = 0; i <= n; i++)
+        {
+            d[i][0] = i;
+        }
+
+        for (j = 0; j <= m; j++)
+        {
+            d[0][j] = j;
+        }
+
+        // Step 3
+        for (i = 1; i <= n; i++)
+        {
+            s_i = s.charAt(i - 1);
+            // Step 4
+            for (j = 1; j <= m; j++)
+            {
+                t_j = t.charAt(j - 1);
+                // Step 5
+                if (s_i == t_j)
+                {
+                    cost = 0;
+                }
+                else
+                {
+                    cost = 1;
+                }
+                // Step 6
+                d[i][j] =
+                    Minimum(
+                        d[i - 1][j] + 1,
+                        d[i][j - 1] + 1,
+                        d[i - 1][j - 1] + cost);
+            }
+        }
+        // Step 7
+        return d[n][m];
+    }
+
+    /**
+     * Calculate the minimum between three values
+     * 
+     * @param a
+     * @param b
+     * @param c
+     * @return
+     */
+    private static int Minimum(int a, int b, int c)
+    {
+        int mi;
+        mi = a;
+        if (b < mi)
+        {
+            mi = b;
+        }
+        if (c < mi)
+        {
+            mi = c;
+        }
+        return mi;
+    }
+
+    private static void fewOperands(String op) throws EvaluationException
+    {
+        throw new EvaluationException(op + ": too few operands");
+    }
+
+    private static void unsupportedType(String opStr, Class clazz)
+        throws EvaluationException
+    {
+        throw new EvaluationException(
+            opStr + ": unsupported type " + clazz.getName(), clazz);
+    }
+}
\ No newline at end of file
diff --git a/src/org/apache/osgi/framework/util/ldap/Unknown.java b/src/org/apache/osgi/framework/util/ldap/Unknown.java
new file mode 100644
index 0000000..8139f8b
--- /dev/null
+++ b/src/org/apache/osgi/framework/util/ldap/Unknown.java
@@ -0,0 +1,29 @@
+/*
+ *   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.osgi.framework.util.ldap;
+
+/**
+ * This class is used to create simple marker instances that are inserted
+ * into the evaluation stack of a LDAP filter expression when an attribute
+ * is referenced that has no defined value. These invalid marker instances
+ * force the operators to throw an "unsupported type" exception, which the
+ * evaluator catches and then converts the entire subexpression containing
+ * the non-existent attribute to <tt>false</tt>.
+**/
+class Unknown
+{
+}