You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ke...@apache.org on 2013/01/31 00:23:53 UTC

[1/5] git commit: refs/heads/javelin - Remove temporary hacking and use Official way to wire-up servlet with injection under Spring

Remove temporary hacking and use Official way to wire-up servlet with injection under Spring


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/da2e6461
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/da2e6461
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/da2e6461

Branch: refs/heads/javelin
Commit: da2e6461a681f428a6db933eb3cc77df36a1ef39
Parents: 1e0709d
Author: Kelven Yang <ke...@gmail.com>
Authored: Tue Jan 29 14:33:18 2013 -0800
Committer: Kelven Yang <ke...@gmail.com>
Committed: Wed Jan 30 15:21:01 2013 -0800

----------------------------------------------------------------------
 .../com/cloud/bridge/service/EC2MainServlet.java   |   16 +-----
 pom.xml                                            |   28 +++++-----
 .../src/com/cloud/servlet/CloudStartupServlet.java |   24 ++-------
 .../src/com/cloud/servlet/ConsoleProxyServlet.java |   30 +++-------
 .../com/cloud/servlet/RegisterCompleteServlet.java |   35 +++----------
 utils/src/com/cloud/utils/LogUtils.java            |   43 +++++++++++++++
 6 files changed, 78 insertions(+), 98 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/da2e6461/awsapi/src/com/cloud/bridge/service/EC2MainServlet.java
----------------------------------------------------------------------
diff --git a/awsapi/src/com/cloud/bridge/service/EC2MainServlet.java b/awsapi/src/com/cloud/bridge/service/EC2MainServlet.java
index e76daae..a193291 100644
--- a/awsapi/src/com/cloud/bridge/service/EC2MainServlet.java
+++ b/awsapi/src/com/cloud/bridge/service/EC2MainServlet.java
@@ -32,6 +32,7 @@ import javax.servlet.http.HttpServletResponse;
 
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
+import org.springframework.web.context.support.SpringBeanAutowiringSupport;
 
 import com.cloud.bridge.persist.dao.CloudStackConfigurationDao;
 import com.cloud.bridge.util.ConfigurationHelper;
