public IOReadableWritable call(Class<?> protocol, IOReadableWritable param, long receivedTime)
        throws IOException {

      try {

        final Invocation call = (Invocation) param;

        final Method method = protocol.getMethod(call.getMethodName(), call.getParameterClasses());
        method.setAccessible(true);

        final Object value = method.invoke((Object) instance, (Object[]) call.getParameters());

        return (IOReadableWritable) value;

      } catch (InvocationTargetException e) {

        final Throwable target = e.getTargetException();
        if (target instanceof IOException) {
          throw (IOException) target;
        } else {
          final IOException ioe = new IOException(target.toString());
          ioe.setStackTrace(target.getStackTrace());
          throw ioe;
        }
      } catch (Throwable e) {
        final IOException ioe = new IOException(e.toString());
        ioe.setStackTrace(e.getStackTrace());
        throw ioe;
      }
    }
Example #2
0
    public Writable call(Class<?> protocol, Writable param, long receivedTime) throws IOException {
      try {
        Invocation call = (Invocation) param;
        if (verbose) log("Call: " + call);

        Method method = protocol.getMethod(call.getMethodName(), call.getParameterClasses());
        method.setAccessible(true);

        int qTime = (int) (System.currentTimeMillis() - receivedTime);
        long startNanoTime = System.nanoTime();
        Object value = method.invoke(instance, call.getParameters());
        long processingMicroTime = (System.nanoTime() - startNanoTime) / 1000;
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "Served: "
                  + call.getMethodName()
                  + " queueTime (millisec)= "
                  + qTime
                  + " procesingTime (microsec)= "
                  + processingMicroTime);
        }
        rpcMetrics.rpcQueueTime.inc(qTime);
        rpcMetrics.rpcProcessingTime.inc(processingMicroTime);

        MetricsTimeVaryingRate m =
            (MetricsTimeVaryingRate) rpcMetrics.registry.get(call.getMethodName());
        if (m == null) {
          try {
            m = new MetricsTimeVaryingRate(call.getMethodName(), rpcMetrics.registry);
          } catch (IllegalArgumentException iae) {
            // the metrics has been registered; re-fetch the handle
            LOG.debug("Error register " + call.getMethodName(), iae);
            m = (MetricsTimeVaryingRate) rpcMetrics.registry.get(call.getMethodName());
          }
        }
        // record call time in microseconds
        m.inc(processingMicroTime);

        if (verbose) log("Return: " + value);

        return new ObjectWritable(method.getReturnType(), value);

      } catch (InvocationTargetException e) {
        Throwable target = e.getTargetException();
        if (target instanceof IOException) {
          throw (IOException) target;
        } else {
          IOException ioe = new IOException(target.toString());
          ioe.setStackTrace(target.getStackTrace());
          throw ioe;
        }
      } catch (Throwable e) {
        if (!(e instanceof IOException)) {
          LOG.error("Unexpected throwable object ", e);
        }
        IOException ioe = new IOException(e.toString());
        ioe.setStackTrace(e.getStackTrace());
        throw ioe;
      }
    }
Example #3
0
 public void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   String hdfsFolder = request.getParameter("hdfsFolder");
   HttpSession session = request.getSession(true);
   ConfigurationManager configurationManager =
       ((LoginPass) session.getAttribute(ShsContext.LOGIN_PASS)).getConfigurationManager();
   PrintWriter out = response.getWriter();
   Map<String, String> jsonKeyValMap = new LinkedHashMap<String, String>();
   try {
     IFileSystem filesystem = configurationManager.getFileSystem();
     IFile[] files = filesystem.listFiles(hdfsFolder);
     response.setContentType("application/json");
     int count = 0;
     for (IFile file : files) {
       jsonKeyValMap.put("fileName" + count, file.getFilename());
       jsonKeyValMap.put("fileOwner" + count, file.getOwner());
       jsonKeyValMap.put("fileLen" + count, FileUtils.byteCountToDisplaySize(file.getLen()));
       jsonKeyValMap.put(
           "fileModificationTime" + count,
           ShsContext.DATE_FORMAT.format(new Date(file.getModificationTime())));
       jsonKeyValMap.put("isDir" + count, file.isDir() ? "0" : "1");
       count++;
     }
   } catch (Exception ee) {
     IOException iee = new IOException(ee);
     iee.setStackTrace(ee.getStackTrace());
     throw iee;
   }
   out.write(ShsContext.getJsonString(jsonKeyValMap));
   out.close();
 }
