View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.extend.server.deployment.danube;
14  
15  import org.abstracthorizon.danube.connection.ConnectionHandler;
16  import org.abstracthorizon.danube.http.Selector;
17  import org.abstracthorizon.danube.http.matcher.Prefix;
18  import org.abstracthorizon.extend.server.deployment.Module;
19  import org.abstracthorizon.extend.server.deployment.ModuleId;
20  import org.abstracthorizon.extend.support.spring.service.ServiceApplicationContextModule;
21  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
22  import org.springframework.context.ApplicationContext;
23  
24  /**
25   * Module that represents Danube web application.
26   *
27   * @author Daniel Sendula
28   */
29  public class DanubeWebApplicationContext extends ServiceApplicationContextModule {
30  
31      /** "path&quot this context is deployed at root level with. */
32      protected Prefix matcher;
33  
34      /** Context path this web application is going to be deployed on */
35      protected String contextPath;
36  
37      /** Context */
38      protected ApplicationContext webServerContext;
39  
40      /**
41       * Empty constructor.
42       */
43      public DanubeWebApplicationContext(ModuleId moduleId) {
44          super(moduleId);
45      }
46  
47  
48      /**
49       * Sets up parser for this application context.
50       * This implementation uses {@link DanubeWarModuleXmlParser} without validation
51       * with {@link #internalClassLoader}.
52       */
53      protected void initBeanDefinitionReader(XmlBeanDefinitionReader xmlbeandefinitionreader) {
54          xmlbeandefinitionreader.setDocumentReaderClass(DanubeWarModuleXmlParser.class);
55          xmlbeandefinitionreader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
56          xmlbeandefinitionreader.setBeanClassLoader(internalClassLoader);
57      }
58  
59      /**
60       * Sets this module dependancy on (danube's) module that created loader.
61       */
62      protected void createInternal() {
63          super.createInternal();
64          Module module = (Module)webServerContext;
65          getDependsOn().add(module);
66          module.getDependOnThis().add(this);
67      }
68  
69      /**
70       * Obtains reference to the "web-application" bean and uses it while
71       * creating {@link Prefix} matcher with context path. Context path is or one supplied through
72       * "<context-path>" tag or name of the module if tag is not specified.
73       * Newly created matcher is then added to the http server connection handler.
74       */
75      @Override
76      protected void startInternal() {
77          super.startInternal();
78          Selector selector = (Selector)webServerContext.getBean("httpServerSelector");
79          ConnectionHandler handler = (ConnectionHandler)getBean("web-application");
80          matcher = new Prefix();
81  
82          ThreadContextHandler threadContextHandler = new ThreadContextHandler(handler, this);
83  
84          String contextPath = getContextPath();
85          if (contextPath == null) {
86              contextPath = "/" + getModuleId().getArtifactId();
87              setContextPath(contextPath);
88          }
89          if (!contextPath.startsWith("/")) {
90              contextPath = "/" + contextPath;
91              setContextPath(contextPath);
92          }
93  
94          matcher.setPrefix(contextPath);
95          matcher.setConnectionHandler(threadContextHandler);
96          selector.getComponents().add(matcher);
97      }
98  
99      /**
100      * Removes previously created matcher.
101      */
102     @Override
103     protected void stopInternal() {
104         super.stopInternal();
105         Selector selector = (Selector)webServerContext.getBean("httpServerSelector");
106         selector.getComponents().remove(matcher);
107     }
108 
109     /**
110      * Empty implementation
111      */
112     @Override
113     protected void destroyInternal() {
114         super.destroyInternal();
115     }
116 
117     /**
118      * Returns "web-application.xml"
119      * @return "web-application.xml"
120      */
121     protected String getContextFileName() {
122         return "web-application.xml";
123     }
124 
125     /**
126      * Returns web server's context
127      * @return web server's context
128      */
129     public ApplicationContext getWebServerContext() {
130         return webServerContext;
131     }
132 
133     /**
134      * Sets web server's context
135      * @param webServerContext web server's context
136      */
137     public void setWebServerContext(ApplicationContext webServerContext) {
138         this.webServerContext = webServerContext;
139     }
140 
141 
142     /**
143      * @return Returns the contextPath.
144      */
145     public String getContextPath() {
146         return contextPath;
147     }
148 
149 
150     /**
151      * @param contextPath The contextPath to set.
152      */
153     public void setContextPath(String contextPath) {
154         this.contextPath = contextPath;
155     }
156 
157 }