View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.services.remoting.protocol;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
25  import org.springframework.web.context.support.AbstractRefreshableWebApplicationContext;
26  import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
27  
28  import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
29  import ch.elca.el4j.services.remoting.RemotingServiceExporter;
30  
31  /**
32   * This is an abstract <code>InetSocketAddress</code> protocol for using in
33   * web servers. It is used to map servlets to a path in the current context.
34   *
35   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/remoting_core/src/main/java/ch/elca/el4j/services/remoting/protocol/AbstractInetSocketAddressWebProtocol.java $
36   *
37   * @author Martin Zeltner (MZE)
38   */
39  public abstract class AbstractInetSocketAddressWebProtocol
40  	extends AbstractInetSocketAddressProtocol {
41  
42  	/**
43  	 * Is the context path of the webserver where the service is running.
44  	 */
45  	private String m_contextPath;
46  
47  	/**
48  	 * This map contains all url mappings for this protocol.
49  	 */
50  	private Map m_urlMappings = new HashMap();
51  
52  	/**
53  	 * This map contains all flags, which indicates if an url map is already
54  	 * initialized.
55  	 */
56  	private Map m_urlMappingsInitialized = new HashMap();
57  
58  	/**
59  	 * @return Returns the contextPath.
60  	 */
61  	public String getContextPath() {
62  		return m_contextPath;
63  	}
64  
65  	/**
66  	 * @param contextPath
67  	 *            The contextPath to set.
68  	 */
69  	public void setContextPath(String contextPath) {
70  		m_contextPath = contextPath;
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	public void afterPropertiesSet() throws Exception {
77  		super.afterPropertiesSet();
78  		CoreNotificationHelper.notifyIfEssentialPropertyIsEmpty(
79  				getContextPath(), "contextPath", this);
80  	}
81  
82  	/**
83  	 * {@inheritDoc}
84  	 */
85  	public void prepareExporterDependentBeans(
86  			RemotingServiceExporter exporterBean) {
87  		/**
88  		 * If the this bean runs in a web server, it has to be a servlet and so
89  		 * it needs to be mapped on the current context. In this phase we only
90  		 * prepare the mapping. It will be done in a second phase to prohibit a
91  		 * circular dependency.
92  		 */
93  		if (exporterBean.getApplicationContext()
94  			instanceof AbstractRefreshableWebApplicationContext) {
95  			AbstractRefreshableWebApplicationContext parentAppContext
96  				= (AbstractRefreshableWebApplicationContext) exporterBean
97  					.getApplicationContext();
98  			Properties mappings = new Properties();
99  			mappings.put("/" + exporterBean.getServiceName(), exporterBean
100 					.getBeanName());
101 			SimpleUrlHandlerMapping urlMapping = new SimpleUrlHandlerMapping();
102 			urlMapping.setMappings(mappings);
103 
104 			m_urlMappings.put(exporterBean.getBeanName(), urlMapping);
105 
106 			ConfigurableListableBeanFactory configurableBeanFactory
107 				= parentAppContext.getBeanFactory();
108 			configurableBeanFactory.registerSingleton("handlerMappingForBean"
109 					+ exporterBean.getBeanName(), urlMapping);
110 		}
111 	}
112 
113 	/**
114 	 * {@inheritDoc}
115 	 */
116 	public void finalizeExporterDependentBeans(
117 			RemotingServiceExporter exporterBean) {
118 		/**
119 		 * If the url mapping is not already allocated to the application, this
120 		 * will be done here. This allocation will be done only once.
121 		 */
122 		String beanName = exporterBean.getBeanName();
123 		if (!m_urlMappingsInitialized.containsKey(beanName)
124 				&& m_urlMappings.containsKey(beanName)) {
125 			SimpleUrlHandlerMapping urlMapping
126 				= (SimpleUrlHandlerMapping) m_urlMappings
127 					.get(beanName);
128 			m_urlMappingsInitialized.put(beanName, urlMapping);
129 			urlMapping.setApplicationContext(exporterBean
130 					.getApplicationContext());
131 		}
132 	}
133 }