1 /*
   2  * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.orbutil;
  27 
  28 import java.util.Stack;
  29 import java.util.Hashtable;
  30 import java.util.EmptyStackException;
  31 import java.util.Enumeration;
  32 
  33 // Really limited pool - in this case just creating several at a time...
  34 class RepositoryIdPool_1_3_1 extends Stack {
  35 
  36     private static int MAX_CACHE_SIZE = 4;
  37     private RepositoryIdCache_1_3_1 cache;
  38 
  39     public final synchronized RepositoryId_1_3_1 popId() {
  40 
  41         try {
  42             return (RepositoryId_1_3_1)super.pop();
  43         }
  44         catch(EmptyStackException e) {
  45             increasePool(5);
  46             return (RepositoryId_1_3_1)super.pop();
  47         }
  48 
  49     }
  50 
  51     // Pool management
  52     final void increasePool(int size) {
  53         //if (cache.size() <= MAX_CACHE_SIZE)
  54         for (int i = size; i > 0; i--)
  55             push(new RepositoryId_1_3_1());
  56         /*
  57           // _REVISIT_ This will not work w/out either thread tracing or weak references.  I am
  58           // betting that thread tracing almost completely negates benefit of reuse.  Until either
  59           // 1.2 only inclusion or proof to the contrary, I'll leave it this way...
  60           else {
  61           int numToReclaim = cache.size() / 2;
  62           Enumeration keys = cache.keys();
  63           Enumeration elements = cache.elements();
  64           for (int i = numToReclaim; i > 0; i--) {
  65           Object key = keys.nextElement();
  66           Object element = elements.nextElement();
  67 
  68           push(element);
  69           cache.remove(key);
  70           }
  71           }
  72         */
  73     }
  74 
  75     final void setCaches(RepositoryIdCache_1_3_1 cache) {
  76         this.cache = cache;
  77     }
  78 
  79 }
  80 
  81 public class RepositoryIdCache_1_3_1 extends Hashtable {
  82 
  83     private RepositoryIdPool_1_3_1 pool = new RepositoryIdPool_1_3_1();
  84 
  85     public RepositoryIdCache_1_3_1() {
  86         pool.setCaches(this);
  87     }
  88 
  89     public final synchronized RepositoryId_1_3_1 getId(String key) {
  90         RepositoryId_1_3_1 repId = (RepositoryId_1_3_1)super.get(key);
  91 
  92         if (repId != null)
  93             return repId;
  94         else {
  95             //repId = pool.popId().init(key);
  96             repId = new RepositoryId_1_3_1(key);
  97             put(key, repId);
  98             return repId;
  99         }
 100 
 101     }
 102 }