1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal

File RemoteSolrInstance.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

2
12
2
1
123
49
5
0.42
6
2
2.5

Classes

Class Line # Actions
RemoteSolrInstance 49 12 0% 5 16
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.search.solr.internal;
21   
22    import java.io.InputStream;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27    import javax.inject.Singleton;
28   
29    import org.apache.solr.client.solrj.impl.HttpSolrClient;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.phase.InitializationException;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.search.solr.internal.api.SolrConfiguration;
34   
35    import com.xpn.xwiki.XWiki;
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.doc.XWikiAttachment;
38    import com.xpn.xwiki.doc.XWikiDocument;
39   
40    /**
41    * Remote Solr instance communicating over HTTP.
42    *
43    * @version $Id: a6c24096ab948dd3b82e20e4f393267cf69d4ab8 $
44    * @since 4.3M2
45    */
46    @Component
47    @Named(RemoteSolrInstance.TYPE)
48    @Singleton
 
49    public class RemoteSolrInstance extends AbstractSolrInstance
50    {
51    /**
52    * Solr instance type for this implementation.
53    */
54    public static final String TYPE = "remote";
55   
56    /**
57    * Default URL to use when none is specified.
58    */
59    public static final String DEFAULT_REMOTE_URL = "http://localhost:8983/solr/";
60   
61    /**
62    * The name of the attachment containing the zipped configuration files.
63    */
64    public static final String CONFIGURATION_ZIP_FILE_NAME = "conf.zip";
65   
66    @Inject
67    private Provider<XWikiContext> xcontextProvider;
68   
69    /**
70    * Solr's configuration.
71    */
72    @Inject
73    private SolrConfiguration configuration;
74   
 
75  0 toggle @Override
76    public void initialize() throws InitializationException
77    {
78  0 String remoteURL = this.configuration.getInstanceConfiguration(TYPE, "url", DEFAULT_REMOTE_URL);
79   
80    // Disabled since this component is initialized when XWiki starts, before any XWikiContext is available, so it
81    // will always fail
82    // try {
83    // this.generateAndAttachConfigurationZipIfNotExist();
84    // } catch (Exception e) {
85    // // This is not a critical issue, since the remote server may already be configured.
86    // // We still log it as an error though.
87    // this.logger.error("Failed to generate the remote server's configuration.", e);
88    // }
89   
90    // Initialize the remote Solr server.
91  0 this.server = new HttpSolrClient(remoteURL);
92    }
93   
94    /**
95    * Generates the configuration files required to properly configure and initialize the remote Solr server.
96    * <p>
97    * The files are available as a zip archive ("solr.zip") attached to the main wiki's XWiki.SolrSearchAdmin document
98    * and exposed in the user interface of the main wiki.
99    * <p>
100    * Note: If the attachment already exists, nothing will be done.
101    *
102    * @throws Exception if problems occur.
103    */
 
104  0 toggle public void generateAndAttachConfigurationZipIfNotExist() throws Exception
105    {
106  0 XWikiContext context = this.xcontextProvider.get();
107  0 XWiki xwiki = context.getWiki();
108   
109    // Get or Create the attachment and dump the content of the zip into the attachment.
110  0 DocumentReference configDocumentReference =
111    new DocumentReference(context.getMainXWiki(), "XWiki", "SolrSearchAdmin");
112  0 XWikiDocument configurationDocument = xwiki.getDocument(configDocumentReference, context);
113  0 XWikiAttachment configurationZipAttachment = configurationDocument.getAttachment(CONFIGURATION_ZIP_FILE_NAME);
114  0 if (configurationZipAttachment == null) {
115    // Create the zip file to attach.
116  0 try (InputStream inputStream = this.configuration.getHomeDirectoryConfiguration()) {
117    // Attach the file.
118  0 configurationDocument.addAttachment(CONFIGURATION_ZIP_FILE_NAME, inputStream, context);
119  0 xwiki.saveDocument(configurationDocument, "Attach default SOLR configuration", context);
120    }
121    }
122    }
123    }