protected Object create(TypeToken<?> type) { Class<?> rawType = type.getRawType(); Instantiator<?> creator = creators.get(rawType); if (creator == null) { creator = creatorFactory.get(type); creators.put(rawType, creator); } return creator.create(); }
protected Object create(TypeToken<?> type) { Class<?> rawType = type.getRawType(); Instantiator<?> creator = creators.get(rawType); if (creator == null) { creator = creatorFactory.get(type); creators.put(rawType, creator); } return creator.create(); }
@Override public Handler get() { // we don't instantiate the handler class via injection, to avoid giving it access to objects bound in guice, // such as SConfiguration return new InstantiatorFactory(false).get(TypeToken.of(handlerClass)).create(); } }
T plugin = (T) instantiatorFactory.get(pluginType).create(); configField.setAccessible(true); configField.set(plugin, config);
/** * Create a new instance of plugin class without any config, config will be null in the instantiated plugin. * @param plugin information about the plugin * @param <T> Type of plugin * @return a new plugin instance */ public <T> T newInstanceWithoutConfig(Plugin plugin) throws IOException, ClassNotFoundException { ClassLoader pluginClassLoader = getPluginClassLoader(plugin); @SuppressWarnings("unchecked") Class<T> pluginClassLoaded = (Class<T>) pluginClassLoader.loadClass(plugin.getPluginClass().getClassName()); return instantiatorFactory.get(TypeToken.of(pluginClassLoaded)).create(); }
/** * Create a new instance of plugin class without any config, config will be null in the instantiated plugin. * @param plugin information about the plugin * @param <T> Type of plugin * @return a new plugin instance */ public <T> T newInstanceWithoutConfig(Plugin plugin) throws IOException, ClassNotFoundException { ClassLoader pluginClassLoader = getPluginClassLoader(plugin); @SuppressWarnings("unchecked") Class<T> pluginClassLoaded = (Class<T>) pluginClassLoader.loadClass(plugin.getPluginClass().getClassName()); return instantiatorFactory.get(TypeToken.of(pluginClassLoaded)).create(); }
T plugin = (T) instantiatorFactory.get(pluginType).create(); configField.setAccessible(true); configField.set(plugin, config);
@SuppressWarnings("unchecked") private CustomAction createCustomAction(BasicCustomActionContext context, InstantiatorFactory instantiator, ClassLoader classLoader) throws Exception { Class<?> clz = Class.forName(context.getSpecification().getClassName(), true, classLoader); Preconditions.checkArgument(CustomAction.class.isAssignableFrom(clz), "%s is not a CustomAction.", clz); CustomAction action = instantiator.get(TypeToken.of((Class<? extends CustomAction>) clz)).create(); Reflections.visit(action, action.getClass(), new PropertyFieldSetter(context.getSpecification().getProperties()), new DataSetFieldSetter(context), new MetricsFieldSetter(context.getMetrics())); return action; }
@SuppressWarnings("unchecked") private CustomAction createCustomAction(BasicCustomActionContext context, InstantiatorFactory instantiator, ClassLoader classLoader) throws Exception { Class<?> clz = Class.forName(context.getSpecification().getClassName(), true, classLoader); Preconditions.checkArgument(CustomAction.class.isAssignableFrom(clz), "%s is not a CustomAction.", clz); CustomAction action = instantiator.get(TypeToken.of((Class<? extends CustomAction>) clz)).create(); Reflections.visit(action, action.getClass(), new PropertyFieldSetter(context.getSpecification().getProperties()), new DataSetFieldSetter(context), new MetricsFieldSetter(context.getMetrics())); return action; }
DynamicPartitionerWriterWrapper(TaskAttemptContext job) { this.job = job; Configuration configuration = job.getConfiguration(); Class<? extends DynamicPartitioner> partitionerClass = configuration .getClass(PartitionedFileSetArguments.DYNAMIC_PARTITIONER_CLASS_NAME, null, DynamicPartitioner.class); this.dynamicPartitioner = new InstantiatorFactory(false).get(TypeToken.of(partitionerClass)).create(); this.partitionWriteOption = DynamicPartitioner.PartitionWriteOption.valueOf( configuration.get(PartitionedFileSetArguments.DYNAMIC_PARTITIONER_WRITE_OPTION)); MapReduceClassLoader classLoader = MapReduceClassLoader.getFromConfiguration(configuration); this.taskContext = classLoader.getTaskContextProvider().get(job); // name the output file 'part-<RunId>-m-00000' instead of 'part-m-00000' String outputName = DynamicPartitioningOutputFormat.getOutputName(job); if (partitionWriteOption == DynamicPartitioner.PartitionWriteOption.CREATE_OR_APPEND) { outputName = outputName + "-" + taskContext.getProgramRunId().getRun(); } this.outputName = outputName; String outputDatasetName = configuration.get(Constants.Dataset.Partitioned.HCONF_ATTR_OUTPUT_DATASET); this.outputDataset = taskContext.getDataset(outputDatasetName); this.partitioning = outputDataset.getPartitioning(); this.dynamicPartitioner.initialize(taskContext); this.fileOutputFormatName = job.getConfiguration() .getClass(Constants.Dataset.Partitioned.HCONF_ATTR_OUTPUT_FORMAT_CLASS_NAME, null, FileOutputFormat.class) .getName(); }
DynamicPartitionerWriterWrapper(TaskAttemptContext job) { this.job = job; Configuration configuration = job.getConfiguration(); Class<? extends DynamicPartitioner> partitionerClass = configuration .getClass(PartitionedFileSetArguments.DYNAMIC_PARTITIONER_CLASS_NAME, null, DynamicPartitioner.class); this.dynamicPartitioner = new InstantiatorFactory(false).get(TypeToken.of(partitionerClass)).create(); this.partitionWriteOption = DynamicPartitioner.PartitionWriteOption.valueOf( configuration.get(PartitionedFileSetArguments.DYNAMIC_PARTITIONER_WRITE_OPTION)); MapReduceClassLoader classLoader = MapReduceClassLoader.getFromConfiguration(configuration); this.taskContext = classLoader.getTaskContextProvider().get(job); // name the output file 'part-<RunId>-m-00000' instead of 'part-m-00000' String outputName = DynamicPartitioningOutputFormat.getOutputName(job); if (partitionWriteOption == DynamicPartitioner.PartitionWriteOption.CREATE_OR_APPEND) { outputName = outputName + "-" + taskContext.getProgramRunId().getRun(); } this.outputName = outputName; String outputDatasetName = configuration.get(Constants.Dataset.Partitioned.HCONF_ATTR_OUTPUT_DATASET); this.outputDataset = taskContext.getDataset(outputDatasetName); this.partitioning = outputDataset.getPartitioning(); this.dynamicPartitioner.initialize(taskContext); this.fileOutputFormatName = job.getConfiguration() .getClass(Constants.Dataset.Partitioned.HCONF_ATTR_OUTPUT_FORMAT_CLASS_NAME, null, FileOutputFormat.class) .getName(); }
Authorizer authorizer; try { authorizer = instantiatorFactory.get(TypeToken.of(authorizerClass)).create(); } catch (Exception e) { throw new InvalidAuthorizerException(
@SuppressWarnings("unchecked") private Workflow initializeWorkflow() throws Exception { Class<?> clz = Class.forName(workflowSpec.getClassName(), true, program.getClassLoader()); if (!Workflow.class.isAssignableFrom(clz)) { throw new IllegalStateException(String.format("%s is not Workflow.", clz)); } Class<? extends Workflow> workflowClass = (Class<? extends Workflow>) clz; final Workflow workflow = new InstantiatorFactory(false).get(TypeToken.of(workflowClass)).create(); // set metrics Reflections.visit(workflow, workflow.getClass(), new MetricsFieldSetter(workflowContext.getMetrics())); if (!(workflow instanceof ProgramLifecycle)) { return workflow; } final TransactionControl txControl = Transactions.getTransactionControl(workflowContext.getDefaultTxControl(), Workflow.class, workflow, "initialize", WorkflowContext.class); basicWorkflowToken.setCurrentNode(workflowSpec.getName()); workflowContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null)); workflowContext.initializeProgram((ProgramLifecycle) workflow, txControl, false); workflowStateWriter.setWorkflowToken(workflowRunId, basicWorkflowToken); return workflow; }
@SuppressWarnings("unchecked") private Workflow initializeWorkflow() throws Exception { Class<?> clz = Class.forName(workflowSpec.getClassName(), true, program.getClassLoader()); if (!Workflow.class.isAssignableFrom(clz)) { throw new IllegalStateException(String.format("%s is not Workflow.", clz)); } Class<? extends Workflow> workflowClass = (Class<? extends Workflow>) clz; final Workflow workflow = new InstantiatorFactory(false).get(TypeToken.of(workflowClass)).create(); // set metrics Reflections.visit(workflow, workflow.getClass(), new MetricsFieldSetter(workflowContext.getMetrics())); if (!(workflow instanceof ProgramLifecycle)) { return workflow; } final TransactionControl txControl = Transactions.getTransactionControl(workflowContext.getDefaultTxControl(), Workflow.class, workflow, "initialize", WorkflowContext.class); basicWorkflowToken.setCurrentNode(workflowSpec.getName()); workflowContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null)); workflowContext.initializeProgram((ProgramLifecycle) workflow, txControl, false); workflowStateWriter.setWorkflowToken(workflowRunId, basicWorkflowToken); return workflow; }
@Test public void testUnsafe() { Record record = new InstantiatorFactory(false).get(TypeToken.of(Record.class)).create(); Reflections.visit(record, Record.class, new FieldVisitor() { @Override public void visit(Object instance, Type inspectType, Type declareType, Field field) throws Exception { if (!Modifier.isStatic(field.getModifiers())) { Assert.assertEquals(Defaults.defaultValue(field.getType()), field.get(instance)); } } }); }
spark = new InstantiatorFactory(false).get(TypeToken.of(program.<Spark>getMainClass())).create(); } catch (Exception e) { LOG.error("Failed to instantiate Spark class for {}", spec.getClassName(), e);
@Override protected void startUp() throws Exception { LoggingContextAccessor.setLoggingContext(context.getLoggingContext()); // Instantiate worker instance Class<?> workerClass = program.getClassLoader().loadClass(spec.getClassName()); @SuppressWarnings("unchecked") TypeToken<Worker> workerType = (TypeToken<Worker>) TypeToken.of(workerClass); worker = new InstantiatorFactory(false).get(workerType).create(); // Fields injection Reflections.visit(worker, workerType.getType(), new MetricsFieldSetter(context.getMetrics()), new PropertyFieldSetter(spec.getProperties())); LOG.debug("Starting Worker Program {}", program.getId()); // Initialize worker // Worker is always using Explicit transaction TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.EXPLICIT, Worker.class, worker, "initialize", WorkerContext.class); context.initializeProgram(worker, txControl, false); }
@Override protected void startUp() throws Exception { LoggingContextAccessor.setLoggingContext(context.getLoggingContext()); // Instantiate worker instance Class<?> workerClass = program.getClassLoader().loadClass(spec.getClassName()); @SuppressWarnings("unchecked") TypeToken<Worker> workerType = (TypeToken<Worker>) TypeToken.of(workerClass); worker = new InstantiatorFactory(false).get(workerType).create(); // Fields injection Reflections.visit(worker, workerType.getType(), new MetricsFieldSetter(context.getMetrics()), new PropertyFieldSetter(spec.getProperties())); LOG.debug("Starting Worker Program {}", program.getId()); // Initialize worker // Worker is always using Explicit transaction TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.EXPLICIT, Worker.class, worker, "initialize", WorkerContext.class); context.initializeProgram(worker, txControl, false); }