View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * JPAPersistenceUnitPostProcessor : adds classpath entities to the PersistenceUnitInfo and finds *.orm.xml.
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       * Scans for *.orm.xml and adds Entites from classpath.
69       *
70       * @param pui
71       *            the pui
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      * Search for <code>WEB-INF/conf/plugins/*.orm.xml</code>.
109      * 
110      * @return list of files found
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      * Show PUI infos
122      * 
123      * @param pui
124      *            PersistenceUnitInfo
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 }