browseDirectory.jsp
上传用户:quxuerui
上传日期:2018-01-08
资源大小:41811k
文件大小:6k
源码类别:

网格计算

开发平台:

Java

  1. <%@ page
  2.   contentType="text/html; charset=UTF-8"
  3.   import="javax.servlet.*"
  4.   import="javax.servlet.http.*"
  5.   import="java.io.*"
  6.   import="java.util.*"
  7.   import="java.net.*"
  8.   import="org.apache.hadoop.fs.*"
  9.   import="org.apache.hadoop.hdfs.*"
  10.   import="org.apache.hadoop.hdfs.server.namenode.*"
  11.   import="org.apache.hadoop.hdfs.server.datanode.*"
  12.   import="org.apache.hadoop.hdfs.protocol.*"
  13.   import="org.apache.hadoop.io.*"
  14.   import="org.apache.hadoop.conf.*"
  15.   import="org.apache.hadoop.net.DNS"
  16.   import="org.apache.hadoop.util.*"
  17.   import="java.text.DateFormat"
  18. %>
  19. <%!
  20.   static JspHelper jspHelper = new JspHelper();
  21.   
  22.   public void generateDirectoryStructure( JspWriter out, 
  23.                                           HttpServletRequest req,
  24.                                           HttpServletResponse resp) 
  25.     throws IOException {
  26.     String dir = req.getParameter("dir");
  27.     if (dir == null || dir.length() == 0) {
  28.       out.print("Invalid input");
  29.       return;
  30.     }
  31.     
  32.     String namenodeInfoPortStr = req.getParameter("namenodeInfoPort");
  33.     int namenodeInfoPort = -1;
  34.     if (namenodeInfoPortStr != null)
  35.       namenodeInfoPort = Integer.parseInt(namenodeInfoPortStr);
  36.     
  37.     DFSClient dfs = new DFSClient(jspHelper.nameNodeAddr, jspHelper.conf);
  38.     String target = dir;
  39.     if (!dfs.exists(target)) {
  40.       out.print("<h3>File or directory : " + target + " does not exist</h3>");
  41.       JspHelper.printGotoForm(out, namenodeInfoPort, target);
  42.     }
  43.     else {
  44.       if( !dfs.isDirectory(target) ) { // a file
  45.         List<LocatedBlock> blocks = 
  46.           dfs.namenode.getBlockLocations(dir, 0, 1).getLocatedBlocks();
  47.       
  48.         LocatedBlock firstBlock = null;
  49.         DatanodeInfo [] locations = null;
  50.         if (blocks.size() > 0) {
  51.           firstBlock = blocks.get(0);
  52.           locations = firstBlock.getLocations();
  53.         }
  54.         if (locations == null || locations.length == 0) {
  55.           out.print("Empty file");
  56.         } else {
  57.           DatanodeInfo chosenNode = jspHelper.bestNode(firstBlock);
  58.           String fqdn = InetAddress.getByName(chosenNode.getHost()).
  59.             getCanonicalHostName();
  60.           String datanodeAddr = chosenNode.getName();
  61.           int datanodePort = Integer.parseInt(
  62.                                               datanodeAddr.substring(
  63.                                                                      datanodeAddr.indexOf(':') + 1, 
  64.                                                                      datanodeAddr.length())); 
  65.           String redirectLocation = "http://"+fqdn+":" +
  66.             chosenNode.getInfoPort() + 
  67.             "/browseBlock.jsp?blockId=" +
  68.             firstBlock.getBlock().getBlockId() +
  69.             "&blockSize=" + firstBlock.getBlock().getNumBytes() +
  70.             "&genstamp=" + firstBlock.getBlock().getGenerationStamp() +
  71.             "&filename=" + URLEncoder.encode(dir, "UTF-8") + 
  72.             "&datanodePort=" + datanodePort + 
  73.             "&namenodeInfoPort=" + namenodeInfoPort;
  74.           resp.sendRedirect(redirectLocation);
  75.         }
  76.         return;
  77.       }
  78.       // directory
  79.       FileStatus[] files = dfs.listPaths(target);
  80.       //generate a table and dump the info
  81.       String [] headings = { "Name", "Type", "Size", "Replication", 
  82.                               "Block Size", "Modification Time",
  83.                               "Permission", "Owner", "Group" };
  84.       out.print("<h3>Contents of directory ");
  85.       JspHelper.printPathWithLinks(dir, out, namenodeInfoPort);
  86.       out.print("</h3><hr>");
  87.       JspHelper.printGotoForm(out, namenodeInfoPort, dir);
  88.       out.print("<hr>");
  89.       File f = new File(dir);
  90.       String parent;
  91.       if ((parent = f.getParent()) != null)
  92.         out.print("<a href="" + req.getRequestURL() + "?dir=" + parent +
  93.                   "&namenodeInfoPort=" + namenodeInfoPort +
  94.                   "">Go to parent directory</a><br>");
  95.       if (files == null || files.length == 0) {
  96.         out.print("Empty directory");
  97.       }
  98.       else {
  99.         jspHelper.addTableHeader(out);
  100.         int row=0;
  101.         jspHelper.addTableRow(out, headings, row++);
  102.         String cols [] = new String[headings.length];
  103.         for (int i = 0; i < files.length; i++) {
  104.           //Get the location of the first block of the file
  105.           if (files[i].getPath().toString().endsWith(".crc")) continue;
  106.           if (!files[i].isDir()) {
  107.             cols[1] = "file";
  108.             cols[2] = StringUtils.byteDesc(files[i].getLen());
  109.             cols[3] = Short.toString(files[i].getReplication());
  110.             cols[4] = StringUtils.byteDesc(files[i].getBlockSize());
  111.           }
  112.           else {
  113.             cols[1] = "dir";
  114.             cols[2] = "";
  115.             cols[3] = "";
  116.             cols[4] = "";
  117.           }
  118.           String datanodeUrl = req.getRequestURL()+"?dir="+
  119.               URLEncoder.encode(files[i].getPath().toString(), "UTF-8") + 
  120.               "&namenodeInfoPort=" + namenodeInfoPort;
  121.           cols[0] = "<a href=""+datanodeUrl+"">"+files[i].getPath().getName()+"</a>";
  122.           cols[5] = FsShell.dateForm.format(new Date((files[i].getModificationTime())));
  123.           cols[6] = files[i].getPermission().toString();
  124.           cols[7] = files[i].getOwner();
  125.           cols[8] = files[i].getGroup();
  126.           jspHelper.addTableRow(out, cols, row++);
  127.         }
  128.         jspHelper.addTableFooter(out);
  129.       }
  130.     } 
  131.     String namenodeHost = jspHelper.nameNodeAddr.getHostName();
  132.     out.print("<br><a href="http://" + 
  133.               InetAddress.getByName(namenodeHost).getCanonicalHostName() + ":" +
  134.               namenodeInfoPort + "/dfshealth.jsp">Go back to DFS home</a>");
  135.     dfs.close();
  136.   }
  137. %>
  138. <html>
  139. <head>
  140. <style type=text/css>
  141. <!--
  142. body 
  143.   {
  144.   font-face:sanserif;
  145.   }
  146. -->
  147. </style>
  148. <%JspHelper.createTitle(out, request, request.getParameter("dir")); %>
  149. </head>
  150. <body onload="document.goto.dir.focus()">
  151. <% 
  152.   try {
  153.     generateDirectoryStructure(out,request,response);
  154.   }
  155.   catch(IOException ioe) {
  156.     String msg = ioe.getLocalizedMessage();
  157.     int i = msg.indexOf("n");
  158.     if (i >= 0) {
  159.       msg = msg.substring(0, i);
  160.     }
  161.     out.print("<h3>" + msg + "</h3>");
  162.   }
  163. %>
  164. <hr>
  165. <h2>Local logs</h2>
  166. <a href="/logs/">Log</a> directory
  167. <%
  168. out.println(ServletUtil.htmlFooter());
  169. %>