org.primeoservices.cfgateway.twitter.AbstractTwitterStream.java Source code

Java tutorial

Introduction

Here is the source code for org.primeoservices.cfgateway.twitter.AbstractTwitterStream.java

Source

/*
 * Copyright 2014 Jean-Bernard van Zuylen
 *
 * 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 org.primeoservices.cfgateway.twitter;

import java.util.Map;

import org.primeoservices.cfgateway.twitter.utils.TwitterUtils;

import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.Configuration;

public abstract class AbstractTwitterStream implements org.primeoservices.cfgateway.twitter.TwitterStream {
    private TwitterGateway gateway;

    private Configuration configuration;

    private TwitterStream stream;

    public AbstractTwitterStream(final TwitterGateway gateway, final Configuration configuration) {
        this.gateway = gateway;
    }

    /**
     * Starts this stream 
     */
    @Override
    public void startStream() {
        this.stream = new TwitterStreamFactory(this.configuration).getInstance();
        this.initStream(this.stream);
    }

    /**
     * Initializes the specified stream
     * 
     * @param stream the stream to be initialized
     */
    protected abstract void initStream(final TwitterStream stream);

    /**
     * Stops this stream
     */
    @Override
    public void stopStream() {
        TwitterUtils.closeQuietly(this.stream);
        this.stream = null;
    }

    /**
     * Invokes the listener of this gateway
     * 
     * @param method the name of the method to be invoked on the listener
     * @param data the date to be sent to the listener
     */
    protected void invokeListener(final String method, final Map<String, Object> data) {
        this.gateway.invokeListener(method, data);
    }

    /**
     * Handles the specified exception
     * 
     * @param ex the exception to be handled
     */
    public void onException(final Exception ex) {
        this.gateway.onException(ex);
    }
}