001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.action.plugin.jdbc.state.get;
017
018import org.elasticsearch.ElasticsearchException;
019import org.elasticsearch.action.ActionListener;
020import org.elasticsearch.action.support.master.TransportMasterNodeReadOperationAction;
021import org.elasticsearch.cluster.ClusterService;
022import org.elasticsearch.cluster.ClusterState;
023import org.elasticsearch.common.inject.Inject;
024import org.elasticsearch.common.settings.Settings;
025import org.elasticsearch.threadpool.ThreadPool;
026import org.elasticsearch.transport.TransportService;
027import org.xbib.elasticsearch.plugin.jdbc.state.RiverStatesMetaData;
028
029public class TransportGetRiverStateAction extends TransportMasterNodeReadOperationAction<GetRiverStateRequest, GetRiverStateResponse> {
030
031    @Inject
032    public TransportGetRiverStateAction(Settings settings, ThreadPool threadPool,
033                                        ClusterService clusterService, TransportService transportService) {
034        super(settings, GetRiverStateAction.NAME, transportService, clusterService, threadPool);
035    }
036
037    @Override
038    protected String executor() {
039        return ThreadPool.Names.SAME;
040    }
041
042    @Override
043    protected GetRiverStateRequest newRequest() {
044        return new GetRiverStateRequest();
045    }
046
047    @Override
048    protected GetRiverStateResponse newResponse() {
049        return new GetRiverStateResponse();
050    }
051
052    @Override
053    protected void masterOperation(final GetRiverStateRequest request,
054                                   final ClusterState clusterState,
055                                   final ActionListener<GetRiverStateResponse> listener)
056            throws ElasticsearchException {
057        RiverStatesMetaData riverStatesMetaData = clusterState.metaData().custom(RiverStatesMetaData.TYPE);
058        listener.onResponse(new GetRiverStateResponse(riverStatesMetaData != null ?
059                riverStatesMetaData.getRiverStates(request.getRiverName(), request.getRiverType()) : null));
060    }
061
062}