KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > util > RepositoryIdCache


1 /*
2  * @(#)RepositoryIdCache.java 1.16 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.util;
17
18 import java.util.Stack JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.EmptyStackException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 // Really limited pool - in this case just creating several at a time...
24
class RepositoryIdPool extends Stack JavaDoc {
25     
26     private static int MAX_CACHE_SIZE = 4;
27     private RepositoryIdCache cache;
28     
29     public final synchronized RepositoryId popId() {
30
31         try {
32             return (RepositoryId)super.pop();
33         }
34         catch(EmptyStackException JavaDoc e) {
35             increasePool(5);
36             return (RepositoryId)super.pop();
37         }
38
39     }
40
41     // Pool management
42
final void increasePool(int size) {
43         //if (cache.size() <= MAX_CACHE_SIZE)
44
for (int i = size; i > 0; i--)
45         push(new RepositoryId());
46         /*
47       // _REVISIT_ This will not work w/out either thread tracing or weak references. I am
48       // betting that thread tracing almost completely negates benefit of reuse. Until either
49       // 1.2 only inclusion or proof to the contrary, I'll leave it this way...
50       else {
51       int numToReclaim = cache.size() / 2;
52       Enumeration keys = cache.keys();
53       Enumeration elements = cache.elements();
54       for (int i = numToReclaim; i > 0; i--) {
55       Object key = keys.nextElement();
56       Object element = elements.nextElement();
57                 
58       push(element);
59       cache.remove(key);
60       }
61       }
62         */

63     }
64     
65     final void setCaches(RepositoryIdCache cache) {
66         this.cache = cache;
67     }
68
69 }
70
71 public class RepositoryIdCache extends Hashtable JavaDoc {
72
73     private RepositoryIdPool pool = new RepositoryIdPool();
74     
75     public RepositoryIdCache() {
76         pool.setCaches(this);
77     }
78     
79     public final synchronized RepositoryId getId(String JavaDoc key) {
80         RepositoryId repId = (RepositoryId)super.get(key);
81
82         if (repId != null)
83             return repId;
84         else {
85             //repId = pool.popId().init(key);
86
repId = new RepositoryId(key);
87             put(key, repId);
88             return repId;
89         }
90
91     }
92 }
93
94
95
Popular Tags