View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server.handler;
20  
21  import java.util.Enumeration;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import javax.servlet.ServletContextAttributeEvent;
26  import javax.servlet.ServletContextAttributeListener;
27  import javax.servlet.ServletContextEvent;
28  import javax.servlet.ServletContextListener;
29  
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.util.log.Logger;
32  
33  /* ------------------------------------------------------------ */
34  /** Enable Jetty style JMX MBeans from within a Context 
35   */
36  public class ManagedAttributeListener implements  ServletContextListener, ServletContextAttributeListener
37  {
38      private static final Logger LOG = Log.getLogger(ManagedAttributeListener.class);
39  
40      final Set<String> _managedAttributes=new HashSet<>();
41      final ContextHandler _context;
42      
43      public ManagedAttributeListener(ContextHandler context,String... managedAttributes)
44      {
45          _context=context;
46  
47          for (String attr:managedAttributes)
48              _managedAttributes.add(attr);
49          
50          if (LOG.isDebugEnabled())
51              LOG.debug("managedAttributes {}",_managedAttributes);
52      }
53  
54      @Override
55      public void attributeReplaced(ServletContextAttributeEvent event)
56      {
57          if (_managedAttributes.contains(event.getName()))
58              updateBean(event.getName(),event.getValue(),event.getServletContext().getAttribute(event.getName()));
59      }
60      
61      @Override
62      public void attributeRemoved(ServletContextAttributeEvent event)
63      {
64          if (_managedAttributes.contains(event.getName()))
65              updateBean(event.getName(),event.getValue(),null);                    
66      }
67      
68      @Override
69      public void attributeAdded(ServletContextAttributeEvent event)
70      {
71          if (_managedAttributes.contains(event.getName()))
72              updateBean(event.getName(),null,event.getValue());    
73      }
74  
75      @Override
76      public void contextInitialized(ServletContextEvent event)
77      {                 
78          // Update existing attributes
79          Enumeration<String> e = event.getServletContext().getAttributeNames();
80          while (e.hasMoreElements())
81          {
82              String name = e.nextElement();
83              if (_managedAttributes.contains(name))
84                  updateBean(name,null,event.getServletContext().getAttribute(name));
85          }
86      }
87      
88      @Override
89      public void contextDestroyed(ServletContextEvent event)
90      {
91          Enumeration<String> e = _context.getServletContext().getAttributeNames();
92          while (e.hasMoreElements())
93          {
94              String name = e.nextElement();
95              if (_managedAttributes.contains(name))
96                  updateBean(name,event.getServletContext().getAttribute(name),null);
97          }
98      }
99      
100     protected void updateBean(String name,Object oldBean,Object newBean)
101     {
102         LOG.info("update {} {}->{} on {}",name,oldBean,newBean,_context);
103         if (LOG.isDebugEnabled())
104             LOG.debug("update {} {}->{} on {}",name,oldBean,newBean,_context);
105         _context.updateBean(oldBean,newBean,false);
106     }
107 }