View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.config.metadata;
19  
20  import javax.xml.datatype.Duration;
21  
22  import org.opensaml.xml.util.DatatypeHelper;
23  import org.opensaml.xml.util.XMLHelper;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
27  import org.springframework.beans.factory.xml.ParserContext;
28  import org.w3c.dom.Element;
29  
30  import edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils;
31  
32  /** Base class for metadata providers that reload their metadata. */
33  public abstract class AbstractReloadingMetadataProviderBeanDefinitionParser extends
34          AbstractMetadataProviderBeanDefinitionParser {
35  
36      /** Class logger. */
37      private final Logger log = LoggerFactory.getLogger(AbstractReloadingMetadataProviderBeanDefinitionParser.class);
38  
39      /** {@inheritDoc} */
40      protected void doParse(Element config, ParserContext parserContext, BeanDefinitionBuilder builder) {
41          super.doParse(config, parserContext, builder);
42  
43          String taskTimerRef = getTaskTimerRef(config);
44          log.debug("Metadata provider using task timer: {}", taskTimerRef);
45          builder.addConstructorArgReference(taskTimerRef);
46  
47          float refreshDelayFactor = getRefreshDelayFactor(config);
48          log.debug("Metadata provider refresh delay factor: {}", refreshDelayFactor);
49          builder.addPropertyValue("refreshDelayFactor", refreshDelayFactor);
50  
51          long minRefreshDelay = getMinRefreshDelay(config);
52          log.debug("Metadata provider min refresh delay: {}ms", minRefreshDelay);
53          builder.addPropertyValue("minRefreshDelay", minRefreshDelay);
54          
55          long maxRefreshDelay = getMaxRefreshDelay(config);
56          log.debug("Metadata provider max refresh delay: {}ms", maxRefreshDelay);
57          builder.addPropertyValue("maxRefreshDelay", maxRefreshDelay);
58      }
59  
60      /**
61       * Gets the default parser pool reference for the metadata provider.
62       * 
63       * @param config metadata provider configuration element
64       * 
65       * @return parser pool reference
66       */
67      protected String getParserPoolRef(Element config) {
68          String parserPoolRef = null;
69          if (config.hasAttributeNS(null, "parerPoolRef")) {
70              parserPoolRef = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "parserPoolRef"));
71          }
72  
73          if (parserPoolRef == null) {
74              parserPoolRef = "shibboleth.ParserPool";
75          }
76  
77          return parserPoolRef;
78      }
79  
80      /**
81       * Gets the default task timer reference for the metadata provider.
82       * 
83       * @param config metadata provider configuration element
84       * 
85       * @return task timer reference
86       */
87      protected String getTaskTimerRef(Element config) {
88          String taskTimerRef = null;
89          if (config.hasAttributeNS(null, "taskTimerRef")) {
90              taskTimerRef = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "taskTimerRef"));
91          }
92  
93          if (taskTimerRef == null) {
94              taskTimerRef = "shibboleth.TaskTimer";
95          }
96  
97          return taskTimerRef;
98      }
99  
100     /**
101      * Gets the refresh delay factor for the metadata provider.
102      * 
103      * @param config provider configuration element
104      * 
105      * @return refresh delay factor
106      */
107     protected float getRefreshDelayFactor(Element config) {
108         float delayFactor = 0.75f;
109 
110         if (config.hasAttributeNS(null, "refreshDelayFactor")) {
111             String factorString = config.getAttributeNS(null, "refreshDelayFactor");
112             try {
113                 delayFactor = Float.parseFloat(factorString);
114             } catch (NumberFormatException e) {
115                 log.error("Metadata provider had invalid refreshDelayFactor value '{}', using default value",
116                         factorString);
117             }
118         }
119 
120         if (delayFactor <= 0.0 || delayFactor >= 1.0) {
121             log.error("Metadata provider had invalid refreshDelayFactor value '{}', using default value", delayFactor);
122             delayFactor = 0.75f;
123         }
124 
125         return delayFactor;
126     }
127 
128     /**
129      * Gets the maximum refresh delay for the metadata provider.
130      * 
131      * @param config provider configuration element
132      * 
133      * @return the maximum refresh delay, in milliseconds
134      */
135     protected long getMaxRefreshDelay(Element config) {
136         long maxRefreshDelay = 14400000L;
137 
138         if (config.hasAttributeNS(null, "cacheDuration")) {
139             int cacheDuration = Integer.parseInt(config.getAttributeNS(null, "cacheDuration"));
140             maxRefreshDelay = cacheDuration * 1000;
141             log.warn("Metadata provider cacheDuration attribute is deprecated, use maxRefreshDelay=\"{}\" instead.",
142                     XMLHelper.getDataTypeFactory().newDuration(maxRefreshDelay).toString());
143         }
144 
145         if (config.hasAttributeNS(null, "maxCacheDuration")) {
146             int cacheDuration = Integer.parseInt(config.getAttributeNS(null, "maxCacheDuration"));
147             Duration duration = XMLHelper.getDataTypeFactory().newDuration(cacheDuration * 1000);
148             log.warn("Metadata provider maxCacheDuration attribute is deprecated, use maxRefreshDelay=\"{}\" instead.",
149                     duration.toString());
150         }
151 
152         if (config.hasAttributeNS(null, "maxRefreshDelay")) {
153             String delayString = config.getAttributeNS(null, "maxRefreshDelay");
154             try {
155                 maxRefreshDelay = SpringConfigurationUtils.parseDurationToMillis("maxRefreshDelay", delayString, 1);
156             } catch (NumberFormatException e) {
157                 log.error("Metadata provider had invalid maxRefreshDelay value '{}', using default value", delayString);
158             }
159         }
160 
161         if (maxRefreshDelay <= 0) {
162             log.error("Metadata provider had invalid maxRefreshDelay value '{}', using default value", maxRefreshDelay);
163             maxRefreshDelay = 14400000L;
164         }
165 
166         return maxRefreshDelay;
167     }
168 
169     /**
170      * Gets the minimum refresh delay for the metadata provider.
171      * 
172      * @param config provider configuration element
173      * 
174      * @return the minimum refresh delay, in milliseconds
175      */
176     protected long getMinRefreshDelay(Element config) {
177         long minRefreshDelay = 300000L;
178 
179         if (config.hasAttributeNS(null, "minRefreshDelay")) {
180             String delayString = config.getAttributeNS(null, "minRefreshDelay");
181             try {
182                 minRefreshDelay = SpringConfigurationUtils.parseDurationToMillis("minRefreshDelay", delayString, 1);
183             } catch (NumberFormatException e) {
184                 log.error("Metadata provider had invalid minRefreshDelay value '{}', using default value", delayString);
185             }
186         }
187 
188         if (minRefreshDelay <= 0) {
189             log.error("Metadata provider had invalid minRefreshDelay value '{}', using default value", minRefreshDelay);
190             minRefreshDelay = 300000L;
191         }
192 
193         return minRefreshDelay;
194     }
195 }