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

This commit is contained in:
Kelven Yang 2013-01-29 14:33:18 -08:00
parent 1e0709d16d
commit da2e6461a6
6 changed files with 78 additions and 98 deletions

View File

@ -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);

26
pom.xml
View File

@ -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>
@ -211,12 +221,6 @@
<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-context-support</artifactId>
@ -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>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>

View File

@ -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());
}
}
}
}

View File

@ -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;
@Override
public void init(ServletConfig config) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
s_ms = _ms;
s_identityService = _identityService;
} else {
_accountMgr = s_accountMgr;
_vmMgr = s_vmMgr;
_ms = s_ms;
_identityService = s_identityService;
}
}
@Override

View File

@ -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,33 +49,12 @@ 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

View 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());
}
}
}
}