1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package edu.internet2.middleware.shibboleth.common.config.service;
19
20 import java.util.Collection;
21
22 import javax.servlet.ServletContext;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.BeanNameAware;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.context.ApplicationContextAware;
29 import org.springframework.web.context.WebApplicationContext;
30
31 import edu.internet2.middleware.shibboleth.common.service.Service;
32 import edu.internet2.middleware.shibboleth.common.service.ServiceException;
33
34
35 public class ServletContextAttributeExporter implements Service, ApplicationContextAware, BeanNameAware {
36
37
38 private final Logger log = LoggerFactory.getLogger(ServletContextAttributeExporter.class);
39
40
41 private ApplicationContext appCtx;
42
43
44 private String id;
45
46
47 private boolean initialized;
48
49
50 private Collection<String> exportedBeans;
51
52
53
54
55
56
57 public ServletContextAttributeExporter(Collection<String> beans) {
58 exportedBeans = beans;
59 }
60
61
62 public void destroy() throws ServiceException {
63
64 }
65
66
67 public String getId() {
68 return id;
69 }
70
71
72 public void initialize() throws ServiceException {
73 if (!(appCtx instanceof WebApplicationContext)) {
74 log.warn("This service may only be used when services are loaded within a web application context.");
75 return;
76 }
77
78 Object bean;
79 if (exportedBeans != null) {
80 WebApplicationContext webAppCtx = (WebApplicationContext) appCtx;
81 ServletContext servletCtx = webAppCtx.getServletContext();
82 for (String beanId : exportedBeans) {
83 bean = webAppCtx.getBean(beanId);
84 if(bean != null){
85 log.debug("Exporting bean {} to servlet context.", beanId);
86 servletCtx.setAttribute(beanId, bean);
87 }else{
88 log.warn("No {} bean located, unable to export it to the servlet context", beanId);
89 }
90 }
91 }
92
93 initialized = true;
94 }
95
96
97 public boolean isInitialized() {
98 return initialized;
99 }
100
101
102 public void setApplicationContext(ApplicationContext context) {
103 appCtx = context;
104 }
105
106
107 public void setBeanName(String name) {
108 id = name;
109 }
110
111
112 public boolean isDestroyed() {
113 return false;
114 }
115 }