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.processor;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.springframework.context.ApplicationContextAware;
27  import org.springframework.extensions.surf.core.processor.ProcessorExtension;
28  import org.springframework.extensions.webscripts.MultiScriptLoader;
29  import org.springframework.extensions.webscripts.ScriptLoader;
30  import org.springframework.extensions.webscripts.ScriptProcessor;
31  import org.springframework.extensions.webscripts.SearchPath;
32  import org.springframework.extensions.webscripts.Store;
33  import org.springframework.extensions.webscripts.WebScriptException;
34  
35  /**
36   * Abstract class for use in helping developers to build script
37   * processors that take advantage of the web script framework's
38   * inherent support for search paths.
39   * 
40   * @author muzquiano
41   */
42  public abstract class AbstractScriptProcessor extends BaseRegisterableScriptProcessor implements ApplicationContextAware, ScriptProcessor
43  {	
44      /** Script loading SearchPath */
45      private SearchPath searchPath;
46      
47      /** Object that gets the script location for the script at the specified path */
48      private ScriptLoader scriptLoader;    
49      
50      /**
51       * @param searchPath
52       */
53      public void setSearchPath(SearchPath searchPath)
54      {
55          this.searchPath = searchPath;
56      }
57      
58      /**
59       * Gets the search path.
60       * 
61       * @return the search path
62       */
63      protected SearchPath getSearchPath()
64      {
65          return this.searchPath;
66      }
67      
68      /**
69       * Gets the script loader.
70       * 
71       * @return the script loader
72       */
73      protected ScriptLoader getScriptLoader()
74      {
75          return this.scriptLoader;
76      }
77          
78      /**
79       * Add any configured processor model extensions to the model.
80       * 
81       * @param model
82       */
83      protected void addProcessorModelExtensions(Object model)
84      {
85          // there's always a model, if only to hold the extension objects
86          if (model == null)
87          {
88              model = new HashMap<String, Object>();
89          }
90          if (model instanceof Map)
91          {
92              // add any processor extensions
93              for (ProcessorExtension ex : this.processorExtensions.values()) 
94              {
95                  ((Map<String, Object>)model).put(ex.getExtensionName(), ex);
96              }
97          }
98      }    
99      
100     /**
101      * Initializes the script loaders
102      */
103     protected void initLoaders() 
104     {
105         List<ScriptLoader> loaders = new ArrayList<ScriptLoader>(searchPath.getStores().size());
106         for (Store apiStore : searchPath.getStores())
107         {
108             ScriptLoader loader = apiStore.getScriptLoader();
109             if (loader == null)
110             {
111                 throw new WebScriptException("Unable to retrieve script loader for Web Script store " + apiStore.getBasePath());
112             }
113             loaders.add(loader);
114         }
115         this.scriptLoader = new MultiScriptLoader(loaders.toArray(new ScriptLoader[loaders.size()]));
116     }
117     
118     /* (non-Javadoc)
119      * @see org.alfresco.web.scripts.processor.BaseRegisterableScriptProcessor#init()
120      */
121     public void init()
122     {
123         this.initLoaders();
124     }
125 
126     /* (non-Javadoc)
127      * @see org.alfresco.web.scripts.processor.BaseRegisterableScriptProcessor#register()
128      */
129     public void register()
130     {
131         this.getScriptProcessorRegistry().registerScriptProcessor(this);
132     }
133 }