001 /** 002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved. 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. See accompanying LICENSE file. 014 */ 015 package org.apache.oozie.servlet; 016 017 import java.io.IOException; 018 import java.util.Arrays; 019 import java.util.List; 020 021 import javax.servlet.ServletException; 022 import javax.servlet.http.HttpServletRequest; 023 import javax.servlet.http.HttpServletResponse; 024 025 import org.apache.oozie.ErrorCode; 026 import org.apache.oozie.SLAEventBean; 027 import org.apache.oozie.XException; 028 import org.apache.oozie.client.rest.RestConstants; 029 import org.apache.oozie.command.CommandException; 030 import org.apache.oozie.command.coord.SLAEventsCommand; 031 import org.apache.oozie.service.SLAStoreService; 032 import org.apache.oozie.service.Services; 033 import org.apache.oozie.store.SLAStore; 034 import org.apache.oozie.store.StoreException; 035 import org.apache.oozie.util.XLog; 036 import org.apache.oozie.util.XmlUtils; 037 import org.jdom.Element; 038 import org.jdom.JDOMException; 039 040 public class SLAServlet extends JsonRestServlet { 041 private static final String INSTRUMENTATION_NAME = "sla"; 042 043 private static final JsonRestServlet.ResourceInfo RESOURCES_INFO[] = new JsonRestServlet.ResourceInfo[1]; 044 045 static { 046 RESOURCES_INFO[0] = new JsonRestServlet.ResourceInfo("", Arrays 047 .asList("GET"), Arrays.asList( 048 new JsonRestServlet.ParameterInfo( 049 RestConstants.SLA_GT_SEQUENCE_ID, String.class, true, 050 Arrays.asList("GET")), 051 new JsonRestServlet.ParameterInfo(RestConstants.MAX_EVENTS, 052 String.class, false, Arrays.asList("GET")))); 053 } 054 055 public SLAServlet() { 056 super(INSTRUMENTATION_NAME, RESOURCES_INFO); 057 } 058 059 /** 060 * Return information about SLA Events. 061 */ 062 @SuppressWarnings("unchecked") 063 public void doGet(HttpServletRequest request, HttpServletResponse response) 064 throws ServletException, IOException { 065 066 try { 067 String gtSequenceNum = request 068 .getParameter(RestConstants.SLA_GT_SEQUENCE_ID); 069 String strMaxEvents = request 070 .getParameter(RestConstants.MAX_EVENTS); 071 int maxNoEvents = 100; // Default 072 XLog.getLog(getClass()).debug( 073 "Got SLA GET request for :" + gtSequenceNum 074 + " and max-events :" + strMaxEvents); 075 if (strMaxEvents != null && strMaxEvents.length() > 0) { 076 maxNoEvents = Integer.parseInt(strMaxEvents); 077 } 078 if (gtSequenceNum != null) { 079 long seqId = Long.parseLong(gtSequenceNum); 080 stopCron(); 081 SLAEventsCommand seCommand = new SLAEventsCommand(seqId, maxNoEvents); 082 List<SLAEventBean> slaEvntList = seCommand.call(); 083 long lastSeqId = seCommand.getLastSeqId(); 084 085 Element eResponse = new Element("sla-message"); 086 for (SLAEventBean event : slaEvntList) { 087 eResponse.addContent(event.toXml()); 088 } 089 Element eLastSeq = new Element("last-sequence-id"); 090 eLastSeq.addContent(String.valueOf(lastSeqId)); 091 eResponse.addContent(eLastSeq); 092 response.setContentType(XML_UTF8); 093 XLog.getLog(getClass()).debug("Writing back SLA Servlet Caller with last-seq-id " + lastSeqId); 094 startCron(); 095 response.setStatus(HttpServletResponse.SC_OK); 096 response.getWriter().write( 097 XmlUtils.prettyPrint(eResponse) + "\n"); 098 } 099 else { 100 XLog.getLog(getClass()).error( 101 "Not implemented witout gt_seq_id"); 102 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, 103 ErrorCode.E0401, "Not implemented without gtSeqID"); 104 } 105 } 106 catch (CommandException ce) { 107 ce.printStackTrace(); 108 XLog.getLog(getClass()).error("Command exception ", ce); 109 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ce); 110 } 111 catch (RuntimeException re) { 112 re.printStackTrace(); 113 XLog.getLog(getClass()).error("Runtime error ", re); 114 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0307, re.getMessage()); 115 } 116 } 117 118 }