KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > ResourceCache


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.util;
24
25 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 //START OF IASRI 4660742
29
import java.util.logging.*;
30 import com.sun.logging.*;
31 //END OF IASRI 4660742
32

33 public abstract class ResourceCache
34 {
35
36     // START OF IASRI 4660742
37
static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
38     // END OF IASRI 4660742
39

40     /**
41      *
42      */

43     public ResourceCache(int count)
44     {
45     resources = new Hashtable JavaDoc();
46     this.count = count;
47     }
48
49     /**
50      * @exception Exception on exception
51      */

52     public Object JavaDoc getResource(Object JavaDoc name) throws Exception JavaDoc
53     {
54     Object JavaDoc resource = null;
55     Vector JavaDoc list = null;
56     Resource r = null;
57
58     // get the list of resources
59
synchronized (resources) {
60
61         list = (Vector JavaDoc) resources.get(name);
62
63         if (list == null) {
64         list = new Vector JavaDoc(count);
65             resources.put(name, list);
66         }
67     }
68
69     // check for available resource
70
boolean newR = false; // this thread needs to create a new resource
71
synchronized (list) {
72
73         while (resource == null) {
74
75             // try to find an available resource
76
for (int i=0; i < list.size(); i++) {
77             if ((r = (Resource) list.elementAt(i)).isAvailable()) {
78                 r.markInUse();
79                 resource = r.getResource();
80             break;
81             }
82             }
83     
84             // create new resource if limit not reached
85
if (resource == null) {
86             if (list.size() < count) {
87                 r = new Resource();
88                 list.addElement(r);
89                 newR = true;
90             break;
91             }
92
93             // wait for the resource to free up
94
if (!newR) list.wait();
95         }
96         }
97     }
98
99     // if new resource created, initialize the resource
100
if (newR) {
101
102         try {
103             resource = createResource(name);
104
105             // XXX need to handle case when resource is null
106

107         } catch (Exception JavaDoc ex) {
108         
109                 /** IASRI 4660742
110                 ex.printStackTrace();
111                 **/

112                     // START OF IASRI 4660742
113
_logger.log(Level.SEVERE,"enterprise_util.excep_rescache_getres",ex);
114                 // END OF IASRI 4660742
115
}
116
117         synchronized (list) {
118             r.setResource(resource);
119             r.markInUse();
120         }
121     }
122
123     return resource;
124     }
125
126     /**
127      *
128      */

129     public void returnResource(Object JavaDoc name, Object JavaDoc oldResource,
130     Object JavaDoc newResource)
131     {
132     Resource r = null;
133     Vector JavaDoc list = (Vector JavaDoc) resources.get(name);
134
135     if (list != null) {
136         synchronized (list) {
137             for (int i=0; i < list.size(); i++) {
138             r = (Resource) list.elementAt(i);
139             if (r.getResource() == oldResource) {
140             r.setResource(newResource);
141                 r.markAvailable();
142             list.notifyAll();
143             break;
144             }
145             }
146         }
147     }
148     }
149
150     /**
151      * Typically a long operation.
152      *
153      * @exception Exception on exception
154      */

155     public abstract Object JavaDoc createResource(Object JavaDoc name) throws Exception JavaDoc;
156
157     private Hashtable JavaDoc resources;
158     private int count;
159 }
160
161 /**
162  *
163  */

164 class Resource
165 {
166     private static final int IN_USE=1;
167     private static final int AVAILABLE=2;
168     private static final int NOT_AVAILABLE=3;
169
170     private Object JavaDoc resource;
171     private int state;
172
173     /**
174      *
175      */

176     Resource()
177     {
178     resource = null;
179     state = NOT_AVAILABLE;
180     }
181
182     /**
183      *
184      */

185     Object JavaDoc getResource()
186     {
187     return resource;
188     }
189
190     /**
191      *
192      */

193     void setResource(Object JavaDoc resource)
194     {
195     this.resource = resource;
196     }
197
198     /**
199      *
200      */

201     void markAvailable()
202     {
203     state = AVAILABLE;
204     }
205
206     /**
207      *
208      */

209     void markInUse()
210     {
211     state = IN_USE;
212     }
213
214     /**
215      *
216      */

217     boolean isAvailable()
218     {
219     return (state == AVAILABLE) ? true : false;
220     }
221 }
222
Popular Tags