@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 checkPrefix(ServiceProfile<?> profile, String prefix) { assert profile != null; assert prefix != null; if (profile.getPrefix().equals(prefix) == false) { throw new IllegalArgumentException(MessageFormat.format( "Profile \"{1}\" is invalid, this must be \"{0}\" ({2})", prefix, profile.getPrefix(), profile.getServiceClass().getName())); } }
/** * Creates a new instance. * The created service will automatically {@link Service#configure(ServiceProfile) configured} * by using this profile. * @param variables the variable resolver * @return the created instance. * @throws InterruptedException if interrupted in configuring the target service * @throws IOException if failed to create or configure the service * @deprecated use {@link #newInstance()} instead */ @Deprecated public T newInstance(VariableResolver variables) throws InterruptedException, IOException { if (variables == null) { throw new IllegalArgumentException("variables must not be null"); //$NON-NLS-1$ } return newInstance(); }
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; } }
/** * 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 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); throw new IOException(MessageFormat.format( "Default profile for multidispatch plugin is not defined: {0}.{1}", profile.getPrefix(), PREFIX_DEFAULT)); String key = profile.getPrefix() + "." + entry.getKey(); String value = entry.getValue(); properties.setProperty(key, value); String subPrefix = profile.getPrefix() + "." + key; ServiceProfile<? extends ExecutionScriptHandler<T>> subProfile; try { subProfile = ServiceProfile.load( properties, subPrefix, handlerKind, profile.getContext()); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( ExecutionScriptHandler<T> subInstance = subProfile.newInstance(); results.put(key, subInstance);
@Override public final void configure(ServiceProfile<?> profile) throws InterruptedException, IOException { try { doConfigure(profile); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( "Failed to configure \"{0}\" ({1})", profile.getPrefix(), profile.getPrefix()), 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)); } }
throw new IllegalArgumentException(MessageFormat.format( "The profile \"{0}\" must not be defined", getPrefix() + '.' + key)); } else { return null; if (resolve) { try { string = getContext().getContextParameters().replace(string, true); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(MessageFormat.format( "Failed to resolve the profile \"{0}\": {1}", getPrefix() + '.' + key, string), e); throw new IllegalArgumentException(MessageFormat.format( "The profile \"{0}\" must not be defined", getPrefix() + '.' + key)); } else { return null;
/** * 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()); } } }
private static long extractLong( ServiceProfile<?> profile, Map<String, String> conf, String key, long defaultValue) { assert profile != null; assert conf != null; assert key != null; String value = profile.normalize(key, conf.remove(key), false, true); if (value == null) { return defaultValue; } try { return Long.parseLong(value); } catch (RuntimeException e) { throw new IllegalArgumentException(MessageFormat.format( "{0}.{1} must be an integer ({2})", profile.getPrefix(), key, value)); } }
private void configureVersion(ServiceProfile<?> profile) { assert profile != null; this.version = profile.getConfiguration(KEY_VERSION, true, false); }
/** * Returns the class loader which loaded this service class. * @return the class loader * @deprecated use {@link #getContext()} instead */ @Deprecated public ClassLoader getClassLoader() { return getContext().getClassLoader(); }
ServiceProfile.load(properties, PREFIX_CORE, CoreProfile.class, context); ServiceProfile<ExecutionMonitorProvider> monitors = ServiceProfile.load(properties, PREFIX_MONITOR, ExecutionMonitorProvider.class, context); ServiceProfile<ExecutionLockProvider> locks = ServiceProfile.load(properties, PREFIX_LOCK, ExecutionLockProvider.class, context); ServiceProfile<JobScheduler> scheduler = ServiceProfile.load(properties, PREFIX_SCHEDULER, JobScheduler.class, context); ServiceProfile<HadoopScriptHandler> hadoopHandler = ServiceProfile.load(properties, PREFIX_HADOOP, HadoopScriptHandler.class, context); List<ServiceProfile<CommandScriptHandler>> commandHandlers = new ArrayList<>(); for (String commandHandlerPrefix : PropertiesUtil.getChildKeys(properties, GROUP_PREFIX_COMMAND, ".")) { ServiceProfile<CommandScriptHandler> profile = ServiceProfile.load(properties, commandHandlerPrefix, CommandScriptHandler.class, context); commandHandlers.add(profile);
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)); } } }
@Override public final void configure(ServiceProfile<?> profile) throws InterruptedException, IOException { try { doConfigure(profile); } catch (IllegalArgumentException e) { throw new IOException(MessageFormat.format( "Failed to configure \"{0}\" ({1})", profile.getPrefix(), profile.getPrefix()), e); } }
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; }
private static String resolve(ServiceProfile<?> profile, String key, String value) { assert profile != null; assert key != null; assert value != null; try { return profile.getContext().getContextParameters().replace(value, true); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(MessageFormat.format( "Failed to resolve {0} ({1})", key, value), e); } }
throw new IllegalArgumentException("classLoader must not be null"); //$NON-NLS-1$ return load(properties, prefix, serviceBaseClass, ProfileContext.system(classLoader));
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); } }