com.gantzgulch.sharing.configuration.ApplicationContext.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.configuration.ApplicationContext.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GantzFileSharing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.configuration;

import java.io.File;
import java.util.HashMap;

import javax.sql.DataSource;

import liquibase.integration.spring.SpringLiquibase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan(basePackages = { "com.gantzgulch.sharing.dao",
        "com.gantzgulch.sharing.service" }, excludeFilters = @ComponentScan.Filter(Configuration.class))
@EnableTransactionManagement
public class ApplicationContext {

    private static final Log LOG = LogFactory.getLog(ApplicationContext.class);

    public ApplicationContext() {
        LOG.debug("ctor");
    }

    @Autowired
    @Qualifier("sharingDS")
    private DataSource sharingDataSource;

    @Bean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME)
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }

    @Bean(name = "storageLocation")
    public String storageLocation() {

        File storageDirectory = tryDirectory(System.getProperty("GantzSharingStorageLocation"));

        if (storageDirectory == null) {
            storageDirectory = tryDirectory("/tmp/sharing");
        }

        if (storageDirectory == null) {
            throw new RuntimeException("Unable to establish a storage directory.");
        }

        return storageDirectory.getAbsolutePath();
    }

    private File tryDirectory(String defaultStorageLocation) {
        System.out.println("Looking for storage location : trying : " + defaultStorageLocation);

        if (defaultStorageLocation == null) {
            return null;
        }

        File file = new File(defaultStorageLocation);
        file.mkdirs();
        return file.isDirectory() && file.canWrite() ? file : null;
    }

    @Bean(name = "liquibase")
    public SpringLiquibase liquibase() {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(sharingDataSource);
        liquibase.setChangeLog("classpath:sharing-changelog.xml");
        liquibase.setChangeLogParameters(new HashMap<String, String>());
        return liquibase;
    }

    @Bean(name = "sessionFactory")
    public SessionFactory sessionFactory() throws Exception {
        AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
        sessionFactory.setDataSource(sharingDataSource);
        sessionFactory.setPackagesToScan("com.gantzgulch");
        sessionFactory.afterPropertiesSet();
        return sessionFactory.getObject();
    }

    @Bean(name = "transactionManager")
    public HibernateTransactionManager transactionManager() throws Exception {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory());
        return transactionManager;
    }
}