This project has retired. For details please refer to its Attic page.
Source code
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            }
061        }
062    }
063
064    @Override
065    protected JsonBean getJMSConnectionInfo(HttpServletRequest request, HttpServletResponse response)
066            throws XServletException, IOException {
067        Configuration conf = Services.get().getConf();
068        JMSTopicService jmsTopicService = Services.get().get(JMSTopicService.class);
069        String connectionProperties = conf.get(JMSJobEventListener.JMS_CONNECTION_PROPERTIES);
070        if (connectionProperties == null) {
071            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E1601,
072                    "JMS connection property is not defined");
073        }
074        JMSConnectionInfoBean jmsBean = new JMSConnectionInfoBean();
075        JMSConnectionInfo jmsInfo = new JMSConnectionInfo(connectionProperties);
076        Properties jmsInfoProps = jmsInfo.getJNDIProperties();
077        jmsInfoProps.remove("java.naming.security.principal");
078        jmsBean.setJNDIProperties(jmsInfoProps);
079        if (jmsTopicService != null) {
080            jmsBean.setTopicPrefix(jmsTopicService.getTopicPrefix());
081            jmsBean.setTopicPatternProperties(jmsTopicService.getTopicPatternProperties());
082        }
083        else {
084            throw new XServletException(
085                    HttpServletResponse.SC_BAD_REQUEST,
086                    ErrorCode.E1601,
087                    "JMSTopicService is not initialized. JMS notification"
088                            + "may not be enabled");
089        }
090        return jmsBean;
091    }
092
093    @Override
094    protected Map<String, String> getOozieURLs() throws XServletException {
095        Map<String, String> serverUrls = null;
096        try {
097            serverUrls = Services.get().get(JobsConcurrencyService.class).getServerUrls();
098        } catch (Exception ex) {
099            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0307, ex.getMessage(), ex);
100        }
101        return serverUrls;
102    }
103
104    @Override
105    protected void sendMetricsResponse(HttpServletResponse response) throws IOException, XServletException {
106        if (metricsInstrumentation != null) {
107            response.setStatus(HttpServletResponse.SC_OK);
108            response.setContentType(JSON_UTF8);
109            try {
110                metricsInstrumentation.writeJSONResponse(response.getOutputStream());
111            } finally {
112                response.getOutputStream().close();
113            }
114        } else {
115            response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
116            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "MetricsInstrumentationService is not running");
117        }
118    }
119
120    @Override
121    protected void sendInstrumentationResponse(HttpServletResponse response, Instrumentation instr)
122            throws IOException, XServletException {
123        if (metricsInstrumentation == null) {
124            super.sendInstrumentationResponse(response, instr);
125        } else {
126            response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
127            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "InstrumentationService is not running");
128        }
129    }
130}