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    package org.apache.oozie.servlet;
019    
020    import java.io.IOException;
021    import java.util.Map;
022    import java.util.Properties;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    import org.apache.hadoop.conf.Configuration;
028    import org.apache.oozie.ErrorCode;
029    import org.apache.oozie.client.rest.JMSConnectionInfoBean;
030    import org.apache.oozie.client.rest.JsonBean;
031    import org.apache.oozie.jms.JMSConnectionInfo;
032    import org.apache.oozie.jms.JMSJobEventListener;
033    import org.apache.oozie.service.InstrumentationService;
034    import org.apache.oozie.service.JMSTopicService;
035    import org.apache.oozie.service.JobsConcurrencyService;
036    import org.apache.oozie.service.Services;
037    import org.apache.oozie.util.Instrumentation;
038    import org.apache.oozie.util.MetricsInstrumentation;
039    
040    /**
041     * V2 admin servlet
042     *
043     */
044    public class V2AdminServlet extends V1AdminServlet {
045    
046        private static final long serialVersionUID = 1L;
047        private static final String INSTRUMENTATION_NAME = "v2admin";
048        private static MetricsInstrumentation metricsInstrumentation = null;
049    
050        public V2AdminServlet() {
051            super(INSTRUMENTATION_NAME);
052    
053            // If MetricsInstrumentationService is used, we will enable the metrics endpoint and disable the instrumentation endpoint
054            Services services = Services.get();
055            if (services != null) {
056                Instrumentation instrumentation = services.get(InstrumentationService.class).get();
057                if (instrumentation instanceof MetricsInstrumentation) {
058                    metricsInstrumentation = (MetricsInstrumentation) instrumentation;
059                }
060            }
061        }
062    
063        @Override
064        protected JsonBean getJMSConnectionInfo(HttpServletRequest request, HttpServletResponse response)
065                throws XServletException, IOException {
066            Configuration conf = Services.get().getConf();
067            JMSTopicService jmsTopicService = Services.get().get(JMSTopicService.class);
068            String connectionProperties = conf.get(JMSJobEventListener.JMS_CONNECTION_PROPERTIES);
069            if (connectionProperties == null) {
070                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E1601,
071                        "JMS connection property is not defined");
072            }
073            JMSConnectionInfoBean jmsBean = new JMSConnectionInfoBean();
074            JMSConnectionInfo jmsInfo = new JMSConnectionInfo(connectionProperties);
075            Properties jmsInfoProps = jmsInfo.getJNDIProperties();
076            jmsInfoProps.remove("java.naming.security.principal");
077            jmsBean.setJNDIProperties(jmsInfoProps);
078            if (jmsTopicService != null) {
079                jmsBean.setTopicPrefix(jmsTopicService.getTopicPrefix());
080                jmsBean.setTopicPatternProperties(jmsTopicService.getTopicPatternProperties());
081            }
082            else {
083                throw new XServletException(
084                        HttpServletResponse.SC_BAD_REQUEST,
085                        ErrorCode.E1601,
086                        "JMSTopicService is not initialized. JMS notification"
087                                + "may not be enabled");
088            }
089            return jmsBean;
090        }
091    
092        @Override
093        protected Map<String, String> getOozieURLs() throws XServletException {
094            Map<String, String> serverUrls = null;
095            try {
096                serverUrls = Services.get().get(JobsConcurrencyService.class).getServerUrls();
097            } catch (Exception ex) {
098                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0307, ex.getMessage(), ex);
099            }
100            return serverUrls;
101        }
102    
103        @Override
104        protected void sendMetricsResponse(HttpServletResponse response) throws IOException, XServletException {
105            if (metricsInstrumentation != null) {
106                response.setStatus(HttpServletResponse.SC_OK);
107                response.setContentType(JSON_UTF8);
108                try {
109                    metricsInstrumentation.writeJSONResponse(response.getOutputStream());
110                } finally {
111                    response.getOutputStream().close();
112                }
113            } else {
114                response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
115                response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "MetricsInstrumentationService is not running");
116            }
117        }
118    
119        @Override
120        protected void sendInstrumentationResponse(HttpServletResponse response, Instrumentation instr)
121                throws IOException, XServletException {
122            if (metricsInstrumentation == null) {
123                super.sendInstrumentationResponse(response, instr);
124            } else {
125                response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
126                response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "InstrumentationService is not running");
127            }
128        }
129    }