View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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  
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   * JPAPersistenceUnitPostProcessor : adds classpath entities to the PersistenceUnitInfo and finds *.orm.xml.
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       * Scans for *.orm.xml and adds Entites from classpath.
71       *
72       * @param pui the pui
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      * Search for <code>WEB-INF/conf/plugins/*.orm.xml</code>.
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 ),
118             TrueFileFilter.INSTANCE );
119     }
120 
121     /**
122      * Show PUI infos
123      * @param pui PersistenceUnitInfo
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 }