Source for gnu.CORBA.Poa.gnuPoaCurrent

   1: /* gnuPoaCurrent.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.Poa;
  40: 
  41: import org.omg.CORBA.CurrentHelper;
  42: import org.omg.CORBA.portable.ObjectImpl;
  43: import org.omg.PortableServer.Current;
  44: import org.omg.PortableServer.CurrentOperations;
  45: import org.omg.PortableServer.CurrentPackage.NoContext;
  46: import org.omg.PortableServer.POA;
  47: 
  48: import java.util.Iterator;
  49: import java.util.Map;
  50: import java.util.TreeMap;
  51: 
  52: /**
  53:  * Supports the "Poa current" concept, providing the id and poa of
  54:  * the object currently being served. There is only one instance
  55:  * of this class per ORB. It maintains a thread to information map.
  56:  *
  57:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  58:  */
  59: public class gnuPoaCurrent
  60:   extends ObjectImpl
  61:   implements Current
  62: {
  63:   /**
  64:    * The table, mapping threads to records.
  65:    */
  66:   private TreeMap threads = new TreeMap();
  67: 
  68:   /**
  69:    * Get the array of POA current repository ids.
  70:    *
  71:    * @return a single member array, containing value, returned
  72:    * by the {@link CurrentHelper#id}, normally
  73:    * "IDL:omg.org/PortableServer/Current:2.3".
  74:    */
  75:   public String[] _ids()
  76:   {
  77:     return new String[] { CurrentHelper.id() };
  78:   }
  79: 
  80:   /**
  81:    * Get the object id, associated with the thread currently being served.
  82:    *
  83:    * @throws NoContext if the current thread is not associated with any
  84:    * object.
  85:    */
  86:   public byte[] get_object_id()
  87:                        throws NoContext
  88:   {
  89:     CurrentOperations r;
  90:     synchronized (threads)
  91:       {
  92:         r = (CurrentOperations) threads.get(Thread.currentThread().getName());
  93:       }
  94:     if (r != null)
  95:       return r.get_object_id();
  96:     else
  97:       throw new NoContext(Thread.currentThread().getName());
  98:   }
  99: 
 100:   /**
 101:    * Get the object POA, associated with the thread currently being served.
 102:    *
 103:    * @throws NoContext if the current thread is not associated with any
 104:    * object.
 105:    */
 106:   public POA get_POA()
 107:               throws NoContext
 108:   {
 109:     CurrentOperations r;
 110:     synchronized (threads)
 111:       {
 112:         r = (CurrentOperations) threads.get(Thread.currentThread().getName());
 113:       }
 114:     if (r != null)
 115:       return r.get_POA();
 116:     else
 117:       throw new NoContext(Thread.currentThread().getName());
 118:   }
 119: 
 120:   /**
 121:    * Add the entry to the map.
 122:    */
 123:   public void put(Thread t, CurrentOperations record)
 124:   {
 125:     synchronized (threads)
 126:       {
 127:         threads.put(t.getName(), record);
 128:       }
 129:   }
 130: 
 131:   /**
 132:    * Check if this Poa has some running threads.
 133:    */
 134:   public boolean has(POA poa)
 135:   {
 136:     synchronized (threads)
 137:       {
 138:         Iterator iter = threads.entrySet().iterator();
 139:         while (iter.hasNext())
 140:           {
 141:             Map.Entry item = (Map.Entry) iter.next();
 142:             try
 143:               {
 144:                 if (((CurrentOperations) item.getValue()).get_POA() == poa)
 145:                   {
 146:                     return true;
 147:                   }
 148:               }
 149:             catch (NoContext ex)
 150:               {
 151:                 throw new InternalError();
 152:               }
 153:           }
 154:       }
 155:     return false;
 156:   }
 157: 
 158:   /**
 159:    * Check if this thread is registered.
 160:    */
 161:   public boolean has(Thread t)
 162:   {
 163:     synchronized (threads)
 164:       {
 165:         return threads.containsKey(t.getName());
 166:       }
 167:   }
 168: 
 169:   /**
 170:    * Remove the entry from the map.
 171:    */
 172:   public void remove(Thread t)
 173:   {
 174:     synchronized (threads)
 175:       {
 176:         threads.remove(t.getName());
 177:       }
 178:   }
 179: }