name.marcelomorales.siqisiqi.examples.crud.CrudContext.java Source code

Java tutorial

Introduction

Here is the source code for name.marcelomorales.siqisiqi.examples.crud.CrudContext.java

Source

/*
 * Copyright 2013 Marcelo Morales me@marcelomorales.name
 *
 *    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 name.marcelomorales.siqisiqi.examples.crud;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableSet;
import com.typesafe.config.Config;
import name.marcelomorales.siqisiqi.bonecp.DataSourceProvider;
import name.marcelomorales.siqisiqi.configuration.ConfigProvider;
import name.marcelomorales.siqisiqi.openjpa.EntityManagerFactoryProvider;
import name.marcelomorales.siqisiqi.openjpa.api.EntityRepository;
import name.marcelomorales.siqisiqi.openjpa.api.EntityRespositoryPlugin;
import name.marcelomorales.siqisiqi.openjpa.spring.OpenJpaRepositoryFactoryBean;
import name.marcelomorales.siqisiqi.openjpa.spring.SpringRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.vendor.OpenJpaDialect;
import org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

/**
 * @author Marcelo Morales
 */
@Configuration
@ComponentScan({ "name.marcelomorales.siqisiqi.examples.crud.empdept.services",
        "name.marcelomorales.siqisiqi.parameters.services" })
@EnableTransactionManagement
@EnableJpaRepositories(value = { "name.marcelomorales.siqisiqi.examples.crud.empdept.repositories",
        "name.marcelomorales.siqisiqi.parameters.repositories" }, repositoryFactoryBeanClass = OpenJpaRepositoryFactoryBean.class)
public class CrudContext {

    @Bean
    public ConfigProvider configProvider() {
        return new ConfigProvider("marcelomorales", "simple-example").lifted(System.getProperty("stage"));
    }

    @Bean
    public Config config() {
        return configProvider().get();
    }

    @Bean
    public Cache<String, String> cache() {
        return CacheBuilder.newBuilder().build(); // no cache module yet!
    }

    @Bean(destroyMethod = "close")
    public DataSourceProvider boneCpProvider() throws Exception {
        return new DataSourceProvider(config());
    }

    @Bean
    public DataSource boneCp() throws Exception {
        return boneCpProvider().get();
    }

    @Bean(destroyMethod = "close")
    public EntityManagerFactoryProvider entityManagerFactoryProvider() throws Exception {
        final DataSource dataSource = boneCp();
        final EntityRespositoryPlugin element = new EntityRespositoryPlugin() {

            @Override
            public void plugInto(EntityRepository entityRepository) {
                entityRepository.setProperty("openjpa.ConnectionFactory", dataSource);
                entityRepository.setDataSource(dataSource);
                entityRepository.setNonJtaDataSource(dataSource);
                SpringRegistrar.registerEntities(entityRepository,
                        "name.marcelomorales.siqisiqi.parameters.entities",
                        "name.marcelomorales.siqisiqi.examples.crud.empdept.entities");
            }
        };
        return new EntityManagerFactoryProvider(config(), ImmutableSet.of(element));
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() throws Exception {
        return entityManagerFactoryProvider().get();
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws Exception {
        return new JpaTransactionManager(entityManagerFactory());
    }

    @Bean
    public OpenJpaDialect openJpaDialect() {
        return new OpenJpaDialect();
    }

    @Bean
    public SimpleJaxWsServiceExporter simpleJaxWsServiceExporter() {
        final SimpleJaxWsServiceExporter serviceExporter = new SimpleJaxWsServiceExporter();
        serviceExporter.setBaseAddress("http://0.0.0.0:8181");
        return serviceExporter;
    }
}