public static void main(final String[] args) throws BindException, InjectionException { final Tang tang = Tang.Factory.getTang(); final JavaConfigurationBuilder cb = tang.newConfigurationBuilder(); final Configuration conf = cb.build(); final Injector injector = tang.newInjector(conf); final TimerV1 timer = injector.getInstance(TimerV1.class); try { System.out.println("Tick..."); timer.sleep(); System.out.println("Tock."); } catch (final InterruptedException e) { e.printStackTrace(); } }
protected ConfigurationBuilderImpl() { this.namespace = Tang.Factory.getTang().getDefaultClassHierarchy(); }
/** * @return an {@link Injector} which has {@link LocalMessageDispatcher} for testing. * @throws InjectionException when failed to inject {@link LocalMessageDispatcher} */ public static Injector getInjector() throws InjectionException { return forkInjector(TANG.newInjector()); }
public CommandLine() { this.conf = Tang.Factory.getTang().newConfigurationBuilder(); }
/** * @return an {@link Injector} which has {@link LocalMessageDispatcher} for testing. * @throws InjectionException when failed to inject {@link LocalMessageDispatcher} */ public static Injector getInjector() throws InjectionException { return forkInjector(TANG.newInjector()); }
/** * Merge a set of Configurations. * * @param configurations the configuration to be merged * @return the merged configuration. * @throws org.apache.reef.tang.exceptions.BindException if the merge fails. */ public static Configuration merge(final Configuration... configurations) { return Tang.Factory.getTang().newConfigurationBuilder(configurations).build(); }
/** * Read json file and return its contents as configuration parameter. * * @param jobConf job configuration to get json path. * @param pathParameter named parameter represents path to the json file, or an empty string * @param contentsParameter named parameter represents contents of the file * @param defaultContent the default configuration * @return configuration with contents of the file, or an empty string as value for {@code contentsParameter} * @throws InjectionException exception while injection. */ private static Configuration getJSONConf(final Configuration jobConf, final Class<? extends Name<String>> pathParameter, final Class<? extends Name<String>> contentsParameter, final String defaultContent) throws InjectionException { final Injector injector = TANG.newInjector(jobConf); try { final String path = injector.getNamedInstance(pathParameter); final String contents = path.isEmpty() ? defaultContent : new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8); return TANG.newConfigurationBuilder() .bindNamedParameter(contentsParameter, contents) .build(); } catch (final IOException e) { throw new RuntimeException(e); } }
/** * Extracts a task id from the given configuration. * * @param config * @return the task id in the given configuration. * @throws RuntimeException if the configuration can't be parsed. */ public static String getTaskId(final Configuration config) { try { return Tang.Factory.getTang().newInjector(config).getNamedInstance(TaskConfigurationOptions.Identifier.class); } catch (final InjectionException ex) { throw new RuntimeException("Unable to determine task identifier. Giving up.", ex); } }
/** * Utility to build a ConfigurationBuilder from an Optional<Configuration>. */ private static ConfigurationBuilder getConfigurationBuilder(final Optional<Configuration> configuration) { if (configuration.isPresent()) { return Tang.Factory.getTang().newConfigurationBuilder(configuration.get()); } else { return Tang.Factory.getTang().newConfigurationBuilder(); } }
protected ConfigurationBuilderImpl(final URL[] jars, final Configuration[] confs, final Class<? extends ExternalConstructor<?>>[] parsers) throws BindException { this.namespace = Tang.Factory.getTang().getDefaultClassHierarchy(jars, parsers); for (final Configuration tc : confs) { addConfiguration((ConfigurationImpl) tc); } }
/** * Fetch scheduler configuration. * @param jobConf job configuration. * @return the scheduler configuration. * @throws ClassNotFoundException exception while finding the class. * @throws InjectionException exception while injection (REEF Tang). */ private static Configuration getSchedulerConf(final Configuration jobConf) throws ClassNotFoundException, InjectionException { final Injector injector = TANG.newInjector(jobConf); final String classImplName = injector.getNamedInstance(JobConf.SchedulerImplClassName.class); final JavaConfigurationBuilder jcb = Tang.Factory.getTang().newConfigurationBuilder(); final Class schedulerImpl = ((Class<Scheduler>) Class.forName(classImplName)); jcb.bindImplementation(Scheduler.class, schedulerImpl); return jcb.build(); }
/** * Run user-provided main method. * * @param jobConf the job configuration * @throws Exception on any exceptions on the way */ private static void runUserProgramMain(final Configuration jobConf) throws Exception { final Injector injector = TANG.newInjector(jobConf); final String className = injector.getNamedInstance(JobConf.UserMainClass.class); final String userArgsString = injector.getNamedInstance(JobConf.UserMainArguments.class); final String[] args = userArgsString.isEmpty() ? EMPTY_USER_ARGS : userArgsString.split(" "); final Class userCode = Class.forName(className); final Method method = userCode.getMethod("main", String[].class); if (!Modifier.isStatic(method.getModifiers())) { throw new RuntimeException("User Main Method not static"); } if (!Modifier.isPublic(userCode.getModifiers())) { throw new RuntimeException("User Main Class not public"); } LOG.info("User program started"); method.invoke(null, (Object) args); LOG.info("User program finished"); }
/** * Generates needed driver configuration such as class path provider. * * @return Instance of <code>Configuration</code> */ @Override public Configuration getMainConfiguration() { return Tang.Factory.getTang().newConfigurationBuilder().build(); } }
/** * Instantiate REEF Launcher. This method is called from REEFLauncher.main(). * @param clockConfigPath Path to the local file that contains serialized configuration * of a REEF component to launch (can be either Driver or Evaluator). * @return An instance of the configured REEFLauncher object. */ private static REEFLauncher getREEFLauncher(final String clockConfigPath) { try { final Configuration clockArgConfig = TANG.newConfigurationBuilder() .bindNamedParameter(ClockConfigurationPath.class, clockConfigPath) .build(); return TANG.newInjector(clockArgConfig).getInstance(REEFLauncher.class); } catch (final BindException ex) { throw fatal("Error in parsing the command line", ex); } catch (final InjectionException ex) { throw fatal("Unable to instantiate REEFLauncher.", ex); } }
/** * Extracts a context id from the given configuration. * * @param c * @return the context id in the given configuration. * @throws RuntimeException if the configuration can't be parsed. */ public static String getIdentifier(final Configuration c) { try { return Tang.Factory.getTang().newInjector(c).getNamedInstance( ContextIdentifier.class); } catch (final InjectionException e) { throw new RuntimeException("Unable to determine context identifier. Giving up.", e); } }
/** * Get driver message configuration. * * @return driver message configuration. * @throws InjectionException exception while injection. */ private static Configuration getDriverMessageConf() throws InjectionException { return TANG.newConfigurationBuilder() .bindNamedParameter(MessageParameters.SenderId.class, MessageEnvironment.MASTER_COMMUNICATION_ID) .build(); }
/** * @param args command line arguments. * @throws BindException configuration error. * @throws InjectionException configuration error. * @throws IOException cannot process command line parameters. */ public static void main(final String[] args) throws BindException, InjectionException, IOException { final Tang tang = Tang.Factory.getTang(); final ConfigurationBuilder confBuilder = tang.newConfigurationBuilder(); new CommandLine(confBuilder).processCommandLine(args); final Configuration config = confBuilder.build(); final Injector injector = tang.newInjector(config); final PrintTypeHierarchy myself = injector.getInstance(PrintTypeHierarchy.class); try (final Writer out = new OutputStreamWriter( new FileOutputStream("type-hierarchy.dot"), StandardCharsets.UTF_8)) { out.write(GraphvizConfigVisitor.getGraphvizString(config, true, true)); } final InjectionPlan<PrintTypeHierarchy> plan = injector.getInjectionPlan(PrintTypeHierarchy.class); try (final Writer out = new OutputStreamWriter( new FileOutputStream("injection-plan.dot"), StandardCharsets.UTF_8)) { out.write(GraphvizInjectionPlanVisitor.getGraphvizString(plan, true)); } System.out.println(myself); }
/** * Instantiate a launcher for the given Configuration. * * @param runtimeConfiguration the resourcemanager configuration to be used * @return a DriverLauncher based on the given resourcemanager configuration * @throws InjectionException on configuration errors */ public static DriverLauncher getLauncher(final Configuration runtimeConfiguration) throws InjectionException { return Tang.Factory.getTang() .newInjector(runtimeConfiguration, CLIENT_CONFIG) .getInstance(DriverLauncher.class); }
/** * Converts a given AvroConfiguration to Configuration. * * @param avroConfiguration a Avro configuration * @return a Configuration version of the given AvroConfiguration */ public Configuration fromAvro(final AvroConfiguration avroConfiguration) throws BindException { final ConfigurationBuilder configurationBuilder = Tang.Factory.getTang().newConfigurationBuilder(); fromAvro(avroConfiguration, configurationBuilder); return configurationBuilder.build(); }
public static void main(final String[] argv) throws IOException, BindException, InjectionException { @SuppressWarnings("unchecked") final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder( (Class<? extends ExternalConstructor<?>>[]) new Class[]{FileParser.class}); final CommandLine cl = new CommandLine(cb); cl.processCommandLine(argv, Target.class, ClassHierarchyIn.class, ConfigurationIn.class, ConfigurationOut.class); final ValidateConfiguration bip = Tang.Factory.getTang().newInjector(cb.build()).getInstance(ValidateConfiguration.class); bip.validatePlan(); }