Пример #1
0
  protected <T> void setOption(
      NetworkChannel socket, String property, SocketOption<T> option, T defaultValue)
      throws IOException {
    PropertyResolver manager = getFactoryManager();
    String valStr = PropertyResolverUtils.getString(manager, property);
    T val = defaultValue;
    if (!GenericUtils.isEmpty(valStr)) {
      Class<T> type = option.type();
      if (type == Integer.class) {
        val = type.cast(Integer.valueOf(valStr));
      } else if (type == Boolean.class) {
        val = type.cast(Boolean.valueOf(valStr));
      } else {
        throw new IllegalStateException("Unsupported socket option type " + type);
      }
    }

    if (val != null) {
      Collection<? extends SocketOption<?>> supported = socket.supportedOptions();
      if ((GenericUtils.size(supported) <= 0) || (!supported.contains(option))) {
        log.warn(
            "Unsupported socket option ("
                + option
                + ") to set using property '"
                + property
                + "' value="
                + val);
        return;
      }

      try {
        socket.setOption(option, val);
        if (log.isDebugEnabled()) {
          log.debug("setOption({})[{}] from property={}", option, val, property);
        }
      } catch (IOException | RuntimeException e) {
        log.warn(
            "Unable ("
                + e.getClass().getSimpleName()
                + ")"
                + " to set socket option "
                + option
                + " using property '"
                + property
                + "' value="
                + val
                + ": "
                + e.getMessage());
      }
    }
  }
Пример #2
0
  protected ChannelExec openCommandChannel(ClientSession session, String cmd) throws IOException {
    long waitTimeout =
        PropertyResolverUtils.getLongProperty(
            session, SCP_EXEC_CHANNEL_OPEN_TIMEOUT, DEFAULT_EXEC_CHANNEL_OPEN_TIMEOUT);
    ChannelExec channel = session.createExecChannel(cmd);

    long startTime = System.nanoTime();
    try {
      channel.open().verify(waitTimeout);
      long endTime = System.nanoTime();
      long nanosWait = endTime - startTime;
      if (log.isTraceEnabled()) {
        log.trace(
            "openCommandChannel("
                + session
                + ")["
                + cmd
                + "]"
                + " completed after "
                + nanosWait
                + " nanos out of "
                + TimeUnit.MILLISECONDS.toNanos(waitTimeout));
      }

      return channel;
    } catch (IOException | RuntimeException e) {
      long endTime = System.nanoTime();
      long nanosWait = endTime - startTime;
      if (log.isTraceEnabled()) {
        log.trace(
            "openCommandChannel("
                + session
                + ")["
                + cmd
                + "]"
                + " failed ("
                + e.getClass().getSimpleName()
                + ")"
                + " to complete after "
                + nanosWait
                + " nanos out of "
                + TimeUnit.MILLISECONDS.toNanos(waitTimeout)
                + ": "
                + e.getMessage());
      }

      channel.close(false);
      throw e;
    }
  }
 @Override
 protected Void doInBackground(String... urls) {
   locationsList = new ArrayList<>();
   final JsonFactory jfactory = new JsonFactory();
   JsonParser jParser = null;
   try {
     jParser = jfactory.createParser(new URL(urls[0]));
     Log.d("MyLogs", "URL - " + urls[0]);
     while (jParser.nextToken() != JsonToken.END_ARRAY) {
       String fieldname = jParser.getCurrentName();
       if ("id".equals(fieldname)) {
         map = new HashMap<>();
         jParser.nextToken();
         map.put("id", jParser.getText());
       } else if ("name".equals(fieldname)) {
         jParser.nextToken();
         map.put("name", jParser.getText());
         locationsList.add(map);
       }
     }
   } catch (IOException | RuntimeException e) {
     e.printStackTrace();
   } finally {
     if (jParser != null) {
       try {
         jParser.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
   list = new ArrayList<>();
   for (HashMap<String, String> a : locationsList) {
     list.add(a.get("name"));
   }
   return null;
 }