nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / utils / ParallelExecutor.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.utils;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
30
31 public abstract class ParallelExecutor<T> {
32         
33         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ParallelExecutor.class);
34
35         protected static abstract class ThreadOperation<T> {
36                 public abstract T execute(List<Object> parms);
37         }
38
39         protected abstract ThreadOperation<T> getThreadOperation();
40
41         private static class CallableOperationThread<T> implements Callable<T> {
42
43                 List<Object> parms;
44
45                 private ThreadOperation<T> operation;
46
47                 public CallableOperationThread(ThreadOperation<T> operation, List<Object> parms) {
48                         this.parms = parms;
49                         this.operation = operation;
50                 }
51
52                 @Override
53                 public T call() throws Exception {
54                         return this.operation.execute(this.parms);
55                 }
56
57         }
58
59         public List<T> performAllOperations(int ThreadPoolSize, List<List<Object>> listOfParms) {
60                 List<T> result = new ArrayList<T>();
61                 if (ThreadPoolSize > 0 && listOfParms != null) {
62                         ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize);
63                         List<Future<T>> list = new ArrayList<Future<T>>();
64                         for (List<Object> parms : listOfParms) {
65                                 CallableOperationThread<T> getter = new CallableOperationThread<T>(this.getThreadOperation(), parms);
66                                 Future<T> submit = executor.submit(getter);
67                                 list.add(submit);
68                         }
69                         for (Future<T> future : list) {
70                                 try {
71                                         if (future != null) {
72                                                 result.add(future.get());
73                                         }
74                                 } catch (Exception e) {
75                                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
76                                 }
77                         }
78                         executor.shutdown();
79                 }
80                 return result;
81         }
82
83 }