private void configureVersion(ServiceProfile<?> profile) { assert profile != null; this.version = profile.getConfiguration(KEY_VERSION, true, false); }
private File prepareDirectory(ServiceProfile<?> profile) throws IOException { assert profile != null; String path = profile.getConfiguration(KEY_DIRECTORY, true, true); File dir = new File(path); if (dir.isDirectory() == false && dir.mkdirs() == false) { throw new IOException(MessageFormat.format( "Failed to prepare lock directory: {0}", dir.getAbsolutePath())); } return dir; }
/** * Merges this profile into the specified properties. * If properties already contains entries related to this profile, * then this method will overwrite them. * @param properties target properties * @throws IllegalArgumentException if some parameters were {@code null} */ public void storeTo(Properties properties) { if (properties == null) { throw new IllegalArgumentException("properties must not be null"); //$NON-NLS-1$ } properties.setProperty(prefix, getServiceClass().getName()); for (Map.Entry<String, String> entry : getConfiguration().entrySet()) { properties.setProperty(prefix + '.' + entry.getKey(), entry.getValue()); } } }
/** * Returns the target configuration. * @param key the configuration key * @param mandatory whether the configuration is mandatory * @param resolve whether resolves the configuration * @return the corresponded configuration, or {@code null} if is not defined/empty * @throws IllegalArgumentException if some parameters were {@code null} * @since 0.4.0 */ public String getConfiguration(String key, boolean mandatory, boolean resolve) { if (key == null) { throw new IllegalArgumentException("key must not be null"); //$NON-NLS-1$ } String value = getConfiguration().get(key); return normalize(key, value, mandatory, resolve); }
private File getConfDirectory(ServiceProfile<?> profile) { assert profile != null; String value = profile.getConfiguration(KEY_DIRECTORY, true, true); File dir = new File(value); if (dir.exists() == false) { YSLOG.info("I00001", profile.getPrefix(), KEY_DIRECTORY, value); } return dir; }
private boolean extractBoolean(ServiceProfile<?> profile, String key, boolean defaultValue) throws IOException { assert profile != null; assert key != null; String string = profile.getConfiguration(key, false, true); if (string == null) { return defaultValue; } string = string.trim(); if (string.isEmpty()) { return defaultValue; } try { return Boolean.parseBoolean(string); } catch (RuntimeException e) { throw new IOException(MessageFormat.format( "Failed to resolve boolean value ({0}={1})", profile.getPrefix() + '.' + key, string), e); } }
private void configureResourceId(ServiceProfile<?> profile) { assert profile != null; String override = profile.getConfiguration(ExecutionScriptHandler.KEY_RESOURCE, false, true); if (override == null) { LOG.debug("resourceId is not override in {}", profile.getPrefix()); resourceId = ExecutionScriptHandler.DEFAULT_RESOURCE_ID; } else { LOG.debug("resourceId is overriden in {}: {}", profile.getPrefix(), override); resourceId = override; } }
private void configureStepUnit(ServiceProfile<?> profile) throws IOException { assert profile != null; String stepUnitString = profile.getConfiguration(KEY_STEP_UNIT, false, true); if (stepUnitString == null) { LOG.debug("{} is not defined in {}", KEY_STEP_UNIT, profile.getPrefix()); } else { try { stepUnit = Double.parseDouble(stepUnitString); } catch (NumberFormatException e) { throw new IOException(MessageFormat.format( "{0}.{1} must be a number: {2}", profile.getPrefix(), KEY_STEP_UNIT, stepUnitString)); } } }
private List<String> extractCommand(ServiceProfile<?> profile, String prefix) throws IOException { try { return ProcessUtil.extractCommandLineTokens( prefix, profile.getConfiguration(), profile.getContext().getContextParameters()); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( "Failed to resolve command line tokens ({0})", profile.getPrefix() + '.' + prefix + '*'), e); } }
private void configureScope(ServiceProfile<?> profile) throws IOException { assert profile != null; String scopeSymbol = profile.getConfiguration(KEY_SCOPE, false, true); if (scopeSymbol == null) { scope = ExecutionLock.Scope.getDefault(); LOG.debug("Lock scope is not defined, use default: {}", scope.getSymbol()); } else { scope = ExecutionLock.Scope.findFromSymbol(scopeSymbol); if (scope == null) { throw new IOException(MessageFormat.format( "Unknown lock scope in \"{0}.{1}\": {2}", profile.getPrefix(), KEY_SCOPE, scopeSymbol)); } } }
private List<String> extractCommand( ServiceProfile<?> profile, String prefix) throws IOException { try { return ProcessUtil.extractCommandLineTokens( prefix, profile.getConfiguration(), profile.getContext().getContextParameters()); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( "Failed to resolve command line tokens ({0})", profile.getPrefix() + '.' + prefix + '*'), e); } }
@Override public void configure(ServiceProfile<?> profile) throws IOException, InterruptedException { this.prefix = profile.getPrefix(); try { this.confDirectory = getConfDirectory(profile); this.delegations = getDelegations(profile); this.forceSetUp = profile.getConfiguration(KEY_SETUP, false, true); this.forceCleanUp = profile.getConfiguration(KEY_CLEANUP, false, true); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( "Failed to configure \"{0}\" ({1})", profile.getPrefix(), profile.getServiceClass().getName()), e); } if (forceSetUp != null && delegations.containsKey(forceSetUp) == false) { throw new IOException(MessageFormat.format( "Failed to detect setUp target: \"{2}\" in {0}.{1}", profile.getPrefix(), KEY_SETUP, forceSetUp)); } if (forceCleanUp != null && delegations.containsKey(forceCleanUp) == false) { throw new IOException(MessageFormat.format( "Failed to detect cleanUp target: \"{2}\" in {0}.{1}", profile.getPrefix(), KEY_CLEANUP, forceCleanUp)); } }
@Override protected void doConfigure(ServiceProfile<?> profile) throws InterruptedException, IOException { try { this.executor = ParallelJobExecutor.extract( profile.getPrefix(), profile.getConfiguration(), profile.getContext().getContextParameters()); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( "Failed to configure job scheduler: {0}", profile.getPrefix()), e); } }
private void checkCleanupConfigurations(ServiceProfile<?> profile) throws IOException { assert profile != null; String workingDirectory = profile.getConfiguration().get(KEY_WORKING_DIRECTORY); if (workingDirectory != null) { YSLOG.warn("W10001", profile.getPrefix(), KEY_WORKING_DIRECTORY, KEY_CLEANUP); } List<String> cleanupPrefix = extractCommand(profile, ProcessUtil.PREFIX_CLEANUP); if (cleanupPrefix.isEmpty() == false) { YSLOG.warn("W10001", profile.getPrefix(), ProcessUtil.PREFIX_CLEANUP + "*", KEY_CLEANUP); } }
@Override protected void configureExtension(ServiceProfile<?> profile) throws InterruptedException, IOException { try { this.executor = JschProcessExecutor.extract( profile.getPrefix(), profile.getConfiguration(), profile.getContext().getContextParameters()); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( "Failed to configure SSH: {0}", profile.getPrefix()), e); } catch (JSchException e) { throw new IOException(MessageFormat.format( "Failed to configure SSH: {0}", profile.getPrefix()), e); } }
private Map<String, ExecutionScriptHandler<T>> getDelegations( ServiceProfile<?> profile) throws IOException, InterruptedException { assert profile != null; Map<String, String> conf = profile.getConfiguration(); Set<String> keys = PropertiesUtil.getChildKeys(conf, "", "."); keys.remove(PREFIX_CONF);
@Override protected void configureExtension(ServiceProfile<?> profile) throws InterruptedException, IOException { try { this.executor = JschProcessExecutor.extract( profile.getPrefix(), profile.getConfiguration(), profile.getContext().getContextParameters()); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( "Failed to configure SSH: {0}", profile.getPrefix()), e); } catch (JSchException e) { throw new IOException(MessageFormat.format( "Failed to configure SSH: {0}", profile.getPrefix()), e); } }
private Map<String, String> getDesiredProperties(ServiceProfile<?> profile) throws IOException { assert profile != null; NavigableMap<String, String> vars = PropertiesUtil.createPrefixMap( profile.getConfiguration(), ExecutionScriptHandler.KEY_PROP_PREFIX); Map<String, String> resolved = new TreeMap<>();
private Map<String, String> getDesiredEnvironmentVariables(ServiceProfile<?> profile) throws IOException { assert profile != null; NavigableMap<String, String> vars = PropertiesUtil.createPrefixMap( profile.getConfiguration(), ExecutionScriptHandler.KEY_ENV_PREFIX); Map<String, String> resolved = new TreeMap<>();
throw new IllegalArgumentException("profile must not be null"); //$NON-NLS-1$ Map<String, String> conf = new HashMap<>(profile.getConfiguration()); conf.remove(ExecutionScriptHandler.KEY_RESOURCE); removeKeyPrefix(conf, ExecutionScriptHandler.KEY_PROP_PREFIX);