1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.jpa;
35
36 import fr.paris.lutece.portal.service.database.AppConnectionService;
37 import fr.paris.lutece.portal.service.init.StartUpService;
38 import fr.paris.lutece.portal.service.spring.SpringContextService;
39 import fr.paris.lutece.portal.service.util.AppPropertiesService;
40 import fr.paris.lutece.util.ReferenceItem;
41 import fr.paris.lutece.util.ReferenceList;
42 import fr.paris.lutece.util.jpa.JPAConstants;
43 import fr.paris.lutece.util.jpa.JPAPersistenceUnitPostProcessor;
44 import fr.paris.lutece.util.jpa.transaction.ChainedTransactionManager;
45
46 import org.apache.commons.lang3.StringUtils;
47 import org.apache.logging.log4j.LogManager;
48 import org.apache.logging.log4j.Logger;
49 import org.springframework.orm.jpa.JpaDialect;
50 import org.springframework.orm.jpa.JpaTransactionManager;
51 import org.springframework.orm.jpa.JpaVendorAdapter;
52 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
53 import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
54 import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
55
56 import org.springframework.transaction.PlatformTransactionManager;
57
58 import java.util.ArrayList;
59 import java.util.HashMap;
60 import java.util.List;
61 import java.util.Map;
62
63 import javax.persistence.EntityManagerFactory;
64
65 import javax.sql.DataSource;
66
67
68
69
70 public class JPAStartupService implements StartUpService
71 {
72
73 private static final String JPA_DIALECT_PROPERTY = "jpa.dialect.property";
74 private static Logger _log = LogManager.getLogger( JPAConstants.JPA_LOGGER );
75
76
77
78
79 public void process( )
80 {
81 ReferenceListnceList.html#ReferenceList">ReferenceList list = new ReferenceList( );
82 AppConnectionService.getPoolList( list );
83
84 Map<String, EntityManagerFactory> mapFactories = new HashMap<>( );
85 List<PlatformTransactionManager> listTransactionManagers = new ArrayList<>( );
86 _log.info( "JPA Startup Service : Initializing JPA objects ..." );
87
88 String strDialectProperty = AppPropertiesService.getProperty( JPA_DIALECT_PROPERTY );
89
90 for ( ReferenceItem poolItem : list )
91 {
92 String strPoolname = poolItem.getCode( );
93
94 DataSource ds = AppConnectionService.getPoolManager( ).getDataSource( strPoolname );
95 _log.info( "JPA Startup Service : DataSource retrieved for pool : " + strPoolname );
96 _log.debug( "> DS : " + ds.toString( ) );
97
98 DefaultPersistenceUnitManager pum = new DefaultPersistenceUnitManager( );
99 pum.setDefaultDataSource( ds );
100
101 PersistenceUnitPostProcessor [ ] postProcessors = {
102 new JPAPersistenceUnitPostProcessor( )
103 };
104 pum.setPersistenceUnitPostProcessors( postProcessors );
105
106 pum.afterPropertiesSet( );
107
108 _log.info( "JPA Startup Service : Persistence Unit Manager for pool : " + strPoolname );
109 _log.debug( "> PUM : " + pum.toString( ) );
110
111 LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean( );
112 lcemfb.setDataSource( ds );
113 lcemfb.setPersistenceUnitManager( pum );
114 lcemfb.setPersistenceUnitName( "jpaLuteceUnit" );
115
116 JpaDialect jpaDialect = SpringContextService.getBean( "jpaDialect" );
117 lcemfb.setJpaDialect( jpaDialect );
118
119 Map mapJpaProperties = SpringContextService.getBean( "jpaPropertiesMap" );
120 lcemfb.setJpaPropertyMap( mapJpaProperties );
121
122 String strDialect = AppPropertiesService.getProperty( poolItem.getName( ) + ".dialect" );
123
124
125 if ( StringUtils.isNotBlank( strDialect ) )
126 {
127 mapJpaProperties.put( strDialectProperty, strDialect );
128 }
129
130 _log.debug( "Using dialect " + mapJpaProperties.get( strDialectProperty ) + " for pool " + poolItem.getName( ) );
131
132 JpaVendorAdapter jpaVendorAdapter = SpringContextService.getBean( "jpaVendorAdapter" );
133 lcemfb.setJpaVendorAdapter( jpaVendorAdapter );
134
135 lcemfb.afterPropertiesSet( );
136
137 EntityManagerFactory emf = lcemfb.getNativeEntityManagerFactory( );
138 _log.info( "JPA Startup Service : EntityManagerFactory created for pool : " + strPoolname );
139 _log.debug( "> EMF : " + emf.toString( ) );
140
141 JpaTransactionManager tm = new JpaTransactionManager( );
142 tm.setEntityManagerFactory( emf );
143 tm.setJpaDialect( jpaDialect );
144 _log.debug( "> JpaDialect " + jpaDialect );
145 tm.afterPropertiesSet( );
146 _log.info( "JPA Startup Service : JPA TransactionManager created for pool : " + strPoolname );
147 _log.debug( "> TM : " + tm.toString( ) );
148
149 mapFactories.put( strPoolname, emf );
150 listTransactionManagers.add( tm );
151 }
152
153 EntityManagerService ems = SpringContextService.getBean( "entityManagerService" );
154 ems.setMapFactories( mapFactories );
155
156 ChainedTransactionManager ctm = SpringContextService.getBean( "transactionManager" );
157 ctm.setTransactionManagers( listTransactionManagers );
158 _log.info( "JPA Startup Service : completed successfully" );
159 }
160
161
162
163
164
165
166 public String getName( )
167 {
168 return ( "JPA Startup Service" );
169 }
170 }