3434import java .net .NetworkInterface ;
3535import java .net .SocketException ;
3636import java .util .*;
37+ import java .util .concurrent .*;
3738import java .util .logging .Handler ;
3839
3940/**
@@ -56,6 +57,8 @@ public class ShowPackageTool {
5657 private static ApiLoginResponse loginResponse ;
5758 private static JSONObject allTypes = null ;
5859
60+ private static final int NUMBER_OF_EXECUTORS = 2 ;
61+
5962 private static final String TYPE = "type" ;
6063 private static final String UNDEFINED = "undefined" ;
6164
@@ -749,22 +752,42 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
749752 }
750753
751754 if (totalObjects > 0 ) {
752- int offset = 0 ;
755+ final ExecutorService executorService = Executors .newFixedThreadPool (NUMBER_OF_EXECUTORS );
756+ List <Callable <ApiResponse >> tasks = new ArrayList <>(totalObjects / limit );
753757
754- List < JSONObject > chunks = new ArrayList <>( totalObjects / limit ) ;
758+ int offset = 0 ;
755759 while (offset < totalObjects ) {
756760 JSONObject payload = new JSONObject (payloadTemplate );
757761 payload .put ("offset" , offset );
758762 payload .put ("limit" , limit );
759- chunks .add (payload );
763+ tasks .add (new ApiCallTask ( command , payload ) );
760764
761765 offset += limit ;
762766 }
763767
764768 try {
769+ configuration .getLogger ().debug ("Command [" + command + "] uid " + payloadTemplate .get ("uid" )
770+ + " : Starting execution of " + tasks .size () + " tasks (with " + NUMBER_OF_EXECUTORS + " executor(s))" );
771+
772+ final List <Future <ApiResponse >> futures = executorService .invokeAll (tasks );
773+ executorService .shutdown ();
774+
775+ boolean serviceTerminatedSuccessfully = executorService .awaitTermination (3 , TimeUnit .HOURS );
776+
777+ if (!serviceTerminatedSuccessfully ) {
778+ configuration .getLogger ().severe ("Failed to run show rulebase (" + layer .getName () + "). Timeout after 3 hours." );
779+ configuration .getLogger ().debug ("Following the error, creating an empty html file for layer: '"
780+ + layer .getName () + "'" );
781+ writeRulebase (layer .getName (), packageName , rulebaseType , layer .getDomain (), inlineLayers , true );
782+ return false ;
783+ }
784+
785+ configuration .getLogger ().debug ("Command [" + command + "] uid " + payloadTemplate .get ("uid" ) + " : Finished execution of " + tasks .size () + " tasks" );
786+
765787 JSONArray rulebases = new JSONArray ();
766- for (JSONObject chunk : chunks ) {
767- res = client .apiCall (loginResponse , command , chunk );
788+
789+ for (Future <ApiResponse > f : futures ) {
790+ res = f .get ();
768791
769792 JSONArray jsonArrayOfObjectDictionary = (JSONArray )res .getPayload ().get ("objects-dictionary" );
770793 addObjectsInfoIntoCollections (jsonArrayOfObjectDictionary );
@@ -786,7 +809,6 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
786809 // firstNew is deleted
787810 currentRulebase .remove (0 );
788811 }
789-
790812 }
791813
792814 rulebases .addAll (currentRulebase );
@@ -806,7 +828,7 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
806828
807829 inlineLayers .addAll (addRulebase (rulebases , types , rulebaseType ));
808830 }
809- catch (ApiClientException e ) {
831+ catch (InterruptedException | ExecutionException e ) {
810832 handleException (e , "Failed to run show rulebase (" + layer .getName () + ")" );
811833 configuration .getLogger ().debug ("Following the error, creating an empty html file for layer: '"
812834 + layer .getName () + "'" );
@@ -815,10 +837,10 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
815837 }
816838 }
817839
818-
819840 configuration .getLogger ().debug ("Found " + totalObjects + " rules in : '" + layer .getName () + "'" );
820841 configuration .getLogger ().debug ("Found " + inlineLayers .size () + " inline layer(s)" );
821842 configuration .getLogger ().debug ("Creating html file for layer: '" + layer .getName () + "'" );
843+
822844 boolean writeRulebaseResult = writeRulebase (layer .getName (), packageName , rulebaseType ,
823845 layer .getDomain (), inlineLayers , false );
824846
@@ -840,6 +862,7 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
840862 configuration .getLogger ().warning ("Failed to create inline-layer, name: '" + inlineLayer .getName () + "'" );
841863 }
842864 }
865+
843866 configuration .getLogger ().info ("Done handling rulebase '" + layer .getName () + "'" );
844867
845868 return writeRulebaseResult ;
@@ -1623,7 +1646,37 @@ private static void freeResources(){
16231646 handle .close ();
16241647 }
16251648 }
1626- }
16271649
1650+ private static class ApiCallTask implements Callable <ApiResponse > {
1651+
1652+ private JSONObject payload ;
1653+ private String command ;
16281654
1655+ ApiCallTask (String command , JSONObject payload )
1656+ {
1657+ this .payload = payload ;
1658+ this .command = command ;
1659+ }
16291660
1661+ @ Override
1662+ public ApiResponse call ()
1663+ {
1664+ ApiResponse res ;
1665+ try {
1666+ res = client .apiCall (loginResponse , command , payload );
1667+ }
1668+ catch (Exception e ) {
1669+ res = null ;
1670+ }
1671+
1672+ if (res == null || !res .isSuccess ()) {
1673+ res = null ;
1674+ }
1675+
1676+ String log = "Command [" + command + "] uid " + payload .get ("uid" ) + " limit " + payload .get ("limit" ) + " offset " + payload .get ("offset" ) + " " ;
1677+ configuration .getLogger ().debug (log + (res == null ? "FAILED" : "SUCCESSFUL" ));
1678+
1679+ return res ;
1680+ }
1681+ }
1682+ }
0 commit comments