/** * 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)); }
private static void createAndCollectClassLoadAction(String className, Class<?> clazz) { try { actions.add((ClassLoadAction) clazz.getDeclaredConstructor().newInstance()); } catch (Exception e) { Exceptions.handle() .error(e) .to(LOG) .withSystemErrorMessage("Failed to instantiate ClassLoadAction: %s - %s (%s)", className) .handle(); } }
private static void startLifecycle(Startable lifecycle) { LOG.INFO("Starting: %s", lifecycle.getClass().getName()); try { lifecycle.started(); } catch (Exception e) { Exceptions.handle() .error(e) .to(LOG) .withSystemErrorMessage("Startup of: %s failed!", lifecycle.getClass().getName()) .handle(); } }
@SuppressWarnings("unchecked") protected R getEntityRef(Object entity) { try { return (R) super.getValueFromField(entity); } catch (Exception e) { throw Exceptions.handle() .to(Mixing.LOG) .error(e) .withSystemErrorMessage( "Unable to obtain EntityRef object from entity ref field ('%s' in '%s'): %s (%s)", getName(), descriptor.getType().getName()) .handle(); } }
private Tuple<String, Integer> parsePort(Tuple<String, String> hostnameAndPort) { if (Strings.isFilled(hostnameAndPort.getSecond())) { try { return Tuple.create(hostnameAndPort.getFirst(), Integer.parseInt(hostnameAndPort.getSecond())); } catch (NumberFormatException e) { Exceptions.handle() .to(LOG) .withSystemErrorMessage("Invalid port in 'elasticsearch.hosts': %s %s", hostnameAndPort.getFirst(), hostnameAndPort.getSecond()) .handle(); } } return Tuple.create(hostnameAndPort.getFirst(), DEFAULT_HTTP_PORT); }
private static void handleBeforeSaveMethod(EntityDescriptor descriptor, AccessPath accessPath, Method method) { warnOnWrongVisibility(method); if (descriptor.beforeSaveHandlerCollector == null) { throw Exceptions.handle() .to(Mixing.LOG) .withSystemErrorMessage("Cannot provide a before-save-handler, as the sorted list was" + " already computed. Descriptor: %s, Method: %s.%s", descriptor.getType().getName(), method.getDeclaringClass().getName(), method.getName()) .handle(); } descriptor.beforeSaveHandlerCollector.add(method.getAnnotation(BeforeSave.class).priority(), e -> invokeHandler(accessPath, method, e)); }
@Override protected void onBeforeSaveChecks(Object entity) { R ref = getEntityRef(accessPath.apply(entity)); if (ref.containsNonpersistentValue()) { throw Exceptions.handle() .to(OMA.LOG) .withSystemErrorMessage( "Cannot save '%s' (%s) because the referenced entity '%s' in '%s' was not persisted yet.", entity, entity.getClass().getName(), ref.getValue(), getName()) .handle(); } }
protected SafeMap<?, ?> getMap(Object entity) { try { return (SafeMap<?, ?>) super.getValueFromField(entity); } catch (Exception e) { throw Exceptions.handle() .to(Mixing.LOG) .error(e) .withSystemErrorMessage( "Unable to obtain EntityRef object from entity ref field ('%s' in '%s'): %s (%s)", getName(), descriptor.getType().getName()) .handle(); } }
protected BaseEntityRefList<?, ?> getEntityRefList(Object entity) { try { return (BaseEntityRefList<?, ?>) super.getValueFromField(entity); } catch (Exception e) { throw Exceptions.handle() .to(Mixing.LOG) .error(e) .withSystemErrorMessage( "Unable to obtain EntityRef object from entity ref field ('%s' in '%s'): %s (%s)", getName(), descriptor.getType().getName()) .handle(); } }
protected NestedList<?> getNestedList(Object entity) { try { return (NestedList<?>) super.getValueFromField(entity); } catch (Exception e) { throw Exceptions.handle() .to(Mixing.LOG) .error(e) .withSystemErrorMessage( "Unable to obtain EntityRef object from entity ref field ('%s' in '%s'): %s (%s)", getName(), descriptor.getType().getName()) .handle(); } }
protected JSONObject extractErrorJSON(ResponseException e) { try { JSONObject response = JSON.parseObject(EntityUtils.toString(e.getResponse().getEntity())); return response.getJSONObject(PARAM_ERROR); } catch (IOException ex) { Exceptions.handle(Elastic.LOG, ex); throw Exceptions.handle() .to(Elastic.LOG) .error(e) .withSystemErrorMessage("Elasticsearch (%s) reported an error which cannot be unpacked: %s", e.getResponse().getHost()) .handle(); } }
private void executeInitialStatement(Database database, String initSql, Connection connection) { try (Statement stmt = connection.createStatement()) { stmt.execute(initSql); } catch (SQLException e) { Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage( "An error occured while executing the initial SQL statement for: %s (%s) - %s (%s)", database.getUrl(), initSql) .handle(); } }
private void safeCloseConnection(Connection connection) { try { connection.close(); } catch (SQLException e) { Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage("An exception occured while closing a database connection: %s (%s)") .handle(); } }
protected RequestBuilder execute(String uri) { try { return tryExecute(uri); } catch (OptimisticLockException e) { throw Exceptions.handle() .to(Elastic.LOG) .error(e) .withSystemErrorMessage("An unexpected optimitic locking error ocurred: %s") .handle(); } }
protected HandledException queryError(Compiler compiler, Exception e) { return Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage("Error executing query '%s' for type '%s': %s (%s)", compiler, descriptor.getType().getName()) .handle(); }
protected void failOnOverflow(List<E> result) { if (result.size() > MAX_LIST_SIZE) { throw Exceptions.handle() .to(Mixing.LOG) .withSystemErrorMessage("More than %s results were loaded into a list by executing: %s", MAX_LIST_SIZE, this) .handle(); } }
private void ensureReady() { if (!ready) { throw Exceptions.handle().to(LOG).withSystemErrorMessage("Index is not ready yet.").handle(); } }
protected Connection createConnection(String realm) { try { return oma.getDatabase(realm).getConnection(); } catch (SQLException e) { throw Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage("Failed to open a database connection for realm '%s': %s (%s)", realm) .handle(); } }
private static void stopLifecycle(Stoppable lifecycle) { LOG.INFO("Stopping: %s", lifecycle.getClass().getName()); try { lifecycle.stopped(); } catch (Exception e) { Exceptions.handle() .error(e) .to(LOG) .withSystemErrorMessage("Stop of: %s failed!", lifecycle.getClass().getName()) .handle(); } }
private static void verifyUniquenessOfName(String name) { if (caches.containsKey(name)) { throw Exceptions.handle() .to(LOG) .withSystemErrorMessage("A cache named '%s' has already been created!", name) .handle(); } }