Bug 426059 - IllegalStateException stopping eclipse
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
index 92ecfd7..ec62923 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008, 2013 IBM Corporation and others.
+ * Copyright (c) 2008, 2014 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -12,6 +12,7 @@
 
 import java.io.*;
 import java.net.*;
+import java.security.Permission;
 import java.security.PrivilegedAction;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
@@ -2033,6 +2034,54 @@
 
 	}
 
+	public void testDynamicSecurityManager() throws BundleException {
+		SecurityManager sm = System.getSecurityManager();
+		assertNull("SecurityManager must be null to test.", sm);
+		try {
+			File config = OSGiTestsActivator.getContext().getDataFile(getName()); //$NON-NLS-1$
+			Map<String, Object> configuration = new HashMap<String, Object>();
+			configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
+			Equinox equinox = new Equinox(configuration);
+			try {
+				equinox.start();
+			} catch (BundleException e) {
+				fail("Unexpected exception in start()", e); //$NON-NLS-1$
+			}
+			Bundle substitutesA = null;
+			try {
+				substitutesA = equinox.getBundleContext().installBundle(installer.getBundleLocation("substitutes.a")); //$NON-NLS-1$
+			} catch (BundleException e1) {
+				fail("failed to install a bundle", e1); //$NON-NLS-1$
+			}
+			assertTrue("BundleCould not resolve.", equinox.adapt(FrameworkWiring.class).resolveBundles(Collections.singleton(substitutesA)));
+			substitutesA.adapt(BundleWiring.class).findEntries("/", null, 0);
+			// set security manager after resolving
+			System.setSecurityManager(new SecurityManager() {
+
+				@Override
+				public void checkPermission(Permission perm, Object context) {
+					// do nothing
+				}
+
+				@Override
+				public void checkPermission(Permission perm) {
+					// do nothing
+				}
+			});
+			equinox.stop();
+			try {
+				FrameworkEvent event = equinox.waitForStop(10000);
+				assertEquals("Wrong event.", FrameworkEvent.STOPPED, event.getType());
+			} catch (InterruptedException e) {
+				Thread.currentThread().interrupt();
+				fail("Unexpected interruption.", e);
+			}
+			assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState()); //$NON-NLS-1$
+		} finally {
+			System.setSecurityManager(null);
+		}
+	}
+
 	private static File[] createBundles(File outputDir, int bundleCount) throws IOException {
 		outputDir.mkdirs();
 
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxBundle.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxBundle.java
index 9ec838a..3e939f0 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxBundle.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxBundle.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2013 IBM Corporation and others.
+ * Copyright (c) 2012, 2014 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -156,9 +156,8 @@
 							public void run() {
 								try {
 									stop();
-								} catch (BundleException e) {
-									// TODO not sure we can even log if this fails
-									e.printStackTrace();
+								} catch (Throwable e) {
+									SystemBundle.this.getEquinoxContainer().getLogServices().log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Error stopping the framework.", e); //$NON-NLS-1$
 								}
 							}
 						}, "Framework stop"); //$NON-NLS-1$
@@ -178,9 +177,8 @@
 							public void run() {
 								try {
 									update();
-								} catch (BundleException e) {
-									e.printStackTrace();
-									// TODO not sure we can even log if this fails
+								} catch (Throwable e) {
+									SystemBundle.this.getEquinoxContainer().getLogServices().log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Error updating the framework.", e); //$NON-NLS-1$
 								}
 							}
 						}, "Framework update"); //$NON-NLS-1$
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/SecurityAdmin.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/SecurityAdmin.java
index 33a94da..09eeaac 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/SecurityAdmin.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/permadmin/SecurityAdmin.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008, 2012 IBM Corporation and others.
+ * Copyright (c) 2008, 2014 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -345,7 +345,13 @@
 
 	private ProtectionDomain createProtectionDomain(Bundle bundle, SecurityAdmin sa) {
 		PermissionInfoCollection impliedPermissions = getImpliedPermission(bundle);
-		PermissionInfo[] restrictedInfos = getFileRelativeInfos(SecurityAdmin.getPermissionInfos(bundle.getEntry("OSGI-INF/permissions.perm")), bundle); //$NON-NLS-1$
+		URL permEntry = null;
+		try {
+			permEntry = bundle.getEntry("OSGI-INF/permissions.perm"); //$NON-NLS-1$
+		} catch (IllegalStateException e) {
+			// bundle may be uninstalled
+		}
+		PermissionInfo[] restrictedInfos = getFileRelativeInfos(SecurityAdmin.getPermissionInfos(permEntry), bundle);
 		PermissionInfoCollection restrictedPermissions = restrictedInfos == null ? null : new PermissionInfoCollection(restrictedInfos);
 		BundlePermissions bundlePermissions = new BundlePermissions(bundle, sa, impliedPermissions, restrictedPermissions);
 		return new ProtectionDomain(null, bundlePermissions);
@@ -372,9 +378,14 @@
 				if (!"<<ALL FILES>>".equals(permissionInfos[i].getName())) { //$NON-NLS-1$
 					File file = new File(permissionInfos[i].getName());
 					if (!file.isAbsolute()) { // relative name
-						File target = bundle.getDataFile(permissionInfos[i].getName());
-						if (target != null)
-							results[i] = new PermissionInfo(permissionInfos[i].getType(), target.getPath(), permissionInfos[i].getActions());
+						try {
+							File target = bundle.getDataFile(permissionInfos[i].getName());
+							if (target != null)
+								results[i] = new PermissionInfo(permissionInfos[i].getType(), target.getPath(), permissionInfos[i].getActions());
+						} catch (IllegalStateException e) {
+							// can happen if the bundle has been uninstalled;
+							// we just keep the original permission in this case.
+						}
 					}
 				}
 			}