org.dbflute.utflute.spring.EmbeddedH2UrlFactoryBean.java Source code

Java tutorial

Introduction

Here is the source code for org.dbflute.utflute.spring.EmbeddedH2UrlFactoryBean.java

Source

/*
 * Copyright 2014-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.dbflute.utflute.spring;

import java.io.File;
import java.io.IOException;

import org.dbflute.util.DfReflectionUtil;
import org.dbflute.util.DfResourceUtil;
import org.springframework.beans.factory.FactoryBean;

/**
 * The bean for resolving a path to a database of H2 Database. <br />
 * This is NOT an important class as example. So you don't need to read this.
 * @author jflute
 */
public class EmbeddedH2UrlFactoryBean implements FactoryBean<Object> {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    protected String _urlSuffix;
    protected String _referenceClassName;

    // ===================================================================================
    //                                                                                Main
    //                                                                                ====
    public Object getObject() throws Exception {
        return getUrl();
    }

    public Class<?> getObjectType() {
        return String.class;
    }

    public boolean isSingleton() {
        return true;
    }

    protected String getUrl() {
        try {
            final File buildDir = getBuildDir();
            final String canonicalPath = buildDir.getCanonicalPath();
            return "jdbc:h2:file:" + canonicalPath.replace('\\', '/') + _urlSuffix;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected File getBuildDir() {
        final Class<?> clazz = DfReflectionUtil.forName(_referenceClassName);
        return DfResourceUtil.getBuildDir(clazz);
    }

    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    public void setReferenceClassName(String referenceClassName) {
        _referenceClassName = referenceClassName;
    }

    public void setUrlSuffix(String urlSuffix) {
        _urlSuffix = urlSuffix;
    }
}