001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 */ 017 018 package org.apache.geronimo.derby; 019 020 import org.apache.commons.logging.Log; 021 import org.apache.commons.logging.LogFactory; 022 import org.apache.geronimo.gbean.GBeanInfo; 023 import org.apache.geronimo.gbean.GBeanInfoBuilder; 024 import org.apache.geronimo.gbean.GBeanLifecycle; 025 import org.apache.geronimo.system.serverinfo.ServerInfo; 026 027 import java.sql.DriverManager; 028 import java.sql.SQLException; 029 030 /** 031 * A GBean that represents an instance of an Apache Derby system (a system being 032 * a collection of different databases). 033 * 034 * @version $Rev: 406493 $ $Date: 2006-05-14 18:14:11 -0700 (Sun, 14 May 2006) $ 035 */ 036 public class DerbySystemGBean implements DerbySystem, GBeanLifecycle { 037 private static final Log log = LogFactory.getLog("DerbySystem"); 038 private static final String SYSTEM_HOME = "derby.system.home"; 039 private static final String SHUTDOWN_ALL = "jdbc:derby:;shutdown=true"; 040 041 private final ServerInfo serverInfo; 042 private final String systemHome; 043 private String actualHome; 044 045 public DerbySystemGBean(ServerInfo serverInfo, String derbySystemHome) { 046 this.serverInfo = serverInfo; 047 this.systemHome = derbySystemHome; 048 } 049 050 public String getDerbyHome() { 051 return actualHome; 052 } 053 054 public void doStart() throws Exception { 055 // set up the system property for the database home 056 actualHome = System.getProperty(SYSTEM_HOME); 057 if (actualHome == null) { 058 actualHome = serverInfo.resolveServerPath(systemHome); 059 } 060 System.setProperty(SYSTEM_HOME, actualHome); 061 062 // set the magic system property that causes derby to use explicity 063 // file sync instead of relying on vm support for file open rws 064 System.setProperty("derby.storage.fileSyncTransactionLog", "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.createStatic(DerbySystemGBean.class); 099 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 }