/*
* Copyright (c) 2012 Daniel Huckaby
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.handlerexploit.prime;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.handlerexploit.common.utils.AssetsHelper;
public class Configuration {
private static final Properties sProperties = loadProperties();
public static final boolean DEBUGGING = (Boolean) checkValue(sProperties.get("debug"), false);
public static final int MEM_CACHE_SIZE_KB = (Integer) checkValue(sProperties.get("cache.memSize"), (int)(Runtime.getRuntime().maxMemory() / 2 / 1024));
public static final int DISK_CACHE_SIZE_KB = (Integer) checkValue(sProperties.get("cache.diskSize"), 10 * 1024);
public static final int ASYNC_THREAD_COUNT = (Integer) checkValue(sProperties.get("async.numThreads"), Runtime.getRuntime().availableProcessors() * 4);
public static final Location DOWNLOAD_LOCATION = (Location) checkValue(sProperties.get("cache.diskLocation"), Location.INTERNAL);
private static Properties loadProperties() {
Properties properties = new Properties();
try {
InputStream configInputStream = AssetsHelper.open("prime.properties");
if (configInputStream != null) {
properties.load(configInputStream);
}
} catch (IOException ignore) {}
return properties;
}
private static Object checkValue(Object object, Object defValue) {
if (object != null) {
try {
if (defValue instanceof Boolean) {
return Boolean.valueOf(object.toString());
} else if (defValue instanceof Integer) {
return ((Integer) Integer.parseInt(object.toString()));
} else if (defValue instanceof Location) {
return Location.valueOf(String.valueOf(object).toUpperCase());
} else {
throw new RuntimeException();
}
} catch (NumberFormatException ignore) {} catch (IllegalArgumentException ignore) {} catch (Throwable e) {
throw new RuntimeException(e);
}
}
return defValue;
}
public static enum Location {
INTERNAL, EXTERNAL
}
}