Skip to content

Commit da2e646

Browse files
committed
Remove temporary hacking and use Official way to wire-up servlet with injection under Spring
1 parent 1e0709d commit da2e646

6 files changed

Lines changed: 78 additions & 98 deletions

File tree

awsapi/src/com/cloud/bridge/service/EC2MainServlet.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import org.apache.log4j.Logger;
3434
import org.springframework.stereotype.Component;
35+
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
3536

3637
import com.cloud.bridge.persist.dao.CloudStackConfigurationDao;
3738
import com.cloud.bridge.util.ConfigurationHelper;
@@ -49,23 +50,10 @@ public class EC2MainServlet extends HttpServlet{
4950
private static boolean isEC2APIEnabled = false;
5051
public static final Logger logger = Logger.getLogger(EC2MainServlet.class);
5152
@Inject CloudStackConfigurationDao csDao;
52-
static CloudStackConfigurationDao s_csDao;
5353

5454
public EC2MainServlet() {
5555
}
5656

57-
@PostConstruct
58-
void initComponent() {
59-
// Servlet injection does not always work for servlet container
60-
// We use a hacking here to initialize static variables at Spring wiring time
61-
if(csDao != null) {
62-
s_csDao = csDao;
63-
} else {
64-
csDao = s_csDao;
65-
}
66-
}
67-
68-
6957
/**
7058
* We build the path to where the keystore holding the WS-Security X509 certificates
7159
* are stored.
@@ -74,7 +62,7 @@ void initComponent() {
7462
@DB
7563
public void init( ServletConfig config ) throws ServletException {
7664
try{
77-
initComponent();
65+
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
7866
ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext());
7967
// check if API is enabled
8068
String value = csDao.getConfigValue(ENABLE_EC2_API);

pom.xml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,17 @@
192192
<artifactId>spring-core</artifactId>
193193
<version>${org.springframework.version}</version>
194194
</dependency>
195-
195+
<dependency>
196+
<groupId>org.springframework</groupId>
197+
<artifactId>spring-context</artifactId>
198+
<version>${org.springframework.version}</version>
199+
</dependency>
200+
<dependency>
201+
<groupId>org.springframework</groupId>
202+
<artifactId>spring-web</artifactId>
203+
<version>${org.springframework.version}</version>
204+
</dependency>
205+
<!--
196206
<dependency>
197207
<groupId>org.springframework</groupId>
198208
<artifactId>spring-expression</artifactId>
@@ -211,12 +221,6 @@
211221
<version>${org.springframework.version}</version>
212222
</dependency>
213223
214-
<dependency>
215-
<groupId>org.springframework</groupId>
216-
<artifactId>spring-context</artifactId>
217-
<version>${org.springframework.version}</version>
218-
</dependency>
219-
220224
<dependency>
221225
<groupId>org.springframework</groupId>
222226
<artifactId>spring-context-support</artifactId>
@@ -247,18 +251,12 @@
247251
<version>${org.springframework.version}</version>
248252
</dependency>
249253
250-
<dependency>
251-
<groupId>org.springframework</groupId>
252-
<artifactId>spring-web</artifactId>
253-
<version>${org.springframework.version}</version>
254-
</dependency>
255-
256-
<dependency>
254+
<dependency>
257255
<groupId>org.springframework</groupId>
258256
<artifactId>spring-webmvc</artifactId>
259257
<version>${org.springframework.version}</version>
260258
</dependency>
261-
259+
-->
262260
<dependency>
263261
<groupId>org.mockito</groupId>
264262
<artifactId>mockito-all</artifactId>

server/src/com/cloud/servlet/CloudStartupServlet.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,18 @@
1616
// under the License.
1717
package com.cloud.servlet;
1818

19-
import java.io.File;
20-
2119
import javax.servlet.ServletContextEvent;
2220
import javax.servlet.ServletContextListener;
2321
import javax.servlet.ServletException;
2422
import javax.servlet.http.HttpServlet;
2523

2624
import org.apache.log4j.Logger;
27-
import org.apache.log4j.PropertyConfigurator;
28-
import org.apache.log4j.xml.DOMConfigurator;
25+
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
2926

3027
import com.cloud.exception.InvalidParameterValueException;
3128
import com.cloud.server.ConfigurationServer;
3229
import com.cloud.server.ManagementServer;
33-
import com.cloud.utils.PropertiesUtil;
30+
import com.cloud.utils.LogUtils;
3431
import com.cloud.utils.SerialVersionUID;
3532
import com.cloud.utils.component.ComponentContext;
3633

@@ -41,7 +38,7 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi
4138

4239
@Override
4340
public void init() throws ServletException {
44-
initLog4j();
41+
LogUtils.initLog4j("log4j-cloud.xml");
4542
ConfigurationServer c = (ConfigurationServer)ComponentContext.getComponent(ConfigurationServer.Name);
4643
try {
4744
c.persistDefaultValues();
@@ -61,6 +58,7 @@ public void init() throws ServletException {
6158
@Override
6259
public void contextInitialized(ServletContextEvent sce) {
6360
try {
61+
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, sce.getServletContext());
6462
init();
6563
} catch (ServletException e) {
6664
s_logger.error("Exception starting management server ", e);
@@ -71,18 +69,4 @@ public void contextInitialized(ServletContextEvent sce) {
7169
@Override
7270
public void contextDestroyed(ServletContextEvent sce) {
7371
}
74-
75-
private void initLog4j() {
76-
File file = PropertiesUtil.findConfigFile("log4j-cloud.xml");
77-
if (file != null) {
78-
s_logger.info("log4j configuration found at " + file.getAbsolutePath());
79-
DOMConfigurator.configureAndWatch(file.getAbsolutePath());
80-
} else {
81-
file = PropertiesUtil.findConfigFile("log4j-cloud.properties");
82-
if (file != null) {
83-
s_logger.info("log4j configuration found at " + file.getAbsolutePath());
84-
PropertyConfigurator.configureAndWatch(file.getAbsolutePath());
85-
}
86-
}
87-
}
8872
}

server/src/com/cloud/servlet/ConsoleProxyServlet.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28-
import javax.annotation.PostConstruct;
2928
import javax.crypto.Mac;
3029
import javax.crypto.spec.SecretKeySpec;
3130
import javax.inject.Inject;
31+
import javax.servlet.ServletConfig;
32+
import javax.servlet.ServletException;
3233
import javax.servlet.http.HttpServlet;
3334
import javax.servlet.http.HttpServletRequest;
3435
import javax.servlet.http.HttpServletResponse;
@@ -38,6 +39,7 @@
3839
import org.apache.commons.codec.binary.Base64;
3940
import org.apache.log4j.Logger;
4041
import org.springframework.stereotype.Component;
42+
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
4143

4244
import com.cloud.exception.PermissionDeniedException;
4345
import com.cloud.host.HostVO;
@@ -71,29 +73,15 @@ public class ConsoleProxyServlet extends HttpServlet {
7173
@Inject ManagementServer _ms;
7274
@Inject IdentityService _identityService;
7375

74-
static AccountManager s_accountMgr;
75-
static VirtualMachineManager s_vmMgr;
7676
static ManagementServer s_ms;
77-
static IdentityService s_identityService;
78-
77+
7978
public ConsoleProxyServlet() {
8079
}
81-
82-
@PostConstruct
83-
void initComponent() {
84-
// Servlet injection does not always work for servlet container
85-
// We use a hacking here to initialize static variables at Spring wiring time
86-
if(_accountMgr != null) {
87-
s_accountMgr = _accountMgr;
88-
s_vmMgr = _vmMgr;
89-
s_ms = _ms;
90-
s_identityService = _identityService;
91-
} else {
92-
_accountMgr = s_accountMgr;
93-
_vmMgr = s_vmMgr;
94-
_ms = s_ms;
95-
_identityService = s_identityService;
96-
}
80+
81+
@Override
82+
public void init(ServletConfig config) throws ServletException {
83+
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
84+
s_ms = _ms;
9785
}
9886

9987
@Override

server/src/com/cloud/servlet/RegisterCompleteServlet.java

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
import java.net.URLEncoder;
2020
import java.util.List;
2121

22-
import javax.annotation.PostConstruct;
2322
import javax.inject.Inject;
24-
import javax.servlet.ServletContextEvent;
25-
import javax.servlet.ServletContextListener;
23+
import javax.servlet.ServletConfig;
24+
import javax.servlet.ServletException;
2625
import javax.servlet.http.HttpServlet;
2726
import javax.servlet.http.HttpServletRequest;
2827
import javax.servlet.http.HttpServletResponse;
2928

3029
import org.apache.log4j.Logger;
3130
import org.springframework.stereotype.Component;
31+
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
3232

3333
import com.cloud.configuration.Configuration;
3434
import com.cloud.configuration.dao.ConfigurationDao;
@@ -40,7 +40,7 @@
4040
import com.cloud.utils.SerialVersionUID;
4141

4242
@Component("registerCompleteServlet")
43-
public class RegisterCompleteServlet extends HttpServlet implements ServletContextListener {
43+
public class RegisterCompleteServlet extends HttpServlet {
4444
public static final Logger s_logger = Logger.getLogger(RegisterCompleteServlet.class.getName());
4545

4646
static final long serialVersionUID = SerialVersionUID.CloudStartupServlet;
@@ -49,35 +49,14 @@ public class RegisterCompleteServlet extends HttpServlet implements ServletConte
4949
@Inject ConfigurationDao _configDao;
5050
@Inject UserDao _userDao;
5151

52-
static AccountService s_accountSvc;
53-
static ConfigurationDao s_configDao;
54-
static UserDao s_userDao;
55-
5652
public RegisterCompleteServlet() {
5753
}
5854

59-
@PostConstruct
60-
void initComponent() {
61-
// Hakcing way to make servlet injection work for now
62-
if(_accountSvc != null) {
63-
s_accountSvc = _accountSvc;
64-
s_configDao = _configDao;
65-
s_userDao = _userDao;
66-
} else {
67-
_accountSvc = s_accountSvc;
68-
_configDao = s_configDao;
69-
_userDao = s_userDao;
70-
}
71-
}
72-
73-
@Override
74-
public void contextInitialized(ServletContextEvent sce) {
75-
}
76-
7755
@Override
78-
public void contextDestroyed(ServletContextEvent sce) {
56+
public void init(ServletConfig config) throws ServletException {
57+
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
7958
}
80-
59+
8160
@Override
8261
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
8362
doGet(req, resp);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.utils;
18+
19+
import java.io.File;
20+
21+
import org.apache.log4j.Logger;
22+
import org.apache.log4j.PropertyConfigurator;
23+
import org.apache.log4j.xml.DOMConfigurator;
24+
25+
public class LogUtils {
26+
public static final Logger s_logger = Logger.getLogger(LogUtils.class);
27+
28+
public static void initLog4j(String log4jConfigFileName) {
29+
assert(log4jConfigFileName != null);
30+
File file = PropertiesUtil.findConfigFile(log4jConfigFileName);
31+
if (file != null) {
32+
s_logger.info("log4j configuration found at " + file.getAbsolutePath());
33+
DOMConfigurator.configureAndWatch(file.getAbsolutePath());
34+
} else {
35+
String nameWithoutExtension = log4jConfigFileName.substring(0, log4jConfigFileName.lastIndexOf('.'));
36+
file = PropertiesUtil.findConfigFile(nameWithoutExtension + ".properties");
37+
if (file != null) {
38+
s_logger.info("log4j configuration found at " + file.getAbsolutePath());
39+
PropertyConfigurator.configureAndWatch(file.getAbsolutePath());
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)