You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2008/11/21 19:28:19 UTC

svn commit: r719667 - in /felix/trunk/framework/src/main/java/org/apache/felix/framework: Felix.java searchpolicy/R4SearchPolicyCore.java util/Util.java

Author: rickhall
Date: Fri Nov 21 10:28:19 2008
New Revision: 719667

URL: http://svn.apache.org/viewvc?rev=719667&view=rev
Log:
Applied patch (FELIX-820) to throw an exception when there is an attempt
to start or stop a fragment, as per the spec.

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java?rev=719667&r1=719666&r2=719667&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java Fri Nov 21 10:28:19 2008
@@ -1181,8 +1181,9 @@
                     }
                 }
                 // Stop the bundle if necessary.
-                else if (impl.getInfo().getStartLevel(getInitialBundleStartLevel())
-                    > m_activeStartLevel)
+                else if ((impl.getInfo().getPersistentState() == Bundle.ACTIVE) &&
+                    (impl.getInfo().getStartLevel(getInitialBundleStartLevel())
+                        > m_activeStartLevel))
                 {
                     try
                     {
@@ -1598,6 +1599,13 @@
             return;
         }
 
+        // As per the OSGi spec, fragment bundles can not be started and must
+        // throw a BundleException when there is an attempt to start one.
+        if (Util.isFragment(info.getCurrentModule()))
+        {
+            throw new BundleException("Fragment bundles can not be started.");
+        }
+
         // Set and save the bundle's persistent state to active
         // if we are supposed to record state change.
         if (record)
@@ -1831,8 +1839,12 @@
                 updateLocation = info.getLocation();
             }
 
-            // Stop the bundle, but do not change the persistent state.
-            stopBundle(bundle, false);
+            // Stop the bundle if it is active, but do not change its
+            // persistent state.
+            if (oldState == Bundle.ACTIVE)
+            {
+                stopBundle(bundle, false);
+            }
 
             try
             {
@@ -1962,9 +1974,18 @@
                 }
             }
 
-            // Restart the bundle if necessary, but do not change its
-            // persistent state.
-            if (oldState == Bundle.ACTIVE)
+            // If the old state was active, but the new module is a fragment,
+            // then mark the persistent state to inactive.
+            if ((oldState == Bundle.ACTIVE) && Util.isFragment(info.getCurrentModule()))
+            {
+                info.setPersistentStateInactive();
+                m_logger.log(Logger.LOG_WARNING,
+                    "Previously active bundle was updated to a fragment, resetting state to inactive: "
+                    + bundle);
+            }
+            // Otherwise, restart the bundle if it was previously active,
+            // but do not change its persistent state.
+            else if (oldState == Bundle.ACTIVE)
             {
                 startBundle(bundle, false);
             }
@@ -2024,6 +2045,13 @@
 
         BundleInfo info = bundle.getInfo();
 
+        // As per the OSGi spec, fragment bundles can not be stopped and must
+        // throw a BundleException when there is an attempt to stop one.
+        if (Util.isFragment(info.getCurrentModule()))
+        {
+            throw new BundleException("Fragment bundles can not be stopped.");
+        }
+
         switch (info.getState())
         {
             case Bundle.UNINSTALLED:
@@ -3224,7 +3252,7 @@
             releaseBundleLocks(bundles);
         }
     }
-    
+
     protected void _refreshPackages(FelixBundle[] bundles)
     {
         boolean restart = false;
@@ -4148,7 +4176,7 @@
             m_installRequestLock_Priority1.notifyAll();
         }
     }
-    
+
     private long m_lockCount = 0;
     private Thread m_lockThread = null;
 
@@ -4248,7 +4276,7 @@
             }
             m_lockCount = -1;
             m_lockThread = Thread.currentThread();
-            
+
             boolean success = false;
             while (!success)
             {
@@ -4341,7 +4369,7 @@
             }
             m_lockCount = -1;
             m_lockThread = Thread.currentThread();
-            
+
             boolean success = false;
             while (!success)
             {
@@ -4462,4 +4490,4 @@
             m_bundleLock.notifyAll();
         }
     }
-}
+}
\ No newline at end of file

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java?rev=719667&r1=719666&r2=719667&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java Fri Nov 21 10:28:19 2008
@@ -79,7 +79,7 @@
     // Reusable empty array.
     public static final IModule[] m_emptyModules = new IModule[0];
     public static final ICapability[] m_emptyCapabilities = new ICapability[0];
-    public static final PackageSource[] m_emptySources= new PackageSource[0];
+    public static final PackageSource[] m_emptySources = new PackageSource[0];
 
     // Re-usable security manager for accessing class context.
     private static SecurityManagerEx m_sm = new SecurityManagerEx();
@@ -1043,7 +1043,7 @@
 // TODO: FRAGMENT - Currently we just make a single selection of the available
 //       fragments or hosts and try to resolve. In case of failure, we do not
 //       backtrack. We will likely want to add backtracking.
-            if (isFragment(rootModule))
+            if (Util.isFragment(rootModule))
             {
                 targetFragment = rootModule;
                 List hostList = getPotentialHosts(targetFragment);
@@ -1150,20 +1150,7 @@
         }
     }
 
-    private boolean isFragment(IModule module)
-    {
-        if (module.getDefinition() instanceof ModuleDefinition)
-        {
-            Map headerMap = ((ModuleDefinition) module.getDefinition()).getHeaders();
-            if (headerMap.containsKey(Constants.FRAGMENT_HOST))
-            {
-                return true;
-            }
-        }
-        return false;
-    }
-
-// TODO: FRAGMENT - Not very efficient.
+    // TODO: FRAGMENT - Not very efficient.
     private List getPotentialHosts(IModule fragment)
         throws ResolveException
     {

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java?rev=719667&r1=719666&r2=719667&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/Util.java Fri Nov 21 10:28:19 2008
@@ -25,9 +25,12 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+
+import org.apache.felix.framework.searchpolicy.ModuleDefinition;
 import org.apache.felix.framework.util.manifestparser.Capability;
 import org.apache.felix.moduleloader.*;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 
 public class Util
@@ -512,4 +515,21 @@
         // Return the value.
         return val;
     }
+
+    /**
+     * Checks if the provided module definition declares a fragment host.
+     *
+     * @param module the module to check
+     * @return <code>true</code> if the module declares a fragment host, <code>false</code>
+     *      otherwise.
+     */
+    public static boolean isFragment(IModule module)
+    {
+        if (module.getDefinition() instanceof ModuleDefinition)
+        {
+            Map headerMap = ((ModuleDefinition) module.getDefinition()).getHeaders();
+            return headerMap.containsKey(Constants.FRAGMENT_HOST);
+        }
+        return false;
+    }
 }
\ No newline at end of file