/** * Constructs new instance of the specified class. * * @param exp Expected class for the new instance. * @param clsName Class name to create new instance for. * @param <T> Expected class type for the new instance. * @return New instance of specified class. * @throws GridClientException If loading failed. */ private static <T> T newInstance(Class<T> exp, String clsName) throws GridClientException { Object obj; try { obj = Class.forName(clsName).newInstance(); } // Catch all for convenience. catch (Exception e) { throw new GridClientException("Failed to create class instance: " + clsName, e); } return exp.cast(obj); }
/** {@inheritDoc} */ @Override public String toString() { return getClass() + ": " + getMessage(); } }
/** * Checks if passed in {@code 'Throwable'} has given class in {@code 'cause'} hierarchy * <b>including</b> that throwable itself. * <p> * Note that this method follows includes {@link Throwable#getSuppressed()} * into check. * * @param cls Cause classes to check (if {@code null} or empty, {@code false} is returned). * @return {@code True} if one of the causing exception is an instance of passed in classes, * {@code false} otherwise. */ public boolean hasCause(@Nullable Class<? extends Throwable>... cls) { return hasCause(this, cls); }
/** {@inheritDoc} */ @Override public void run() { try { GridClientData data = client.data("partitioned"); for (int i = 0; i < iterationCnt; i++) performIteration(data); } catch (GridClientException e) { e.printStackTrace(); } }
/** * Maps Collection of strings to collection of {@code InetSocketAddress}es. * * @param cfgAddrs Collection fo string representations of addresses. * @return Collection of {@code InetSocketAddress}es * @throws GridClientException In case of error. */ private static Collection<InetSocketAddress> parseAddresses(Collection<String> cfgAddrs) throws GridClientException { Collection<InetSocketAddress> addrs = new ArrayList<>(cfgAddrs.size()); for (String srvStr : cfgAddrs) { try { String[] split = srvStr.split(":"); InetSocketAddress addr = new InetSocketAddress(split[0], Integer.parseInt(split[1])); addrs.add(addr); } catch (RuntimeException e) { throw new GridClientException("Failed to create client (invalid server address specified): " + srvStr, e); } } return Collections.unmodifiableCollection(addrs); }
/** {@inheritDoc} */ @Override public void run() { try { while (!isInterrupted()) { Thread.sleep(cfg.getTopologyRefreshFrequency()); try { tryInitTopology(); } catch (GridClientException e) { top.fail(e); if (log.isLoggable(Level.FINE)) log.fine("Failed to update topology: " + e.getMessage()); } } } catch (InterruptedException ignored) { // Client is shutting down. Thread.currentThread().interrupt(); } } }
/** * Checks if passed in {@code 'Throwable'} has given class in {@code 'cause'} hierarchy * <b>including</b> that throwable itself. * <p> * Note that this method follows includes {@link Throwable#getSuppressed()} * into check. * * @param t Throwable to check (if {@code null}, {@code false} is returned). * @param cls Cause classes to check (if {@code null} or empty, {@code false} is returned). * @return {@code True} if one of the causing exception is an instance of passed in classes, * {@code false} otherwise. */ private boolean hasCause(@Nullable Throwable t, @Nullable Class<? extends Throwable>... cls) { if (t == null || F.isEmpty(cls)) return false; assert cls != null; for (Throwable th = t; th != null; th = th.getCause()) { for (Class<? extends Throwable> c : cls) if (c.isAssignableFrom(th.getClass())) return true; for (Throwable n : th.getSuppressed()) if (hasCause(n, cls)) return true; if (th.getCause() == th) break; } return false; }
/** * Get future result. * * @return Future result. * @throws GridClientException In case of error. */ private R getResult() throws GridClientException { assert getState() == DONE; if (err == null) return res; if (err instanceof Error) throw (Error)err; if (err instanceof GridClientException) throw (GridClientException)err; throw new GridClientException(err); }
/** * Creates a failure response, based on the given exception. * * @param e Exception to extract failure report from. * @param clientId Client id. * @param reqId Request id. * @return Failure response. */ private GridClientResponse makeFailureResponse(GridClientException e, UUID clientId, Long reqId) { U.error(log, "Failed to process message on router.", e); GridClientResponse res = new GridClientResponse(); res.clientId(clientId); res.requestId(reqId); res.successStatus(GridClientResponse.STATUS_FAILED); res.errorMessage("Failed to process message on router " + "[exception=" + e.getClass().getSimpleName() + ", message=" + e.getMessage() + ']'); return res; } }
/** * Checks if passed in {@code 'Throwable'} has given class in {@code 'cause'} hierarchy * <b>including</b> that throwable itself. * <p> * Note that this method follows includes {@link Throwable#getSuppressed()} * into check. * * @param cls Cause classes to check (if {@code null} or empty, {@code false} is returned). * @return {@code True} if one of the causing exception is an instance of passed in classes, * {@code false} otherwise. */ public boolean hasCause(@Nullable Class<? extends Throwable>... cls) { return hasCause(this, cls); }
/** {@inheritDoc} */ @Override public R get() throws GridClientException { try { if (!done) acquireSharedInterruptibly(0); return getResult(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new GridClientException("Operation was interrupted.", e); } }
/** {@inheritDoc} */ @Override public String toString() { return getClass() + ": " + getMessage(); } }
/** * Checks if passed in {@code 'Throwable'} has given class in {@code 'cause'} hierarchy * <b>including</b> that throwable itself. * <p> * Note that this method follows includes {@link Throwable#getSuppressed()} * into check. * * @param t Throwable to check (if {@code null}, {@code false} is returned). * @param cls Cause classes to check (if {@code null} or empty, {@code false} is returned). * @return {@code True} if one of the causing exception is an instance of passed in classes, * {@code false} otherwise. */ private boolean hasCause(@Nullable Throwable t, @Nullable Class<? extends Throwable>... cls) { if (t == null || F.isEmpty(cls)) return false; assert cls != null; for (Throwable th = t; th != null; th = th.getCause()) { for (Class<? extends Throwable> c : cls) if (c.isAssignableFrom(th.getClass())) return true; for (Throwable n : th.getSuppressed()) if (hasCause(n, cls)) return true; if (th.getCause() == th) break; } return false; }
ProjectionFactory<T> factory) throws GridClientException { if (nodes != null && nodes.isEmpty()) throw new GridClientException("Failed to create projection: given nodes collection is empty."); throw new GridClientException("Failed to create projection (given node set does not overlap with " + "existing node set) [prjNodes=" + this.nodes + ", nodes=" + nodes); throw new GridClientException("Failed to create projection (none of the nodes in projection node " + "set passed the filter) [prjNodes=" + subset + ", filter=" + filter + ']');
/** {@inheritDoc} */ @Override public void run() { try { while (!isInterrupted()) { Thread.sleep(cfg.getTopologyRefreshFrequency()); try { tryInitTopology(); } catch (GridClientException e) { top.fail(e); if (log.isLoggable(Level.FINE)) log.fine("Failed to update topology: " + e.getMessage()); } } } catch (InterruptedException ignored) { // Client is shutting down. Thread.currentThread().interrupt(); } } }
throw new GridClientException("Passed nodes doesn't present in topology " + "[nodes=" + nodes + ", top=" + nodeQueue);
/** * Creates a failure response, based on the given exception. * * @param e Exception to extract failure report from. * @param clientId Client id. * @param reqId Request id. * @return Failure response. */ private GridClientResponse makeFailureResponse(GridClientException e, UUID clientId, Long reqId) { U.error(log, "Failed to process message on router.", e); GridClientResponse res = new GridClientResponse(); res.clientId(clientId); res.requestId(reqId); res.successStatus(GridClientResponse.STATUS_FAILED); res.errorMessage("Failed to process message on router " + "[exception=" + e.getClass().getSimpleName() + ", message=" + e.getMessage() + ']'); return res; } }
/** {@inheritDoc} */ @Override public R get(long timeout, TimeUnit unit) throws GridClientException { A.ensure(timeout >= 0, "timeout >= 0"); try { if (!done && !tryAcquireSharedNanos(0, unit.toNanos(timeout))) throw new GridClientFutureTimeoutException("Failed to get future result due to waiting timed out."); } catch (InterruptedException e) { throw new GridClientException("Operation was interrupted.", e); } return getResult(); }
throw new GridClientException("Can't get localhost name.", e); throw new GridClientException("Can't find localhost name.");
/** {@inheritDoc} */ @Override public <K> UUID affinity(K key) throws GridClientException { A.notNull(key, "key"); GridClientDataAffinity affinity = client.affinity(cacheName); if (affinity == null) return null; Collection<? extends GridClientNode> prj = projectionNodes(); if (prj.isEmpty()) throw new GridClientException("Failed to get affinity node (projection node set for cache is empty): " + cacheName()); GridClientNode node = affinity.node(key, prj); assert node != null; return node.nodeId(); }