|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DatabaseShutdownHandler.java | 100% | 93.8% | 100% | 95.5% |
|
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | * | |
17 | */ | |
18 | package org.apache.xindice.core; | |
19 | ||
20 | import java.util.HashSet; | |
21 | import java.util.Iterator; | |
22 | import java.util.Map; | |
23 | import java.util.Set; | |
24 | ||
25 | /** | |
26 | * Going to handle the JVM Shutdown hook and insuring clean shutdown | |
27 | * of all registered Database instances. | |
28 | * | |
29 | * @author <a href="mailto:toddbyrne@gmail.com">Todd Byrne</a> | |
30 | * @version $Revision: 717977 $, $Date: 2008-11-16 04:35:08 +0000 (Sun, 16 Nov 2008) $ | |
31 | */ | |
32 | public class DatabaseShutdownHandler implements Runnable { | |
33 | ||
34 | private final Set databases; | |
35 | ||
36 | ||
37 | 19 | public DatabaseShutdownHandler() { |
38 | 19 | databases = new HashSet(); |
39 | 19 | Runtime.getRuntime().addShutdownHook(new Thread(this)); |
40 | } | |
41 | ||
42 | /** | |
43 | * @param db database to register | |
44 | */ | |
45 | 123 | public void registerDatabase(Database db) { |
46 | 123 | synchronized (databases) { |
47 | 123 | databases.add(db); |
48 | } | |
49 | } | |
50 | ||
51 | /** | |
52 | * @param db removed the database from the hashtable | |
53 | */ | |
54 | 119 | public void removeDatabase(Database db) { |
55 | 119 | synchronized (databases) { |
56 | 119 | databases.remove(db); |
57 | } | |
58 | } | |
59 | ||
60 | /** | |
61 | * Cleans up any unclosed databases before the JVM exits. | |
62 | */ | |
63 | 19 | public void run() { |
64 | 19 | synchronized (databases) { |
65 | 19 | Iterator dbs = databases.iterator(); |
66 | 19 | while (dbs.hasNext()) { |
67 | 4 | Database db = (Database) dbs.next(); |
68 | ||
69 | // synch again on the db object just incase a close operation | |
70 | // is happening on a different thread | |
71 | 4 | synchronized (db) { |
72 | // call close but don't bother removing the db from the database | |
73 | // table | |
74 | 4 | try { |
75 | 4 | db.close(false); |
76 | 4 | System.out.println("SHUTDOWN Dirty close on: " + db.getName()); |
77 | } catch (DBException e) { | |
78 | 0 | System.out.println("SHUTDOWN Failed to close: " + db.getName() + ": " + e); |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | 19 | databases.clear(); |
84 | } | |
85 | } | |
86 | } |
|