1 package org.openecomp.core.tools.commands;
3 import org.openecomp.core.tools.concurrent.ItemAddContributorsTask;
4 import org.openecomp.core.tools.exceptions.CommandExecutionRuntimeException;
5 import org.openecomp.core.tools.store.ItemHandler;
6 import org.openecomp.core.tools.store.NotificationHandler;
7 import org.openecomp.core.tools.store.PermissionHandler;
9 import java.io.IOException;
10 import java.nio.file.Files;
11 import java.nio.file.Paths;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.Future;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
21 public class AddContributorCommand {
24 private static final int DEFAULT_THREAD_NUMBER = 8;
25 private static final String ERROR_TRYING_TO_READ_FILE = "Error while trying to read item list";
26 private static final String COMMAND_ADD_CONTRIBUTOR_FAILED =
27 "Command AddContributor execution failed.";
29 private AddContributorCommand() {
30 // it's a utility class, prevent instantiation
33 public static void add(String itemListPath, String userListPath) {
35 List<String> itemList;
37 itemList = getItemList(itemListPath);
38 } catch (IOException e) {
39 throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE +
40 "from:" + itemListPath, e);
42 List<String> userList;
44 userList = load(userListPath).collect(Collectors.toList());
45 } catch (IOException e) {
46 throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE +
47 "from:" + userListPath, e);
50 List<ItemAddContributorsTask> tasks =
51 itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList());
53 ExecutorService executor = null;
56 executor = Executors.newFixedThreadPool(DEFAULT_THREAD_NUMBER);
57 executeAllTasks(executor, tasks);
58 } catch (InterruptedException e) {
59 throw new CommandExecutionRuntimeException(COMMAND_ADD_CONTRIBUTOR_FAILED, e);
61 if (executor != null) {
62 executor.shutdownNow();
67 private static List<String> getItemList(String itemListPath) throws IOException {
68 List<String> itemList;
69 if (itemListPath != null) {
70 itemList = load(itemListPath).collect(Collectors.toList());
72 itemList = new ItemHandler().getItemList();
78 private static void executeAllTasks(ExecutorService executor,
79 Collection<? extends Callable<String>> tasks)
80 throws InterruptedException {
81 List<Future<String>> futureTasks;
82 futureTasks = executor.invokeAll(tasks);
83 boolean isThreadOpen = true;
84 while (isThreadOpen) {
85 isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
92 private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
93 return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(),
97 private static Stream<String> load(String filePath)
99 return Files.lines(Paths.get(filePath));