View Javadoc

1   /*
2    * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  /** A simple service that exports Spring beans into the Servlet context as an attribute. */
34  public class ServletContextAttributeExporter implements Service, ApplicationContextAware, BeanNameAware {
35  
36      /** Class logger. */
37      private final Logger log = LoggerFactory.getLogger(ServletContextAttributeExporter.class);
38  
39      /** Application context into which we're loaded. */
40      private ApplicationContext appCtx;
41  
42      /** ID of this service. */
43      private String id;
44  
45      /** Whether this service has been initialized. */
46      private boolean initialized;
47  
48      /** ID of beans exported into the servlet context. */
49      private Collection<String> exportedBeans;
50  
51      /**
52       * Constructor.
53       * 
54       * @param beans ID of beans exported into the servlet context
55       */
56      public ServletContextAttributeExporter(Collection<String> beans) {
57          exportedBeans = beans;
58      }
59  
60      /** {@inheritDoc} */
61      public void destroy() throws ServiceException {
62  
63      }
64  
65      /** {@inheritDoc} */
66      public String getId() {
67          return id;
68      }
69  
70      /** {@inheritDoc} */
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      /** {@inheritDoc} */
96      public boolean isInitialized() {
97          return initialized;
98      }
99  
100     /** {@inheritDoc} */
101     public void setApplicationContext(ApplicationContext context) {
102         appCtx = context;
103     }
104 
105     /** {@inheritDoc} */
106     public void setBeanName(String name) {
107         id = name;
108     }
109 
110     /** {@inheritDoc} */
111     public boolean isDestroyed() {
112         return false;
113     }
114 }