JPAPersistenceUnitPostProcessor.java

  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. import fr.paris.lutece.portal.service.util.AppPathService;
  36. import fr.paris.lutece.util.annotation.AnnotationUtil;

  37. import org.apache.commons.io.FileUtils;
  38. import org.apache.commons.io.filefilter.FileFilterUtils;
  39. import org.apache.commons.io.filefilter.TrueFileFilter;
  40. import org.apache.logging.log4j.LogManager;
  41. import org.apache.logging.log4j.Logger;
  42. import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
  43. import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;

  44. import java.io.File;

  45. import java.util.Collection;
  46. import java.util.Set;

  47. import javax.persistence.Embeddable;
  48. import javax.persistence.Entity;
  49. import javax.persistence.MappedSuperclass;

  50. /**
  51.  *
  52.  * JPAPersistenceUnitPostProcessor : adds classpath entities to the PersistenceUnitInfo and finds *.orm.xml.
  53.  */
  54. public class JPAPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor
  55. {
  56.     private static final Logger _Log = LogManager.getLogger( JPAConstants.JPA_LOGGER );
  57.     private static final String PATH_CONF = "/WEB-INF/classes/";
  58.     private static final String SUFFIX_ORM_XML = ".orm.xml";
  59.     private static final String CLASSPATH_PATH_IDENTIFIER = "fr" + File.separator + "paris";

  60.     /**
  61.      * Scans for *.orm.xml and adds Entites from classpath.
  62.      *
  63.      * @param pui
  64.      *            the pui
  65.      */
  66.     @Override
  67.     public void postProcessPersistenceUnitInfo( MutablePersistenceUnitInfo pui )
  68.     {
  69.         _Log.info( "Scanning for JPA orm.xml files" );

  70.         for ( File ormFile : getListORMFiles( ) )
  71.         {
  72.             String ormAbsolutePath = ormFile.getAbsolutePath( );
  73.             _Log.info( "Found ORM file : {}", ormAbsolutePath );
  74.             pui.addMappingFileName( ormAbsolutePath.substring( ormAbsolutePath.indexOf( CLASSPATH_PATH_IDENTIFIER ) ) );
  75.         }

  76.         _Log.info( "Scanning for JPA entities..." );

  77.         Set<String> entityClasses = AnnotationUtil.find( Entity.class.getName( ) );
  78.         entityClasses.addAll( AnnotationUtil.find( Embeddable.class.getName( ) ) );
  79.         entityClasses.addAll( AnnotationUtil.find( MappedSuperclass.class.getName( ) ) );

  80.         for ( String strClass : entityClasses )
  81.         {
  82.             _Log.info( "Found entity class : {}", strClass );

  83.             if ( !pui.getManagedClassNames( ).contains( strClass ) )
  84.             {
  85.                 pui.addManagedClassName( strClass );
  86.             }
  87.         }

  88.         if ( _Log.isDebugEnabled( ) )
  89.         {
  90.             dumpPersistenceUnitInfo( pui );
  91.         }
  92.     }

  93.     /**
  94.      * Search for <code>WEB-INF/conf/plugins/*.orm.xml</code>.
  95.      *
  96.      * @return list of files found
  97.      */
  98.     private Collection<File> getListORMFiles( )
  99.     {
  100.         String strConfPath = AppPathService.getAbsolutePathFromRelativePath( PATH_CONF );
  101.         File dirConfPlugins = new File( strConfPath );

  102.         return FileUtils.listFiles( dirConfPlugins, FileFilterUtils.suffixFileFilter( SUFFIX_ORM_XML ), TrueFileFilter.INSTANCE );
  103.     }

  104.     /**
  105.      * Show PUI infos
  106.      *
  107.      * @param pui
  108.      *            PersistenceUnitInfo
  109.      */
  110.     private void dumpPersistenceUnitInfo( MutablePersistenceUnitInfo pui )
  111.     {
  112.         _Log.debug( "Dumping content for PersistenceUnitInfo of " + pui.getPersistenceUnitName( ) );

  113.         _Log.debug( "** getTransactionType : " + pui.getTransactionType( ) );
  114.         _Log.debug( "** getPersistenceProviderClassName : " + pui.getPersistenceProviderClassName( ) );
  115.         _Log.debug( "** getPersistenceProviderPackageName : " + pui.getPersistenceProviderPackageName( ) );
  116.         _Log.debug( "** getPersistenceUnitName : " + pui.getPersistenceUnitName( ) );
  117.         _Log.debug( "** getPersistenceXMLSchemaVersion : " + pui.getPersistenceXMLSchemaVersion( ) );
  118.         _Log.debug( "** getJtaDataSource : " + pui.getJtaDataSource( ) );
  119.         _Log.debug( "** getManagedClassNames : " + pui.getManagedClassNames( ) );
  120.         _Log.debug( "** getMappingFileNames : " + pui.getMappingFileNames( ) );
  121.         _Log.debug( "** getNonJtaDataSource : " + pui.getNonJtaDataSource( ) );
  122.         _Log.debug( "** getPersistenceUnitRootUrl :" + pui.getPersistenceUnitRootUrl( ) );
  123.         _Log.debug( "** getProperties : " + pui.getProperties( ) );
  124.     }
  125. }