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  package org.miloss.fgsms.agentcore;
22  
23  import java.util.Map;
24  import javax.xml.ws.BindingProvider;
25  import org.miloss.fgsms.common.Utility;
26  import org.miloss.fgsms.services.interfaces.datacollector.AddStatisticalDataRequestMsg;
27  import org.miloss.fgsms.services.interfaces.datacollector.AddStatisticalDataResponseMsg;
28  import org.apache.log4j.Level;
29  import org.miloss.fgsms.common.Logger;;
30  
31  /**
32   * Provides some useful functions for building agents.. All functions
33   * use the fgsms-agent.properties file within this JAR for configuration
34   * purposes All functions support discovery mechanisms, retry counts,
35   * failover/roundrobin
36   *
37   * @since RC6
38   * @author AO
39   */
40  public class StatisticalHelper extends HelperBase{
41  
42      protected static ConfigLoader cfg = null;
43      static Logger log = Logger.getLogger(org.miloss.fgsms.common.Constants.LoggerName);
44  
45      private static void Init() throws ConfigurationException {
46          if (cfg == null) {
47              cfg = new ConfigLoader();
48          }
49  
50      }
51      
52      
53  
54      /**
55       * Attempts to send statistical data to the fgsms DCS service
56       *
57       * @param req
58       * @return true if successful
59       * @throws ConfigurationException if the current agent configuration is not
60       * available or is invalid
61       */
62      public static boolean send(AddStatisticalDataRequestMsg req) throws ConfigurationException {
63          if (cfg == null) {
64              Init();
65          }
66      discoverEndpoints();
67          req.setClassification(cfg.classlevel);
68          int retrycount = 0;
69          int urlcount = 0;
70          switch (cfg.DCSalgo) {
71              case FAILOVER:
72                  retrycount = 0;
73                  urlcount = 0;
74                  while ((retrycount < cfg.DCSRetryCount)) {
75  
76                      urlcount = 0;
77                      while (urlcount < cfg.DCS_URLS.size()) {
78                          try { // Call Web Service Operation
79  
80                              BindingProvider bp = (BindingProvider) cfg.dcsport;
81                              Map<String, Object> context = bp.getRequestContext();
82  //                            context.remove(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
83                              context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, cfg.DCS_URLS.get(urlcount));
84                              if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) {
85                                  context.put(BindingProvider.USERNAME_PROPERTY, cfg.username);
86                                  context.put(BindingProvider.PASSWORD_PROPERTY, Utility.DE(cfg.password));
87                              }
88                              if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStorePassword"))) {
89                                  context.put("javax.net.ssl.trustStorePassword", Utility.DE(cfg.getJavaxtruststorepass()));
90                                  context.put("javax.net.ssl.trustStore", Utility.DE(cfg.getJavaxtruststore()));
91                              }
92                              try {
93                                  if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.PKI) {
94                                      if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStorePassword"))) {
95                                          context.put("javax.net.ssl.keyStorePassword", Utility.DE(cfg.getJavaxkeystorepass()));
96                                          context.put("javax.net.ssl.keyStore", Utility.DE(cfg.getJavaxkeystore()));
97                                      }
98                                  }
99  
100                             } catch (Exception ex) {
101                                 log.log(Level.FATAL, "error caught when referencing (get or set) System.properties for SSL communication. Check to ensure that this is enabled in your JAAS managemer", ex);
102                             }
103                             //send it
104                             AddStatisticalDataResponseMsg addStatisticalData = cfg.dcsport.addStatisticalData(req);
105                             cfg.classlevel = addStatisticalData.getClassification();
106 
107                             return true;
108 
109 
110                         } catch (Exception ex) {
111                             log.log(Level.WARN, "fgsms unable to transmit to CS at " + cfg.DCS_URLS.get(urlcount) + " will retry " + (cfg.DCSRetryCount - retrycount) + " times." + ex.getMessage(), ex);
112                         }
113                         urlcount++;
114                     }
115                     retrycount++;
116                 }
117 
118                 log.log(Level.FATAL, "fgsms unable to transmit to any of the DCS[" + cfg.DCS_URLS.size() + "] URLs, retry count exceeded. Falling back to default policy");
119                 return false;
120             case ROUNDROBIN:
121                 retrycount = 0;
122                 urlcount = 0;
123                 while ((retrycount < cfg.DCSRetryCount)) {
124 
125                     urlcount = 0;
126                     while (urlcount < cfg.DCS_URLS.size()) {
127                         try { // Call Web Service Operation
128 
129                             BindingProvider bp = (BindingProvider) cfg.dcsport;
130                             Map<String, Object> context = bp.getRequestContext();
131                             //                        context.remove(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
132                             context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, cfg.DCS_URLS.get(urlcount));
133 //                            StubExt sec = (StubExt) pcsport;
134                             if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) {
135 //                                sec.setSecurityConfig("/META-INF/fgsms-username-config.xml");
136                                 context.put(BindingProvider.USERNAME_PROPERTY, cfg.username);
137                                 context.put(BindingProvider.PASSWORD_PROPERTY, Utility.DE(cfg.password));
138                             }
139                             if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.PKI) {
140                                 if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStorePassword"))) {
141                                     System.setProperty("javax.net.ssl.keyStorePassword", Utility.DE(cfg.getJavaxkeystorepass()));
142                                     System.setProperty("javax.net.ssl.keyStore", Utility.DE(cfg.getJavaxkeystore()));
143                                 }
144                             }
145                             if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStorePassword"))) {
146                                 System.setProperty("javax.net.ssl.trustStorePassword", Utility.DE(cfg.getJavaxtruststorepass()));
147                                 System.setProperty("javax.net.ssl.trustStore", Utility.DE(cfg.getJavaxtruststore()));
148                             }
149                             //send it
150                             AddStatisticalDataResponseMsg addStatisticalData = cfg.dcsport.addStatisticalData(req);
151                             cfg.classlevel = addStatisticalData.getClassification();
152                             return true;
153                         } catch (Exception ex) {
154                             log.log(Level.WARN, "fgsms unable to transmit to DCS at " + cfg.DCS_URLS.get(urlcount) + " will retry " + (cfg.DCSRetryCount - retrycount) + " times." + ex.getMessage(), ex);
155                         }
156                         urlcount++;
157                     }
158                     retrycount++;
159                 }
160                 log.log(Level.FATAL, "fgsms unable totransmit to any of the DCS[" + cfg.DCS_URLS.size() + "] URLs, retry count exceeded.");
161         }
162         return false;
163     }
164 }