1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.search;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.messaging.DestinationNames;
25  import com.liferay.portal.kernel.messaging.MessageBusException;
26  import com.liferay.portal.kernel.messaging.MessageBusUtil;
27  import com.liferay.portal.kernel.search.IndexSearcher;
28  import com.liferay.portal.kernel.search.IndexWriter;
29  import com.liferay.portal.kernel.search.SearchEngine;
30  import com.liferay.portal.kernel.search.messaging.SearchRequest;
31  
32  /**
33   * <a href="SearchEngineImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Bruno Farache
36   *
37   */
38  public class SearchEngineImpl implements SearchEngine {
39  
40      public String getName() {
41          throw new UnsupportedOperationException();
42      }
43  
44      public IndexSearcher getSearcher() {
45          return _searcher;
46      }
47  
48      public IndexWriter getWriter() {
49          return _writer;
50      }
51  
52      public boolean isIndexReadOnly() {
53          if (_indexReadOnly != null) {
54              return _indexReadOnly.booleanValue();
55          }
56  
57          try {
58              SearchRequest searchRequest = new SearchRequest();
59  
60              searchRequest.setCommand(SearchRequest.COMMAND_INDEX_ONLY);
61  
62              _indexReadOnly = (Boolean)MessageBusUtil.sendSynchronousMessage(
63                  DestinationNames.SEARCH_READER, searchRequest);
64  
65              if (_indexReadOnly == null) {
66                  _indexReadOnly = Boolean.FALSE;
67              }
68  
69              return _indexReadOnly.booleanValue();
70          }
71          catch (MessageBusException mbe) {
72              if (_log.isWarnEnabled()) {
73                  _log.warn("Unable to check index status", mbe);
74              }
75  
76              return false;
77          }
78      }
79  
80      public boolean isRegistered() {
81          throw new UnsupportedOperationException();
82      }
83  
84      public void register(String name) {
85          SearchRequest searchRequest = new SearchRequest();
86  
87          searchRequest.setCommand(SearchRequest.COMMAND_REGISTER);
88          searchRequest.setId(name);
89  
90          MessageBusUtil.sendMessage(
91              DestinationNames.SEARCH_WRITER, searchRequest);
92      }
93  
94      public void setSearcher(IndexSearcher searcher) {
95          _searcher = searcher;
96      }
97  
98      public void setWriter(IndexWriter writer) {
99          _writer = writer;
100     }
101 
102     public void unregister(String fromName) {
103         SearchRequest searchRequest = new SearchRequest();
104 
105         searchRequest.setCommand(SearchRequest.COMMAND_UNREGISTER);
106         searchRequest.setId(fromName);
107 
108         MessageBusUtil.sendMessage(
109             DestinationNames.SEARCH_WRITER, searchRequest);
110     }
111 
112     private static Log _log = LogFactoryUtil.getLog(SearchEngineImpl.class);
113 
114     private IndexSearcher _searcher;
115     private IndexWriter _writer;
116     private Boolean _indexReadOnly;
117 
118 }