Example #4
0
 static Throwable wrapWithAddendum(Throwable ex, String addendum, boolean after) {
   if (ex instanceof AssertionFailedError) {
     AssertionFailedError ne = new AssertionFailedError(combineMessages(ex, addendum, after));
     if (ex.getCause() != null) {
       ne.initCause(ex.getCause());
     }
     ne.setStackTrace(ex.getStackTrace());
     return ne;
   }
   if (ex instanceof AssertionError) { // preferred in JUnit 4
     AssertionError ne = new AssertionError(combineMessages(ex, addendum, after));
     if (ex.getCause() != null) {
       ne.initCause(ex.getCause());
     }
     ne.setStackTrace(ex.getStackTrace());
     return ne;
   }
   if (ex instanceof IOException) { // #66208
     IOException ne = new IOException(combineMessages(ex, addendum, after));
     if (ex.getCause() != null) {
       ne.initCause(ex.getCause());
     }
     ne.setStackTrace(ex.getStackTrace());
     return ne;
   }
   if (ex instanceof Exception) {
     return new InvocationTargetException(ex, combineMessages(ex, addendum, after));
   }
   return ex;
 }
  protected URLConnection getConnection(String resource) throws IOException {

    URLConnection conn = null;

    try {

      // create the HTTP connection.
      URL request = new URL(resource);
      conn = request.openConnection();

      // set the UserAgent so Spinn3r know which client lib is calling.
      conn.setRequestProperty(USER_AGENT_HEADER, USER_AGENT + "; " + getConfig().getCommandLine());
      conn.setRequestProperty(ACCEPT_ENCODING_HEADER, GZIP_ENCODING);
      conn.setConnectTimeout(20000);
      conn.connect();

    } catch (IOException ioe) {

      // create a custom exception message with the right error.
      String message = conn.getHeaderField(null);
      IOException ce = new IOException(message);
      ce.setStackTrace(ioe.getStackTrace());

      throw ce;
    }

    return conn;
  }
Example #6
0
 public void map(KEYIN inKey, VALUEIN inValue, Context context)
     throws IOException, InterruptedException {
   try {
     callFunction("map", inKey, inValue, context);
   } catch (Exception e) {
     IOException ioe = new IOException(e);
     ioe.setStackTrace(e.getStackTrace());
     throw ioe;
   }
 }
Example #7
0
 /**
  * Converts an IOExcpetion (not subclasses) to SocketException. This is typically done to indicate
  * to upper layers that the error was a socket error rather than often more serious exceptions
  * like disk errors.
  */
 private static IOException ioeToSocketException(IOException ioe) {
   if (ioe.getClass().equals(IOException.class)) {
     // "se" could be a new class in stead of SocketException.
     IOException se = new SocketException("Original Exception : " + ioe);
     se.initCause(ioe);
     /* Change the stacktrace so that original trace is not truncated
      * when printed.*/
     se.setStackTrace(ioe.getStackTrace());
     return se;
   }
   // otherwise just return the same exception.
   return ioe;
 }
