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