diff --git a/awsapi/src/com/cloud/bridge/service/EC2MainServlet.java b/awsapi/src/com/cloud/bridge/service/EC2MainServlet.java index e76daaeceea..a1932918d7f 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); diff --git a/pom.xml b/pom.xml index b273cd2e6be..604b39ec1b0 100644 --- a/pom.xml +++ b/pom.xml @@ -192,7 +192,17 @@ spring-core ${org.springframework.version} - + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-web + ${org.springframework.version} + + org.mockito mockito-all diff --git a/server/src/com/cloud/servlet/CloudStartupServlet.java b/server/src/com/cloud/servlet/CloudStartupServlet.java index bbcf3535300..6401ff163a9 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()); - } - } - } } diff --git a/server/src/com/cloud/servlet/ConsoleProxyServlet.java b/server/src/com/cloud/servlet/ConsoleProxyServlet.java index 20bd511b36b..c4b93349080 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 diff --git a/server/src/com/cloud/servlet/RegisterCompleteServlet.java b/server/src/com/cloud/servlet/RegisterCompleteServlet.java index efddc9232d5..702b6173b87 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 init(ServletConfig config) throws ServletException { + SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext()); } - @Override - public void contextInitialized(ServletContextEvent sce) { - } - - @Override - public void contextDestroyed(ServletContextEvent sce) { - } - @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { doGet(req, resp); diff --git a/utils/src/com/cloud/utils/LogUtils.java b/utils/src/com/cloud/utils/LogUtils.java new file mode 100644 index 00000000000..975de831622 --- /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()); + } + } + } +}