View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.http;
19  
20  import java.io.File;
21  import java.io.IOException;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.mortbay.jetty.servlet.DefaultServlet;
30  
31  /**
32   * Servlet to serve files generated by {@link ProfileServlet}
33   */
34  public class ProfileOutputServlet extends DefaultServlet {
35    private static final long serialVersionUID = 1L;
36    private static final Log LOG = LogFactory.getLog(ProfileOutputServlet.class);
37    private static final int REFRESH_PERIOD = 2;
38  
39    @Override
40    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
41      throws ServletException, IOException {
42      String absoluteDiskPath = getServletContext().getRealPath(req.getPathInfo());
43      File requestedFile = new File(absoluteDiskPath);
44      // async-profiler version 1.4 writes 'Started [cpu] profiling' to output file when profiler is
45      // running which gets replaced by final output. If final output is not ready yet, the file size
46      // will be <100 bytes (in all modes).
47      if (requestedFile.length() < 100) {
48        LOG.info(requestedFile  + " is incomplete. Sending auto-refresh header.");
49        resp.setHeader("Refresh", REFRESH_PERIOD + "," + req.getRequestURI());
50        resp.getWriter().write("This page will be auto-refreshed every " + REFRESH_PERIOD +
51          " seconds until the output file is ready.");
52      } else {
53        super.doGet(req, resp);
54      }
55    }
56  }