001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.flume.appender;
018    
019    import com.google.common.base.Preconditions;
020    import org.apache.flume.lifecycle.LifecycleAware;
021    import org.apache.flume.lifecycle.LifecycleState;
022    import org.apache.flume.node.NodeConfiguration;
023    import org.apache.flume.node.NodeManager;
024    import org.apache.flume.node.nodemanager.NodeConfigurationAware;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    
028    /**
029     *
030     */
031    public class FlumeNode implements LifecycleAware {
032    
033        private static final Logger LOGGER = LoggerFactory.getLogger(FlumeNode.class);
034    
035        private LifecycleState lifecycleState;
036        private final NodeManager nodeManager;
037        private final NodeConfigurationAware configurationAware;
038        private final NodeConfiguration conf;
039    
040        public FlumeNode(final NodeConfigurationAware configurationAware, final NodeManager manager,
041                         final NodeConfiguration conf) {
042            this.nodeManager = manager;
043            this.conf = conf;
044            this.configurationAware = configurationAware;
045        }
046    
047        @Override
048        public void start() {
049    
050            Preconditions.checkState(nodeManager != null, "Node manager can not be null");
051    
052            LOGGER.info("Flume node starting");
053    
054            configurationAware.startAllComponents(conf);
055    
056            lifecycleState = LifecycleState.START;
057        }
058    
059        @Override
060        public void stop() {
061    
062            LOGGER.info("Flume node stopping");
063    
064            configurationAware.stopAllComponents();
065    
066            lifecycleState = LifecycleState.STOP;
067        }
068    
069        public NodeManager getNodeManager() {
070            return nodeManager;
071        }
072    
073        public NodeConfiguration getConfiguration() {
074            return conf;
075        }
076    
077        @Override
078        public LifecycleState getLifecycleState() {
079            return lifecycleState;
080        }
081    }