| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.search.solr.internal; |
| 21 |
|
|
| 22 |
|
import java.io.File; |
| 23 |
|
import java.io.IOException; |
| 24 |
|
import java.io.InputStream; |
| 25 |
|
import java.util.zip.ZipEntry; |
| 26 |
|
import java.util.zip.ZipInputStream; |
| 27 |
|
|
| 28 |
|
import javax.inject.Inject; |
| 29 |
|
import javax.inject.Named; |
| 30 |
|
import javax.inject.Singleton; |
| 31 |
|
|
| 32 |
|
import org.apache.commons.io.FileUtils; |
| 33 |
|
import org.apache.commons.io.input.CloseShieldInputStream; |
| 34 |
|
import org.apache.commons.lang3.StringUtils; |
| 35 |
|
import org.apache.solr.client.solrj.SolrServerException; |
| 36 |
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; |
| 37 |
|
import org.apache.solr.core.CoreContainer; |
| 38 |
|
import org.xwiki.component.annotation.Component; |
| 39 |
|
import org.xwiki.component.manager.ComponentLifecycleException; |
| 40 |
|
import org.xwiki.component.phase.Disposable; |
| 41 |
|
import org.xwiki.component.phase.InitializationException; |
| 42 |
|
import org.xwiki.environment.Environment; |
| 43 |
|
import org.xwiki.search.solr.internal.api.SolrConfiguration; |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@version |
| 49 |
|
@since |
| 50 |
|
|
| 51 |
|
@Component |
| 52 |
|
@Named(EmbeddedSolrInstance.TYPE) |
| 53 |
|
@Singleton |
| |
|
| 86.8% |
Uncovered Elements: 9 (68) |
Complexity: 22 |
Complexity Density: 0.51 |
|
| 54 |
|
public class EmbeddedSolrInstance extends AbstractSolrInstance implements Disposable |
| 55 |
|
{ |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
public static final String TYPE = "embedded"; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
public static final String DEFAULT_SOLR_DIRECTORY_NAME = "solr"; |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
@Inject |
| 70 |
|
private SolrConfiguration solrConfiguration; |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@Inject |
| 76 |
|
private Environment environment; |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
private CoreContainer container; |
| 82 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 83 |
6 |
@Override... |
| 84 |
|
public void initialize() throws InitializationException |
| 85 |
|
{ |
| 86 |
6 |
String solrHome = determineHomeDirectory(); |
| 87 |
6 |
try { |
| 88 |
|
|
| 89 |
6 |
validateAndInitializeHomeDirectory(solrHome); |
| 90 |
|
|
| 91 |
|
|
| 92 |
5 |
this.logger.info("Starting embedded Solr server..."); |
| 93 |
5 |
this.logger.info("Using Solr home directory: [{}]", solrHome); |
| 94 |
|
|
| 95 |
|
|
| 96 |
5 |
this.container = createCoreContainer(solrHome); |
| 97 |
|
|
| 98 |
5 |
String coreName = this.container.getCores().iterator().next().getName(); |
| 99 |
5 |
this.server = new EmbeddedSolrServer(container, coreName); |
| 100 |
|
|
| 101 |
5 |
this.logger.info("Started embedded Solr server."); |
| 102 |
|
} catch (Exception e) { |
| 103 |
1 |
throw new InitializationException(String.format( |
| 104 |
|
"Failed to initialize the Solr embedded server with home directory set to [%s]", solrHome), e); |
| 105 |
|
} |
| 106 |
|
} |
| 107 |
|
|
| |
|
| 63.6% |
Uncovered Elements: 4 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 108 |
5 |
private CoreContainer createCoreContainer(String solrHome) throws SolrServerException... |
| 109 |
|
{ |
| 110 |
5 |
CoreContainer coreContainer = new CoreContainer(solrHome); |
| 111 |
5 |
coreContainer.load(); |
| 112 |
5 |
if (coreContainer.getCores().size() == 0) { |
| 113 |
0 |
throw new SolrServerException("Failed to initialize the Solr core. " |
| 114 |
|
+ "Please check the configuration and log messages."); |
| 115 |
5 |
} else if (coreContainer.getCores().size() > 1) { |
| 116 |
0 |
this.logger.warn("Multiple Solr cores detected: [{}]. Using the first one.", |
| 117 |
|
StringUtils.join(coreContainer.getCoreNames(), ", ")); |
| 118 |
|
} |
| 119 |
5 |
return coreContainer; |
| 120 |
|
} |
| 121 |
|
|
| |
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
| 122 |
5 |
@Override... |
| 123 |
|
public void dispose() throws ComponentLifecycleException |
| 124 |
|
{ |
| 125 |
5 |
if (this.server != null) { |
| 126 |
5 |
try { |
| 127 |
5 |
this.server.close(); |
| 128 |
|
} catch (IOException e) { |
| 129 |
0 |
this.logger.error("Failed to close server", e); |
| 130 |
|
} |
| 131 |
|
} |
| 132 |
|
|
| 133 |
5 |
if (this.container != null) { |
| 134 |
5 |
this.container.shutdown(); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
@return |
| 142 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 143 |
2 |
protected CoreContainer getContainer()... |
| 144 |
|
{ |
| 145 |
2 |
return this.container; |
| 146 |
|
} |
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
@param |
| 152 |
|
@throws |
| 153 |
|
@throws |
| 154 |
|
|
| |
|
| 92% |
Uncovered Elements: 2 (25) |
Complexity: 10 |
Complexity Density: 0.67 |
|
| 155 |
6 |
private void validateAndInitializeHomeDirectory(String solrHome) throws IllegalArgumentException, IOException... |
| 156 |
|
{ |
| 157 |
|
|
| 158 |
6 |
File solrHomeDirectory = new File(solrHome); |
| 159 |
6 |
if (solrHomeDirectory.exists()) { |
| 160 |
|
|
| 161 |
1 |
if (!solrHomeDirectory.isDirectory() || !solrHomeDirectory.canWrite() || !solrHomeDirectory.canRead()) { |
| 162 |
0 |
throw new IllegalArgumentException(String.format( |
| 163 |
|
"The given path [%s] must be a readable and writable directory", solrHomeDirectory)); |
| 164 |
|
} |
| 165 |
|
} else { |
| 166 |
|
|
| 167 |
5 |
if (!solrHomeDirectory.mkdirs()) { |
| 168 |
|
|
| 169 |
1 |
throw new IllegalArgumentException(String.format( |
| 170 |
|
"The given path [%s] could not be created due to and invalid value %s", solrHomeDirectory, |
| 171 |
|
"or to insufficient filesystem permissions")); |
| 172 |
|
} |
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
4 |
InputStream stream = this.solrConfiguration.getHomeDirectoryConfiguration(); |
| 178 |
4 |
try (ZipInputStream zstream = new ZipInputStream(stream)) { |
| 179 |
284 |
for (ZipEntry entry = zstream.getNextEntry(); entry != null; entry = zstream.getNextEntry()) { |
| 180 |
280 |
if (entry.isDirectory()) { |
| 181 |
36 |
File destinationDirectory = new File(solrHomeDirectory, entry.getName()); |
| 182 |
36 |
destinationDirectory.mkdirs(); |
| 183 |
|
} else { |
| 184 |
244 |
File destinationFile = new File(solrHomeDirectory, entry.getName()); |
| 185 |
244 |
FileUtils.copyInputStreamToFile(new CloseShieldInputStream(zstream), destinationFile); |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
|
| 193 |
|
@return |
| 194 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 195 |
6 |
private String determineHomeDirectory()... |
| 196 |
|
{ |
| 197 |
6 |
String defaultValue = getDefaultHomeDirectory(); |
| 198 |
|
|
| 199 |
6 |
return this.solrConfiguration.getInstanceConfiguration(TYPE, "home", defaultValue); |
| 200 |
|
} |
| 201 |
|
|
| 202 |
|
|
| 203 |
|
@return |
| 204 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 205 |
6 |
String getDefaultHomeDirectory()... |
| 206 |
|
{ |
| 207 |
6 |
String result = new File(this.environment.getPermanentDirectory(), DEFAULT_SOLR_DIRECTORY_NAME).getPath(); |
| 208 |
|
|
| 209 |
6 |
return result; |
| 210 |
|
} |
| 211 |
|
} |