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 */ 017 package org.apache.activemq.store.jdbc; 018 019 import java.io.File; 020 import java.io.IOException; 021 022 import javax.sql.DataSource; 023 024 import org.apache.activemq.util.IOHelper; 025 import org.apache.derby.jdbc.EmbeddedDataSource; 026 027 /** 028 * A helper class which provides a factory method to create a default 029 * {@link DataSource) if one is not provided. 030 * 031 * @version $Revision: 564057 $ 032 */ 033 public class DataSourceSupport { 034 035 private String dataDirectory = IOHelper.getDefaultDataDirectory(); 036 private File dataDirectoryFile; 037 private DataSource dataSource; 038 039 public DataSourceSupport() { 040 } 041 042 public DataSourceSupport(DataSource dataSource) { 043 this.dataSource = dataSource; 044 } 045 046 public File getDataDirectoryFile() { 047 if (dataDirectoryFile == null) { 048 dataDirectoryFile = new File(getDataDirectory()); 049 } 050 return dataDirectoryFile; 051 } 052 053 public void setDataDirectoryFile(File dataDirectory) { 054 this.dataDirectoryFile = dataDirectory; 055 } 056 057 public String getDataDirectory() { 058 return dataDirectory; 059 } 060 061 public void setDataDirectory(String dataDirectory) { 062 this.dataDirectory = dataDirectory; 063 } 064 065 public DataSource getDataSource() throws IOException { 066 if (dataSource == null) { 067 dataSource = createDataSource(); 068 if (dataSource == null) { 069 throw new IllegalArgumentException("No dataSource property has been configured"); 070 } 071 } 072 return dataSource; 073 } 074 075 public void setDataSource(DataSource dataSource) { 076 this.dataSource = dataSource; 077 } 078 079 protected DataSource createDataSource() throws IOException { 080 081 // Setup the Derby datasource. 082 System.setProperty("derby.system.home", getDataDirectoryFile().getCanonicalPath()); 083 System.setProperty("derby.storage.fileSyncTransactionLog", "true"); 084 System.setProperty("derby.storage.pageCacheSize", "100"); 085 086 final EmbeddedDataSource ds = new EmbeddedDataSource(); 087 ds.setDatabaseName("derbydb"); 088 ds.setCreateDatabase("create"); 089 return ds; 090 } 091 092 public String toString() { 093 return "" + dataSource; 094 } 095 096 }