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 org.springframework.context.ApplicationContext;
22  import org.springframework.context.ApplicationContextAware;
23  import org.springframework.context.ApplicationEvent;
24  import org.springframework.context.ApplicationListener;
25  import org.springframework.context.event.ContextRefreshedEvent;
26  import org.springframework.extensions.webscripts.TemplateProcessorRegistry;
27  
28  /**
29   * Base class for a registerable processor
30   * 
31   * @author muzquiano
32   */
33  public abstract class BaseRegisterableTemplateProcessor extends BaseProcessor implements ApplicationContextAware, ApplicationListener
34  {
35      private ApplicationContext applicationContext;
36      
37      private TemplateProcessorRegistry templateProcessorRegistry;
38      
39      /**
40       * Sets the template processor registry to which this processor
41       * will be registered
42       * 
43       * @param templateProcessorRegistry the script processor registry
44       */
45      public void setTemplateProcessorRegistry(TemplateProcessorRegistry templateProcessorRegistry)
46      {
47          this.templateProcessorRegistry = templateProcessorRegistry;
48      }
49      
50      /**
51       * Gets the template processor registry
52       * 
53       * @return template processor registry
54       */
55      public TemplateProcessorRegistry getTemplateProcessorRegistry()
56      {
57          return this.templateProcessorRegistry;
58      }
59      
60      /**
61       * Sets the application context.
62       * 
63       * @param applicationContext the new application context
64       */
65      public void setApplicationContext(ApplicationContext applicationContext)
66      {
67          this.applicationContext = applicationContext;
68      }
69      
70      /* (non-Javadoc)
71       * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
72       */
73      public void onApplicationEvent(ApplicationEvent event)
74      {
75          if (event instanceof ContextRefreshedEvent)
76          {
77              ContextRefreshedEvent refreshEvent = (ContextRefreshedEvent)event;
78              ApplicationContext refreshContext = refreshEvent.getApplicationContext();
79              if (refreshContext != null && refreshContext.equals(applicationContext))
80              {
81                  // call init method
82                  init();
83                  
84                  // register with root processor
85                  register();
86              }
87          }
88      }
89      
90      /**
91       * Inits the processor
92       */
93      public abstract void init();
94  
95      /**
96       * Registers this processor with the parent processor
97       */
98      public abstract void register();
99  }