View Javadoc

1   /*
2    * Copyright 2008 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 org.opensaml.util.resource;
18  
19  import java.io.BufferedReader;
20  import java.io.ByteArrayInputStream;
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.util.Iterator;
28  import java.util.Properties;
29  
30  /**
31   * A resource filter that buffers a resource into a string and replaces instance of macros with properties read from a
32   * file. Macros are of the syntax '${MACRO_NAME}', the same syntax used within the Java Expression Language.
33   * 
34   * The property file is read at invocation of this filter.
35   * 
36   * The {@link InputStream} should be a character stream as {@link InputStreamReader} will be used to convert the stream
37   * into a string.
38   */
39  public class PropertyReplacementResourceFilter implements ResourceFilter {
40  
41      /** Location of the property file. */
42      private File propertyFilePath;
43  
44      /**
45       * Constructor.
46       * 
47       * @param propertyFile property file whose properties will be expanded within the resource
48       */
49      public PropertyReplacementResourceFilter(File propertyFile) {
50          if (propertyFile == null) {
51              throw new IllegalArgumentException("Property file may not be null");
52          }
53          propertyFilePath = propertyFile;
54      }
55  
56      /** {@inheritDoc} */
57      public InputStream applyFilter(InputStream resource) throws ResourceException {
58          Properties props = new Properties();
59          try {
60              props.load(new FileInputStream(propertyFilePath));
61          } catch (IOException e) {
62              throw new ResourceException("Unable to read property file", e);
63          }
64  
65          try {
66              StringBuilder resourceBuffer = new StringBuilder();
67              Reader resourceReader = new BufferedReader(new InputStreamReader(resource));
68  
69              char[] resourceCharacters = new char[2048];
70              while (resourceReader.read(resourceCharacters) > -1) {
71                  resourceBuffer.append(resourceCharacters);
72                  resourceCharacters = new char[2048];
73              }
74              resource.close();
75  
76              String resourceString = resourceBuffer.toString();
77              
78              Iterator<String> keyItr = (Iterator<String>) props.propertyNames();
79              String key;
80              while (keyItr.hasNext()) {
81                  key = keyItr.next();
82                  resourceString = resourceString.replace("${" + key + "}", props.getProperty(key));
83              }
84  
85              return new ByteArrayInputStream(resourceString.getBytes());
86          } catch (IOException e) {
87              throw new ResourceException("Unable to read contents of resource", e);
88          }
89      }
90  }