View Javadoc

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