Source for gnu.CORBA.gnuContext

   1: /* gnuContext.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.CORBA;
  40: 
  41: import java.util.Hashtable;
  42: import java.util.Iterator;
  43: import java.util.Map;
  44: import java.util.Set;
  45: 
  46: import org.omg.CORBA.Any;
  47: import org.omg.CORBA.Bounds;
  48: import org.omg.CORBA.CTX_RESTRICT_SCOPE;
  49: import org.omg.CORBA.Context;
  50: import org.omg.CORBA.NVList;
  51: 
  52: /**
  53:  * The working implementation of the {@link org.omg.CORBA.Context}.
  54:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  55:  */
  56: public class gnuContext
  57:   extends Context
  58: {
  59:   /**
  60:    * The parent context.
  61:    */
  62:   Context parent;
  63: 
  64:   /**
  65:    * The collection to store the context properties.
  66:    */
  67:   Map properties = new Hashtable();
  68: 
  69:   /**
  70:    * The name of this context.
  71:    */
  72:   String name;
  73: 
  74:   /**
  75:    * Creates the new context with the given name and parent.
  76:    *
  77:    * @param a_name a name of the new context.
  78:    * @param a_parent a parent, used to resolve the missing references.
  79:    */
  80:   public gnuContext(String a_name, Context a_parent)
  81:   {
  82:     name = a_name;
  83:     parent = a_parent;
  84:   }
  85: 
  86:   /** {@inheritDoc} */
  87:   public String context_name()
  88:   {
  89:     return name;
  90:   }
  91: 
  92:   /** {@inheritDoc} */
  93:   public Context create_child(String child)
  94:   {
  95:     return new gnuContext(child, this);
  96:   }
  97: 
  98:   /** {@inheritDoc} */
  99:   public void delete_values(String property)
 100:   {
 101:     boolean starts = false;
 102:     if (property.endsWith("*"))
 103:       {
 104:         starts = true;
 105:         property = property.substring(0, property.length() - 1);
 106:       }
 107: 
 108:     Set keys = properties.keySet();
 109: 
 110:     Iterator iter = keys.iterator();
 111:     while (iter.hasNext())
 112:       {
 113:         String key = (String) iter.next();
 114:         if ((starts && key.startsWith(property)) ||
 115:             (!starts && key.equals(property))
 116:            )
 117:           iter.remove();
 118:       }
 119:   }
 120: 
 121:   /** {@inheritDoc} */
 122:   public NVList get_values(String start_scope, int flags, String pattern)
 123:   {
 124:     if (start_scope != null)
 125:       {
 126:         Context c = this;
 127:         while (c != null && !c.context_name().equals(start_scope))
 128:           c = c.parent();
 129:         if (c == null)
 130:           return new gnuNVList();
 131:       }
 132: 
 133:     try
 134:       {
 135:         gnuNVList rt = new gnuNVList();
 136: 
 137:         boolean starts = false;
 138:         if (pattern.endsWith("*"))
 139:           {
 140:             starts = true;
 141:             pattern = pattern.substring(0, pattern.length() - 1);
 142:           }
 143: 
 144:         Set keys = properties.keySet();
 145: 
 146:         Iterator iter = keys.iterator();
 147:         while (iter.hasNext())
 148:           {
 149:             String key = (String) iter.next();
 150:             if ((starts && key.startsWith(pattern)) ||
 151:                 (!starts && key.equals(pattern))
 152:                )
 153:               {
 154:                 rt.add_value(key, (Any) properties.get(key), 0);
 155:               }
 156:           }
 157: 
 158:         if ((flags & CTX_RESTRICT_SCOPE.value) == 0 && parent != null)
 159:           {
 160:             NVList par = parent.get_values(start_scope, flags, pattern);
 161:             for (int i = 0; i < par.count(); i++)
 162:               {
 163:                 rt.list.add(par.item(i));
 164:               }
 165:           }
 166: 
 167:         return rt;
 168:       }
 169:     catch (Bounds ex)
 170:       {
 171:         throw new Error("Report this bug.");
 172:       }
 173:   }
 174: 
 175:   /** {@inheritDoc} */
 176:   public Context parent()
 177:   {
 178:     return parent;
 179:   }
 180: 
 181:   /** {@inheritDoc} */
 182:   public void set_one_value(String name, Any value)
 183:   {
 184:     properties.put(name, value);
 185:   }
 186: 
 187:   /** {@inheritDoc} */
 188:   public void set_values(NVList values)
 189:   {
 190:     try
 191:       {
 192:         for (int i = 0; i < values.count(); i++)
 193:           {
 194:             properties.put(values.item(i).name(), values.item(i).value());
 195:           }
 196:       }
 197:     catch (Bounds ex)
 198:       {
 199:         throw new Error("Please report this bug.");
 200:       }
 201:   }
 202: }