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.util.jpa;
35
36 import fr.paris.lutece.portal.service.util.AppPathService;
37 import fr.paris.lutece.util.annotation.AnnotationUtil;
38
39 import org.apache.commons.io.FileUtils;
40 import org.apache.commons.io.filefilter.FileFilterUtils;
41 import org.apache.commons.io.filefilter.TrueFileFilter;
42 import org.apache.logging.log4j.LogManager;
43 import org.apache.logging.log4j.Logger;
44 import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
45 import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
46
47 import java.io.File;
48
49 import java.util.Collection;
50 import java.util.Set;
51
52 import javax.persistence.Embeddable;
53 import javax.persistence.Entity;
54 import javax.persistence.MappedSuperclass;
55
56
57
58
59
60 public class JPAPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor
61 {
62 private static final Logger _Log = LogManager.getLogger( JPAConstants.JPA_LOGGER );
63 private static final String PATH_CONF = "/WEB-INF/classes/";
64 private static final String SUFFIX_ORM_XML = ".orm.xml";
65 private static final String CLASSPATH_PATH_IDENTIFIER = "fr" + File.separator + "paris";
66
67
68
69
70
71
72
73 @Override
74 public void postProcessPersistenceUnitInfo( MutablePersistenceUnitInfo pui )
75 {
76 _Log.info( "Scanning for JPA orm.xml files" );
77
78 for ( File ormFile : getListORMFiles( ) )
79 {
80 String ormAbsolutePath = ormFile.getAbsolutePath( );
81 _Log.info( "Found ORM file : {}", ormAbsolutePath );
82 pui.addMappingFileName( ormAbsolutePath.substring( ormAbsolutePath.indexOf( CLASSPATH_PATH_IDENTIFIER ) ) );
83 }
84
85 _Log.info( "Scanning for JPA entities..." );
86
87 Set<String> entityClasses = AnnotationUtil.find( Entity.class.getName( ) );
88 entityClasses.addAll( AnnotationUtil.find( Embeddable.class.getName( ) ) );
89 entityClasses.addAll( AnnotationUtil.find( MappedSuperclass.class.getName( ) ) );
90
91 for ( String strClass : entityClasses )
92 {
93 _Log.info( "Found entity class : {}", strClass );
94
95 if ( !pui.getManagedClassNames( ).contains( strClass ) )
96 {
97 pui.addManagedClassName( strClass );
98 }
99 }
100
101 if ( _Log.isDebugEnabled( ) )
102 {
103 dumpPersistenceUnitInfo( pui );
104 }
105 }
106
107
108
109
110
111
112 private Collection<File> getListORMFiles( )
113 {
114 String strConfPath = AppPathService.getAbsolutePathFromRelativePath( PATH_CONF );
115 File dirConfPlugins = new File( strConfPath );
116
117 return FileUtils.listFiles( dirConfPlugins, FileFilterUtils.suffixFileFilter( SUFFIX_ORM_XML ), TrueFileFilter.INSTANCE );
118 }
119
120
121
122
123
124
125
126 private void dumpPersistenceUnitInfo( MutablePersistenceUnitInfo pui )
127 {
128 _Log.debug( "Dumping content for PersistenceUnitInfo of " + pui.getPersistenceUnitName( ) );
129
130 _Log.debug( "** getTransactionType : " + pui.getTransactionType( ) );
131 _Log.debug( "** getPersistenceProviderClassName : " + pui.getPersistenceProviderClassName( ) );
132 _Log.debug( "** getPersistenceProviderPackageName : " + pui.getPersistenceProviderPackageName( ) );
133 _Log.debug( "** getPersistenceUnitName : " + pui.getPersistenceUnitName( ) );
134 _Log.debug( "** getPersistenceXMLSchemaVersion : " + pui.getPersistenceXMLSchemaVersion( ) );
135 _Log.debug( "** getJtaDataSource : " + pui.getJtaDataSource( ) );
136 _Log.debug( "** getManagedClassNames : " + pui.getManagedClassNames( ) );
137 _Log.debug( "** getMappingFileNames : " + pui.getMappingFileNames( ) );
138 _Log.debug( "** getNonJtaDataSource : " + pui.getNonJtaDataSource( ) );
139 _Log.debug( "** getPersistenceUnitRootUrl :" + pui.getPersistenceUnitRootUrl( ) );
140 _Log.debug( "** getProperties : " + pui.getProperties( ) );
141 }
142 }