示例#1
0
  public static final <T, C extends Collection<T>> C set(
      final C collection, @SuppressWarnings("unchecked") final T... elements) {
    collection.clear();
    collection.addAll(Arrays.asList(elements));

    return collection;
  }
示例#2
0
 protected <T, C extends Connection<T>> T acquire(C connection) {
   String errorMessage = String.format("(%s) error acquiring %s", toString(), connection);
   for (int i = 0; i < sshRetries; i++) {
     try {
       connection.clear();
       logger.debug(">> (%s) acquiring %s", toString(), connection);
       T returnVal = connection.create();
       logger.debug("<< (%s) acquired %s", toString(), returnVal);
       return returnVal;
     } catch (Exception from) {
       try {
         connection.clear();
       } catch (Exception e1) {
         logger.warn(from, "<< (%s) error closing connection", toString());
       }
       if (i + 1 == sshRetries) {
         logger.error(from, "<< " + errorMessage + ": out of retries %d", sshRetries);
         throw propagate(from, errorMessage);
       } else if (Throwables2.getFirstThrowableOfType(from, IllegalStateException.class) != null) {
         logger.warn(from, "<< " + errorMessage + ": " + from.getMessage());
         disconnect();
         backoffForAttempt(i + 1, errorMessage + ": " + from.getMessage());
         connect();
         continue;
       } else if (shouldRetry(from)) {
         logger.warn(from, "<< " + errorMessage + ": " + from.getMessage());
         backoffForAttempt(i + 1, errorMessage + ": " + from.getMessage());
         continue;
       } else {
         logger.error(from, "<< " + errorMessage + ": exception not retryable");
         throw propagate(from, errorMessage);
       }
     }
   }
   assert false : "should not reach here";
   return null;
 }
示例#3
0
  protected <T, C extends Connection<T>> T acquire(C connection) {
    connection.clear();
    String errorMessage = String.format("(%s) error acquiring %s", toString(), connection);
    for (int i = 0; i < sshRetries; i++) {
      try {
        logger.debug(">> (%s) acquiring %s", toString(), connection);
        T returnVal = connection.create();
        logger.debug("<< (%s) acquired %s", toString(), returnVal);
        return returnVal;
      } catch (Exception from) {
        connection.clear();

        if (i + 1 == sshRetries) {
          throw propagate(from, errorMessage);
        } else if (shouldRetry(from)) {
          logger.warn(from, "<< " + errorMessage + ": " + from.getMessage());
          backoffForAttempt(i + 1, errorMessage + ": " + from.getMessage());
          continue;
        }
      }
    }
    assert false : "should not reach here";
    return null;
  }
  public C read(Bytes bytes, @Nullable C c) {
    long length = bytes.readStopBit();

    if (length == 0 && c != null) {
      c.clear();
      return c;
    }

    if (length < NULL_LENGTH || length > Integer.MAX_VALUE)
      throw new IllegalStateException("Invalid length: " + length);

    if (length == NULL_LENGTH) return null;

    if (c == null) c = newCollection();

    return readCollection(bytes, c, (int) length);
  }
示例#5
0
 /** {@inheritDoc} */
 @Override
 public void clear() {
   collection.clear();
 }
        @Override
        public void readMarshallable(WireIn wire) throws IllegalStateException {
          @SuppressWarnings("ConstantConditions")
          final Bytes<?> outBytes = outWire.bytes();

          try {

            final StringBuilder eventName = Wires.acquireStringBuilder();
            @SuppressWarnings("ConstantConditions")
            final ValueIn valueIn = inWire.readEventName(eventName);

            outWire.writeDocument(
                true, w -> w.writeEventName(CoreFields.tid).int64(CollectionWireHandler.this.tid));

            outWire.writeDocument(
                false,
                out -> {

                  // note :  remove on the key-set returns a boolean and on the map returns the
                  // old value
                  if (EventId.remove.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(fromWire.apply(valueIn)));
                    return;
                  }

                  // note :  remove on the key-set returns a boolean and on the map returns the
                  // old value
                  if (EventId.iterator.contentEquals(eventName)) {
                    final ValueOut valueOut = out.writeEventName(CoreFields.reply);
                    valueOut.sequence(v -> underlyingCollection.forEach(e -> toWire.accept(v, e)));
                    return;
                  }

                  if (EventId.numberOfSegments.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).int32(1);
                    return;
                  }

                  if (EventId.isEmpty.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).bool(underlyingCollection.isEmpty());
                    return;
                  }

                  if (EventId.size.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).int32(underlyingCollection.size());
                    return;
                  }

                  if (EventId.clear.contentEquals(eventName)) {
                    underlyingCollection.clear();
                    return;
                  }

                  if (EventId.contains.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.contains(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.add.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.add(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.remove.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.containsAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(collectionFromWire()));
                    return;
                  }

                  if (EventId.addAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.addAll(collectionFromWire()));
                    return;
                  }

                  if (EventId.removeAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.removeAll(collectionFromWire()));
                    return;
                  }

                  if (EventId.retainAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.retainAll(collectionFromWire()));
                    return;
                  }

                  throw new IllegalStateException("unsupported event=" + eventName);
                });
          } catch (Exception e) {
            LOG.error("", e);
          } finally {

            if (YamlLogging.showServerWrites) {
              long len = outBytes.writePosition();
              if (len >= SIZE_OF_SIZE) {
                String s = Wires.fromSizePrefixedBlobs(outBytes);

                LOG.info("server writes:\n\n" + s);
              }
            }
          }
        }