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