001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.servlet;
020
021import java.io.IOException;
022import java.util.Map;
023import java.util.Properties;
024
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027
028import org.apache.hadoop.conf.Configuration;
029import org.apache.oozie.ErrorCode;
030import org.apache.oozie.client.rest.JMSConnectionInfoBean;
031import org.apache.oozie.client.rest.JsonBean;
032import org.apache.oozie.jms.JMSConnectionInfo;
033import org.apache.oozie.jms.JMSJobEventListener;
034import org.apache.oozie.service.InstrumentationService;
035import org.apache.oozie.service.JMSTopicService;
036import org.apache.oozie.service.JobsConcurrencyService;
037import org.apache.oozie.service.Services;
038import org.apache.oozie.util.Instrumentation;
039import org.apache.oozie.util.MetricsInstrumentation;
040
041/**
042 * V2 admin servlet
043 *
044 */
045public class V2AdminServlet extends V1AdminServlet {
046
047    private static final long serialVersionUID = 1L;
048    private static final String INSTRUMENTATION_NAME = "v2admin";
049    private static MetricsInstrumentation metricsInstrumentation = null;
050
051    public V2AdminServlet() {
052        super(INSTRUMENTATION_NAME);
053
054        // If MetricsInstrumentationService is used, we will enable the metrics endpoint and disable the instrumentation endpoint
055        Services services = Services.get();
056        if (services != null) {
057            Instrumentation instrumentation = services.get(InstrumentationService.class).get();
058            if (instrumentation instanceof MetricsInstrumentation) {
059                metricsInstrumentation = (MetricsInstrumentation) instrumentation;
060            } else {
061                metricsInstrumentation = null;
062            }
063        }
064    }
065
066    @Override
067    protected JsonBean getJMSConnectionInfo(HttpServletRequest request, HttpServletResponse response)
068            throws XServletException, IOException {
069        Configuration conf = Services.get().getConf();
070        JMSTopicService jmsTopicService = Services.get().get(JMSTopicService.class);
071        String connectionProperties = conf.get(JMSJobEventListener.JMS_CONNECTION_PROPERTIES);
072        if (connectionProperties == null) {
073            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E1601,
074                    "JMS connection property is not defined");
075        }
076        JMSConnectionInfoBean jmsBean = new JMSConnectionInfoBean();
077        JMSConnectionInfo jmsInfo = new JMSConnectionInfo(connectionProperties);
078        Properties jmsInfoProps = jmsInfo.getJNDIProperties();
079        jmsInfoProps.remove("java.naming.security.principal");
080        jmsBean.setJNDIProperties(jmsInfoProps);
081        if (jmsTopicService != null) {
082            jmsBean.setTopicPrefix(jmsTopicService.getTopicPrefix());
083            jmsBean.setTopicPatternProperties(jmsTopicService.getTopicPatternProperties());
084        }
085        else {
086            throw new XServletException(
087                    HttpServletResponse.SC_BAD_REQUEST,
088                    ErrorCode.E1601,
089                    "JMSTopicService is not initialized. JMS notification"
090                            + "may not be enabled");
091        }
092        return jmsBean;
093    }
094
095    @Override
096    protected Map<String, String> getOozieURLs() throws XServletException {
097        Map<String, String> serverUrls = null;
098        try {
099            serverUrls = Services.get().get(JobsConcurrencyService.class).getServerUrls();
100        } catch (Exception ex) {
101            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0307, ex.getMessage(), ex);
102        }
103        return serverUrls;
104    }
105
106    @Override
107    protected void sendMetricsResponse(HttpServletResponse response) throws IOException, XServletException {
108        if (metricsInstrumentation != null) {
109            response.setStatus(HttpServletResponse.SC_OK);
110            response.setContentType(JSON_UTF8);
111            try {
112                metricsInstrumentation.writeJSONResponse(response.getOutputStream());
113            } finally {
114                response.getOutputStream().close();
115            }
116        } else {
117            response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
118            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "MetricsInstrumentationService is not running");
119        }
120    }
121
122    @Override
123    protected void sendInstrumentationResponse(HttpServletResponse response, Instrumentation instr)
124            throws IOException, XServletException {
125        if (metricsInstrumentation == null) {
126            super.sendInstrumentationResponse(response, instr);
127        } else {
128            response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
129            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "InstrumentationService is not running");
130        }
131    }
132}