KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > CoordinatorLogPool


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
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.jts.CosTransactions;
30
31 // Import required classes.
32

33 import java.util.*;
34
35
36 /**
37  * The CoordinatorLogPool is used as a cache for CoordinatorLog objects.
38  * This pool allows the re-use of these objects which are very expensive
39  * to instantiate.
40  *
41  * The pool is used by replacing calls to 'new CoordinatorLog()' in the
42  * TopCoordinator with calls to CoordinatorLogPool.getCoordinatorLog().
43  * The getCoordinatorLog() method attempts to return a CoordinatorLog
44  * from the pool. If the pool is empty it instantiates a new
45  * CoordinatorLog.
46  *
47  * Objects are re-used by calling CoordinatorLogPool.putCoordinatorLog()
48  * to return a CoordinatorLog object back to the pool. At this time a
49  * check is made to ensure that the internal pool size doesn't exceed a
50  * pre set limit. If it does, then the object is discarded and not put
51  * back into the pool.
52  *
53  * The pool was added to improve performance of transaction logging
54  *
55  * @version 1.00
56  *
57  * @author Arun Krishnan
58  *
59  * @see
60 */

61
62
63
64 class CoordinatorLogPool {
65
66     private Stack pool;
67     private static final int MAXSTACKSIZE = 3;
68
69     public static CoordinatorLogPool CLPool = new CoordinatorLogPool();
70     public static Hashtable CLPooltable = new Hashtable();
71     
72
73     /**
74      * constructor
75      *
76      */

77     public CoordinatorLogPool() {
78     pool = new Stack();
79     }
80
81     /**
82      * get a CoordinatorLog object from the cache. Instantiate a
83      * new CoordinatorLog object if the cache is empty.
84      *
85      */

86     public static synchronized CoordinatorLog getCoordinatorLog() {
87         if (Configuration.isDBLoggingEnabled() ||
88             Configuration.isFileLoggingDisabled())
89             return null;
90     if (CLPool.pool.empty()) {
91         return new CoordinatorLog();
92     }
93     else {
94         CoordinatorLog cl = (CoordinatorLog) CLPool.pool.pop();
95         return cl;
96     }
97     }
98
99     /**
100      * return a CoordinatorLog object to the cache. To limit the size of
101      * the cache a check is made to ensure that the cache doesn't
102      * already have more that MAXSTACKSIZE elements. If so the object
103      * being returned is discarded.
104      *
105      */

106     public static void putCoordinatorLog(CoordinatorLog cl) {
107     if (CLPool.pool.size() <= MAXSTACKSIZE) {
108         CLPool.pool.push(cl);
109     }
110     }
111
112     // Added to support delegated recovery: multiple logs should coexist
113
public static synchronized CoordinatorLog getCoordinatorLog(String JavaDoc logPath) {
114         CoordinatorLogPool clpool = (CoordinatorLogPool)CLPooltable.get(logPath);
115         if (clpool == null) {
116             clpool = new CoordinatorLogPool();
117             CLPooltable.put(logPath,clpool);
118         }
119         if (clpool.pool.empty()) {
120             return new CoordinatorLog(logPath);
121         }
122         else {
123             return (CoordinatorLog)clpool.pool.pop();
124         }
125     }
126
127     // Added to support delegated recovery: multiple logs should coexist
128
public static void putCoordinatorLog(CoordinatorLog cl, String JavaDoc logPath) {
129         CoordinatorLogPool clpool = (CoordinatorLogPool)CLPooltable.get(logPath);
130         if (clpool == null) {
131             clpool = new CoordinatorLogPool();
132             CLPooltable.put(logPath,clpool);
133         }
134         if (clpool.pool.size() <= MAXSTACKSIZE) {
135             clpool.pool.push(cl);
136         }
137     }
138
139 }
140
141
Popular Tags