KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > derby > DerbySystemGBean


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.geronimo.derby;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.geronimo.gbean.GBeanInfo;
23 import org.apache.geronimo.gbean.GBeanInfoBuilder;
24 import org.apache.geronimo.gbean.GBeanLifecycle;
25 import org.apache.geronimo.system.serverinfo.ServerInfo;
26
27 import java.sql.DriverManager JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 /**
31  * A GBean that represents an instance of an Apache Derby system (a system being
32  * a collection of different databases).
33  *
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class DerbySystemGBean implements DerbySystem, GBeanLifecycle {
37     private static final Log log = LogFactory.getLog("DerbySystem");
38     private static final String JavaDoc SYSTEM_HOME = "derby.system.home";
39     private static final String JavaDoc SHUTDOWN_ALL = "jdbc:derby:;shutdown=true";
40
41     private final ServerInfo serverInfo;
42     private final String JavaDoc systemHome;
43     private String JavaDoc actualHome;
44
45     public DerbySystemGBean(ServerInfo serverInfo, String JavaDoc derbySystemHome) {
46         this.serverInfo = serverInfo;
47         this.systemHome = derbySystemHome;
48     }
49
50     public String JavaDoc getDerbyHome() {
51         return actualHome;
52     }
53
54     public void doStart() throws Exception JavaDoc {
55         // set up the system property for the database home
56
actualHome = System.getProperty(SYSTEM_HOME);
57         if (actualHome == null) {
58             actualHome = serverInfo.resolveServerPath(systemHome);
59         }
60         System.setProperty(SYSTEM_HOME, actualHome);
61
62         // set the magic system property that causes derby to use explicity
63
// file sync instead of relying on vm support for file open rws
64
System.setProperty("derby.storage.fileSyncTransactionLog", "true");
65
66         // load the Embedded driver to initialize the home
67
new org.apache.derby.jdbc.EmbeddedDriver();
68         log.debug("Started in " + actualHome);
69     }
70
71     public void doStop() throws Exception JavaDoc {
72         try {
73             DriverManager.getConnection(SHUTDOWN_ALL, null, null);
74         } catch (SQLException JavaDoc e) {
75             // SQLException gets thrown on successful shutdown so ignore
76
}
77         System.gc(); // Added per recommendation Derby documentation
78
log.debug("Stopped");
79     }
80
81     public void doFail() {
82         try {
83             DriverManager.getConnection(SHUTDOWN_ALL, null, null);
84         } catch (SQLException JavaDoc e) {
85             // SQLException gets thrown on successful shutdown so ignore
86
}
87         System.gc(); // Added per recommendation Derby documentation
88
log.warn("Failed");
89     }
90
91     public static final GBeanInfo GBEAN_INFO;
92
93     public static GBeanInfo getGBeanInfo() {
94         return GBEAN_INFO;
95     }
96
97     static {
98         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(DerbySystemGBean.class);
99         infoFactory.addAttribute("derbySystemHome", String JavaDoc.class, true);
100         infoFactory.addAttribute("derbyHome", String JavaDoc.class, false);
101         infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
102         infoFactory.setConstructor(new String JavaDoc[]{"ServerInfo", "derbySystemHome"});
103         GBEAN_INFO = infoFactory.getBeanInfo();
104     }
105 }
106
Popular Tags