source: proiecte/HadoopJUnit/hadoop-0.20.1/src/hdfs/org/apache/hadoop/hdfs/server/namenode/TransferFsImage.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 5.5 KB
Line 
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 */
18package org.apache.hadoop.hdfs.server.namenode;
19
20import java.io.*;
21import java.net.*;
22import java.util.Iterator;
23import java.util.Map;
24import javax.servlet.http.HttpServletResponse;
25import javax.servlet.http.HttpServletRequest;
26
27import org.apache.hadoop.hdfs.protocol.FSConstants;
28import org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.ErrorSimulator;
29
30/**
31 * This class provides fetching a specified file from the NameNode.
32 */
33class TransferFsImage implements FSConstants {
34 
35  private boolean isGetImage;
36  private boolean isGetEdit;
37  private boolean isPutImage;
38  private int remoteport;
39  private String machineName;
40  private CheckpointSignature token;
41 
42  /**
43   * File downloader.
44   * @param pmap key=value[] map that is passed to the http servlet as
45   *        url parameters
46   * @param request the object from which this servelet reads the url contents
47   * @param response the object into which this servelet writes the url contents
48   * @throws IOException
49   */
50  public TransferFsImage(Map<String,String[]> pmap,
51                         HttpServletRequest request,
52                         HttpServletResponse response
53                         ) throws IOException {
54    isGetImage = isGetEdit = isPutImage = false;
55    remoteport = 0;
56    machineName = null;
57    token = null;
58
59    for (Iterator<String> it = pmap.keySet().iterator(); it.hasNext();) {
60      String key = it.next();
61      if (key.equals("getimage")) { 
62        isGetImage = true;
63      } else if (key.equals("getedit")) { 
64        isGetEdit = true;
65      } else if (key.equals("putimage")) { 
66        isPutImage = true;
67      } else if (key.equals("port")) { 
68        remoteport = new Integer(pmap.get("port")[0]).intValue();
69      } else if (key.equals("machine")) { 
70        machineName = pmap.get("machine")[0];
71      } else if (key.equals("token")) { 
72        token = new CheckpointSignature(pmap.get("token")[0]);
73      }
74    }
75
76    int numGets = (isGetImage?1:0) + (isGetEdit?1:0);
77    if ((numGets > 1) || (numGets == 0) && !isPutImage) {
78      throw new IOException("Illegal parameters to TransferFsImage");
79    }
80  }
81
82  boolean getEdit() {
83    return isGetEdit;
84  }
85
86  boolean getImage() {
87    return isGetImage;
88  }
89
90  boolean putImage() {
91    return isPutImage;
92  }
93
94  CheckpointSignature getToken() {
95    return token;
96  }
97
98  String getInfoServer() throws IOException{
99    if (machineName == null || remoteport == 0) {
100      throw new IOException ("MachineName and port undefined");
101    }
102    return machineName + ":" + remoteport;
103  }
104
105  /**
106   * A server-side method to respond to a getfile http request
107   * Copies the contents of the local file into the output stream.
108   */
109  static void getFileServer(OutputStream outstream, File localfile) 
110    throws IOException {
111    byte buf[] = new byte[BUFFER_SIZE];
112    FileInputStream infile = null;
113    try {
114      infile = new FileInputStream(localfile);
115      if (ErrorSimulator.getErrorSimulation(2)
116          && localfile.getAbsolutePath().contains("secondary")) {
117        // throw exception only when the secondary sends its image
118        throw new IOException("If this exception is not caught by the " +
119            "name-node fs image will be truncated.");
120      }
121      int num = 1;
122      while (num > 0) {
123        num = infile.read(buf);
124        if (num <= 0) {
125          break;
126        }
127        outstream.write(buf, 0, num);
128      }
129    } finally {
130      if (infile != null) {
131        infile.close();
132      }
133    }
134  }
135
136  /**
137   * Client-side Method to fetch file from a server
138   * Copies the response from the URL to a list of local files.
139   */
140  static void getFileClient(String fsName, String id, File[] localPath)
141    throws IOException {
142    byte[] buf = new byte[BUFFER_SIZE];
143    StringBuffer str = new StringBuffer("http://"+fsName+"/getimage?");
144    str.append(id);
145
146    //
147    // open connection to remote server
148    //
149    URL url = new URL(str.toString());
150    URLConnection connection = url.openConnection();
151    InputStream stream = connection.getInputStream();
152    FileOutputStream[] output = null;
153
154    try {
155      if (localPath != null) {
156        output = new FileOutputStream[localPath.length];
157        for (int i = 0; i < output.length; i++) {
158          output[i] = new FileOutputStream(localPath[i]);
159        }
160      }
161      int num = 1;
162      while (num > 0) {
163        num = stream.read(buf);
164        if (num > 0 && localPath != null) {
165          for (int i = 0; i < output.length; i++) {
166            output[i].write(buf, 0, num);
167          }
168        }
169      }
170    } finally {
171      stream.close();
172      if (output != null) {
173        for (int i = 0; i < output.length; i++) {
174          if (output[i] != null) {
175            output[i].close();
176          }
177        }
178      }
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.