001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.derby;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.gbean.GBeanInfo;
021: import org.apache.geronimo.gbean.GBeanInfoBuilder;
022: import org.apache.geronimo.gbean.GBeanLifecycle;
023: import org.apache.geronimo.system.serverinfo.ServerInfo;
024:
025: import java.sql.DriverManager;
026: import java.sql.SQLException;
027:
028: /**
029: * A GBean that represents an instance of an Apache Derby system (a system being
030: * a collection of different databases).
031: *
032: * @version $Rev: 514683 $ $Date: 2007-03-05 06:56:29 -0800 (Mon, 05 Mar 2007) $
033: */
034: public class DerbySystemGBean implements DerbySystem, GBeanLifecycle {
035: private static final Log log = LogFactory.getLog("DerbySystem");
036: private static final String SYSTEM_HOME = "derby.system.home";
037: private static final String SHUTDOWN_ALL = "jdbc:derby:;shutdown=true";
038:
039: private final ServerInfo serverInfo;
040: private final String systemHome;
041: private String actualHome;
042:
043: public DerbySystemGBean(ServerInfo serverInfo,
044: String derbySystemHome) {
045: this .serverInfo = serverInfo;
046: this .systemHome = derbySystemHome;
047: }
048:
049: public String getDerbyHome() {
050: return actualHome;
051: }
052:
053: public void doStart() throws Exception {
054: // set up the system property for the database home
055: actualHome = System.getProperty(SYSTEM_HOME);
056: if (actualHome == null) {
057: actualHome = serverInfo.resolveServerPath(systemHome);
058: }
059: System.setProperty(SYSTEM_HOME, actualHome);
060:
061: // set the magic system property that causes derby to use explicity
062: // file sync instead of relying on vm support for file open rws
063: System.setProperty("derby.storage.fileSyncTransactionLog",
064: "true");
065:
066: // load the Embedded driver to initialize the home
067: new org.apache.derby.jdbc.EmbeddedDriver();
068: log.debug("Started in " + actualHome);
069: }
070:
071: public void doStop() throws Exception {
072: try {
073: DriverManager.getConnection(SHUTDOWN_ALL, null, null);
074: } catch (SQLException e) {
075: // SQLException gets thrown on successful shutdown so ignore
076: }
077: System.gc(); // Added per recommendation Derby documentation
078: log.debug("Stopped");
079: }
080:
081: public void doFail() {
082: try {
083: DriverManager.getConnection(SHUTDOWN_ALL, null, null);
084: } catch (SQLException e) {
085: // SQLException gets thrown on successful shutdown so ignore
086: }
087: System.gc(); // Added per recommendation Derby documentation
088: log.warn("Failed");
089: }
090:
091: public static final GBeanInfo GBEAN_INFO;
092:
093: public static GBeanInfo getGBeanInfo() {
094: return GBEAN_INFO;
095: }
096:
097: static {
098: GBeanInfoBuilder infoFactory = GBeanInfoBuilder
099: .createStatic(DerbySystemGBean.class);
100: infoFactory.addAttribute("derbySystemHome", String.class, true);
101: infoFactory.addAttribute("derbyHome", String.class, false);
102: infoFactory.addReference("ServerInfo", ServerInfo.class,
103: "GBean");
104: infoFactory.setConstructor(new String[] { "ServerInfo",
105: "derbySystemHome" });
106: infoFactory.setPriority(GBeanInfo.PRIORITY_CLASSLOADER);
107: GBEAN_INFO = infoFactory.getBeanInfo();
108: }
109: }
|