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 */ 017package org.apache.activemq.store.jdbc; 018 019import java.io.File; 020import java.io.IOException; 021import java.sql.SQLException; 022 023import javax.sql.DataSource; 024 025import org.apache.activemq.broker.LockableServiceSupport; 026import org.apache.activemq.util.IOHelper; 027import org.apache.derby.jdbc.EmbeddedDataSource; 028 029/** 030 * A helper class which provides a factory method to create a default 031 * {@link DataSource) if one is not provided. 032 * 033 * 034 */ 035abstract public class DataSourceServiceSupport extends LockableServiceSupport { 036 037 private String dataDirectory = IOHelper.getDefaultDataDirectory(); 038 private File dataDirectoryFile; 039 private DataSource dataSource; 040 private DataSource createdDefaultDataSource; 041 042 public DataSourceServiceSupport() { 043 } 044 045 public DataSourceServiceSupport(DataSource dataSource) { 046 this.dataSource = dataSource; 047 } 048 049 public File getDataDirectoryFile() { 050 if (dataDirectoryFile == null) { 051 dataDirectoryFile = new File(getDataDirectory()); 052 } 053 return dataDirectoryFile; 054 } 055 056 public void setDataDirectoryFile(File dataDirectory) { 057 this.dataDirectoryFile = dataDirectory; 058 } 059 060 public String getDataDirectory() { 061 return dataDirectory; 062 } 063 064 public void setDataDirectory(String dataDirectory) { 065 this.dataDirectory = dataDirectory; 066 } 067 068 public DataSource getDataSource() throws IOException { 069 if (dataSource == null) { 070 dataSource = createDataSource(getDataDirectoryFile().getCanonicalPath()); 071 if (dataSource == null) { 072 throw new IllegalArgumentException("No dataSource property has been configured"); 073 } else { 074 createdDefaultDataSource = dataSource; 075 } 076 } 077 return dataSource; 078 } 079 080 public void closeDataSource(DataSource dataSource) { 081 if (createdDefaultDataSource != null && createdDefaultDataSource.equals(dataSource)) { 082 shutdownDefaultDataSource(dataSource); 083 createdDefaultDataSource = this.dataSource = null; 084 } 085 } 086 087 public void setDataSource(DataSource dataSource) { 088 this.dataSource = dataSource; 089 } 090 091 public static DataSource createDataSource(String homeDir) throws IOException { 092 return createDataSource(homeDir, "derbydb"); 093 } 094 095 public static DataSource createDataSource(String homeDir, String dbName) throws IOException { 096 097 // Setup the Derby datasource. 098 System.setProperty("derby.system.home", homeDir); 099 System.setProperty("derby.storage.fileSyncTransactionLog", "true"); 100 System.setProperty("derby.storage.pageCacheSize", "100"); 101 102 final EmbeddedDataSource ds = new EmbeddedDataSource(); 103 ds.setDatabaseName(dbName); 104 ds.setCreateDatabase("create"); 105 return ds; 106 } 107 108 public static void shutdownDefaultDataSource(DataSource dataSource) { 109 final EmbeddedDataSource ds = (EmbeddedDataSource) dataSource; 110 ds.setCreateDatabase("shutdown"); 111 ds.setShutdownDatabase("shutdown"); 112 try { 113 ds.getConnection(); 114 } catch (SQLException expectedAndIgnored) { 115 } 116 } 117 118 public String toString() { 119 return "" + dataSource; 120 } 121 122 123 124}