private Logger getRootLogger() throws JPServiceException { try { return (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); } catch (Exception ex) { throw new JPServiceException("RootLogger is not available!", ex); } }
public static void printError(String message, Throwable cause) { printError(new JPServiceException(message, cause)); }
private static List<AbstractJavaProperty> loadAllProperties(final boolean errorReport) throws JPServiceException { List<AbstractJavaProperty> properties = new ArrayList<>(); Collection<Class<? extends AbstractJavaProperty>> currentlyregisteredPropertyClasses = new HashSet(registeredPropertyClasses); boolean modification = true; // load recursive all properties which are not already loaded. while (modification) { modification = false; for (Class<? extends AbstractJavaProperty> propertyClass : currentlyregisteredPropertyClasses) { try { properties.add(getProperty(propertyClass)); } catch (Exception ex) { if (errorReport) { throw new JPServiceException("Could not load Property[" + propertyClass.getSimpleName() + "]!", ex); } else { LOGGER.debug("Could not load Property[" + propertyClass.getSimpleName() + "]!", ex); } } } } return properties; }
/** * @param property * * @throws JPServiceException */ @SuppressWarnings("unchecked") private static void loadProperty(final AbstractJavaProperty property) throws JPServiceException { try { if (loadedProperties.containsKey(property.getClass()) && !loadedProperties.get(property.getClass()).neetToBeParsed()) { return; } parseProperty(property); if (overwrittenDefaultValueMap.containsKey(property.getClass())) { property.overwriteDefaultValue(overwrittenDefaultValueMap.get(property.getClass())); } property.updateValue(); property.validate(); } catch (JPBadArgumentException | JPValidationException ex) { throw new JPServiceException("Could not load " + property + "!", ex); } loadedProperties.put(property.getClass(), property); try { property.loadAction(); } catch (Throwable ex) { throw new JPServiceException("Could not load Property[" + property.getClass().getSimpleName() + "] action!", ex); } }
/** * @param propertyClass * * @return * * @throws JPServiceException */ private static synchronized AbstractJavaProperty initProperty(Class<? extends AbstractJavaProperty> propertyClass) throws JPServiceException { try { // Avoid double initialization if (initializedProperties.containsKey(propertyClass)) { throw new JPServiceException("Already initialized!"); } if (!registeredPropertyClasses.contains(propertyClass)) { registeredPropertyClasses.add(propertyClass); } AbstractJavaProperty newInstance = propertyClass.newInstance(); initializedProperties.put(propertyClass, newInstance); // init load dependencies. for (Class<? extends AbstractJavaProperty> dependentPropertyClass : (List<Class<? extends AbstractJavaProperty>>) newInstance.getDependencyList()) { if (!initializedProperties.containsKey(dependentPropertyClass)) { initProperty(dependentPropertyClass); } } return newInstance; } catch (JPServiceException | InstantiationException | IllegalAccessException ex) { throw new JPInitializationException("Could not init " + propertyClass.getSimpleName(), ex); } }
/** * Returns the property related to the given {@code propertyClass}. * <p> * If the property is never registered but the class is known in the classpath, the method returns the default value. * * @param <C> the property type. * @param propertyClass property class which defines the property. * * @return the property. * * @throws org.openbase.jps.exception.JPNotAvailableException thrown if the given property could not be found. */ public static synchronized <C extends AbstractJavaProperty> C getProperty(Class<C> propertyClass) throws JPNotAvailableException { try { if (propertyClass == null) { throw new JPNotAvailableException(propertyClass, new JPServiceException("Given propertyClass is a Nullpointer!")); } // load if not already done. if (!loadedProperties.containsKey(propertyClass) || loadedProperties.get(propertyClass).neetToBeParsed()) { // init if not already done. if (!initializedProperties.containsKey(propertyClass)) { initProperty(propertyClass); } loadProperty(initializedProperties.get(propertyClass)); } return (C) loadedProperties.get(propertyClass); } catch (JPServiceException ex) { throw new JPNotAvailableException(propertyClass, ex); } }
/** * Analyze the input arguments and setup all registered Properties. * <p> * Make sure all desired properties are registered before calling this method. Otherwise the properties will not be listed in the help screen. * * @param args Arguments given by the main method. * * @throws JPServiceException */ public static void parse(final String[] args) throws JPServiceException { argumentsAnalyzed = true; try { printValueModification(args); initRegisteredProperties(args); } catch (Exception ex) { throw new JPServiceException("Could not analyse arguments: " + ex.getMessage(), ex); } handleHelpCall(); }
@Override protected void setValue(File value, ValueType valueType) { // if file is already referred absolute than use it and ignore parent directory. if (value.isAbsolute()) { super.setValue(value, valueType); return; } try { File parent = getParentDirectory(); // if parent file is not defined just use the value directly if (parent == null) { super.setValue(value, valueType); return; } // build value with parent folder super.setValue(new File(parent, value.getPath()), valueType); } catch (JPServiceException ex) { // ignore value because parent seems to be invalid and value itself is not absolute! addErrorReport(new JPServiceException("Parent directory is not valid!", ex), valueType); } }
throw new JPServiceException("Could not init registered properties!", ex);
/** * Generate help page. * * @throws JPServiceException is thrown if something went wrong. */ private static void handleHelpCall() throws JPServiceException { try { if (JPService.getProperty(JPHelp.class).isIdentified()) { try { JPService.printHelp(); } catch (Exception ex) { LOGGER.error("Could not fully generate help page!", ex); } if (!JPService.testMode()) { System.exit(0); } } } catch (JPServiceException ex) { throw new JPServiceException("Could not generate help page!", ex); } }
throw new JPServiceException("Could not parse Argument[" + arg + "]!", ex);
@Override public File getParentDirectory() throws JPServiceException { if (JPService.getProperty(JPBCOVarDirectory.class).getValue().exists() || JPService.testMode()) { return JPService.getProperty(JPBCOVarDirectory.class).getValue(); } throw new JPServiceException("Could not auto detect bco var path!"); }
/** * Setup JPService for JUnitTests By using the JPService during JUnit Tests it's recommended to call this method after property registration instead using the parsing methods because command line * property handling makes no sense in the context of unit tests.. * <p> * The following properties are activated by default while running JPService in TestMode: * <p> * - JPVerbose is set to true to print more debug messages. * <p> * - JPTestMode is activated. * * @throws JPServiceException */ public static void setupJUnitTestMode() throws JPServiceException { try { registerProperty(JPVerbose.class, true); registerProperty(JPTestMode.class, true); initRegisteredProperties(); } catch (JPValidationException ex) { throw new JPServiceException("Could not setup JPService for UnitTestMode!", ex); } }
throw new JPServiceException("Could not auto detect database location!");