View Javadoc

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