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 | |
---|
90 | File f = new File(dir); |
---|
91 | String parent; |
---|
92 | if ((parent = f.getParent()) != null) |
---|
93 | out.print("<a href=\"" + req.getRequestURL() + "?dir=" + parent + |
---|
94 | "&namenodeInfoPort=" + namenodeInfoPort + |
---|
95 | "\">Go to parent directory</a><br>"); |
---|
96 | |
---|
97 | if (files == null || files.length == 0) { |
---|
98 | out.print("Empty directory"); |
---|
99 | } |
---|
100 | else { |
---|
101 | jspHelper.addTableHeader(out); |
---|
102 | int row=0; |
---|
103 | jspHelper.addTableRow(out, headings, row++); |
---|
104 | String cols [] = new String[headings.length]; |
---|
105 | for (int i = 0; i < files.length; i++) { |
---|
106 | //Get the location of the first block of the file |
---|
107 | if (files[i].getPath().toString().endsWith(".crc")) continue; |
---|
108 | if (!files[i].isDir()) { |
---|
109 | cols[1] = "file"; |
---|
110 | cols[2] = StringUtils.byteDesc(files[i].getLen()); |
---|
111 | cols[3] = Short.toString(files[i].getReplication()); |
---|
112 | cols[4] = StringUtils.byteDesc(files[i].getBlockSize()); |
---|
113 | } |
---|
114 | else { |
---|
115 | cols[1] = "dir"; |
---|
116 | cols[2] = ""; |
---|
117 | cols[3] = ""; |
---|
118 | cols[4] = ""; |
---|
119 | } |
---|
120 | String datanodeUrl = req.getRequestURL()+"?dir="+ |
---|
121 | URLEncoder.encode(files[i].getPath().toString(), "UTF-8") + |
---|
122 | "&namenodeInfoPort=" + namenodeInfoPort; |
---|
123 | cols[0] = "<a href=\""+datanodeUrl+"\">"+files[i].getPath().getName()+"</a>"; |
---|
124 | cols[5] = FsShell.dateForm.format(new Date((files[i].getModificationTime()))); |
---|
125 | cols[6] = files[i].getPermission().toString(); |
---|
126 | cols[7] = files[i].getOwner(); |
---|
127 | cols[8] = files[i].getGroup(); |
---|
128 | jspHelper.addTableRow(out, cols, row++); |
---|
129 | } |
---|
130 | jspHelper.addTableFooter(out); |
---|
131 | } |
---|
132 | } |
---|
133 | String namenodeHost = jspHelper.nameNodeAddr.getHostName(); |
---|
134 | out.print("<br><a href=\"http://" + |
---|
135 | InetAddress.getByName(namenodeHost).getCanonicalHostName() + ":" + |
---|
136 | namenodeInfoPort + "/dfshealth.jsp\">Go back to DFS home</a>"); |
---|
137 | dfs.close(); |
---|
138 | } |
---|
139 | |
---|
140 | %> |
---|
141 | |
---|
142 | <html> |
---|
143 | <head> |
---|
144 | <style type=text/css> |
---|
145 | <!-- |
---|
146 | body |
---|
147 | { |
---|
148 | font-face:sanserif; |
---|
149 | } |
---|
150 | --> |
---|
151 | </style> |
---|
152 | <%JspHelper.createTitle(out, request, request.getParameter("dir")); %> |
---|
153 | </head> |
---|
154 | |
---|
155 | <body onload="document.goto.dir.focus()"> |
---|
156 | <% |
---|
157 | try { |
---|
158 | generateDirectoryStructure(out,request,response); |
---|
159 | } |
---|
160 | catch(IOException ioe) { |
---|
161 | String msg = ioe.getLocalizedMessage(); |
---|
162 | int i = msg.indexOf("\n"); |
---|
163 | if (i >= 0) { |
---|
164 | msg = msg.substring(0, i); |
---|
165 | } |
---|
166 | out.print("<h3>" + msg + "</h3>"); |
---|
167 | } |
---|
168 | %> |
---|
169 | <hr> |
---|
170 | |
---|
171 | <h2>Local logs</h2> |
---|
172 | <a href="/logs/">Log</a> directory |
---|
173 | |
---|
174 | <% |
---|
175 | out.println(ServletUtil.htmlFooter()); |
---|
176 | %> |
---|