narendly commented on a change in pull request #1169:
URL: https://github.com/apache/helix/pull/1169#discussion_r470080833



##########
File path: helix-core/src/main/java/org/apache/helix/task/TaskStateModel.java
##########
@@ -283,6 +287,68 @@ public void reset() {
     }
   }
 
+  /**
+   * Loads className using classLoader
+   * @param classLoader
+   * @param className
+   * @return Class className loaded by classLoader
+   */
+  private Class loadClass(URLClassLoader classLoader, String className) {
+    try {
+      return classLoader.loadClass(className);
+    } catch (ClassNotFoundException e) {
+      LOG.error("Failed to load Task class " + className + " for new task in 
instance " + _manager
+          .getInstanceName() + " in cluster " + _manager.getClusterName() + 
".");
+      throw new IllegalStateException("Class not found for task");
+    }
+  }
+
+  /**
+   * Loads Task and TaskFactory classes for command input from
+   * a JAR file, and registers the TaskFactory in _taskFactoryRegistry.
+   * @param command The command indicating what task to be loaded
+   */
+  private void loadNewTask(String command) {
+    // If the path for dynamic tasks doesn't exist, skip loading the task
+    if (!_manager.getHelixDataAccessor().getBaseDataAccessor()
+        .exists(TaskConstants.DYNAMICALLY_LOADED_TASK_PATH, 0)) {
+      return;
+    }
+
+    // Read DynamicTaskConfig containing task definition information.
+    DynamicTaskConfig taskConfig = new DynamicTaskConfig(
+        _manager.getHelixDataAccessor().getBaseDataAccessor()
+            .get(TaskConstants.DYNAMICALLY_LOADED_TASK_PATH + "/" + command, 
null, 0));
+    if (taskConfig.getTaskConfigZNRecord() == null) {
+      LOG.error("Failed to read ZNRecord for task " + command + " for instance 
" + _manager
+          .getInstanceName() + " in cluster " + _manager.getClusterName() + 
".");
+      throw new IllegalArgumentException("No ZNRecord for task " + command);
+    }
+
+    // Open the JAR file containing Task(s) and TaskFactory classes.
+    JarLoader jarLoader = new LocalJarLoader();
+    URL taskJarUrl = jarLoader.loadJar(taskConfig.getJarFilePath());
+
+    // Import Task(s) class(es).
+    URLClassLoader classLoader = URLClassLoader.newInstance(new 
URL[]{taskJarUrl});
+    for (String taskClass : taskConfig.getTaskClassesFqns()) {
+      loadClass(classLoader, taskClass);
+    }
+
+    try {
+      // Import and instantiate TaskFactory class.
+      TaskFactory taskFactory =
+          (TaskFactory) loadClass(classLoader, 
taskConfig.getTaskFactoryFqn()).newInstance();
+
+      // Register the TaskFactory.
+      _taskFactoryRegistry.put(command, taskFactory);

Review comment:
       nit: this piece of code could sit outside the try-catch?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to