Example #8
0
  /** This method copies file recursively from src to dest */
  private void copyFiles(File src, File dest) throws IOException {
    // Check to ensure that the source is valid
    if (!src.exists()) {
      throw new IOException("copyFiles: Cannot find source: " + src.getAbsolutePath() + ".");
    } else if (!src.canRead()) {
      // check to ensure we have rights to the source
      throw new IOException("copyFiles: No right to read source: " + src.getAbsolutePath() + ".");
    }

    // is this a directory copy?
    if (src.isDirectory()) {
      if (!dest.exists()) { // does the destination already exist?
        // if not we need to make it exist if possible (note this is
        // mkdirs not mkdir)
        if (!dest.mkdirs()) {
          throw new IOException(
              "copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + ".");
        }
      }

      String list[] = src.list();
      // copy all the files in the list.
      for (String path : list) {
        File dest1 = new File(dest, path);
        File src1 = new File(src, path);
        copyFiles(src1, dest1);
      }
    } else {
      // This was not a directory, just copy the file
      try {
        if (!dest.getParentFile().exists()) {
          dest.getParentFile().mkdirs();
        }

        // open the files for input and output
        FileManager.copyFile(src, dest);
        _curPaths.add(dest.getAbsolutePath());
      } catch (IOException e) { // Error copying file
        IOException wrapper =
            new IOException(
                "copyFiles: Unable to copy file: "
                    + src.getAbsolutePath()
                    + " to "
                    + dest.getAbsolutePath()
                    + ".");
        wrapper.initCause(e);
        wrapper.setStackTrace(e.getStackTrace());
        throw wrapper;
      }
    }
  }
  // parse the message and extract the name of the exception and the message
  private static IOException getExceptionFromResponse(HttpURLConnection con) {
    IOException e = null;
    String resp;
    if (con == null) return null;

    try {
      resp = con.getResponseMessage();
    } catch (IOException ie) {
      return null;
    }
    if (resp == null || resp.isEmpty()) return null;

    String exceptionClass = "", exceptionMsg = "";
    String[] rs = resp.split(";");
    if (rs.length < 2) return null;
    exceptionClass = rs[0];
    exceptionMsg = rs[1];
    LOG.info(
        "Error response from HTTP request="
            + resp
            + ";ec="
            + exceptionClass
            + ";em="
            + exceptionMsg);

    if (exceptionClass == null || exceptionClass.isEmpty()) return null;

    // recreate exception objects
    try {
      Class<? extends Exception> ec = Class.forName(exceptionClass).asSubclass(Exception.class);
      // we are interested in constructor with String arguments
      java.lang.reflect.Constructor<? extends Exception> constructor =
          (java.lang.reflect.Constructor<? extends Exception>)
              ec.getConstructor(new Class[] {String.class});

      // create an instance
      e = (IOException) constructor.newInstance(exceptionMsg);

    } catch (Exception ee) {
      LOG.warn("failed to create object of this class", ee);
    }
    if (e == null) return null;

    e.setStackTrace(new StackTraceElement[0]); // local stack is not relevant
    LOG.info("Exception from HTTP response=" + e.getLocalizedMessage());
    return e;
  }
Example #10
0
 protected void setup(Context context) throws IOException, InterruptedException {
   super.setup(context);
   scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
   try {
     Configuration configuration = context.getConfiguration();
     mapOutputKey = MrUtils.getWritableComparableType("js.map.output.key.type", configuration);
     mapOutputValue = MrUtils.getWritableComparableType("js.map.output.value.type", configuration);
     scriptEngine.eval(
         MrUtils.getScripts(
             context.getConfiguration(), MrUtils.getPathFilter("js.map.filename", configuration)));
     scriptEngine.put("mapOutputKey", mapOutputKey);
     scriptEngine.put("mapOutputValue", mapOutputValue);
     OUTPUT_KEY_CLASS = mapOutputKey.getClass();
     OUTPUT_VALUE_CLASS = mapOutputValue.getClass();
   } catch (ScriptException se) {
     IOException ioe = new IOException(se);
     ioe.setStackTrace(se.getStackTrace());
     throw ioe;
   }
 }
Example #11
0
  public boolean reciveObject() throws IOException, ClassNotFoundException {
    /*odbiera przesylke i zwraca jako zwykly obiekt
    trzeba sobie samemu zrzutowac pozniej;
    JEST TO FUNKCJA BLOKUJACA, dopoki nie otrzyma zwrotu to reszta stoi*/
    // przesylka=null;
    try {
      ObjectInputStream przychodzacy = new ObjectInputStream(socket.getInputStream());

      try {
        przesylka = przychodzacy.readObject();
      } catch (ClassNotFoundException e) {
        System.out.println("Nie rozpoznano przesylki");
        return false;
      } // koniec try dla rzutowania przesylki

    } catch (IOException e) {
      System.out.println("Blad odbioru wiadomosci");
      e.setStackTrace(null);
      return false;
    } // koniec try dla odbierania przesylki

    return true;
  }
Example #12
0
      @Override
      public Writable call(
          org.apache.hadoop.ipc.RPC.Server server,
          String protocolName,
          Writable rpcRequest,
          long receivedTime)
          throws IOException {
        try {
          Invocation call = (Invocation) rpcRequest;
          if (server.verbose) log("Call: " + call);

          // Verify rpc version
          if (call.getRpcVersion() != writableRpcVersion) {
            // Client is using a different version of WritableRpc
            throw new IOException(
                "WritableRpc version mismatch, client side version="
                    + call.getRpcVersion()
                    + ", server side version="
                    + writableRpcVersion);
          }

          long clientVersion = call.getProtocolVersion();
          final String protoName;
          ProtoClassProtoImpl protocolImpl;
          if (call.declaringClassProtocolName.equals(VersionedProtocol.class.getName())) {
            // VersionProtocol methods are often used by client to figure out
            // which version of protocol to use.
            //
            // Versioned protocol methods should go the protocolName protocol
            // rather than the declaring class of the method since the
            // the declaring class is VersionedProtocol which is not
            // registered directly.
            // Send the call to the highest  protocol version
            VerProtocolImpl highest =
                server.getHighestSupportedProtocol(RPC.RpcKind.RPC_WRITABLE, protocolName);
            if (highest == null) {
              throw new IOException("Unknown protocol: " + protocolName);
            }
            protocolImpl = highest.protocolTarget;
          } else {
            protoName = call.declaringClassProtocolName;

            // Find the right impl for the protocol based on client version.
            ProtoNameVer pv = new ProtoNameVer(call.declaringClassProtocolName, clientVersion);
            protocolImpl = server.getProtocolImplMap(RPC.RpcKind.RPC_WRITABLE).get(pv);
            if (protocolImpl == null) { // no match for Protocol AND Version
              VerProtocolImpl highest =
                  server.getHighestSupportedProtocol(RPC.RpcKind.RPC_WRITABLE, protoName);
              if (highest == null) {
                throw new IOException("Unknown protocol: " + protoName);
              } else { // protocol supported but not the version that client wants
                throw new RPC.VersionMismatch(protoName, clientVersion, highest.version);
              }
            }
          }

          // Invoke the protocol method

          long startTime = Time.now();
          Method method =
              protocolImpl.protocolClass.getMethod(
                  call.getMethodName(), call.getParameterClasses());
          method.setAccessible(true);
          server.rpcDetailedMetrics.init(protocolImpl.protocolClass);
          Object value = method.invoke(protocolImpl.protocolImpl, call.getParameters());
          int processingTime = (int) (Time.now() - startTime);
          int qTime = (int) (startTime - receivedTime);
          if (LOG.isDebugEnabled()) {
            LOG.debug(
                "Served: "
                    + call.getMethodName()
                    + " queueTime= "
                    + qTime
                    + " procesingTime= "
                    + processingTime);
          }
          server.rpcMetrics.addRpcQueueTime(qTime);
          server.rpcMetrics.addRpcProcessingTime(processingTime);
          server.rpcDetailedMetrics.addProcessingTime(call.getMethodName(), processingTime);
          if (server.verbose) log("Return: " + value);

          return new ObjectWritable(method.getReturnType(), value);

        } catch (InvocationTargetException e) {
          Throwable target = e.getTargetException();
          if (target instanceof IOException) {
            throw (IOException) target;
          } else {
            IOException ioe = new IOException(target.toString());
            ioe.setStackTrace(target.getStackTrace());
            throw ioe;
          }
        } catch (Throwable e) {
          if (!(e instanceof IOException)) {
            LOG.error("Unexpected throwable object ", e);
          }
          IOException ioe = new IOException(e.toString());
          ioe.setStackTrace(e.getStackTrace());
          throw ioe;
        }
      }