View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.springframework.extensions.webscripts.processor.BaseProcessor;
27  
28  
29  /**
30   * Registry of Script Processors
31   * 
32   * If no processors are registered, the default script processor is
33   * the javascript processor.
34   * 
35   * @author muzquiano
36   */
37  public class ScriptProcessorRegistry
38  {
39      private static final Log logger = LogFactory.getLog(ScriptProcessorRegistry.class);
40  
41      /** The name of the default script processor */
42      private String defaultScriptProcessorName = "javascript";
43      
44      /** Maps containing the script processors */
45      private Map<String, ScriptProcessor> scriptProcessors = new HashMap<String, ScriptProcessor>(8);
46      private Map<String, String> scriptProcessorNamesByExtension = new HashMap<String, String>(8);
47      
48      /**
49       * Sets the name of the default script processor
50       * 
51       * @param defaultScriptProcessor    the name of the default script processor
52       */
53      public void setDefaultScriptProcessor(String defaultScriptProcessorName)
54      {
55          this.defaultScriptProcessorName = defaultScriptProcessorName;
56      }
57  
58      /**
59       * Registers a script processor
60       * 
61       * @param   scriptProcessor     the script processor to register
62       */
63      public void registerScriptProcessor(ScriptProcessor scriptProcessor)
64      {
65          registerScriptProcessor(scriptProcessor, null, null);
66      }
67      
68      /**
69       * Registers a script processor
70       * 
71       * @param   scriptProcessor     the script processor to register
72       * @param   extension
73       * @param   name
74       */
75      public void registerScriptProcessor(ScriptProcessor scriptProcessor, String extension, String name)
76      {
77          if (name == null && extension == null)
78          {
79              // try to determine name and extension from processor itself
80              if (scriptProcessor instanceof BaseProcessor)
81              {
82                  name = ((BaseProcessor)scriptProcessor).getName();
83                  extension = ((BaseProcessor)scriptProcessor).getExtension();
84              }
85          }
86          
87          // if we have a name and extension to use, register
88          if (name != null && extension != null)
89          {            
90              this.scriptProcessors.put(name, scriptProcessor);
91              this.scriptProcessorNamesByExtension.put(extension, name);
92              
93              if (logger.isInfoEnabled())
94              {
95                  logger.info("Registered script processor " + name + " for extension " + extension);
96              }
97          }
98      }
99      
100     /**
101      * Gets the default script processor.
102      * 
103      * @return the default script processor
104      */
105     protected ScriptProcessor getDefaultScriptProcessor()
106     {
107         return (ScriptProcessor) this.scriptProcessors.get(this.defaultScriptProcessorName);
108     }
109     
110     /**
111      * Returns the script processor that matches the file
112      * extension for the given path.
113      * 
114      * If a script processor cannot be matched to the path, the default
115      * script processor will be returned.
116      * 
117      * @param path the path
118      * 
119      * @return the script processor
120      */
121     public ScriptProcessor getScriptProcessor(String path)
122     {
123         ScriptProcessor processor = null;
124         
125         int i = path.lastIndexOf(".");
126         if (i > -1)
127         {
128             String extension = path.substring(i+1);
129             processor = getScriptProcessorByExtension(extension);
130         }
131         
132         if (processor == null)
133         {
134             processor = getDefaultScriptProcessor();
135         }
136         
137         return processor;
138     }
139     
140     /**
141      * Returns the best fit script processor for the given script
142      * content object.
143      * 
144      * If a script processor cannot be matched, the default
145      * script processor will be returned.
146      * 
147      * @param path the path
148      * 
149      * @return the script processor
150      */
151     public ScriptProcessor getScriptProcessor(ScriptContent scriptContent)
152     {
153         return getScriptProcessor(scriptContent.getPath());
154     }
155     
156     /**
157      * Gets the script processor registered for the given extension
158      * 
159      * @param extension the extension
160      * 
161      * @return the script processor by extension or null if no match
162      */
163     public ScriptProcessor getScriptProcessorByExtension(String extension)
164     {
165         ScriptProcessor processor = null;
166         
167         String scriptProcessorName = (String) scriptProcessorNamesByExtension.get(extension);
168         if (scriptProcessorName != null)
169         {
170             processor = (ScriptProcessor) this.scriptProcessors.get(scriptProcessorName);
171         }
172         
173         return processor;
174     }    
175     
176     /**
177      * Returns a variation on the provided path that exists and
178      * is processable by one of the processors in this registry.
179      * 
180      * First attempts to find a script processor that contains
181      * the content located at the given path (using extension
182      * information, if available).
183      * 
184      * If no match is found, iterates over the file extensions
185      * and attempts to find a match.
186      * 
187      * Path can therefore be values like:
188      * 
189      *   testfile.js
190      *     - matches to file testfile.js using javascript procesor
191      *     
192      *   testfile
193      *     - matches for all extensions, potentially looking at 
194      *       testfile.js, testfile.groovy, etc.
195      *       
196      * The extension annotated path is returned which will correctly
197      * dispatch to the discovered processor.
198      * 
199      * @param path the path
200      * 
201      * @return a valid processor file path or null if no match
202      */
203     public String findValidScriptPath(String path)
204     {
205         String validScriptPath = null;
206         
207         // look up by file extension
208         int i = path.lastIndexOf(".");
209         if (i > -1)
210         {
211             String extension = path.substring(i+1);
212             ScriptProcessor processor = getScriptProcessorByExtension(extension);
213             if (processor != null && processor.findScript(path) != null)
214             {
215                 validScriptPath = path;
216             }
217         }
218         
219         if (validScriptPath == null)
220         {
221             // look across all of the extensions
222             String[] extensions = this.getRegisteredExtensions();
223             for (String extension: extensions)
224             {
225                 ScriptProcessor processor = getScriptProcessorByExtension(extension);
226                 if (processor.findScript(path + "." + extension) != null)
227                 {
228                     validScriptPath = path + "." + extension;
229                 }
230             }
231         }
232         
233         return validScriptPath;
234     }    
235     
236     /**
237      * Returns the extensions with registered processors
238      * 
239      * @return the registered extensions
240      */
241     public String[] getRegisteredExtensions()
242     {
243         return scriptProcessorNamesByExtension.keySet().toArray(new String[scriptProcessorNamesByExtension.keySet().size()]);
244     }
245     
246     /**
247      * Gets the extension for given processor.
248      * 
249      * @param scriptProcessor the script processor
250      * 
251      * @return the extension for processor
252      */
253     public String getExtensionForProcessor(ScriptProcessor scriptProcessor)
254     {
255         String ext = null;
256         
257         String[] extensions = this.getRegisteredExtensions();
258         for (String extension: extensions)
259         {
260             ScriptProcessor processor = getScriptProcessorByExtension(extension);
261             if (processor == scriptProcessor)
262             {
263                 ext = extension;
264             }
265         }
266         
267         return ext;        
268     }
269     
270 
271     /**
272      * Resets all of the registered script processors
273      */
274     public void reset()
275     {
276         for (ScriptProcessor p : this.scriptProcessors.values())
277         {
278             p.reset();
279         }
280     }    
281 }