/** * Returns the child object with the given id. * * @param id the name of the requested child object. Must not contain .. / or \ * @return the object with the given id, might not exist, but is always non null */ public StoredObject getObject(String id) { if (id.contains("..") || id.contains("/") || id.contains("\\")) { throw Exceptions.createHandled() .withSystemErrorMessage( "Invalid object name: %s. A object name must not contain '..' '/' or '\\'", id) .handle(); } return new StoredObject(new File(file, id)); }
/** * Returns the generated XML as string using the given encoding. * * @param encoding the encoding to use when converting the binary buffer to a String. * @return a string representation of the generated XML. */ public String generate(String encoding) { try { return new String(((ByteArrayOutputStream) out).toByteArray(), encoding); } catch (UnsupportedEncodingException e) { throw Exceptions.handle(e); } }
@Override protected Long coerceToId(Object id) { try { if (id instanceof Long) { return (Long) id; } return Long.parseLong(id.toString()); } catch (NumberFormatException e) { Exceptions.ignore(e); return SQLEntity.NON_PERSISTENT_ENTITY_ID; } } }
private static void invokeHandler(AccessPath accessPath, Method m, Object entity, Object... params) { try { m.setAccessible(true); if (m.getParameterCount() == 0) { m.invoke(accessPath.apply(entity)); } else { m.invoke(accessPath.apply(entity), params.length == 0 ? new Object[]{entity} : params); } } catch (IllegalAccessException ex) { throw Exceptions.handle(Mixing.LOG, ex); } catch (InvocationTargetException ex) { Exceptions.ignore(ex); throw Exceptions.handle(Mixing.LOG, ex.getTargetException()); } }
@Override protected Object transformFromRequest(String name, WebContext ctx) { Value value = ctx.get(name); if (value.isEmptyString()) { return null; } try { return NLS.parseUserString(LocalDate.class, value.getString()); } catch (IllegalArgumentException e) { Exceptions.ignore(e); UserContext.setFieldError(name, value.get()); throw Exceptions.createHandled() .withNLSKey("Property.invalidInput") .set("field", NLS.get(getField().getDeclaringClass().getSimpleName() + "." + name)) .set("value", value.asString()) .handle(); } }
/** * Marks the promise as failed due to the given error. * * @param exception the error to be used as reason for failure. * @return <tt>this</tt> for fluent method chaining */ public Promise<V> fail(@Nonnull final Throwable exception) { this.failure = exception; if (logErrors) { Exceptions.handle(Tasks.LOG, exception); } else if (Tasks.LOG.isFINE() && !(exception instanceof HandledException)) { Tasks.LOG.FINE(Exceptions.createHandled().error(exception)); } for (final CompletionHandler<V> handler : handlers) { failHandler(exception, handler); } return this; }
protected void safeClose() { for (BatchQuery<?> query : queries) { try { query.tryCommit(false); } catch (HandledException e) { Exceptions.ignore(e); } catch (Exception e) { Exceptions.handle(OMA.LOG, e); } query.safeClose(); } queries.clear(); connectionsPerRealm.values().forEach(this::safeCloseConnection); connectionsPerRealm.clear(); }
@Override protected Object transformFromRequest(String name, WebContext ctx) { Value value = ctx.get(name); if (value.isEmptyString()) { return null; } try { return NLS.parseUserString(LocalDateTime.class, value.getString()); } catch (IllegalArgumentException e) { Exceptions.ignore(e); UserContext.setFieldError(name, value.get()); throw Exceptions.createHandled() .withNLSKey("Property.invalidInput") .set("field", NLS.get(getField().getDeclaringClass().getSimpleName() + "." + name)) .set("value", value.asString()) .handle(); } }
@Override public String toString() { try { StringWriter writer = new StringWriter(); XMLGenerator.writeXML(node, writer, Charsets.UTF_8.name(), true); return writer.toString(); } catch (Exception e) { Exceptions.handle(e); return node.toString(); } } }
/** * Waits the given amount of milliseconds. * <p> * If the given value is 0 or negative, the method returns immediately. If an <tt>InterruptedException</tt> * is thrown while waiting, the method will return immediately but ignore the exception. * * @param millisToWait the number of milliseconds to wait */ public static void millis(int millisToWait) { if (millisToWait > 0) { try { Thread.sleep(millisToWait); } catch (InterruptedException e) { Exceptions.ignore(e); Thread.currentThread().interrupt(); } } }
@Override public long getMilliseconds(String path) { try { return configObject.toConfig().getDuration(path, TimeUnit.MILLISECONDS); } catch (ConfigException.Missing e) { Exceptions.ignore(e); return defaultConfig.toConfig().getDuration(path, TimeUnit.MILLISECONDS); } catch (Exception e) { throw Exceptions.handle(LOG, e); } }
/** * Returns a bucket with the given name * * @param bucket the name of the bucket to fetch. Must not contain .. or / or \ * @return the bucket with the given id. Might not exist, but will never be <tt>null</tt> */ public Bucket getBucket(String bucket) { if (bucket.contains("..") || bucket.contains("/") || bucket.contains("\\")) { throw Exceptions.createHandled() .withSystemErrorMessage( "Invalid bucket name: %s. A bucket name must not contain '..' '/' or '\\'", bucket) .handle(); } return new Bucket(new File(getBaseDir(), bucket)); }
@Override protected void endArray(String name) { try { hd.endElement("", "", name); } catch (SAXException e) { throw Exceptions.handle(e); } }
private String getETag(File file) { try { return com.google.common.io.Files.hash(file, Hashing.md5()).toString(); } catch (IOException e) { Exceptions.ignore(e); } return null; }
/** * Checks if the value is non-null or the property accepts null values. * * @param propertyValue the value to check */ protected void checkNullability(Object propertyValue) { if (!isNullable() && propertyValue == null) { throw Exceptions.createHandled().withNLSKey("Property.fieldNotNullable").set("field", getLabel()).handle(); } }
@Override protected void endObject(String name) { try { hd.endElement("", "", name); } catch (SAXException e) { throw Exceptions.handle(e); } }
private boolean hasSetter(Field field) { try { field.getDeclaringClass().getMethod("set" + Reflection.toFirstUpper(field.getName()), field.getType()); return true; } catch (NoSuchMethodException e) { Exceptions.ignore(e); return false; } }
/** * Parses the given value and applies it to the given entity if possible. * * @param e the entity to receive the parsed value * @param value the value to parse and apply */ public void parseValue(Object e, Value value) { try { setValue(e, transformValue(value)); } catch (IllegalArgumentException exception) { throw Exceptions.createHandled() .withNLSKey("Property.parseValueErrorMessage") .set("message", exception.getMessage()) .error(exception) .handle(); } }
@Override protected void startArray(String name) { try { hd.startElement("", "", name, null); } catch (SAXException e) { throw Exceptions.handle(e); } }
/** * Waits until all previously added promises completed or the given timeout expires. * * @param time the number of time intervals to wait for completion * @param unit the unit of time intervals to wait for completion * @return <tt>true</tt> if all promises completed within the given timout, <tt>false</tt> otherwise. */ public boolean await(long time, TimeUnit unit) { try { return semaphore.tryAcquire(promisesMade.get(), time, unit); } catch (InterruptedException e) { Thread.currentThread().interrupt(); Exceptions.ignore(e); return false; } } }