View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  
22  package org.miloss.fgsms.agentcore;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import org.miloss.fgsms.common.Logger;;
27  import org.miloss.fgsms.plugins.agents.IEndpointDiscovery;
28  
29  /**
30   * Supports DCS, SS, and PCS discovery
31   * @author AO
32   */
33  public abstract class HelperBase {
34  
35      static ConfigLoader cfg = null;
36  
37      private static void Init() throws ConfigurationException {
38          if (cfg == null) {
39              cfg = new ConfigLoader();
40          }
41  
42      }
43      static Logger log = Logger.getLogger(org.miloss.fgsms.common.Constants.LoggerName);
44      static List<IEndpointDiscovery> endpointproviders=null;
45      
46      /**
47       * calls all {@link IEndpointDiscovery} interfaces to discover the location
48       * of FGSMS's server, if configured
49       * @throws ConfigurationException 
50       */
51      public static void discoverEndpoints() throws ConfigurationException {
52          Init();
53          if (cfg == null || cfg.prop == null) {
54              throw new NullPointerException("fgsms properties file is not available.");
55          }
56          if (endpointproviders == null) {
57              endpointproviders = DataPusher.LoadEndpointProviders(cfg);
58          }
59          List<String> endpointspcs = new ArrayList<String>();
60          List<String> endpointsdcs = new ArrayList<String>();
61          List<String> endpointsss = new ArrayList<String>();
62          boolean ran = false;
63          for (int i = 0; i < endpointproviders.size(); i++) {
64              if ((System.currentTimeMillis() - cfg.discoveryInterval) > endpointproviders.get(i).GetLastLookup()) {
65                  if (endpointproviders.get(i).IsEnabled()) {
66                      endpointspcs.addAll(endpointproviders.get(i).GetPCSURLs());
67                      endpointsdcs.addAll(endpointproviders.get(i).GetDCSURLs());
68                      endpointsss.addAll(endpointproviders.get(i).GetSSURLs());
69                      ran = true;
70                      endpointproviders.get(i).SetLastLookup(System.currentTimeMillis());
71                  }
72              }
73  
74          }
75          if (ran) {
76              for (int i = 0; i < endpointspcs.size(); i++) {
77                  if (!cfg.PCS_URLS.contains(endpointspcs.get(i))) {
78                      cfg.PCS_URLS.add(endpointspcs.get(i));
79                  }
80              }
81              for (int i = 0; i < endpointspcs.size(); i++) {
82                  if (!cfg.DCS_URLS.contains(endpointsdcs.get(i))) {
83                      cfg.DCS_URLS.add(endpointsdcs.get(i));
84                  }
85              }
86              for (int i = 0; i < endpointsss.size(); i++) {
87                  if (!cfg.SS_URLS.contains(endpointsss.get(i))) {
88                      cfg.SS_URLS.add(endpointsss.get(i));
89                  }
90              }
91          }
92  
93      }
94  }