HazelcastManager.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.pdd.core.controllo_traffico.policy.driver.hazelcast;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;

  28. import org.apache.commons.lang.StringUtils;
  29. import org.openspcoop2.core.controllo_traffico.driver.PolicyException;
  30. import org.openspcoop2.core.controllo_traffico.driver.PolicyGroupByActiveThreadsType;
  31. import org.openspcoop2.utils.Utilities;
  32. import org.openspcoop2.utils.json.JsonPathExpressionEngine;
  33. import org.openspcoop2.utils.json.JsonPathNotFoundException;
  34. import org.openspcoop2.utils.json.YAMLUtils;
  35. import org.openspcoop2.utils.resources.Charset;
  36. import org.openspcoop2.utils.resources.FileSystemUtilities;
  37. import org.slf4j.Logger;

  38. import com.fasterxml.jackson.databind.JsonNode;
  39. import com.hazelcast.config.Config;
  40. import com.hazelcast.config.InMemoryYamlConfig;
  41. import com.hazelcast.config.NetworkConfig;
  42. import com.hazelcast.config.YamlConfigBuilder;
  43. import com.hazelcast.core.Hazelcast;
  44. import com.hazelcast.core.HazelcastInstance;

  45. /**    
  46.  *  HazelcastManager
  47.  *
  48.  * @author Francesco Scarlato (scarlato@link.it)
  49.  * @author $Author$
  50.  * @version $Rev$, $Date$
  51.  */
  52. public class HazelcastManager {

  53.     private static String GOVWAY_INSTANCE_PORT = "GOVWAY_INSTANCE_PORT";
  54.    
  55.     public static Map<PolicyGroupByActiveThreadsType, HazelcastInstance> staticMapInstance = null;
  56.     private static Map<PolicyGroupByActiveThreadsType,String> staticMapConfig = null;
  57.     private static String groupId;
  58.    
  59.     private static YAMLUtils yamlUtils = null;
  60.     private static JsonPathExpressionEngine engine = null;
  61.    
  62.     private static JsonNode sharedConfigNode;
  63.     private static File shareConfigFile;
  64.    
  65.     private static Logger logStartup;
  66.     private static Logger log;
  67.    
  68.     public static synchronized void initialize(Logger logStartup, Logger log, Map<PolicyGroupByActiveThreadsType,String> config, String groupId, File shareConfigFile) throws Exception {
  69.        
  70.         if(HazelcastManager.staticMapInstance==null){
  71.        
  72.             /*
  73.              * This configuration disables the shutdown hook in hazelcast, which ensures that the hazelcast instance shuts down gracefully whenever the product node shuts down.
  74.              * If the hazelcast shutdown hook is enabled (which is the default behavior of a product),
  75.              * you will see errors such as " Hazelcast instance is not active! " at the time of shutting down the product node:
  76.              * This is because the hazelcast instance shuts down too early when the shutdown hook is enabled.
  77.              **/
  78.             System.setProperty("hazelcast.shutdownhook.enabled","false");
  79.    
  80.             /*
  81.              * This configuration sets the hazelcast logging type to log4j2, which allows hazelcast logs to be written to the govway_hazelcast.log file.
  82.              **/
  83.             System.setProperty("hazelcast.logging.type", "log4j2");
  84.            
  85.            
  86.             HazelcastManager.staticMapInstance = new HashMap<PolicyGroupByActiveThreadsType, HazelcastInstance>();
  87.             HazelcastManager.staticMapConfig = new HashMap<PolicyGroupByActiveThreadsType, String>();
  88.             HazelcastManager.groupId = groupId;
  89.             HazelcastManager.logStartup = logStartup;
  90.             HazelcastManager.log = log;
  91.                        
  92.             if(shareConfigFile!=null) {
  93.                
  94.                 HazelcastManager.shareConfigFile = shareConfigFile;
  95.                 HazelcastManager.yamlUtils = YAMLUtils.getInstance();
  96.                 HazelcastManager.engine = new JsonPathExpressionEngine();
  97.                
  98.                 String sharedContentBytes = FileSystemUtilities.readFile(shareConfigFile);
  99.                
  100.                 try {
  101.                     HazelcastManager.sharedConfigNode = yamlUtils.getAsNode(sharedContentBytes.getBytes());
  102.                 }catch(Throwable t) {
  103.                     throw new PolicyException("Configuration '"+shareConfigFile.getAbsolutePath()+"' is not valid yaml config: "+t.getMessage()+"\n"+sharedContentBytes,t);
  104.                 }
  105.                 Config sharedConfig = null;
  106.                 try {
  107.                     // La valido ma poi la ricostruiro' tutte le volte per poi sostituire la porta
  108.                     try(ByteArrayInputStream bin = new ByteArrayInputStream(sharedContentBytes.getBytes())){
  109.                         YamlConfigBuilder builder = new  YamlConfigBuilder(bin);
  110.                         sharedConfig = builder.build();
  111.                     }
  112.                 }catch(Throwable t) {
  113.                     throw new PolicyException("Configuration '"+shareConfigFile.getAbsolutePath()+"' is not valid yaml config (YamlConfigBuilder): "+t.getMessage()+"\n"+sharedContentBytes,t);
  114.                 }
  115.            
  116.                 if(sharedConfig!=null &&
  117.                         sharedConfig.getClusterName()!=null &&
  118.                         StringUtils.isNotEmpty(sharedConfig.getClusterName())) {
  119.                     // Se definita sovrascrive il valore indicato nella proprietà 'org.openspcoop2.pdd.controlloTraffico.gestorePolicy.inMemory.HAZELCAST.group_id' in govway_local.properties
  120.                     // passato come parametro 'groupId'
  121.                     HazelcastManager.groupId = sharedConfig.getClusterName();
  122.                 }
  123.             }
  124.            
  125.             if(config!=null && !config.isEmpty()) {
  126.                 for (PolicyGroupByActiveThreadsType type : config.keySet()) {
  127.                     String pathConfig = config.get(type);
  128.                     String content = null;
  129.                     if(pathConfig != null) {
  130.                         File hazelcastConfigFile = new File(pathConfig);
  131.                         if(!hazelcastConfigFile.exists()) {
  132.                             //consento di definire la proprietà senza che sia presente il file
  133.                             //throw new Exception("Hazelcast file config ["+hazelcastConfigFile.getAbsolutePath()+"] not exists");
  134.                         }
  135.                         else {
  136.                             if(!hazelcastConfigFile.canRead()) {
  137.                                 throw new Exception("Hazelcast (type:"+type+") file config ["+hazelcastConfigFile.getAbsolutePath()+"] cannot read");
  138.                             }
  139.                             content = FileSystemUtilities.readFile(hazelcastConfigFile);
  140.                         }
  141.                     }
  142.                     if(content==null) {
  143.                         // default
  144.                         String name = "";
  145.                         switch (type) {
  146.                         case HAZELCAST_MAP:
  147.                             name = "govway.hazelcast-map.yaml";
  148.                             break;
  149.                         case HAZELCAST_NEAR_CACHE:
  150.                             name = "govway.hazelcast-near-cache.yaml";
  151.                             break;
  152.                         case HAZELCAST_NEAR_CACHE_UNSAFE_SYNC_MAP:
  153.                             name = "govway.hazelcast-near-cache-unsafe-sync-map.yaml";
  154.                             break;
  155.                         case HAZELCAST_NEAR_CACHE_UNSAFE_ASYNC_MAP:
  156.                             name = "govway.hazelcast-near-cache-unsafe-async-map.yaml";
  157.                             break;
  158.                         case HAZELCAST_LOCAL_CACHE:
  159.                             name = "govway.hazelcast-local-cache.yaml";
  160.                             break;
  161.                         case HAZELCAST_REPLICATED_MAP:
  162.                             name= "govway.hazelcast-replicated-map.yaml";
  163.                             break;
  164.                         case HAZELCAST_ATOMIC_LONG:
  165.                             name = "govway.hazelcast-atomic-long-counters.yaml";
  166.                             break;
  167.                         case HAZELCAST_ATOMIC_LONG_ASYNC:
  168.                             name = "govway.hazelcast-atomic-long-async-counters.yaml";
  169.                             break;
  170.                         case HAZELCAST_PNCOUNTER:
  171.                             name = "govway.hazelcast-pn-counters.yaml";
  172.                             break;
  173.                         default:
  174.                             throw new Exception("Hazelcast type '"+type+"' unsupported");
  175.                         }
  176.                         content = Utilities.getAsString(HazelcastManager.class.getResourceAsStream("/"+name),Charset.UTF_8.getValue());
  177.                     }
  178.                     if(content==null) {
  179.                         throw new Exception("Hazelcast (type:"+type+") config undefined");
  180.                     }
  181.                     else {
  182.                         HazelcastManager.staticMapConfig.put(type, content);
  183.                     }
  184.                 }
  185.             }

  186.         }
  187.     }
  188.    
  189.    
  190.     public static List<PolicyGroupByActiveThreadsType> getTipiGestoriHazelcastAttivi() throws PolicyException{
  191.         if(staticMapInstance==null){
  192.             throw new PolicyException("Nessun gestore Hazelcast inizializzato");
  193.         }
  194.         List<PolicyGroupByActiveThreadsType> l = new ArrayList<PolicyGroupByActiveThreadsType>();
  195.         l.addAll(staticMapInstance.keySet());
  196.         return l;
  197.     }
  198.    
  199.     public static boolean isAttivo(PolicyGroupByActiveThreadsType type) {
  200.         if(staticMapInstance==null){
  201.             return false;
  202.         }
  203.         return staticMapInstance.containsKey(type);
  204.     }
  205.    
  206.     public static HazelcastInstance getInstance(PolicyGroupByActiveThreadsType type) throws PolicyException{
  207.         if(staticMapInstance==null){
  208.             throw new PolicyException("Nessun gestore Hazelcast inizializzato");
  209.         }
  210.         HazelcastInstance gestore = staticMapInstance.get(type);
  211.         if(gestore==null) {
  212.             HazelcastManager.initialize(type);
  213.             gestore = staticMapInstance.get(type);
  214.         }
  215.         if(gestore==null) {
  216.             throw new PolicyException("Gestore Hazelcast '"+type+"' non inizializzato ??");
  217.         }
  218.         return gestore;
  219.     }
  220.    
  221.     private static synchronized void initialize(PolicyGroupByActiveThreadsType type) throws PolicyException{
  222.         if(!HazelcastManager.staticMapInstance.containsKey(type)) {
  223.            
  224.             info("Inizializzazione Gestore Hazelcast '"+type+"' ...");
  225.            
  226.             HazelcastManager.staticMapInstance.put(type, newInstance(type));
  227.            
  228.             info("Inizializzazione Gestore Hazelcast '"+type+"' effettuata con successo");
  229.            
  230.         }
  231.     }  
  232.    
  233.     private static synchronized HazelcastInstance newInstance(PolicyGroupByActiveThreadsType type) throws PolicyException {
  234.        
  235.         String content = HazelcastManager.staticMapConfig.get(type);
  236.         if(content==null) {
  237.             throw new PolicyException("Hazelcast config undefined for type '"+type+"'");
  238.         }
  239.        
  240.         String groupId = null;
  241.         switch (type) {
  242.         case HAZELCAST_MAP:
  243.             groupId = HazelcastManager.groupId+"-map";
  244.             break;
  245.         case HAZELCAST_NEAR_CACHE:
  246.             groupId = HazelcastManager.groupId+"-near-cache";
  247.             break;
  248.         case HAZELCAST_NEAR_CACHE_UNSAFE_SYNC_MAP:
  249.             groupId = HazelcastManager.groupId+"-near-cache-unsafe-sync-map";
  250.             break;
  251.         case HAZELCAST_NEAR_CACHE_UNSAFE_ASYNC_MAP:
  252.             groupId = HazelcastManager.groupId+"-near-cache-unsafe-async-map";
  253.             break;
  254.         case HAZELCAST_LOCAL_CACHE:
  255.             groupId = HazelcastManager.groupId+"-local-cache";
  256.             break;
  257.         case HAZELCAST_REPLICATED_MAP:
  258.             groupId = HazelcastManager.groupId+"-replicated-map";
  259.             break;
  260.         case HAZELCAST_PNCOUNTER:
  261.             groupId = HazelcastManager.groupId+"-pncounter";
  262.             break;
  263.         case HAZELCAST_ATOMIC_LONG:
  264.             groupId = HazelcastManager.groupId+"-atomic-long";
  265.             break;
  266.         case HAZELCAST_ATOMIC_LONG_ASYNC:
  267.             groupId = HazelcastManager.groupId+"-atomic-long-async";
  268.             break;
  269.         default:
  270.             throw new PolicyException("Hazelcast type '"+type+"' unsupported");
  271.         }
  272.        
  273.         content = content.replace("cluster-name:", "cluster-name: "+groupId+"\n#cluster-name:");
  274.        
  275.         debug("Inizializzo hazelcast con la seguente configurazione (cluster-id"+groupId+"): " + content);
  276.        
  277.         InMemoryYamlConfig hazelcastConfig = new InMemoryYamlConfig(content);
  278.         hazelcastConfig.setClusterName(groupId);
  279.         setNetwork(hazelcastConfig, content, groupId);
  280.        
  281.         HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(hazelcastConfig);
  282.         if(hazelcast==null) {
  283.             throw new PolicyException("Hazelcast init failed");
  284.         }
  285.    
  286.         return hazelcast;
  287.     }
  288.    
  289.     private static void setNetwork(InMemoryYamlConfig hazelcastConfig, String hazelcastConfigContent, String groupId) throws PolicyException {
  290.        
  291.         Config sharedConfig = null;
  292.         if(HazelcastManager.shareConfigFile!=null) {
  293.             try {
  294.                 // La valido ma poi la ricostruiro' tutte le volte per poi sostituire la porta
  295.                 try(FileInputStream fin = new FileInputStream(HazelcastManager.shareConfigFile)){
  296.                     YamlConfigBuilder builder = new  YamlConfigBuilder(fin);
  297.                     sharedConfig = builder.build();
  298.                 }
  299.             }catch(Throwable t) {
  300.                 // e' gia' stato validato in fase di inizializzazione. Un errore qua non dovrebbe succedere
  301.                 throw new PolicyException("Configuration '"+shareConfigFile.getAbsolutePath()+"' is not valid yaml config (YamlConfigBuilder): "+t.getMessage(),t);
  302.             }
  303.         }
  304.        
  305.         if(sharedConfig!=null && sharedConfig.getNetworkConfig()!=null) {
  306.             NetworkConfig ncShared = sharedConfig.getNetworkConfig();
  307.             NetworkConfig ncInstance = hazelcastConfig.getNetworkConfig();
  308.             Integer portHazelcastConfigInstance = null;
  309.             if(ncInstance==null) {
  310.                 // non dovrebbe mai esserlo, essendo definita una porta per ogni tipo di istanza
  311.                 ncInstance = new NetworkConfig();
  312.                 hazelcastConfig.setNetworkConfig(ncInstance);
  313.             }
  314.             else {
  315.                 portHazelcastConfigInstance = hazelcastConfig.getNetworkConfig().getPort();
  316.             }
  317.            
  318.            
  319.             JsonNode instanceNode = null;
  320.             try {
  321.                 instanceNode = yamlUtils.getAsNode(hazelcastConfigContent);
  322.             }catch(Throwable t) {
  323.                 throw new PolicyException("Configuration '"+groupId+"' is not valid yaml config:\n"+hazelcastConfigContent);
  324.             }
  325.            
  326.             // overrides the public address of a member
  327.             if(ncShared.getPublicAddress()!=null) {
  328.                 boolean definedInstance = isDefined("public-address", yamlUtils, engine, instanceNode);
  329.                 if(!definedInstance) {
  330.                     ncInstance.setPublicAddress(ncShared.getPublicAddress());
  331.                     debug("(cluster-id"+groupId+") override public-address: " + ncInstance.getPublicAddress());
  332.                 }
  333.             }
  334.            
  335.             // overrides only defined outbound ports
  336.             if( (ncShared.getOutboundPorts()!=null && !ncShared.getOutboundPorts().isEmpty())
  337.                     ||
  338.                 (ncShared.getOutboundPortDefinitions()!=null && !ncShared.getOutboundPortDefinitions().isEmpty())) {
  339.                 boolean definedInstance = isDefined("outbound-ports", yamlUtils, engine, instanceNode);
  340.                 if(!definedInstance) {
  341.                     if(ncShared.getOutboundPorts()!=null && !ncShared.getOutboundPorts().isEmpty()){
  342.                         ncInstance.setOutboundPorts(ncShared.getOutboundPorts());
  343.                         debug("(cluster-id"+groupId+") override outbound-ports: " + ncInstance.getOutboundPorts());
  344.                     }
  345.                     if((ncShared.getOutboundPortDefinitions()!=null && !ncShared.getOutboundPortDefinitions().isEmpty())) {
  346.                         ncInstance.setOutboundPortDefinitions(ncShared.getOutboundPortDefinitions());
  347.                         debug("(cluster-id"+groupId+") override outbound-ports (definitions): " + ncInstance.getOutboundPortDefinitions());
  348.                     }
  349.                 }
  350.             }
  351.            
  352.             // If you set the reuse-address element to true, the TIME_WAIT state is ignored and you can bind the member to the same port again
  353.             boolean definedShared = isDefined("reuse-address", yamlUtils, engine, HazelcastManager.sharedConfigNode);
  354.             if(definedShared) {
  355.                 boolean definedInstance = isDefined("reuse-address", yamlUtils, engine, instanceNode);
  356.                 if(!definedInstance) {
  357.                     ncInstance.setReuseAddress(ncShared.isReuseAddress());
  358.                     debug("(cluster-id"+groupId+") override reuse-address: " + ncInstance.isReuseAddress());
  359.                 }
  360.             }
  361.            
  362.             // the ports that Hazelcast uses to communicate between cluster members
  363.             //ncShared.getPort();
  364.             //ncShared.getPortCount();
  365.             //ncShared.isPortAutoIncrement();
  366.             // L'unico elemento che viene ignorato se presente è il 'port'.
  367.             // Anche se definito verrà ignorato poichè ogni singola configurazione utilizzata in GovWay richiede una porta dedicata.
  368.             // Per modificarla si deve agire nel file govway.hazelcast-*.yaml presente all'interno dell'archivio govway.ear,
  369.             // modificandolo direttamente dentro l'archivio o riportandolo nella directory di configurazione esterna.

  370.             // override join
  371.             if(ncShared.getJoin()!=null) {
  372.                 boolean definedInstance = isDefined("join", yamlUtils, engine, instanceNode);
  373.                 if(!definedInstance) {
  374.                     ncInstance.setJoin(ncShared.getJoin());
  375.                     if(ncInstance.getJoin().getTcpIpConfig()!=null) {
  376.                         if(portHazelcastConfigInstance!=null && portHazelcastConfigInstance.intValue()>0) {
  377.                             if(ncInstance.getJoin().getTcpIpConfig().getMembers()!=null && !ncInstance.getJoin().getTcpIpConfig().getMembers().isEmpty()) {
  378.                                 // Se vengono usate le porte, devo clonare la lista e sistemarla con la porta usata sull'attuale istanza
  379.                                 List<String> newList = new ArrayList<>();
  380.                                 for (String member : ncInstance.getJoin().getTcpIpConfig().getMembers()) {
  381.                                     try {
  382.                                         if(member.contains(GOVWAY_INSTANCE_PORT)) {
  383.                                             newList.add(member.replace(GOVWAY_INSTANCE_PORT, portHazelcastConfigInstance.intValue()+""));
  384.                                         }
  385.                                         else if(member.contains(GOVWAY_INSTANCE_PORT.toLowerCase())) {
  386.                                             newList.add(member.replace(GOVWAY_INSTANCE_PORT.toLowerCase(), portHazelcastConfigInstance.intValue()+""));
  387.                                         }
  388.                                         else {
  389.                                             newList.add(member);
  390.                                         }
  391.                                         /*
  392.                                         int indexOfFirst = member.indexOf(":");
  393.                                         if(indexOfFirst>0 && indexOfFirst!=(member.length()-1)) {
  394.                                             int indexOfSecondIpv6 = member.indexOf(":",(indexOfFirst+1));
  395.                                             if(indexOfSecondIpv6<0) {
  396.                                                 usePortIpv4=true;
  397.                                             }
  398.                                         }
  399.                                         if(usePortIpv4) {
  400.                                             String [] split = member.split(":");
  401.                                             String newIp = split[0] + ":" + portHazelcastConfigInstance.intValue();
  402.                                             newList.add(newIp);
  403.                                         }
  404.                                         else {
  405.                                             newList.add(member);
  406.                                         }*/
  407.                                     }catch(Throwable t) {
  408.                                         error("(cluster-id"+groupId+") tpcip analisi member '"+member+"' fallita: " +t.getMessage(),t);
  409.                                         //newList.add(member);
  410.                                     }
  411.                                 }
  412.                                 ncInstance.getJoin().getTcpIpConfig().setMembers(newList);
  413.                             }
  414.                         }
  415.                     }
  416.                     debug("(cluster-id"+groupId+") override join: " + ncInstance.getJoin());
  417.                 }
  418.             }
  419.            
  420.             // override interface
  421.             if(ncShared.getInterfaces()!=null && ncShared.getInterfaces().isEnabled()) {
  422.                 boolean definedInstance = isDefined("interfaces", yamlUtils, engine, instanceNode);
  423.                 if(!definedInstance) {
  424.                     ncInstance.setInterfaces(ncShared.getInterfaces());
  425.                     debug("(cluster-id"+groupId+") override interfaces: " + ncInstance.getInterfaces());
  426.                 }
  427.             }

  428.            
  429.             // override altre configurazioni avanzate
  430.             // example yaml: https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/test/java/com/hazelcast/config/YamlConfigBuilderTest.java
  431.             if(ncShared.getIcmpFailureDetectorConfig()!=null && ncShared.getIcmpFailureDetectorConfig().isEnabled()) {
  432.                 boolean definedInstance = isDefined("failure-detector", yamlUtils, engine, instanceNode);
  433.                 if(!definedInstance) {
  434.                     ncInstance.setIcmpFailureDetectorConfig(ncShared.getIcmpFailureDetectorConfig());
  435.                     debug("(cluster-id"+groupId+") override failure-detector icmp: " + ncInstance.getIcmpFailureDetectorConfig());
  436.                 }
  437.             }
  438.             if(ncShared.getMemberAddressProviderConfig()!=null && ncShared.getMemberAddressProviderConfig().isEnabled()) {
  439.                 boolean definedInstance = isDefined("member-address-provider", yamlUtils, engine, instanceNode);
  440.                 if(!definedInstance) {
  441.                     ncInstance.setMemberAddressProviderConfig(ncShared.getMemberAddressProviderConfig());
  442.                     debug("(cluster-id"+groupId+") override member-address-provider: " + ncInstance.getMemberAddressProviderConfig());
  443.                 }
  444.             }
  445.             if(ncShared.getMemcacheProtocolConfig()!=null && ncShared.getMemcacheProtocolConfig().isEnabled()) {
  446.                 boolean definedInstance = isDefined("memcache-protocol", yamlUtils, engine, instanceNode);
  447.                 if(!definedInstance) {
  448.                     ncInstance.setMemcacheProtocolConfig(ncShared.getMemcacheProtocolConfig());
  449.                     debug("(cluster-id"+groupId+") override memcache-protocol: " + ncInstance.getMemcacheProtocolConfig());
  450.                 }
  451.             }
  452.             if(ncShared.getRestApiConfig()!=null && ncShared.getRestApiConfig().isEnabled()) {
  453.                 boolean definedInstance = isDefined("rest-api", yamlUtils, engine, instanceNode);
  454.                 if(!definedInstance) {
  455.                     ncInstance.setRestApiConfig(ncShared.getRestApiConfig());
  456.                     debug("(cluster-id"+groupId+") override rest-api: " + ncInstance.getRestApiConfig());
  457.                 }
  458.             }
  459.             if(ncShared.getSocketInterceptorConfig()!=null && ncShared.getSocketInterceptorConfig().getClassName()!=null) {
  460.                 boolean definedInstance = isDefined("socket-interceptor", yamlUtils, engine, instanceNode);
  461.                 if(!definedInstance) {
  462.                     ncInstance.setSocketInterceptorConfig(ncShared.getSocketInterceptorConfig());
  463.                     debug("(cluster-id"+groupId+") override socket-interceptor: " + ncInstance.getSocketInterceptorConfig());
  464.                 }
  465.             }
  466.             if(ncShared.getSSLConfig()!=null && ncShared.getSSLConfig().isEnabled()) {
  467.                 boolean definedInstance = isDefined("ssl", yamlUtils, engine, instanceNode);
  468.                 if(!definedInstance) {
  469.                     ncInstance.setSSLConfig(ncShared.getSSLConfig());
  470.                     debug("(cluster-id"+groupId+") override ssl: " + ncInstance.getSSLConfig());
  471.                 }
  472.             }
  473.             /* Deprecata
  474.             if(ncShared.getSymmetricEncryptionConfig()!=null && ncShared.getSymmetricEncryptionConfig().isEnabled()) {
  475.                 boolean definedInstance = isDefined("symmetric-encryption", yamlUtils, engine, node);
  476.                 if(!definedInstance) {
  477.                     ncInstance.setSymmetricEncryptionConfig(ncShared.getSymmetricEncryptionConfig());
  478.                     debug("(cluster-id"+groupId+") override symmetric-encryption: " + ncInstance.getSymmetricEncryptionConfig());
  479.                 }
  480.             }
  481.             */
  482.    
  483.         }
  484.     }
  485.    
  486.     private static boolean isDefined(String pattern, YAMLUtils yamlUtils, JsonPathExpressionEngine engine, JsonNode node) throws PolicyException {
  487.         String prefixPattern = "$.hazelcast.network.";
  488.         try {
  489.             JsonNode result = engine.getJsonNodeMatchPattern(node, prefixPattern+pattern);
  490.             return result!=null;
  491.         }catch(JsonPathNotFoundException notFound) {
  492.             return false;
  493.         }catch(Throwable t) {
  494.             throw new PolicyException(t.getMessage(),t);
  495.         }
  496.     }
  497.    
  498.     private static void debug(String msg) {
  499.         if(HazelcastManager.log!=null) {
  500.             HazelcastManager.log.debug(msg);
  501.         }
  502.         if(HazelcastManager.logStartup!=null) {
  503.             HazelcastManager.logStartup.debug(msg);
  504.         }
  505.     }
  506.     private static void info(String msg) {
  507.         if(HazelcastManager.log!=null) {
  508.             HazelcastManager.log.info(msg);
  509.         }
  510.         if(HazelcastManager.logStartup!=null) {
  511.             HazelcastManager.logStartup.info(msg);
  512.         }
  513.     }
  514.     private static void error(String msg, Throwable e) {
  515.         if(HazelcastManager.log!=null) {
  516.             HazelcastManager.log.error(msg,e);
  517.         }
  518.         if(HazelcastManager.logStartup!=null) {
  519.             HazelcastManager.logStartup.error(msg,e);
  520.         }
  521.     }
  522.    
  523.     public static synchronized void close() {
  524.         if(HazelcastManager.staticMapInstance!=null && !HazelcastManager.staticMapInstance.isEmpty()) {
  525.             for (PolicyGroupByActiveThreadsType type : HazelcastManager.staticMapInstance.keySet()) {
  526.                 HazelcastInstance hazelcast = HazelcastManager.staticMapInstance.get(type);
  527.                 try {
  528.                     hazelcast.shutdown();
  529.                 }catch(Throwable t) {
  530.                     HazelcastManager.log.debug("Hazelcast '"+type+"' shutdown failed: " + t.getMessage(),t);
  531.                 }
  532.             }
  533.         }
  534.     }
  535.    

  536. }