@@ -49,23 +50,10 @@ public class EC2MainServlet extends HttpServlet{
     private static boolean isEC2APIEnabled = false;
     public static final Logger logger = Logger.getLogger(EC2MainServlet.class);
     @Inject CloudStackConfigurationDao csDao;
-    static CloudStackConfigurationDao s_csDao;
     
     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
      * are stored.
@@ -74,7 +62,7 @@ public class EC2MainServlet extends HttpServlet{
     @DB
     public void init( ServletConfig config ) throws ServletException {
         try{
-        	initComponent();
+        	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());       	
             ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext());
             // check if API is enabled
             String value = csDao.getConfigValue(ENABLE_EC2_API);

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/da2e6461/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index b273cd2..604b39e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -192,7 +192,17 @@
 	  <artifactId>spring-core</artifactId>
 	  <version>${org.springframework.version}</version>
 	</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>
   	  <groupId>org.springframework</groupId>
   	  <artifactId>spring-expression</artifactId>
@@ -213,12 +223,6 @@
  
 	<dependency>
 	  <groupId>org.springframework</groupId>
-	  <artifactId>spring-context</artifactId>
-	  <version>${org.springframework.version}</version>
-	</dependency>
- 
-	<dependency>
-	  <groupId>org.springframework</groupId>
 	  <artifactId>spring-context-support</artifactId>
 	  <version>${org.springframework.version}</version>
 	</dependency>
@@ -247,18 +251,12 @@
 	  <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>
 	  <artifactId>spring-webmvc</artifactId>
 	  <version>${org.springframework.version}</version>
 	</dependency>
- 
+ -->
     <dependency>
     	<groupId>org.mockito</groupId>
     	<artifactId>mockito-all</artifactId>

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/da2e6461/server/src/com/cloud/servlet/CloudStartupServlet.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/servlet/CloudStartupServlet.java b/server/src/com/cloud/servlet/CloudStartupServlet.java
index bbcf353..6401ff1 100755
--- a/server/src/com/cloud/servlet/CloudStartupServlet.java
+++ b/server/src/com/cloud/servlet/CloudStartupServlet.java
@@ -16,21 +16,18 @@
 // under the License.
 package com.cloud.servlet;
 
-import java.io.File;
-
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 
 import org.apache.log4j.Logger;
-import org.apache.log4j.PropertyConfigurator;
-import org.apache.log4j.xml.DOMConfigurator;
+import org.springframework.web.context.support.SpringBeanAutowiringSupport;
 
 import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.server.ConfigurationServer;
 import com.cloud.server.ManagementServer;
-import com.cloud.utils.PropertiesUtil;
+import com.cloud.utils.LogUtils;
 import com.cloud.utils.SerialVersionUID;
 import com.cloud.utils.component.ComponentContext;
 
@@ -41,7 +38,7 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi
 
     @Override
     public void init() throws ServletException {
-    	initLog4j();
+    	LogUtils.initLog4j("log4j-cloud.xml");
         ConfigurationServer c = (ConfigurationServer)ComponentContext.getComponent(ConfigurationServer.Name);
         try {
             c.persistDefaultValues();
@@ -61,6 +58,7 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi
     @Override
     public void contextInitialized(ServletContextEvent sce) {
         try {
+        	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, sce.getServletContext());       	
             init();
         } catch (ServletException e) {
             s_logger.error("Exception starting management server ", e);
@@ -71,18 +69,4 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi
     @Override
     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());
-	        }
-	    }
-   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/da2e6461/server/src/com/cloud/servlet/ConsoleProxyServlet.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/servlet/ConsoleProxyServlet.java b/server/src/com/cloud/servlet/ConsoleProxyServlet.java
index 20bd511..c4b9334 100644
--- a/server/src/com/cloud/servlet/ConsoleProxyServlet.java
+++ b/server/src/com/cloud/servlet/ConsoleProxyServlet.java
@@ -25,10 +25,11 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import javax.annotation.PostConstruct;
 import javax.crypto.Mac;
 import javax.crypto.spec.SecretKeySpec;
 import javax.inject.Inject;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 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.log4j.Logger;
 import org.springframework.stereotype.Component;
+import org.springframework.web.context.support.SpringBeanAutowiringSupport;
 
 import com.cloud.exception.PermissionDeniedException;
 import com.cloud.host.HostVO;
@@ -71,29 +73,15 @@ public class ConsoleProxyServlet extends HttpServlet {
     @Inject ManagementServer _ms;
     @Inject IdentityService _identityService; 
 
-    static AccountManager s_accountMgr;
-    static VirtualMachineManager s_vmMgr;
     static ManagementServer s_ms;
-    static IdentityService s_identityService;
-    
+
     public ConsoleProxyServlet() {
     }
-    
-    @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(_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
+    public void init(ServletConfig config) throws ServletException {
+    	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());       	
+    	s_ms = _ms;
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/da2e6461/server/src/com/cloud/servlet/RegisterCompleteServlet.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/servlet/RegisterCompleteServlet.java b/server/src/com/cloud/servlet/RegisterCompleteServlet.java
index efddc92..702b617 100644
--- a/server/src/com/cloud/servlet/RegisterCompleteServlet.java
+++ b/server/src/com/cloud/servlet/RegisterCompleteServlet.java
@@ -19,16 +19,16 @@ package com.cloud.servlet;
 import java.net.URLEncoder;
 import java.util.List;
 
-import javax.annotation.PostConstruct;
 import javax.inject.Inject;
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
+import org.springframework.web.context.support.SpringBeanAutowiringSupport;
 
 import com.cloud.configuration.Configuration;
 import com.cloud.configuration.dao.ConfigurationDao;
@@ -40,7 +40,7 @@ import com.cloud.user.dao.UserDao;
 import com.cloud.utils.SerialVersionUID;
 
 @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());
 
     static final long serialVersionUID = SerialVersionUID.CloudStartupServlet;
@@ -49,35 +49,14 @@ public class RegisterCompleteServlet extends HttpServlet implements ServletConte
     @Inject ConfigurationDao _configDao;
     @Inject UserDao _userDao;
 
-    static AccountService s_accountSvc;
-    static ConfigurationDao s_configDao;
-    static UserDao s_userDao;
-    
     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
-    public void contextInitialized(ServletContextEvent sce) {
-    }
-
     @Override
-    public void contextDestroyed(ServletContextEvent sce) {
+    public void init(ServletConfig config) throws ServletException {
+    	SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());       	
     }
-
+    
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
         doGet(req, resp);

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/da2e6461/utils/src/com/cloud/utils/LogUtils.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/LogUtils.java b/utils/src/com/cloud/utils/LogUtils.java
new file mode 100644
index 0000000..975de83
--- /dev/null
+++ b/utils/src/com/cloud/utils/LogUtils.java
@@ -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());
+	        }
+	    }
+   }
+}