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.plugins.directory.service.directorysearch;
35  
36  import java.io.IOException;
37  import java.nio.file.Paths;
38  
39  import org.apache.commons.lang3.mutable.MutableBoolean;
40  import org.apache.lucene.analysis.Analyzer;
41  import org.apache.lucene.index.DirectoryReader;
42  import org.apache.lucene.index.IndexWriter;
43  import org.apache.lucene.index.IndexWriterConfig;
44  import org.apache.lucene.index.IndexWriterConfig.OpenMode;
45  import org.apache.lucene.search.IndexSearcher;
46  import org.apache.lucene.store.Directory;
47  import org.apache.lucene.store.NIOFSDirectory;
48  
49  import fr.paris.lutece.plugins.lucene.service.analyzer.LuteceFrenchAnalyzer;
50  import fr.paris.lutece.portal.service.util.AppLogService;
51  import fr.paris.lutece.portal.service.util.AppPathService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  
54  /**
55   * Factory for the search on Directory
56   */
57  public class DirectorySearchFactory
58  {
59      // Constants
60      private static final String PATH_INDEX = "directory.internalIndexer.lucene.indexPath";
61      private static final String PROPERTY_ANALYSER_CLASS_NAME = "directory.internalIndexer.lucene.analyser.className";
62  
63      // Variables
64      private Analyzer _analyzer;
65  
66      /**
67       * Constructor
68       */
69      private DirectorySearchFactory( )
70      {
71          try
72          {
73              String strAnalyserClassName = AppPropertiesService.getProperty( PROPERTY_ANALYSER_CLASS_NAME );
74              _analyzer = (Analyzer) Class.forName( strAnalyserClassName ).newInstance( );
75          }
76          catch( InstantiationException | IllegalAccessException | ClassNotFoundException exception )
77          {
78              AppLogService.error( "Failed to instanciate the analyzer of the type: " + PROPERTY_ANALYSER_CLASS_NAME );
79  
80              _analyzer = new LuteceFrenchAnalyzer( );
81          }
82      }
83  
84      /**
85       * Return the instance of the DirectorySearchFactory
86       * 
87       * @return the instance of the DirectorySearchFactory
88       */
89      public static DirectorySearchFactory getInstance( )
90      {
91          return DirectorySearchFactoryHolder._instance;
92      }
93  
94      /**
95       * Return the Analyzer to use for the search
96       * 
97       * @return the Analyzer to use for the search
98       */
99      public Analyzer getAnalyzer( )
100     {
101         return _analyzer;
102     }
103 
104     /**
105      * Return the IndexSearcher to use for the search
106      * 
107      * @return the index searcher to use for the search
108      * @throws IOException
109      *             - if there is a low-level IO error
110      */
111     public IndexSearcher getIndexSearcher( ) throws IOException
112     {
113         Directory luceneDirectory = getDirectory( );
114 
115         return new IndexSearcher( DirectoryReader.open( luceneDirectory ) );
116     }
117 
118     /**
119      * Create the IndexWriter with its configuration
120      * 
121      * @param bCreateIndex
122      *            The boolean which tell if the index must be created
123      * @return the created IndexWriter
124      * @throws IOException
125      *             - if there is a low level IO error
126      */
127     public IndexWriter getIndexWriter( MutableBoolean bCreateIndex ) throws IOException
128     {
129         Directory luceneDirectory = getDirectory( );
130 
131         if ( !DirectoryReader.indexExists( luceneDirectory ) )
132         {
133             bCreateIndex.setValue( Boolean.TRUE );
134         }
135 
136         IndexWriterConfig conf = new IndexWriterConfig( getAnalyzer( ) );
137 
138         if ( bCreateIndex.isTrue( ) )
139         {
140             conf.setOpenMode( OpenMode.CREATE );
141         }
142         else
143         {
144             conf.setOpenMode( OpenMode.APPEND );
145         }
146 
147         return new IndexWriter( luceneDirectory, conf );
148     }
149 
150     /**
151      * Return the Directory to use for the search
152      * 
153      * @return the Directory to use for the search
154      * @throws IOException
155      *             - if the path string cannot be converted to a Path
156      */
157     private Directory getDirectory( ) throws IOException
158     {
159         String strIndex = AppPathService.getPath( PATH_INDEX );
160 
161         return NIOFSDirectory.open( Paths.get( strIndex ) );
162     }
163 
164     /**
165      * Holder to manage the DirectorySearchFactory singleton
166      */
167     private static class DirectorySearchFactoryHolder
168     {
169         public static final DirectorySearchFactory _instance = new DirectorySearchFactory( );
170     }
171 }