/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.search.configuration.internal;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;
import com.perforce.search.configuration.Configuration;
public class ConfigurationImpl implements Configuration {
private static final Logger log = Logger.getLogger(ConfigurationImpl.class);
private Properties configuration = new Properties();
public ConfigurationImpl(final InputStream configurationStream) {
if (configurationStream == null) {
return;
}
try {
configuration.load(configurationStream);
} catch (IOException e) {
log.error("Configuration could not load properties.", e);
} finally {
try {
configurationStream.close();
} catch (IOException e) {
log.error("IOException closing stream in try/catch");
}
}
}
@Override
public String get(final String key) {
return this.configuration.getProperty(key);
}
@Override
public String get(final String key, final String defaultValue) {
String val = this.get(key);
return val == null ? defaultValue : val;
}
private String getKey(final String key, final String defaultValue) {
final String keyname = "com.perforce.search." + key;
return get(keyname, defaultValue);
}
@Override
public String getProtocol() {
return getKey("serverProtocol", "none");
}
@Override
public String getHost() {
return getKey("serverHost", "none");
}
@Override
public int getPort() {
return Integer.valueOf(getKey("serverPort", "1666"));
}
@Override
public String getCharset() {
return getKey("serverCharset", null);
}
@Override
public String getIndexerUser() {
return getKey("indexerUser", "indexerUser");
}
@Override
public String getIndexerPassword() {
return getKey("indexerPassword", "indexerPassword");
}
@Override
public String getSearchURL() {
return getKey("searchEngine", "http://localhost:8983");
}
@Override
public String getQueueToken() {
return getKey("searchEngineToken", "UUIDTOKEN");
}
private String endWithSlash(final String s) {
return s.endsWith("/") ? s : s + "/";
}
@Override
public String getQueueDir() {
return endWithSlash(getKey("queueLocation", "/tmp/p4search/queue"));
}
@Override
public int getCapacity() {
return Integer.valueOf(getKey("connectionPool", "20"));
}
@Override
public long getMaxFileSize() {
return Long.valueOf(getKey("maxStreamFileSize", "100000000"));
}
@Override
public Set<String> getIgnoredExtensions() {
String file = getKey("ignoredExtensions", null);
Set<String> ret = new HashSet<String>();
BufferedReader buf = null;
if (file != null) {
try {
buf = new BufferedReader(new FileReader(file));
String line = null;
while ((line = buf.readLine()) != null) {
ret.add(line.startsWith(".") ? line : "." + line);
}
return ret;
} catch (FileNotFoundException e) {
// this will fall through and deliver the default
log.debug(e);
} catch (IOException e) {
// this will fall through and deliver the default
log.debug(e);
} finally {
try {
if (buf != null) {
buf.close();
}
} catch (IOException e) {
// just give up, we can't win
log.debug(e);
}
}
}
String[] defaults = { ".ova", ".zip", ".tar", ".gz", ".rar", ".tgz",
".bz2" };
for (String ext : defaults) {
ret.add(ext);
}
return ret;
}
@Override
public int getMaxSearchResults() {
return Integer.valueOf(getKey("maxSearchResults", "100"));
}
@Override
public String getCollectionName() {
return getKey("collectionName", "collection1");
}
@Override
public String getCommonsUrl() {
return getKey("commonsURL", "https://commons.perforce.com/");
}
// return swarm links when configured or a default when p4web is not
// configured
@Override
public String getSwarmUrl() {
if (getKey("p4webURL", "").isEmpty()
|| getKey("swarmURL", "").isEmpty())
return getKey("swarmURL", "http://swarm.perforce.com/");
else
return "";
}
@Override
public String getP4WebUrl() {
return getKey("p4webURL", "http://p4web.perforce.com:8080/");
}
@Override
public Set<String> getNeverProcessList() {
String file = getKey("neverProcessList", null);
Set<String> ret = new HashSet<String>();
BufferedReader buf = null;
if (file != null) {
try {
buf = new BufferedReader(new FileReader(file));
String line = null;
while ((line = buf.readLine()) != null) {
ret.add(line);
}
return ret;
} catch (FileNotFoundException e) {
// this will fall through and deliver the default
log.debug(e);
} catch (IOException e) {
// this will fall through and deliver the default
log.debug(e);
} finally {
try {
if (buf != null) {
buf.close();
}
} catch (IOException e) {
// just give up, we can't win
log.debug(e);
}
}
}
String[] defaults = { ".commons" };
for (String ext : defaults) {
ret.add(ext);
}
return ret;
}
@Override
public String getFileScannerTokenKey() {
return getKey("fileScannerTokenKey", "p4search_filescan_token");
}
@Override
public String getFileScannerTokenValue() {
return getKey("fileScannerTokenValue", "completed");
}
@Override
public int getMaxScannerThreads() {
return Integer.valueOf(getKey("fileScannerThreads", "3"));
}
@Override
public int getFileScanSleep() {
// default is no sleep between calls (in ms)
return Integer.valueOf(getKey("fileScannerSleep", "0"));
}
@Override
public Set<String> getScanPaths() {
String paths = getKey("scanPaths", null);
Set<String> ret = new HashSet<String>();
if (paths != null) {
String[] pathArray = paths.split(",");
ret.addAll(Arrays.asList(pathArray));
}
return ret;
}
@Override
public long getMaxScanQueueSize() {
return Long.valueOf(getKey("maxScanQueueSize", "100000"));
}
@Override
public boolean isIndexAllRevisions() {
return new String("ALL_REVISIONS").equals(getKey("indexMethod",
"ALL_REVISIONS"));
}
@Override
public int getFileScanRevisionDepth() {
// 0 == everything, 1 == head, 2 = head & head-1, etc.
return Integer.valueOf(getKey("fileScannerDepth", "0"));
}
@Override
public String getChangelistCatchupKey() {
// do we try to catch up from some downtime?
return getKey("changelistCatchupKey", "p4search_changes");
}
@Override
public int getMaxCatchup() {
return Integer.valueOf(getKey("maxCatchupChanges", "1000"));
}
@Override
public String getBlackFstatRegex() {
// reject standard fstat data, it's not use, duplicated elsewhere, or
// client-based, and attrProp is just not needed
return getKey("blackFstatRegex",
"depotFile|clientFile|isMapped|headAction|headType|headTime|"
+ "headRev|headChange|headModTime|haveRev|fileSize|"
+ "attrProp\\-|other*");
}
@Override
public String getWhiteFstatRegex() {
return getKey("whiteFstatRegex", "");
}
@Override
public boolean indexFstats() {
return Boolean.valueOf(getKey("indexFstats", "true"));
}
}