PropertiesDictionary.java

00001 /*
00002  * Copyright (c) 2003-2005, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
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   // These aren't used but we implement to fulfill Dictionary class
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 }

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4