1 36 package org.ungoverned.oscar.util; 37 38 import java.security.AccessControlContext ; 39 import java.security.AccessController ; 40 import java.security.PrivilegedAction ; 41 42 import org.osgi.framework.*; 43 44 public class ServiceListenerWrapper extends ListenerWrapper implements ServiceListener 45 { 46 private Filter m_filter = null; 48 private AccessControlContext m_acc = null; 50 51 public ServiceListenerWrapper(Bundle bundle, ServiceListener l, Filter filter) 52 { 53 super(bundle, ServiceListener.class, l); 54 m_filter = filter; 55 56 if (System.getSecurityManager() != null) 59 { 60 m_acc = AccessController.getContext(); 61 } 62 } 63 64 public void setFilter(Filter filter) 65 { 66 m_filter = filter; 67 } 68 69 public void serviceChanged(final ServiceEvent event) 70 { 71 if ((getBundle().getState() != Bundle.STARTING) && 74 (getBundle().getState() != Bundle.STOPPING) && 75 (getBundle().getState() != Bundle.ACTIVE)) 76 { 77 return; 78 } 79 80 ServiceReference ref = event.getServiceReference(); 84 String [] objectClass = (String []) ref.getProperty(Constants.OBJECTCLASS); 85 86 if (objectClass != null) 89 { 90 boolean hasPermission = false; 91 if (m_acc != null) 92 { 93 for (int i = 0; 94 !hasPermission && (i < objectClass.length); 95 i++) 96 { 97 try { 98 ServicePermission perm = 99 new ServicePermission( 100 objectClass[i], ServicePermission.GET); 101 m_acc.checkPermission(perm); 102 hasPermission = true; 103 } catch (Exception ex) { 104 } 105 } 106 } 107 else 108 { 109 hasPermission = true; 110 } 111 112 if (hasPermission) 113 { 114 if ((m_filter == null) || m_filter.match(event.getServiceReference())) 116 { 117 if (System.getSecurityManager() != null) 118 { 119 AccessController.doPrivileged(new PrivilegedAction () { 120 public Object run() 121 { 122 ((ServiceListener) getListener()).serviceChanged(event); 123 return null; 124 } 125 }); 126 } 127 else 128 { 129 ((ServiceListener) getListener()).serviceChanged(event); 130 } 131 } 132 } 133 } 134 } 135 } | Popular Tags |