Package com.crashnote.core.collect

Source Code of com.crashnote.core.collect.Collector

/**
* 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.collect;

import com.crashnote.core.Lifecycle;
import com.crashnote.core.collect.impl.*;
import com.crashnote.core.config.*;
import com.crashnote.core.model.data.DataObject;
import com.crashnote.core.model.log.*;
import com.crashnote.core.model.types.ApplicationType;

/**
* This class provides the functionality to transform events, state, properties and context data
* into a structured form that can be parsed by an external API. The structure is based on
* abstract object and array containers.
*/
public class Collector<C extends Config>
    extends BaseCollector<C> implements Lifecycle {

    private boolean started;
    private final EnvCollector<C> env_c;
    private final LogCollector<C> log_c;

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

    public Collector(final C config) {
        super(config);

        this.env_c = createEnvColl(config);
        this.log_c = createLogColl(config);
    }

    // LIFECYCLE ==================================================================================

    public boolean start() {
        if (!started) {
            started = true;
            getLogger().debug("starting module [collector]");
        }
        return started;
    }

    public boolean stop() {
        if (started) {
            started = false;
            getLogger().debug("stopping module [collector]");
        }
        return started;
    }

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

    public DataObject collectLog(final ILogSession session) {
        final DataObject res = createDataObj();
        {
            final LogEvt[] logs = session.getEvents();
            if (logs.length == 1)
                res.putObj("log", log_c.collect(logs[0]));
            else
                res.putArr("logs", log_c.collect(logs));
            //res.putMap("ctx", session.getContext()); (TODO)
            res.put("env", collectEnv());
        }
        return res;
    }

    public DataObject collectEnv() {
        return env_c.collect();
    }

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

    protected EnvCollector<C> createEnvColl(final C config) {
        return new EnvCollector<C>(config);
    }

    protected LogCollector<C> createLogColl(final C config) {
        return new LogCollector<C>(config);
    }

}
TOP

Related Classes of com.crashnote.core.collect.Collector

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.