RepositoryIdCache.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1998, 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 * Licensed Materials - Property of IBM
27 * RMI-IIOP v1.0
28 * Copyright IBM Corp. 1998 1999  All Rights Reserved
29 *
30 */
31
32package com.sun.corba.se.impl.util;
33
34import java.util.Stack;
35import java.util.Hashtable;
36import java.util.EmptyStackException;
37import java.util.Enumeration;
38
39// Really limited pool - in this case just creating several at a time...
40class RepositoryIdPool extends Stack {
41
42    private static int MAX_CACHE_SIZE = 4;
43    private RepositoryIdCache cache;
44
45    public final synchronized RepositoryId popId() {
46
47        try {
48            return (RepositoryId)super.pop();
49        }
50        catch(EmptyStackException e) {
51            increasePool(5);
52            return (RepositoryId)super.pop();
53        }
54
55    }
56
57    // Pool management
58    final void increasePool(int size) {
59        //if (cache.size() <= MAX_CACHE_SIZE)
60        for (int i = size; i > 0; i--)
61            push(new RepositoryId());
62        /*
63          // _REVISIT_ This will not work w/out either thread tracing or weak references.  I am
64          // betting that thread tracing almost completely negates benefit of reuse.  Until either
65          // 1.2 only inclusion or proof to the contrary, I'll leave it this way...
66          else {
67          int numToReclaim = cache.size() / 2;
68          Enumeration keys = cache.keys();
69          Enumeration elements = cache.elements();
70          for (int i = numToReclaim; i > 0; i--) {
71          Object key = keys.nextElement();
72          Object element = elements.nextElement();
73
74          push(element);
75          cache.remove(key);
76          }
77          }
78        */
79    }
80
81    final void setCaches(RepositoryIdCache cache) {
82        this.cache = cache;
83    }
84
85}
86
87public class RepositoryIdCache extends Hashtable {
88
89    private RepositoryIdPool pool = new RepositoryIdPool();
90
91    public RepositoryIdCache() {
92        pool.setCaches(this);
93    }
94
95    public final synchronized RepositoryId getId(String key) {
96        RepositoryId repId = (RepositoryId)super.get(key);
97
98        if (repId != null)
99            return repId;
100        else {
101            //repId = pool.popId().init(key);
102            repId = new RepositoryId(key);
103            put(key, repId);
104            return repId;
105        }
106
107    }
108}
109