andgau

Configuración

Dec 7th, 2015
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | None | 0 0
  1. package es.sinjava.data;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import java.util.Properties;
  9.  
  10. import javax.persistence.EntityManagerFactory;
  11. import javax.sql.DataSource;
  12. import javax.ws.rs.Path;
  13.  
  14. import org.apache.cxf.bus.spring.SpringBus;
  15. import org.apache.cxf.endpoint.Server;
  16. import org.apache.cxf.interceptor.Interceptor;
  17. import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
  18. import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
  19. import org.apache.cxf.jaxrs.spring.JaxRsConfig;
  20. import org.apache.cxf.jaxrs.spring.SpringResourceFactory;
  21. import org.apache.cxf.message.Message;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.context.ApplicationContext;
  26. import org.springframework.context.annotation.Bean;
  27. import org.springframework.context.annotation.ComponentScan;
  28. import org.springframework.context.annotation.Configuration;
  29. import org.springframework.context.annotation.Import;
  30. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  31. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
  32. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
  33. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
  34. import org.springframework.orm.jpa.JpaTransactionManager;
  35. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  36. import org.springframework.orm.jpa.vendor.Database;
  37. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  38. import org.springframework.transaction.PlatformTransactionManager;
  39. import org.springframework.transaction.annotation.EnableTransactionManagement;
  40.  
  41. import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
  42.  
  43. /**
  44.  *
  45.  * @author Andrés Gaudioso mailto:[email protected]
  46.  *
  47.  */
  48. @Configuration
  49. @ComponentScan
  50. @EnableJpaRepositories
  51. @EnableTransactionManagement
  52. @Import(JaxRsConfig.class)
  53. public class BaseConfig {
  54.  
  55.     @Autowired
  56.     ApplicationContext ctx;
  57.  
  58.     // Metemos el interceptor en el contexto por si hay que hacer algo complejo
  59.     // y tal
  60.     @Autowired
  61.     private InInterceptor inInterceptor;
  62.  
  63.     private static final Logger LOG = LoggerFactory.getLogger(BaseConfig.class);
  64.  
  65.     @Bean
  66.     public Server jaxRsServer() {
  67.         LOG.trace("Init");
  68.         LinkedList<ResourceProvider> resourceProviders = new LinkedList<>();
  69.         for (String beanName : ctx.getBeanDefinitionNames()) {
  70.             if (ctx.findAnnotationOnBean(beanName, Path.class) != null) {
  71.                 SpringResourceFactory factory = new SpringResourceFactory(beanName);
  72.                 factory.setApplicationContext(ctx);
  73.                 resourceProviders.add(factory);
  74.             }
  75.         }
  76.         JAXRSServerFactoryBean jaxRSServerFactoryBean = new JAXRSServerFactoryBean();
  77.         jaxRSServerFactoryBean.setBus(ctx.getBean(SpringBus.class));
  78.         jaxRSServerFactoryBean.setProviders(Arrays.asList(new JacksonJsonProvider()));
  79.         jaxRSServerFactoryBean.setResourceProviders(resourceProviders);
  80.         List<Interceptor<? extends Message>> inter = new ArrayList<>();
  81.         inter.add(inInterceptor);
  82.         jaxRSServerFactoryBean.setInInterceptors(inter);
  83.  
  84.         List<Interceptor<? extends Message>> interceptors = new ArrayList<>();
  85.         interceptors.add(new OutInterceptor());
  86.         jaxRSServerFactoryBean.setOutInterceptors(interceptors);
  87.  
  88.         return jaxRSServerFactoryBean.create();
  89.     }
  90.  
  91.     @Bean
  92.     public DataSource dataSource() {
  93.         EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
  94.         EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).build();
  95.         return db;
  96.     }
  97.  
  98.     @Bean
  99.     public EntityManagerFactory entityManagerFactory() {
  100.         LOG.trace("Init");
  101.         HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  102.         vendorAdapter.setGenerateDdl(true);
  103.         vendorAdapter.setDatabase(Database.H2);
  104.         // Si está en debug muestra el SQL
  105.         vendorAdapter.setShowSql(LOG.isDebugEnabled());
  106.         LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  107.         factory.setJpaVendorAdapter(vendorAdapter);
  108.         factory.setPackagesToScan("es.sinjava.data.jpa.domain");
  109.         factory.setDataSource(dataSource());
  110.         Properties jpaProperties = new Properties();
  111.         jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");
  112.         jpaProperties.put("hibernate.format_sql", "true");
  113.         factory.setJpaProperties(jpaProperties);
  114.         factory.afterPropertiesSet();
  115.         return factory.getObject();
  116.     }
  117.  
  118.     @Bean
  119.     public PlatformTransactionManager transactionManager() throws SQLException {
  120.         LOG.trace("Init");
  121.         return new JpaTransactionManager(entityManagerFactory());
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment