Now initialising the controllers with the ServletConfig. This is not a particularly "Springy" thing to do but the aim of the demo is not to write a perfect Spring webapp but to show how to integrate jclouds in a Spring application.

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2584 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
sharedocs1@gmail.com 2010-01-03 20:50:55 +00:00
parent 4bff262634
commit fd7471e4d1
1 changed files with 33 additions and 3 deletions

View File

@ -1,14 +1,21 @@
package org.jclouds.demo.tweetstore.config; package org.jclouds.demo.tweetstore.config;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map; import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import org.jclouds.demo.tweetstore.controller.AddTweetsController; import org.jclouds.demo.tweetstore.controller.AddTweetsController;
import org.jclouds.demo.tweetstore.controller.StoreTweetsController; import org.jclouds.demo.tweetstore.controller.StoreTweetsController;
import org.jclouds.demo.tweetstore.functions.ServiceToStoredTweetStatuses; import org.jclouds.demo.tweetstore.functions.ServiceToStoredTweetStatuses;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.SimpleServletHandlerAdapter; import org.springframework.web.servlet.handler.SimpleServletHandlerAdapter;
@ -22,20 +29,35 @@ import com.google.common.collect.Maps;
* @author Andrew Phillips * @author Andrew Phillips
*/ */
@Configuration @Configuration
public class SpringServletConfig { public class SpringServletConfig implements ServletConfigAware {
private ServletConfig servletConfig;
@Inject @Inject
private SpringAppConfig appConfig; private SpringAppConfig appConfig;
@Bean @Bean
public StoreTweetsController storeTweetsController() { public StoreTweetsController storeTweetsController() {
return new StoreTweetsController(appConfig.providerTypeToBlobStoreMap, StoreTweetsController controller = new StoreTweetsController(appConfig.providerTypeToBlobStoreMap,
appConfig.container, appConfig.twitterClient); appConfig.container, appConfig.twitterClient);
injectServletConfig(controller);
return controller;
} }
@Bean @Bean
public AddTweetsController addTweetsController() { public AddTweetsController addTweetsController() {
return new AddTweetsController(appConfig.providerTypeToBlobStoreMap, AddTweetsController controller = new AddTweetsController(appConfig.providerTypeToBlobStoreMap,
serviceToStoredTweetStatuses()); serviceToStoredTweetStatuses());
injectServletConfig(controller);
return controller;
}
private void injectServletConfig(Servlet servlet) {
checkNotNull(servletConfig);
try {
servlet.init(servletConfig);
} catch (ServletException exception) {
throw new BeanCreationException("Unable to instantiate " + servlet, exception);
}
} }
@Bean @Bean
@ -63,4 +85,12 @@ public class SpringServletConfig {
public HandlerAdapter servletHandlerAdapter() { public HandlerAdapter servletHandlerAdapter() {
return new SimpleServletHandlerAdapter(); return new SimpleServletHandlerAdapter();
} }
/* (non-Javadoc)
* @see org.springframework.web.context.ServletConfigAware#setServletConfig(javax.servlet.ServletConfig)
*/
@Override
public void setServletConfig(ServletConfig servletConfig) {
this.servletConfig = servletConfig;
}
} }