1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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;
28 import java.sql.SQLException;
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: 406493 $ $Date: 2006-05-14 18:14:11 -0700 (Sun, 14 May 2006) $
35 */
36 public class DerbySystemGBean implements DerbySystem, GBeanLifecycle {
37 private static final Log log = LogFactory.getLog("DerbySystem");
38 private static final String SYSTEM_HOME = "derby.system.home";
39 private static final String SHUTDOWN_ALL = "jdbc:derby:;shutdown=true";
40
41 private final ServerInfo serverInfo;
42 private final String systemHome;
43 private String actualHome;
44
45 public DerbySystemGBean(ServerInfo serverInfo, String derbySystemHome) {
46 this.serverInfo = serverInfo;
47 this.systemHome = derbySystemHome;
48 }
49
50 public String getDerbyHome() {
51 return actualHome;
52 }
53
54 public void doStart() throws Exception {
55
56 actualHome = System.getProperty(SYSTEM_HOME);
57 if (actualHome == null) {
58 actualHome = serverInfo.resolveServerPath(systemHome);
59 }
60 System.setProperty(SYSTEM_HOME, actualHome);
61
62
63
64 System.setProperty("derby.storage.fileSyncTransactionLog", "true");
65
66
67 new org.apache.derby.jdbc.EmbeddedDriver();
68 log.debug("Started in " + actualHome);
69 }
70
71 public void doStop() throws Exception {
72 try {
73 DriverManager.getConnection(SHUTDOWN_ALL, null, null);
74 } catch (SQLException e) {
75
76 }
77 System.gc();
78 log.debug("Stopped");
79 }
80
81 public void doFail() {
82 try {
83 DriverManager.getConnection(SHUTDOWN_ALL, null, null);
84 } catch (SQLException e) {
85
86 }
87 System.gc();
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.class, true);
100 infoFactory.addAttribute("derbyHome", String.class, false);
101 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
102 infoFactory.setConstructor(new String[]{"ServerInfo", "derbySystemHome"});
103 GBEAN_INFO = infoFactory.getBeanInfo();
104 }
105 }