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