View Javadoc
1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.logging.log4j.catalog.jpa.config;
17  
18  import javax.persistence.EntityManagerFactory;
19  
20  import java.util.Properties;
21  
22  import org.apache.logging.log4j.LogManager;
23  import org.apache.logging.log4j.Logger;
24  import org.springframework.beans.factory.annotation.Autowired;
25  import org.springframework.context.annotation.Bean;
26  import org.springframework.context.annotation.Configuration;
27  import org.springframework.context.annotation.Profile;
28  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
29  import org.springframework.orm.jpa.JpaTransactionManager;
30  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
31  import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
32  import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
33  import org.springframework.transaction.PlatformTransactionManager;
34  import org.springframework.transaction.annotation.EnableTransactionManagement;
35  
36  @Configuration
37  @EnableJpaRepositories("org.apache.logging.log4j.catalog.jpa.dao")
38  @EnableTransactionManagement
39  @Profile("eclipseLink")
40  public class EclipseLinkConfig {
41  
42      private static Logger LOGGER = LogManager.getLogger(EclipseLinkConfig.class);
43  
44      @Autowired
45      private DataSourceConfig dataSourceConfig;
46  
47      @Bean
48      public EntityManagerFactory entityManagerFactory() {
49          LOGGER.debug("Creating EclipseLink entity manager.");
50          AbstractJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
51          vendorAdapter.setGenerateDdl(false);
52          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
53          Properties properties = new Properties();
54          properties.setProperty("eclipselink.weaving", "static");
55          factory.setJpaProperties(properties);
56          factory.setJpaVendorAdapter(vendorAdapter);
57          factory.setPackagesToScan("org.apache.logging.log4j.catalog");
58          factory.setDataSource(dataSourceConfig.dataSource());
59          factory.afterPropertiesSet();
60  
61          return factory.getObject();
62      }
63  
64      @Bean
65      public PlatformTransactionManager transactionManager() {
66          JpaTransactionManager txManager = new JpaTransactionManager();
67          txManager.setEntityManagerFactory(entityManagerFactory());
68          return txManager;
69      }
70  }