Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package es.sinjava.data;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Properties;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import javax.ws.rs.Path;
- import org.apache.cxf.bus.spring.SpringBus;
- import org.apache.cxf.endpoint.Server;
- import org.apache.cxf.interceptor.Interceptor;
- import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
- import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
- import org.apache.cxf.jaxrs.spring.JaxRsConfig;
- import org.apache.cxf.jaxrs.spring.SpringResourceFactory;
- import org.apache.cxf.message.Message;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Import;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
- import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
- import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.Database;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
- /**
- *
- * @author Andrés Gaudioso mailto:[email protected]
- *
- */
- @Configuration
- @ComponentScan
- @EnableJpaRepositories
- @EnableTransactionManagement
- @Import(JaxRsConfig.class)
- public class BaseConfig {
- @Autowired
- ApplicationContext ctx;
- // Metemos el interceptor en el contexto por si hay que hacer algo complejo
- // y tal
- @Autowired
- private InInterceptor inInterceptor;
- private static final Logger LOG = LoggerFactory.getLogger(BaseConfig.class);
- @Bean
- public Server jaxRsServer() {
- LOG.trace("Init");
- LinkedList<ResourceProvider> resourceProviders = new LinkedList<>();
- for (String beanName : ctx.getBeanDefinitionNames()) {
- if (ctx.findAnnotationOnBean(beanName, Path.class) != null) {
- SpringResourceFactory factory = new SpringResourceFactory(beanName);
- factory.setApplicationContext(ctx);
- resourceProviders.add(factory);
- }
- }
- JAXRSServerFactoryBean jaxRSServerFactoryBean = new JAXRSServerFactoryBean();
- jaxRSServerFactoryBean.setBus(ctx.getBean(SpringBus.class));
- jaxRSServerFactoryBean.setProviders(Arrays.asList(new JacksonJsonProvider()));
- jaxRSServerFactoryBean.setResourceProviders(resourceProviders);
- List<Interceptor<? extends Message>> inter = new ArrayList<>();
- inter.add(inInterceptor);
- jaxRSServerFactoryBean.setInInterceptors(inter);
- List<Interceptor<? extends Message>> interceptors = new ArrayList<>();
- interceptors.add(new OutInterceptor());
- jaxRSServerFactoryBean.setOutInterceptors(interceptors);
- return jaxRSServerFactoryBean.create();
- }
- @Bean
- public DataSource dataSource() {
- EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
- EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).build();
- return db;
- }
- @Bean
- public EntityManagerFactory entityManagerFactory() {
- LOG.trace("Init");
- HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
- vendorAdapter.setGenerateDdl(true);
- vendorAdapter.setDatabase(Database.H2);
- // Si está en debug muestra el SQL
- vendorAdapter.setShowSql(LOG.isDebugEnabled());
- LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
- factory.setJpaVendorAdapter(vendorAdapter);
- factory.setPackagesToScan("es.sinjava.data.jpa.domain");
- factory.setDataSource(dataSource());
- Properties jpaProperties = new Properties();
- jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");
- jpaProperties.put("hibernate.format_sql", "true");
- factory.setJpaProperties(jpaProperties);
- factory.afterPropertiesSet();
- return factory.getObject();
- }
- @Bean
- public PlatformTransactionManager transactionManager() throws SQLException {
- LOG.trace("Init");
- return new JpaTransactionManager(entityManagerFactory());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment