1 26 27 package com.opensugar.cube; 28 29 import org.osgi.framework.ServiceReference; 30 import org.osgi.framework.Bundle; 31 import org.osgi.framework.Constants; 32 33 import java.util.Dictionary; 34 import java.util.Hashtable; 35 import java.util.Enumeration; 36 import java.util.Vector; 37 38 public class ServiceReferenceImpl implements ServiceReference { 40 41 private static long lastAssignedId = -1; 42 43 private Bundle bundle; 45 private String[] objectClass; 47 private long id; 49 private Hashtable properties; 50 private Vector usingBundles; 52 53 public ServiceReferenceImpl( Bundle bundle, String[] objectClass, Dictionary props ) { 54 this.bundle = bundle; 55 this.objectClass = objectClass; 56 57 id = lastAssignedId + 1; 58 lastAssignedId = id; 59 60 setProperties( props ); 61 62 usingBundles = new Vector(); 63 } 64 65 public Object getProperty( String key ) { 68 Util.checkNullParameter( key, getClass().getName(), "getProperty", "key" ); 69 70 String k = getPropertyKey( key ); 71 if ( k != null ) { 72 return properties.get( k ); 73 } 74 return null; 75 } 76 77 public String[] getPropertyKeys() { 80 String[] keys = new String[ properties.size() ]; 81 Enumeration enum = properties.keys(); 82 int i = 0; 83 while ( enum.hasMoreElements() ) { 84 keys[ i++ ] = (String)enum.nextElement(); 85 } 86 return keys; 87 } 88 89 public Bundle getBundle() { 92 return bundle; 96 } 97 98 public Bundle[] getUsingBundles() { 99 Vector tmp = (Vector)usingBundles.clone(); 100 Bundle[] ret = new Bundle[ tmp.size() ]; 101 tmp.copyInto( ret ); 102 return ret; 103 } 104 105 107 protected void setProperties( Dictionary props ) { 109 Hashtable newProperties = new Hashtable(); 111 if ( props != null ) { 113 Enumeration propNames = props.keys(); 114 String propName; 115 Object propValue; 116 while ( propNames.hasMoreElements() ) { 117 propName = (String)propNames.nextElement(); 118 propValue = props.get( propName ); 119 if ( propName.equalsIgnoreCase( Constants.SERVICE_ID ) ) { 120 } 126 if ( propName.equalsIgnoreCase( Constants.SERVICE_RANKING ) ) { 127 if ( !( propValue instanceof Integer ) ) { 128 if ( propValue instanceof String ) { 129 try { 130 Integer.valueOf( (String)propValue ).intValue(); 131 } 132 catch ( NumberFormatException e ) { 133 throw new IllegalArgumentException( "Value of property named '" + propName + "' must be an integer" ); 134 } 135 } 136 else { 137 throw new IllegalArgumentException( "Value of property named '" + propName + "' must be an integer" ); 138 } 139 } 140 } 141 String caseVariant = getPropertyKey( propName ); 143 if ( caseVariant != null && !caseVariant.equals( propName ) ) { 144 throw new IllegalArgumentException( "Property with case-variant of the same key name already exists" ); 145 } 146 newProperties.put( propName, propValue ); 147 } 148 } 149 newProperties.put( Constants.OBJECTCLASS, objectClass ); 152 newProperties.put( Constants.SERVICE_ID, new Long( id ) ); 153 154 properties = newProperties; 156 } 157 158 protected void serviceUnregistered() { 162 bundle = null; 163 } 164 165 protected String[] getObjectClass() { 169 return objectClass; 170 } 171 172 public void addUsingBundle( Bundle bundle ) { 177 if ( usingBundles.indexOf( bundle ) == -1 ) { 180 usingBundles.addElement( bundle ); 181 } 182 } 183 184 public void removeUsingBundle( Bundle bundle ) { 189 usingBundles.removeElement( bundle ); 190 } 191 192 private String getPropertyKey( String key ) { 196 if ( properties == null ) { 198 return null; 199 } 200 201 Enumeration keys = properties.keys(); 202 String k; 203 while ( keys.hasMoreElements() ) { 204 k = (String)keys.nextElement(); 205 if ( k.equalsIgnoreCase( key ) ) { 206 return k; 207 } 208 } 209 return null; 210 } 211 212 } | Popular Tags |