KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > test > SchemaManager


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.test;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.dbobj.Schema;
69 import com.jcorporate.expresso.kernel.ConsoleInstallLog;
70 import org.apache.log4j.Logger;
71
72 import java.util.HashMap JavaDoc;
73 import java.util.TreeSet JavaDoc;
74
75
76 /**
77  * This class reference counts references to various Schema objects.
78  * When a reference count drops to zero, the schema is deleted. [If it's not
79  * marked read only] If it is marked readonly, then the schema is created,
80  * but not deleted. [Makes for much faster checking when running unit tests
81  * on, say, a single controller]
82  *
83  * @author Michael Rimov
84  * @version $Revision: 1.12 $ $Date: 2004/11/17 20:48:22 $
85  */

86 public class SchemaManager {
87     static HashMap JavaDoc references = new HashMap JavaDoc();
88     static TreeSet JavaDoc readOnlySet = new TreeSet JavaDoc();
89     static Logger log = Logger.getLogger(SchemaManager.class);
90
91     public SchemaManager() {
92     }
93
94     /**
95      * Add a reference to a schema. Create the schema if no previous reference
96      *
97      * @param s The Schema to reference
98      */

99     synchronized static public void addReference(Schema s) {
100         SchemaManager.addReference(s, false);
101     }
102
103     /**
104      * Add a reference to a schema. Create the schema if no previous reference
105      *
106      * @param s The Schema to reference
107      * @param forceNoCreate Set to true if you just want a reference added, but no
108      * schema created. This is used if DBToolTests are being run in the test
109      * suite.
110      */

111     synchronized static public void addReference(Schema s,
112                                                  boolean forceNoCreate) {
113         if (log.isDebugEnabled()) {
114             log.debug("Adding Reference To Schema: " + s.getClass().getName());
115         }
116         if (readOnlySet.contains(s.getClass().getName())) {
117             readOnlySet.remove(s.getClass().getName());
118         }
119         if (references.containsKey(s)) {
120             Integer JavaDoc i = (Integer JavaDoc) references.get(s);
121
122             if (i == null) {
123                 log.error("Got null reference count, when schema exists in the " +
124                         " hash map");
125                 references.put(s, new Integer JavaDoc(1));
126             } else {
127                 Integer JavaDoc i2 = new Integer JavaDoc(i.intValue() + 1);
128
129                 if (log.isDebugEnabled()) {
130                     log.debug("Previous Reference Count: " + i.toString());
131                 }
132
133                 references.put(s, i2);
134             }
135         } else {
136             if (log.isDebugEnabled()) {
137                 log.debug("Inserting new reference");
138             }
139
140             references.put(s, new Integer JavaDoc(1));
141
142             if (forceNoCreate == false) {
143                 try {
144
145                     if (SchemaCreator.ensureSchemaExists(s, new ConsoleInstallLog()) == true) {
146
147                         //Schema was already created.
148
//Happens in Cactus test cases where two instances
149
//of this class are running. Just increment the
150
//reference count so that we don't accidentally
151
//delete it before it's time.
152
log.debug("Schema " + s.getClass().getName() +
153                                 " didn't have a reference but already exists.");
154                         references.put(s, new Integer JavaDoc(2));
155                     }
156                 } catch (DBException e) {
157                     log.error("Error Creating Schema", e);
158                 }
159             }
160         }
161     }
162
163     /**
164      * Adds a schema reference that is not removed when the count goes to zero
165      * because it is a read only schema. You can undo this 'read-onliness' by
166      * calling forceSchemaDelete() which will remove the read only flags for
167      * this schema. Note that if any other reference is added for this schema,
168      * then if it's a non-readonly reference, than the schema will be deleted
169      * as usual.
170      *
171      * @param s the Schema to add
172      */

173     synchronized static public void addReadOnlyReference(Schema s) {
174         boolean canBeReadOnly = false;
175
176         if (!references.containsKey(s)) {
177             canBeReadOnly = true;
178         } else if (readOnlySet.contains(s.getClass().getName())) {
179             canBeReadOnly = true;
180         }
181
182         addReference(s);
183         readOnlySet.add(s.getClass().getName());
184     }
185
186     /**
187      * Reduces the reference count to Schema s. Deletes the schema if the
188      * reference count is reduced to zero.
189      *
190      * @param s the Schema to remove a reference.
191      */

192     synchronized static public void removeReference(Schema s) {
193         if (log.isDebugEnabled()) {
194             log.debug("Removing Reference To Schema: " +
195                     s.getClass().getName());
196         }
197         if (references.containsKey(s)) {
198             Integer JavaDoc i = (Integer JavaDoc) references.get(s);
199
200             if (i.intValue() == 1) {
201                 references.remove(s);
202
203                 if (!readOnlySet.contains(s.getClass().getName())) {
204                     if (log.isDebugEnabled()) {
205                         log.debug("Reference going to zero. Removing Schema");
206                     }
207
208                     SchemaDeleter.deleteSchema(TestSystemInitializer.getTestContext(), s);
209                 } else {
210                     readOnlySet.remove(s.getClass().getName());
211
212                     if (log.isDebugEnabled()) {
213                         log.debug("Reference to zero, but read only item. Not removing schema");
214                     }
215                 }
216             } else {
217                 Integer JavaDoc i2 = new Integer JavaDoc(i.intValue() - 1);
218                 references.put(s, i2);
219             }
220         } else {
221             log.error("Schema : " + s.getClass().getName() +
222                     "not found in reference list");
223
224             return;
225         }
226     }
227
228     /**
229      * Forces the schema deletion even if the schema is marked "read only"
230      *
231      * @param s the schema to force a delete
232      */

233     synchronized static public void forceSchemaDelete(Schema s) {
234         readOnlySet.remove(s.getClass().getName());
235     }
236
237     synchronized static public void forceAllSchemaDelete() {
238         readOnlySet.clear();
239     }
240
241 }
Popular Tags