1 36 package org.ungoverned.oscar; 37 38 import java.security.AccessController ; 39 import java.security.PrivilegedExceptionAction ; 40 import java.util.*; 41 import java.util.ArrayList ; 42 import java.util.Dictionary ; 43 import java.util.Enumeration ; 44 45 import org.osgi.framework.*; 46 import org.ungoverned.oscar.util.CaseInsensitiveMap; 47 48 class ServiceRegistrationImpl implements ServiceRegistration 49 { 50 private Oscar m_oscar = null; 52 private BundleImpl m_bundle = null; 54 private String [] m_classes = null; 56 private Long m_serviceId = null; 58 private Object m_svcObj = null; 60 private ServiceFactory m_factory = null; 62 private Map m_propMap = null; 64 private ServiceReferenceImpl m_ref = null; 66 67 public ServiceRegistrationImpl( 68 Oscar oscar, BundleImpl bundle, 69 String [] classes, Long serviceId, 70 Object svcObj, Dictionary dict) 71 { 72 m_oscar = oscar; 73 m_bundle = bundle; 74 m_classes = classes; 75 m_serviceId = serviceId; 76 m_svcObj = svcObj; 77 m_factory = (m_svcObj instanceof ServiceFactory) 78 ? (ServiceFactory) m_svcObj : null; 79 80 initializeProperties(dict); 81 82 m_ref = new ServiceReferenceImpl(this, m_bundle); 88 } 89 90 protected boolean isValid() 91 { 92 return (m_svcObj != null); 93 } 94 95 public ServiceReference getReference() 96 { 97 return m_ref; 98 } 99 100 public void setProperties(Dictionary dict) 101 { 102 if (!isValid()) 104 { 105 throw new IllegalStateException ( 106 "The service registration is no longer valid."); 107 } 108 initializeProperties(dict); 110 m_oscar.servicePropertiesModified(this); 112 } 113 114 private void initializeProperties(Dictionary dict) 115 { 116 if (m_propMap == null) 118 { 119 m_propMap = new CaseInsensitiveMap(); 120 } 121 else 122 { 123 m_propMap.clear(); 124 } 125 126 if (dict != null) 127 { 128 Enumeration keys = dict.keys(); 129 while (keys.hasMoreElements()) 130 { 131 Object key = keys.nextElement(); 132 m_propMap.put(key, dict.get(key)); 133 } 134 } 135 136 m_propMap.put(Constants.OBJECTCLASS, m_classes); 138 m_propMap.put(Constants.SERVICE_ID, m_serviceId); 139 } 140 141 public void unregister() 142 { 143 Oscar.debug("ServiceRegistration.unregister()"); 144 m_oscar.unregisterService(m_bundle, this); 145 m_svcObj = null; 146 m_factory = null; 147 } 148 149 152 153 protected Object getService(Bundle acqBundle) 154 { 155 if (m_factory != null) 158 { 159 try { 160 if (System.getSecurityManager() != null) 161 { 162 return AccessController.doPrivileged( 163 new ServiceFactoryPrivileged(acqBundle, null)); 164 } 165 else 166 { 167 return getFactoryUnchecked(acqBundle); 168 } 169 } catch (Exception ex) { 170 Oscar.error("ServiceRegistrationImpl: Error getting service.", ex); 171 return null; 172 } 173 } 174 else 175 { 176 return m_svcObj; 177 } 178 } 179 180 protected void ungetService(Bundle relBundle, Object svcObj) 181 { 182 if (m_factory != null) 185 { 186 try { 187 if (System.getSecurityManager() != null) 188 { 189 AccessController.doPrivileged( 190 new ServiceFactoryPrivileged(relBundle, svcObj)); 191 } 192 else 193 { 194 ungetFactoryUnchecked(relBundle, svcObj); 195 } 196 } catch (Exception ex) { 197 Oscar.error("ServiceRegistrationImpl: Error ungetting service.", ex); 198 } 199 } 200 } 201 202 protected Bundle[] getUsingBundles() 203 { 204 return m_oscar.getUsingBundles(m_ref); 205 } 206 207 protected Object getProperty(String key) 208 { 209 return m_propMap.get(key); 210 } 211 212 private transient ArrayList m_list = new ArrayList (); 213 214 protected String [] getPropertyKeys() 215 { 216 synchronized (m_list) 217 { 218 m_list.clear(); 219 Iterator i = m_propMap.entrySet().iterator(); 220 while (i.hasNext()) 221 { 222 Map.Entry entry = (Map.Entry) i.next(); 223 m_list.add(entry.getKey()); 224 } 225 return (String []) m_list.toArray(new String [m_list.size()]); 226 } 227 } 228 229 private Object getFactoryUnchecked(Bundle bundle) 230 { 231 return m_factory.getService(bundle, this); 232 } 233 234 private void ungetFactoryUnchecked(Bundle bundle, Object svcObj) 235 { 236 m_factory.ungetService(bundle, this, svcObj); 237 } 238 239 244 private class ServiceFactoryPrivileged implements PrivilegedExceptionAction  245 { 246 private Bundle m_bundle = null; 247 private Object m_svcObj = null; 248 249 public ServiceFactoryPrivileged(Bundle bundle, Object svcObj) 250 { 251 m_bundle = bundle; 252 m_svcObj = svcObj; 253 } 254 255 public Object run() throws Exception  256 { 257 if (m_svcObj == null) 258 { 259 return getFactoryUnchecked(m_bundle); 260 } 261 else 262 { 263 ungetFactoryUnchecked(m_bundle, m_svcObj); 264 } 265 return null; 266 } 267 } 268 }
| Popular Tags
|