1 9 package org.eclipse.osgi.framework.internal.protocol; 10 11 import java.lang.reflect.Method ; 12 import java.util.*; 13 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor; 14 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 15 import org.osgi.framework.BundleContext; 16 import org.osgi.service.packageadmin.PackageAdmin; 17 import org.osgi.util.tracker.ServiceTracker; 18 19 23 public abstract class MultiplexingFactory { 24 25 protected static final String PACKAGEADMINCLASS = "org.osgi.service.packageadmin.PackageAdmin"; protected BundleContext context; 27 protected FrameworkAdaptor adaptor; 28 private List factories; private ServiceTracker packageAdminTracker; 30 31 private static class InternalSecurityManager extends SecurityManager { 33 public Class [] getClassContext() { 34 return super.getClassContext(); 35 } 36 } 37 38 private static InternalSecurityManager internalSecurityManager = new InternalSecurityManager(); 39 40 MultiplexingFactory(BundleContext context, FrameworkAdaptor adaptor) { 41 this.context = context; 42 this.adaptor = adaptor; 43 packageAdminTracker = new ServiceTracker(context, PACKAGEADMINCLASS, null); 44 packageAdminTracker.open(); 45 } 46 47 abstract public void setParentFactory(Object parentFactory); 48 49 abstract public Object getParentFactory(); 50 51 public synchronized boolean isMultiplexing() { 52 return factories != null; 53 } 54 55 public synchronized void register(Object factory) { 56 if (factories == null) 57 factories = new LinkedList(); 58 59 try { 61 Class clazz = factory.getClass(); 62 Method setParentFactory = clazz.getMethod("setParentFactory", new Class [] {Object .class}); setParentFactory.invoke(factory, new Object [] {getParentFactory()}); 64 } catch (Exception e) { 65 adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingFactory.class.getName(), FrameworkLogEntry.ERROR, 0, "register", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 67 } 68 factories.add(factory); 69 } 70 71 public synchronized void unregister(Object factory) { 72 factories.remove(factory); 73 if (factories.isEmpty()) 74 factories = null; 75 try { 77 Method closeTracker = factory.getClass().getSuperclass().getDeclaredMethod("closePackageAdminTracker", null); closeTracker.setAccessible(true); closeTracker.invoke(factory, null); 81 } catch (Exception e) { 82 adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingFactory.class.getName(), FrameworkLogEntry.ERROR, 0, "unregister", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 84 } 85 } 86 87 public synchronized Object designateSuccessor() { 88 Object parentFactory = getParentFactory(); 89 if (factories == null || factories.isEmpty()) 90 return parentFactory; 91 92 Object successor = factories.remove(0); 93 try { 94 Class clazz = successor.getClass(); 95 Method register = clazz.getMethod("register", new Class [] {Object .class}); for (Iterator it = factories.iterator(); it.hasNext();) { 97 register.invoke(successor, new Object [] {it.next()}); 98 } 99 } catch (Exception e) { 100 adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingFactory.class.getName(), FrameworkLogEntry.ERROR, 0, "designateSuccessor", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 102 } 103 factories = null; 104 closePackageAdminTracker(); return successor; 106 } 107 108 private void closePackageAdminTracker() { 109 packageAdminTracker.close(); 110 } 111 112 public synchronized Object findAuthorizedFactory(List ignoredClasses) { 113 Class [] classStack = internalSecurityManager.getClassContext(); 114 for (int i = 0; i < classStack.length; i++) { 115 Class clazz = classStack[i]; 116 if (clazz == InternalSecurityManager.class || clazz == MultiplexingFactory.class || ignoredClasses.contains(clazz)) 117 continue; 118 if (hasAuthority(clazz)) 119 return this; 120 if (factories == null) 121 continue; 122 for (Iterator it = factories.iterator(); it.hasNext();) { 123 Object factory = it.next(); 124 try { 125 Method hasAuthorityMethod = factory.getClass().getMethod("hasAuthority", new Class [] {Class .class}); if (((Boolean ) hasAuthorityMethod.invoke(factory, new Object [] {clazz})).booleanValue()) { 127 return factory; 128 } 129 } catch (Exception e) { 130 adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingFactory.class.getName(), FrameworkLogEntry.ERROR, 0, "findAuthorizedURLStreamHandler-loop", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 132 } 133 } 134 } 135 return null; 136 } 137 138 public boolean hasAuthority(Class clazz) { 139 PackageAdmin packageAdminService = (PackageAdmin) packageAdminTracker.getService(); 140 if (packageAdminService != null) { 141 return packageAdminService.getBundle(clazz) != null; 142 } 143 return false; 144 } 145 } 146 | Popular Tags |