00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package org.knopflerfish.framework;
00036
00037 import java.util.*;
00038 import org.osgi.framework.Constants;
00039
00047 class PropertiesDictionary extends Dictionary
00048 {
00049 private String [] keys;
00050 private Object [] values;
00051 private int size;
00052
00053 private int ocIndex = -1;
00054 private int sidIndex = -1;
00055
00056 private static long nextServiceID = 1;
00057
00058 PropertiesDictionary(Dictionary in) {
00059 int max_size = in != null ? in.size() + 2 : 2;
00060 keys = new String[max_size];
00061 values = new Object[max_size];
00062 size = 0;
00063 if (in != null) {
00064 try {
00065 for (Enumeration e = in.keys(); e.hasMoreElements();) {
00066 String key = (String)e.nextElement();
00067 if (ocIndex == -1 && key.equalsIgnoreCase(Constants.OBJECTCLASS)) {
00068 ocIndex = size;
00069 } else if (sidIndex == -1 && key.equalsIgnoreCase(Constants.SERVICE_ID)) {
00070 sidIndex = size;
00071 } else {
00072 for (int i = size - 1; i >= 0; i--) {
00073 if (key.equalsIgnoreCase(keys[i])) {
00074 throw new IllegalArgumentException("Several entries for property: " + key);
00075 }
00076 }
00077 }
00078 keys[size] = key;
00079 values[size++] = in.get(key);
00080 }
00081 } catch (ClassCastException ignore) {
00082 throw new IllegalArgumentException("Properties contains key that is not of type java.lang.String");
00083 }
00084 }
00085 }
00086
00087
00088 PropertiesDictionary(Dictionary in, String[] classes, Long sid) {
00089 this(in);
00090 if (ocIndex == -1) {
00091 keys[size] = Constants.OBJECTCLASS;
00092 ocIndex = size++;
00093 }
00094 values[ocIndex] = classes;
00095 if (sidIndex == -1) {
00096 keys[size] = Constants.SERVICE_ID;
00097 sidIndex = size++;
00098 }
00099 values[sidIndex] = sid != null ? sid : new Long(nextServiceID++);
00100 }
00101
00102
00103 public Object get(Object key) {
00104 if (key == Constants.OBJECTCLASS) {
00105 return (ocIndex >= 0) ? values[ocIndex] : null;
00106 } else if (key == Constants.SERVICE_ID) {
00107 return (sidIndex >= 0) ? values[sidIndex] : null;
00108 }
00109 for (int i = size - 1; i >= 0; i--) {
00110 if (((String)key).equalsIgnoreCase(keys[i])) {
00111 return values[i];
00112 }
00113 }
00114 return null;
00115 }
00116
00117
00118 public String [] keyArray() {
00119 if (keys.length != size) {
00120 String [] nkeys = new String[size];
00121 System.arraycopy(keys, 0, nkeys, 0, size);
00122 keys = nkeys;
00123 }
00124 return (String [])keys.clone();
00125 }
00126
00127
00128 public int size() {
00129 return size;
00130 }
00131
00132
00133
00134 public Enumeration elements() { throw new UnsupportedOperationException("Not implemented"); }
00135
00136 public boolean isEmpty() { throw new UnsupportedOperationException("Not implemented"); }
00137
00138 public Enumeration keys() { throw new UnsupportedOperationException("Not implemented"); }
00139
00140 public Object put(Object k, Object v) { throw new UnsupportedOperationException("Not implemented"); }
00141
00142 public Object remove(Object k) { throw new UnsupportedOperationException("Not implemented"); }
00143 }