View Javadoc

1   /*
2   * Copyright 1999-2004 The Apache Software Foundation.
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5   * use this file except in compliance with the License. You may obtain a copy of
6   * the License at
7   *
8   * http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  */
17  package org.apache.sandesha.util;
18  
19  /***
20   * @author Saminda Wishwajith Abeyruwan
21   *
22   */
23  
24  
25  import org.apache.axis.components.logger.LogFactory;
26  import org.apache.commons.logging.Log;
27  import org.apache.sandesha.Constants;
28  import org.w3c.dom.*;
29  
30  import javax.xml.parsers.DocumentBuilder;
31  import javax.xml.parsers.DocumentBuilderFactory;
32  import java.io.InputStream;
33  
34  
35  public class PolicyLoader {
36  
37      private static final Log log = LogFactory.getLog(PolicyLoader.class.getName());
38  
39      private long inactivityTimeout;
40      private long baseRetransmissionInterval;
41      private long acknowledgementInterval;
42      private String exponentialBackoff;
43      private String binaryBackOff;
44  
45      private DocumentBuilderFactory factory;
46      private DocumentBuilder builder;
47      private Document document;
48  
49      private static PolicyLoader instance;
50      private static boolean policyInstance = false;
51  
52      private Element rootNodeElement;
53  
54      private PolicyLoader() {
55          helper();
56          policyInstance = true;
57      }
58  
59      public static PolicyLoader getInstance() {
60          if (policyInstance == false)
61              return instance = new PolicyLoader();
62          else
63              return instance;
64      }
65  
66  
67      public long getInactivityTimeout() {
68  
69          if (inactivityTimeout == 0)
70              return Constants.INACTIVITY_TIMEOUT;
71          else
72              return inactivityTimeout;
73      }
74  
75      public long getBaseRetransmissionInterval() {
76          if (baseRetransmissionInterval == 0)
77              return Constants.RETRANSMISSION_INTERVAL;
78          else
79              return baseRetransmissionInterval;
80      }
81  
82      public long getAcknowledgementInterval() {
83          if (acknowledgementInterval == 0)
84              return Constants.ACKNOWLEDGEMENT_INTERVAL;
85          else
86              return acknowledgementInterval;
87      }
88  
89      public String getExponentialBackoff() {
90          return exponentialBackoff;
91      }
92  
93      public void helper() {
94          init();
95          try {
96              inactivityTimeout =
97                      getAttributeValue(Constants.WSRMPolicy.WSRM, Constants.WSRMPolicy.INA_TIMEOUT);
98              baseRetransmissionInterval = getAttributeValue(Constants.WSRMPolicy.WSRM,
99                      Constants.WSRMPolicy.BASE_TX_INTERVAL);
100             acknowledgementInterval =
101                     getAttributeValue(Constants.WSRMPolicy.WSRM, Constants.WSRMPolicy.ACK_INTERVAL);
102             exponentialBackoff = getExpBackoffInterval(Constants.WSRMPolicy.WSRM,
103                     Constants.WSRMPolicy.EXP_BACKOFF);
104             binaryBackOff = geBinaryBackoffInterval(Constants.WSRMPolicy.WSRM,
105                     Constants.WSRMPolicy.BIN_BACKOFF);
106         } catch (Exception e) {
107             log.error(e);
108         }
109     }
110 
111     private String geBinaryBackoffInterval(String namespaceURI, String elementName) {
112         String name = null;
113         NodeList list = rootNodeElement.getElementsByTagNameNS(namespaceURI, elementName);
114         if (list != null) {
115             Node node = list.item(0);
116             if (node != null)
117                 name = list.item(0).getLocalName();
118         }
119         return name;
120     }
121 
122     private long getAttributeValue(String namespaceURI, String elementName) {
123         NodeList list = rootNodeElement.getElementsByTagNameNS(namespaceURI, elementName);
124         NamedNodeMap map = list.item(0).getAttributes();
125         Attr att = (Attr) map.item(0);
126         String value = att.getNodeValue();
127         return Long.parseLong(value.trim());
128 
129     }
130 
131     private String getExpBackoffInterval(String namespaceURI, String elementName) {
132         String name = null;
133         NodeList list = rootNodeElement.getElementsByTagNameNS(namespaceURI, elementName);
134         if (list != null) {
135             Node node = list.item(0);
136             if (node != null)
137                 name = list.item(0).getLocalName();
138         }
139         return name;
140     }
141 
142     private void init() {
143         try {
144             factory = DocumentBuilderFactory.newInstance();
145             factory.setNamespaceAware(true);
146             builder = factory.newDocumentBuilder();
147             InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants.ClientProperties.WSRM_POLICY_FILE);
148 
149             if (in != null) {
150                 document = builder.parse(in);
151                 rootNodeElement = document.getDocumentElement();
152             } else
153                 log.error("No WSRMPolicy.xml Found");
154         } catch (Exception e) {
155             log.error(e);
156         }
157     }
158 
159 
160     public String getBinaryBackOff() {
161         helper();
162         return binaryBackOff;
163 
164     }
165 }