mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	Remove temporary hacking and use Official way to wire-up servlet with injection under Spring
This commit is contained in:
		
							parent
							
								
									1e0709d16d
								
							
						
					
					
						commit
						da2e6461a6
					
				| @ -32,6 +32,7 @@ import javax.servlet.http.HttpServletResponse; | |||||||
| 
 | 
 | ||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
|  | import org.springframework.web.context.support.SpringBeanAutowiringSupport; | ||||||
| 
 | 
 | ||||||
| import com.cloud.bridge.persist.dao.CloudStackConfigurationDao; | import com.cloud.bridge.persist.dao.CloudStackConfigurationDao; | ||||||
| import com.cloud.bridge.util.ConfigurationHelper; | import com.cloud.bridge.util.ConfigurationHelper; | ||||||
| @ -49,23 +50,10 @@ public class EC2MainServlet extends HttpServlet{ | |||||||
|     private static boolean isEC2APIEnabled = false; |     private static boolean isEC2APIEnabled = false; | ||||||
|     public static final Logger logger = Logger.getLogger(EC2MainServlet.class); |     public static final Logger logger = Logger.getLogger(EC2MainServlet.class); | ||||||
|     @Inject CloudStackConfigurationDao csDao; |     @Inject CloudStackConfigurationDao csDao; | ||||||
|     static CloudStackConfigurationDao s_csDao; |  | ||||||
|      |      | ||||||
|     public EC2MainServlet() { |     public EC2MainServlet() { | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     @PostConstruct |  | ||||||
|     void initComponent() {   |  | ||||||
|         // Servlet injection does not always work for servlet container |  | ||||||
|         // We use a hacking here to initialize static variables at Spring wiring time |  | ||||||
|         if(csDao != null) { |  | ||||||
|             s_csDao = csDao; |  | ||||||
|         } else { |  | ||||||
|             csDao = s_csDao; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|      |  | ||||||
|     /** |     /** | ||||||
|      * We build the path to where the keystore holding the WS-Security X509 certificates |      * We build the path to where the keystore holding the WS-Security X509 certificates | ||||||
|      * are stored. |      * are stored. | ||||||
| @ -74,7 +62,7 @@ public class EC2MainServlet extends HttpServlet{ | |||||||
|     @DB |     @DB | ||||||
|     public void init( ServletConfig config ) throws ServletException { |     public void init( ServletConfig config ) throws ServletException { | ||||||
|         try{ |         try{ | ||||||
|         	initComponent(); |         	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());       	 | ||||||
|             ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext()); |             ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext()); | ||||||
|             // check if API is enabled |             // check if API is enabled | ||||||
|             String value = csDao.getConfigValue(ENABLE_EC2_API); |             String value = csDao.getConfigValue(ENABLE_EC2_API); | ||||||
|  | |||||||
							
								
								
									
										28
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								pom.xml
									
									
									
									
									
								
							| @ -192,7 +192,17 @@ | |||||||
| 	  <artifactId>spring-core</artifactId> | 	  <artifactId>spring-core</artifactId> | ||||||
| 	  <version>${org.springframework.version}</version> | 	  <version>${org.springframework.version}</version> | ||||||
| 	</dependency> | 	</dependency> | ||||||
|   |     <dependency> | ||||||
|  |       <groupId>org.springframework</groupId> | ||||||
|  |       <artifactId>spring-context</artifactId> | ||||||
|  |       <version>${org.springframework.version}</version> | ||||||
|  |     </dependency> | ||||||
|  |     <dependency> | ||||||
|  |       <groupId>org.springframework</groupId> | ||||||
|  |       <artifactId>spring-web</artifactId> | ||||||
|  |       <version>${org.springframework.version}</version> | ||||||
|  |     </dependency> | ||||||
|  | <!--            | ||||||
| 	<dependency> | 	<dependency> | ||||||
|   	  <groupId>org.springframework</groupId> |   	  <groupId>org.springframework</groupId> | ||||||
|   	  <artifactId>spring-expression</artifactId> |   	  <artifactId>spring-expression</artifactId> | ||||||
| @ -211,12 +221,6 @@ | |||||||
| 	  <version>${org.springframework.version}</version> | 	  <version>${org.springframework.version}</version> | ||||||
| 	</dependency> | 	</dependency> | ||||||
|   |   | ||||||
| 	<dependency> |  | ||||||
| 	  <groupId>org.springframework</groupId> |  | ||||||
| 	  <artifactId>spring-context</artifactId> |  | ||||||
| 	  <version>${org.springframework.version}</version> |  | ||||||
| 	</dependency> |  | ||||||
|   |  | ||||||
| 	<dependency> | 	<dependency> | ||||||
| 	  <groupId>org.springframework</groupId> | 	  <groupId>org.springframework</groupId> | ||||||
| 	  <artifactId>spring-context-support</artifactId> | 	  <artifactId>spring-context-support</artifactId> | ||||||
| @ -247,18 +251,12 @@ | |||||||
| 	  <version>${org.springframework.version}</version> | 	  <version>${org.springframework.version}</version> | ||||||
| 	</dependency> | 	</dependency> | ||||||
|   |   | ||||||
| 	<dependency> |  	<dependency> | ||||||
| 	  <groupId>org.springframework</groupId> |  | ||||||
| 	  <artifactId>spring-web</artifactId> |  | ||||||
| 	  <version>${org.springframework.version}</version> |  | ||||||
| 	</dependency> |  | ||||||
|   |  | ||||||
| 	<dependency> |  | ||||||
| 	  <groupId>org.springframework</groupId> | 	  <groupId>org.springframework</groupId> | ||||||
| 	  <artifactId>spring-webmvc</artifactId> | 	  <artifactId>spring-webmvc</artifactId> | ||||||
| 	  <version>${org.springframework.version}</version> | 	  <version>${org.springframework.version}</version> | ||||||
| 	</dependency> | 	</dependency> | ||||||
|   |  --> | ||||||
|     <dependency> |     <dependency> | ||||||
|     	<groupId>org.mockito</groupId> |     	<groupId>org.mockito</groupId> | ||||||
|     	<artifactId>mockito-all</artifactId> |     	<artifactId>mockito-all</artifactId> | ||||||
|  | |||||||
| @ -16,21 +16,18 @@ | |||||||
| // under the License. | // under the License. | ||||||
| package com.cloud.servlet; | package com.cloud.servlet; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| 
 |  | ||||||
| import javax.servlet.ServletContextEvent; | import javax.servlet.ServletContextEvent; | ||||||
| import javax.servlet.ServletContextListener; | import javax.servlet.ServletContextListener; | ||||||
| import javax.servlet.ServletException; | import javax.servlet.ServletException; | ||||||
| import javax.servlet.http.HttpServlet; | import javax.servlet.http.HttpServlet; | ||||||
| 
 | 
 | ||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| import org.apache.log4j.PropertyConfigurator; | import org.springframework.web.context.support.SpringBeanAutowiringSupport; | ||||||
| import org.apache.log4j.xml.DOMConfigurator; |  | ||||||
| 
 | 
 | ||||||
| import com.cloud.exception.InvalidParameterValueException; | import com.cloud.exception.InvalidParameterValueException; | ||||||
| import com.cloud.server.ConfigurationServer; | import com.cloud.server.ConfigurationServer; | ||||||
| import com.cloud.server.ManagementServer; | import com.cloud.server.ManagementServer; | ||||||
| import com.cloud.utils.PropertiesUtil; | import com.cloud.utils.LogUtils; | ||||||
| import com.cloud.utils.SerialVersionUID; | import com.cloud.utils.SerialVersionUID; | ||||||
| import com.cloud.utils.component.ComponentContext; | import com.cloud.utils.component.ComponentContext; | ||||||
| 
 | 
 | ||||||
| @ -41,7 +38,7 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void init() throws ServletException { |     public void init() throws ServletException { | ||||||
|     	initLog4j(); |     	LogUtils.initLog4j("log4j-cloud.xml"); | ||||||
|         ConfigurationServer c = (ConfigurationServer)ComponentContext.getComponent(ConfigurationServer.Name); |         ConfigurationServer c = (ConfigurationServer)ComponentContext.getComponent(ConfigurationServer.Name); | ||||||
|         try { |         try { | ||||||
|             c.persistDefaultValues(); |             c.persistDefaultValues(); | ||||||
| @ -61,6 +58,7 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi | |||||||
|     @Override |     @Override | ||||||
|     public void contextInitialized(ServletContextEvent sce) { |     public void contextInitialized(ServletContextEvent sce) { | ||||||
|         try { |         try { | ||||||
|  |         	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, sce.getServletContext());       	 | ||||||
|             init(); |             init(); | ||||||
|         } catch (ServletException e) { |         } catch (ServletException e) { | ||||||
|             s_logger.error("Exception starting management server ", e); |             s_logger.error("Exception starting management server ", e); | ||||||
| @ -71,18 +69,4 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi | |||||||
|     @Override |     @Override | ||||||
|     public void contextDestroyed(ServletContextEvent sce) { |     public void contextDestroyed(ServletContextEvent sce) { | ||||||
|     } |     } | ||||||
|      |  | ||||||
|     private void initLog4j() { |  | ||||||
|     	File file = PropertiesUtil.findConfigFile("log4j-cloud.xml"); |  | ||||||
|     	if (file != null) { |  | ||||||
|         s_logger.info("log4j configuration found at " + file.getAbsolutePath()); |  | ||||||
|         DOMConfigurator.configureAndWatch(file.getAbsolutePath()); |  | ||||||
| 	    } else { |  | ||||||
| 	        file = PropertiesUtil.findConfigFile("log4j-cloud.properties"); |  | ||||||
| 	        if (file != null) { |  | ||||||
| 	            s_logger.info("log4j configuration found at " + file.getAbsolutePath()); |  | ||||||
| 	            PropertyConfigurator.configureAndWatch(file.getAbsolutePath()); |  | ||||||
| 	        } |  | ||||||
| 	    } |  | ||||||
|    } |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -25,10 +25,11 @@ import java.util.HashMap; | |||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| import javax.annotation.PostConstruct; |  | ||||||
| import javax.crypto.Mac; | import javax.crypto.Mac; | ||||||
| import javax.crypto.spec.SecretKeySpec; | import javax.crypto.spec.SecretKeySpec; | ||||||
| import javax.inject.Inject; | import javax.inject.Inject; | ||||||
|  | import javax.servlet.ServletConfig; | ||||||
|  | import javax.servlet.ServletException; | ||||||
| import javax.servlet.http.HttpServlet; | import javax.servlet.http.HttpServlet; | ||||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||||
| import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||||
| @ -38,6 +39,7 @@ import org.apache.cloudstack.api.IdentityService; | |||||||
| import org.apache.commons.codec.binary.Base64; | import org.apache.commons.codec.binary.Base64; | ||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
|  | import org.springframework.web.context.support.SpringBeanAutowiringSupport; | ||||||
| 
 | 
 | ||||||
| import com.cloud.exception.PermissionDeniedException; | import com.cloud.exception.PermissionDeniedException; | ||||||
| import com.cloud.host.HostVO; | import com.cloud.host.HostVO; | ||||||
| @ -71,29 +73,15 @@ public class ConsoleProxyServlet extends HttpServlet { | |||||||
|     @Inject ManagementServer _ms; |     @Inject ManagementServer _ms; | ||||||
|     @Inject IdentityService _identityService;  |     @Inject IdentityService _identityService;  | ||||||
| 
 | 
 | ||||||
|     static AccountManager s_accountMgr; |  | ||||||
|     static VirtualMachineManager s_vmMgr; |  | ||||||
|     static ManagementServer s_ms; |     static ManagementServer s_ms; | ||||||
|     static IdentityService s_identityService; |  | ||||||
| 
 | 
 | ||||||
|     public ConsoleProxyServlet() { |     public ConsoleProxyServlet() { | ||||||
|     } |     } | ||||||
|    |    | ||||||
|     @PostConstruct |     @Override | ||||||
|     void initComponent() {	 |     public void init(ServletConfig config) throws ServletException { | ||||||
|     	// Servlet injection does not always work for servlet container |     	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());       	 | ||||||
|     	// We use a hacking here to initialize static variables at Spring wiring time |     	s_ms = _ms; | ||||||
|     	if(_accountMgr != null) { |  | ||||||
|     	    s_accountMgr = _accountMgr; |  | ||||||
|     	    s_vmMgr = _vmMgr; |  | ||||||
|     	    s_ms = _ms; |  | ||||||
|     	    s_identityService = _identityService; |  | ||||||
|     	} else { |  | ||||||
|     	    _accountMgr = s_accountMgr; |  | ||||||
|     	    _vmMgr = s_vmMgr; |  | ||||||
|     	    _ms = s_ms; |  | ||||||
|     	    _identityService = s_identityService; |  | ||||||
|     	} |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     @Override |     @Override | ||||||
|  | |||||||
| @ -19,16 +19,16 @@ package com.cloud.servlet; | |||||||
| import java.net.URLEncoder; | import java.net.URLEncoder; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| 
 | 
 | ||||||
| import javax.annotation.PostConstruct; |  | ||||||
| import javax.inject.Inject; | import javax.inject.Inject; | ||||||
| import javax.servlet.ServletContextEvent; | import javax.servlet.ServletConfig; | ||||||
| import javax.servlet.ServletContextListener; | import javax.servlet.ServletException; | ||||||
| import javax.servlet.http.HttpServlet; | import javax.servlet.http.HttpServlet; | ||||||
| import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||||
| import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||||
| 
 | 
 | ||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
|  | import org.springframework.web.context.support.SpringBeanAutowiringSupport; | ||||||
| 
 | 
 | ||||||
| import com.cloud.configuration.Configuration; | import com.cloud.configuration.Configuration; | ||||||
| import com.cloud.configuration.dao.ConfigurationDao; | import com.cloud.configuration.dao.ConfigurationDao; | ||||||
| @ -40,7 +40,7 @@ import com.cloud.user.dao.UserDao; | |||||||
| import com.cloud.utils.SerialVersionUID; | import com.cloud.utils.SerialVersionUID; | ||||||
| 
 | 
 | ||||||
| @Component("registerCompleteServlet") | @Component("registerCompleteServlet") | ||||||
| public class RegisterCompleteServlet extends HttpServlet implements ServletContextListener { | public class RegisterCompleteServlet extends HttpServlet { | ||||||
|     public static final Logger s_logger = Logger.getLogger(RegisterCompleteServlet.class.getName()); |     public static final Logger s_logger = Logger.getLogger(RegisterCompleteServlet.class.getName()); | ||||||
| 
 | 
 | ||||||
|     static final long serialVersionUID = SerialVersionUID.CloudStartupServlet; |     static final long serialVersionUID = SerialVersionUID.CloudStartupServlet; | ||||||
| @ -49,33 +49,12 @@ public class RegisterCompleteServlet extends HttpServlet implements ServletConte | |||||||
|     @Inject ConfigurationDao _configDao; |     @Inject ConfigurationDao _configDao; | ||||||
|     @Inject UserDao _userDao; |     @Inject UserDao _userDao; | ||||||
| 
 | 
 | ||||||
|     static AccountService s_accountSvc; |  | ||||||
|     static ConfigurationDao s_configDao; |  | ||||||
|     static UserDao s_userDao; |  | ||||||
|      |  | ||||||
|     public RegisterCompleteServlet() { |     public RegisterCompleteServlet() { | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     @PostConstruct |  | ||||||
|     void initComponent() { |  | ||||||
|     	// Hakcing way to make servlet injection work for now |  | ||||||
|     	if(_accountSvc != null) { |  | ||||||
|     	    s_accountSvc = _accountSvc; |  | ||||||
|     	    s_configDao = _configDao; |  | ||||||
|     	    s_userDao = _userDao; |  | ||||||
|     	} else { |  | ||||||
|     	    _accountSvc = s_accountSvc; |  | ||||||
|     	    _configDao = s_configDao; |  | ||||||
|     	    _userDao = s_userDao; |  | ||||||
|     	} |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     @Override |     @Override | ||||||
|     public void contextInitialized(ServletContextEvent sce) { |     public void init(ServletConfig config) throws ServletException { | ||||||
|     } |     	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());       	 | ||||||
| 
 |  | ||||||
|     @Override |  | ||||||
|     public void contextDestroyed(ServletContextEvent sce) { |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     @Override |     @Override | ||||||
|  | |||||||
							
								
								
									
										43
									
								
								utils/src/com/cloud/utils/LogUtils.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								utils/src/com/cloud/utils/LogUtils.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,43 @@ | |||||||
|  | // Licensed to the Apache Software Foundation (ASF) under one | ||||||
|  | // or more contributor license agreements.  See the NOTICE file | ||||||
|  | // distributed with this work for additional information | ||||||
|  | // regarding copyright ownership.  The ASF licenses this file | ||||||
|  | // to you under the Apache License, Version 2.0 (the | ||||||
|  | // "License"); you may not use this file except in compliance | ||||||
|  | // the License.  You may obtain a copy of the License at | ||||||
|  | // | ||||||
|  | // http://www.apache.org/licenses/LICENSE-2.0 | ||||||
|  | // | ||||||
|  | // Unless required by applicable law or agreed to in writing, | ||||||
|  | // software distributed under the License is distributed on an | ||||||
|  | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
|  | // KIND, either express or implied.  See the License for the | ||||||
|  | // specific language governing permissions and limitations | ||||||
|  | // under the License. | ||||||
|  | package com.cloud.utils; | ||||||
|  | 
 | ||||||
|  | import java.io.File; | ||||||
|  | 
 | ||||||
|  | import org.apache.log4j.Logger; | ||||||
|  | import org.apache.log4j.PropertyConfigurator; | ||||||
|  | import org.apache.log4j.xml.DOMConfigurator; | ||||||
|  | 
 | ||||||
|  | public class LogUtils { | ||||||
|  | 	public static final Logger s_logger = Logger.getLogger(LogUtils.class); | ||||||
|  | 	 | ||||||
|  |     public static void initLog4j(String log4jConfigFileName) { | ||||||
|  |     	assert(log4jConfigFileName != null); | ||||||
|  |     	File file = PropertiesUtil.findConfigFile(log4jConfigFileName); | ||||||
|  |     	if (file != null) { | ||||||
|  | 	        s_logger.info("log4j configuration found at " + file.getAbsolutePath()); | ||||||
|  | 	        DOMConfigurator.configureAndWatch(file.getAbsolutePath()); | ||||||
|  | 	    } else { | ||||||
|  | 	    	String nameWithoutExtension = log4jConfigFileName.substring(0, log4jConfigFileName.lastIndexOf('.')); | ||||||
|  | 	        file = PropertiesUtil.findConfigFile(nameWithoutExtension + ".properties"); | ||||||
|  | 	        if (file != null) { | ||||||
|  | 	            s_logger.info("log4j configuration found at " + file.getAbsolutePath()); | ||||||
|  | 	            PropertyConfigurator.configureAndWatch(file.getAbsolutePath()); | ||||||
|  | 	        } | ||||||
|  | 	    } | ||||||
|  |    } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user