@Override public ImageDescriptor getImage(String name, String reference) { String imageId = getImageId(name, reference); if (imageId == null) { return null; } try { return this.descriptorCache.get(new String[]{name, imageId}); } catch (ExecutionException e) { throw Throwables.asRuntime(e.getCause()); } }
private void checkTrusted(Func func) throws CertificateException { CertificateException ex = null; for (int i =0; i < list.size(); ++i) { X509TrustManager tm = list.get(i); try { func.apply(tm); // accepted return; } catch (CertificateException e) { if(ex == null || Throwables.has(e, CertPathValidatorException.class)) { ex = e; } } } if(ex != null) { throw ex; } }
@ExceptionHandler() @ResponseStatus(INTERNAL_SERVER_ERROR) @ResponseBody public UiError internalError(HttpServletRequest req, Exception e) { log.error("Can't process request", e); return createResponse(e.getMessage(), Throwables.printToString(e), INTERNAL_SERVER_ERROR); }
public static Thread.UncaughtExceptionHandler uncaughtHandler() { return uncaughtHandler(log); }
/** * Do analysing of exception for connection errors, which is mean offline status * * @param e */ private void checkOnline(Exception e) { ConnectException conn = Throwables.find(e, ConnectException.class); if (conn != null) { toggleOnline(conn.toString()); } }
public static Thread.UncaughtExceptionHandler uncaughtHandler(Logger log) { return uncaughtHandler(log, "Uncaught exception."); }
/** * Encode specified value as RFC 2047 * @param name * @return * @throws Exception */ public static String encode(String name) { try { return MimeUtility.encodeText(name, "UTF-8", null); } catch (UnsupportedEncodingException e) { throw Throwables.asRuntime(e); } } }
@ExceptionHandler({IllegalArgumentException.class}) @ResponseStatus(BAD_REQUEST) @ResponseBody public UiError bindErrorHandler(IllegalArgumentException e) { log.error("Can't process request", e); return createResponse(e.getMessage(), Throwables.printToString(e), BAD_REQUEST); }
@Autowired public DockerServiceFactory(ObjectMapper objectMapper, AccessContextFactory aclContextFactory, NodeStorage nodeStorage, @Qualifier(DockerServiceEvent.BUS) MessageBus<DockerServiceEvent> dockerServiceEventMessageBus, RegistryRepository registryRepository, ResourceLoader resourceLoader) { this.executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat(getClass().getSimpleName() + "-executor-%d") .setUncaughtExceptionHandler(Throwables.uncaughtHandler(log)) .build()); this.objectMapper = objectMapper; this.aclContextFactory = aclContextFactory; this.nodeStorage = nodeStorage; this.dockerServiceEventMessageBus = dockerServiceEventMessageBus; this.registryRepository = registryRepository; this.resourceLoader = resourceLoader; }
private DockerServiceInfo loadDockerInfo(NodeRegistrationImpl nr) { DockerServiceInfo tmp = null; try { DockerService docker = nr.getDocker(); if(docker != null) { tmp = docker.getInfo(); } else { log.error("Fail to load node '{}' info due to docker is null (address is '{}').", nr.getName(), nr.getAddress()); } } catch (Exception e) { if(Throwables.has(e, SocketException.class)) { // suppress stack traces when exception is expected log.error("Fail to load node '{}' info due to error: {}", nr.getName(), e.toString()); } else { log.error("Fail to load node '{}' info.", nr.getName(), e); } } return tmp; } }
/** * Wait process start. */ public void waitStart() { Future<?> future; synchronized (lock) { future = this.future; } long timeout = (long) (config.getMaxWaitOnStart() * 1.5 /* time must be greater than */); try { future.get(timeout, TimeUnit.SECONDS); } catch(Exception e) { throw Throwables.asRuntime(e.getCause()); } }
@ExceptionHandler({NotFoundException.class}) @ResponseStatus(NOT_FOUND) @ResponseBody public UiError bindErrorHandler(NotFoundException e) { log.error("Can't process request", e); return createResponse(e.getMessage(), Throwables.printToString(e), NOT_FOUND); }
@Autowired public DockerServices(DockerServicesConfig configuration, SwarmProcesses swarmProcesses, NodeStorage nodeStorage, DockerServiceFactory dockerFactory, @Qualifier(DockerServiceEvent.BUS) MessageBus<DockerServiceEvent> dockerServiceEventMessageBus) { this.swarmProcesses = swarmProcesses; this.nodeStorage = nodeStorage; this.dockerServiceEventMessageBus = dockerServiceEventMessageBus; this.dockerFactory = dockerFactory; String classPrefix = getClass().getSimpleName(); scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat(classPrefix + "-scheduled-%d") .setUncaughtExceptionHandler(Throwables.uncaughtHandler(log)) .build()); scheduledExecutor.scheduleWithFixedDelay(this::updateInfo, configuration.getRefreshInfoSeconds(), configuration.getRefreshInfoSeconds(), TimeUnit.SECONDS); }
/** * Immediately create node in storage */ public void create() { //this is workaround for creating node directory without any attributes, // in some KV we can create dirs, in other - not, therefore we need to make some key value record try { storage.set(KvUtils.join(prefix, "_created"), DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now())); } catch (Exception e) { throw Throwables.asRuntime(e); } }
@ExceptionHandler({HttpClientErrorException.class}) @ResponseBody public ResponseEntity<UiError> bindErrorHandler(HttpClientErrorException e) { log.error("Can't process request", e); HttpStatus statusCode = e.getStatusCode(); UiError response = createResponse(StringUtils.trimWhitespace(e.getResponseBodyAsString()), Throwables.printToString(e), statusCode); return new ResponseEntity<>(response, BAD_REQUEST); }
@Autowired public ContainerStorageImpl(KvMapperFactory kvmf) { String prefix = kvmf.getStorage().getPrefix() + "/containers/"; this.map = KvMap.builder(ContainerRegistration.class) .mapper(kvmf) .path(prefix) .factory((key, type) -> new ContainerRegistration(this, key)) .build(); this.executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat(getClass().getSimpleName() + "-scheduled-%d") .setUncaughtExceptionHandler(Throwables.uncaughtHandler(log)) .build()); }
private UriComponentsBuilder makeBaseUrl() { try { UriComponentsBuilder ucb = newInstance(); ucb.uri(this.uri); return ucb; } catch (Exception e) { log.error("error during creating rest request to docker " + clusterConfig.toString(), e); throw Throwables.asRuntime(e); } }
/** * Utility for creating error dto from exception. * Also support {@link HttpException } * @param ex * @return */ public static UiError from(Exception ex) { UiError error = new UiError(); error.setStack(Throwables.printToString(ex)); if(ex instanceof HttpException) { error.setCode(((HttpException)ex).getStatus().value()); } error.setMessage(ex.getMessage()); return error; }
NodeRegistrationImpl(NodeStorage nodeStorage, PersistentBusFactory pbf, NodeInfo nodeInfo) { String name = nodeInfo.getName(); NodeUtils.checkName(name); this.name = name; this.nodeStorage = nodeStorage; this.oid = SecuredType.NODE.id(name); // name may contain dots this.healthBus = pbf.create(NodeHealthEvent.class, "node[" + name + "].metrics", 2000/* TODO in config */); synchronized (lock) { this.builder = NodeInfoImpl.builder(nodeInfo); } this.logFetcher = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat("node-"+name +"-log-fetcher-%d") .setUncaughtExceptionHandler(Throwables.uncaughtHandler(log, "Uncaught exception in '" + name + "' node log fetcher.")) .build()); }
private <T extends Annotation> T getAnnotation(CacheOperationInvocationContext<?> context, Class<T> clazz) { try { // due to some cache proxy behaviour we can get method of superinterface instead of annotated method from target class // but sometime annotation has been appear on interface therefore we need check both cases Method proxiedMethod = context.getMethod(); Class<?> targetClazz = context.getTarget().getClass(); T annotation = null; if(!targetClazz.equals(proxiedMethod.getDeclaringClass())) { Method origMethod = targetClazz.getMethod(proxiedMethod.getName(), proxiedMethod.getParameterTypes()); annotation = origMethod.getAnnotation(clazz); } if(annotation == null) { annotation = proxiedMethod.getAnnotation(clazz); } return annotation; } catch (NoSuchMethodException e) { throw Throwables.asRuntime(e); } }