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