Package com.crashnote.core.config

Source Code of com.crashnote.core.config.Config

/**
* Copyright (C) 2011 - 101loops.com <dev@101loops.com>
*
* 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.crashnote.core.config;

import com.crashnote.core.build.Builder;
import com.crashnote.core.collect.Collector;
import com.crashnote.core.log.*;
import com.crashnote.core.model.types.*;
import com.crashnote.core.report.Reporter;
import com.crashnote.core.send.Sender;
import com.crashnote.core.util.*;

import java.util.Properties;

/**
* Main configuration object, home to all configurable settings of the notifier like
* authentication and behaviour properties. It is initialized in the according appender/handler,
* user customizations are applied and then each important class receives a copy.
* <p/>
* It assumes that each 'set' method receives a String and converts it to the actual data type by
* manual parsing/converting, thus being independent of the way the concrete logger handles it.
*/
public abstract class Config<C extends Config<C>>
    extends ConfigBase {

    protected SystemUtil sysUtil;
    private LogLogFactory<C> logFactory;

    /**
     * Property names of internal library settings
     */
    public static final String PROP_SYNC = "sync";
    public static final String PROP_DEBUG = "debug";
    public static final String PROP_ENABLED = "enabled";

    public static final String PROP_APP_TYPE = "appType";
    public static final String PROP_APP_VERSION = "appVersion";
    public static final String PROP_APP_PROFILE = "appProfile";

    public static final String PROP_REP_LEVEL = "logLevel";
    public static final String PROP_REP_ENV_FILTER = "envFilter";

    public static final String PROP_API_KEY = "key";
    public static final String PROP_API_HOST = "host";
    public static final String PROP_API_PORT = "port";
    public static final String PROP_API_PORT_SSL = "sslPort";
    public static final String PROP_API_SECURE = "secure";
    public static final String PROP_API_TIMEOUT = "timeout";

    // SETUP ======================================================================================

    public Config() {
        super();
    }

    public Config(final Config<C> c) {
        super(c);
    }

    public void initDefaults() {

        // ==== REPORT
        setLogLevel(LogLevel.ERROR);

        addEnvironmentFilter("aws.");
        addEnvironmentFilter(".key.");
        addEnvironmentFilter(".password.");

        // ==== API
        setPort(80);
        setSSLPort(443);
        setSecure(true);
        setHost("api.crashnote.com");
        setConnectionTimeout(30);
    }

    // INTERFACE ==================================================================================

    public abstract Config<C> copy();

    public String getClientInfo() {
        final Properties p = new ClassUtil().load("crashnote.about.properties");
        if (p.containsKey("name") && p.containsKey("version")) {
            return p.get("name") + ":" + p.get("version");
        } else
            return null;
    }

    // FACTORY ====================================================================================

    /**
     * Create an instance of module 'Reporter'
     */
    public Reporter<C> getReporter() {
        return new Reporter(this);
    }

    /**
     * Create an instance of module 'Sender'
     */
    public Sender<C> getSender() {
        return new Sender(this);
    }

    /**
     * Create an instance of module 'Collector'
     */
    public Collector<C> getCollector() {
        return new Collector(this);
    }

    /**
     * Create an instance of module 'Builder'
     */
    public Builder getBuilder() {
        return new Builder();
    }

    /**
     * Create an instance of the system utility (share same instance, saves memory)
     */
    public SystemUtil getSystemUtil() {
        if (sysUtil == null) sysUtil = new SystemUtil();
        return sysUtil;
    }

    /**
     * Create an instance of a log factory
     */
    protected LogLogFactory<C> getLogFactory() {
        if (logFactory == null) logFactory = new LogLogFactory(this);
        return logFactory;
    }

    public LogLog getLogger(final String name) {
        return getLogFactory().getLogger(name);
    }

    public LogLog getLogger(final Class clazz) {
        return getLogFactory().getLogger(clazz);
    }

    // INTERNALS ==================================================================================

    private String getBaseUrl() {
        final boolean ssl = isSecure();
        return (ssl ? "https://" : "http://") + getHost() + ":" + (ssl ? getSslPort() : getPort());
    }

    // GET+ =======================================================================================

    public String getPostUrl() {
        return getBaseUrl() + "/err?key=" + getKey();
    }

    public LogLevel getLogLevel() {
        final LogLevel maxLvl = LogLevel.INFO;
        final LogLevel reportLvl = getReportLogLevel();
        //final LogLevel historyLvl = getReportHistoryLevel();
        return LogLevel.getMaxLevel(maxLvl, reportLvl);
    }

    // GET ========================================================================================

    public boolean isSync() {
        return getBoolSetting(PROP_SYNC);
    }

    public LogLevel getReportLogLevel() {
        return (LogLevel) getSetting(PROP_REP_LEVEL);
    }

    public String getKey() {
        return getStringSetting(PROP_API_KEY);
    }

    public boolean isEnabled() {
        return getBoolSetting(PROP_ENABLED);
    }

    public String[] getEnvironmentFilters() {
        final String filters = getStringSetting(PROP_REP_ENV_FILTER);
        if (filters == null || filters.length() == 0) return new String[0];
        else return filters.split(":");
    }

    public String getAppProfile() {
        return getStringSetting(PROP_APP_PROFILE);
    }

    public String getAppVersion() {
        return getStringSetting(PROP_APP_VERSION);
    }

    public int getConnectionTimeout() {
        return getIntSetting(PROP_API_TIMEOUT);
    }

    public int getPort() {
        return getIntSetting(PROP_API_PORT);
    }

    public int getSslPort() {
        return getIntSetting(PROP_API_PORT_SSL);
    }

    public Boolean isSecure() {
        return getBoolSetting(PROP_API_SECURE);
    }

    public String getHost() {
        return getStringSetting(PROP_API_HOST);
    }

    public boolean isDebug() {
        return getBoolSetting(PROP_DEBUG);
    }

    public ApplicationType getApplicationType() {
        return (ApplicationType) getSetting(PROP_APP_TYPE);
    }

    // SET+ =======================================================================================

    public void addEnvironmentFilter(final String filter) {
        String filters = getStringSetting(PROP_REP_ENV_FILTER);
        if (filters == null || filters.length() == 0) filters = filter;
        else filters += ":" + filter;
        addSetting(PROP_REP_ENV_FILTER, filters);
    }

    // SET ========================================================================================

    public void setSync(final String sync) {
        addBoolSetting(PROP_SYNC, sync);
    }

    private void setSync(final boolean sync) {
        addSetting(PROP_SYNC, sync);
    }

    public void setEnabled(final String enabled) {
        addBoolSetting(PROP_ENABLED, enabled);
    }

    public void setSSLPort(final String sslPort) {
        addIntSetting(PROP_API_PORT_SSL, sslPort);
    }

    public void setSecure(final String secure) {
        addBoolSetting(PROP_API_SECURE, secure);
    }

    public void setAppProfile(final String profile) {
        addSetting(PROP_APP_PROFILE, profile);
    }

    public void setAppType(final ApplicationType type) {
        addSetting(PROP_APP_TYPE, type);
    }

    public void setPort(final int port) {
        addSetting(PROP_API_PORT, port);
    }

    public void setSSLPort(final int sslPort) {
        addSetting(PROP_API_PORT_SSL, sslPort);
    }

    public void setHost(final String host) {
        addSetting(PROP_API_HOST, host);
    }

    public void setDebug(final Boolean debug) {
        addSetting(PROP_DEBUG, debug);
    }

    public void setEnabled(final boolean enabled) {
        addSetting(PROP_ENABLED, enabled);
    }

    public void setSecure(final Boolean secure) {
        addSetting(PROP_API_SECURE, secure);
    }

    public void setKey(final String key) {
        addSetting(PROP_API_KEY, key);
    }

    public void setPort(final String port) {
        addIntSetting(PROP_API_PORT, port);
    }

    public void setDebug(final String debug) {
        addBoolSetting(PROP_DEBUG, debug);
    }

    public void setAppVersion(final String appVersion) {
        addSetting(PROP_APP_VERSION, appVersion);
    }

    public void setConnectionTimeout(final String timeout) {
        addIntSetting(PROP_API_TIMEOUT, timeout);
    }

    public void setConnectionTimeout(final int timeout) {
        addSetting(PROP_API_TIMEOUT, timeout);
    }

    public void setLogLevel(final LogLevel level) {
        addSetting(PROP_REP_LEVEL, level);
    }
}
TOP

Related Classes of com.crashnote.core.config.Config

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.