View Javadoc

1   /*
2    * Copyright [2007] [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.attribute.resolver.attributeDefinition;
18  
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  
22  import org.opensaml.xml.util.DatatypeHelper;
23  
24  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.attributeDefinition.ScriptedAttributeDefinition;
25  
26  /**
27   * Scripted attribute factory.
28   */
29  public class ScriptedAttributeDefinitionFactoryBean extends BaseAttributeDefinitionFactoryBean {
30  
31      /** The scripting language used. */
32      private String scriptLanguage;
33  
34      /** The file to read the script from. */
35      private String scriptFile;
36  
37      /** The script. */
38      private String script;
39  
40      /**
41       * Gets the scripting language being used.
42       * 
43       * @return scripting language being used
44       */
45      public String getLanguage() {
46          return scriptLanguage;
47      }
48  
49      /**
50       * Sets the scripting language being used.
51       * 
52       * @param language scripting language being used
53       */
54      public void setLanguage(String language) {
55          scriptLanguage = DatatypeHelper.safeTrimOrNullString(language);
56      }
57  
58      /**
59       * Gets the script.
60       * 
61       * @return the script
62       */
63      public String getScript() {
64          return script;
65      }
66  
67      /**
68       * Sets the script.
69       * 
70       * @param newScript the script
71       */
72      public void setScript(String newScript) {
73          script = DatatypeHelper.safeTrimOrNullString(newScript);
74      }
75  
76      /**
77       * Gets the file to read the script from.
78       * 
79       * @return file to read the script from
80       */
81      public String getScriptFile() {
82          return scriptFile;
83      }
84  
85      /**
86       * Sets the file to read the script from.
87       * 
88       * @param file file to read the script from
89       */
90      public void setScriptFile(String file) {
91          scriptFile = DatatypeHelper.safeTrimOrNullString(file);
92      }
93  
94      /** {@inheritDoc} */
95      public Class getObjectType() {
96          return ScriptedAttributeDefinition.class;
97      }
98  
99      /** {@inheritDoc} */
100     protected Object createInstance() throws Exception {
101         ScriptedAttributeDefinition definition = new ScriptedAttributeDefinition(scriptLanguage);
102         populateAttributeDefinition(definition);
103 
104         try {
105             if (getScript() == null) {
106                 FileInputStream ins = new FileInputStream(scriptFile);
107                 byte[] scriptBytes = new byte[ins.available()];
108                 ins.read(scriptBytes);
109                 script = new String(script);
110             }
111         } catch (IOException e) {
112             throw e;
113         }
114         definition.setScript(script);
115 
116         definition.initialize();
117 
118         return definition;
119     }
120 }