Simplified ManagedAttribute mechanism
This commit is contained in:
parent
f9a3bdab2a
commit
b0a3c7c5ea
|
@ -211,13 +211,11 @@ public class LikeJettyXml
|
|||
HashLoginService login = new HashLoginService();
|
||||
login.setName("Test Realm");
|
||||
login.setConfig(jetty_base + "/etc/realm.properties");
|
||||
login.setRefreshInterval(0);
|
||||
login.setHotReload(false);
|
||||
server.addBean(login);
|
||||
|
||||
|
||||
// Start the server
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
server.join();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -155,7 +155,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
protected Context _scontext;
|
||||
private final AttributesMap _attributes;
|
||||
private final Map<String, String> _initParams;
|
||||
|
@ -760,22 +759,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
{
|
||||
String managedAttributes = _initParams.get(MANAGED_ATTRIBUTES);
|
||||
if (managedAttributes != null)
|
||||
{
|
||||
_managedAttributes = new HashMap<String, Object>();
|
||||
String[] attributes = managedAttributes.split(",");
|
||||
for (String attribute : attributes)
|
||||
{
|
||||
_managedAttributes.put(attribute.trim(),null);
|
||||
}
|
||||
|
||||
Enumeration<String> e = _scontext.getAttributeNames();
|
||||
while (e.hasMoreElements())
|
||||
{
|
||||
String name = e.nextElement();
|
||||
Object value = _scontext.getAttribute(name);
|
||||
checkManagedAttribute(name,value);
|
||||
}
|
||||
}
|
||||
addEventListener(new ManagedAttributeListener(this,managedAttributes.split(",")));
|
||||
|
||||
super.doStart();
|
||||
|
||||
|
@ -845,13 +829,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
if (_errorHandler != null)
|
||||
_errorHandler.stop();
|
||||
|
||||
Enumeration<String> e = _scontext.getAttributeNames();
|
||||
while (e.hasMoreElements())
|
||||
{
|
||||
String name = e.nextElement();
|
||||
checkManagedAttribute(name,null);
|
||||
}
|
||||
|
||||
for (EventListener l : _programmaticListeners)
|
||||
removeEventListener(l);
|
||||
_programmaticListeners.clear();
|
||||
|
@ -1247,7 +1224,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
@Override
|
||||
public void removeAttribute(String name)
|
||||
{
|
||||
checkManagedAttribute(name,null);
|
||||
_attributes.removeAttribute(name);
|
||||
}
|
||||
|
||||
|
@ -1261,7 +1237,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
@Override
|
||||
public void setAttribute( String name, Object value)
|
||||
{
|
||||
checkManagedAttribute(name,value);
|
||||
_attributes.setAttribute(name,value);
|
||||
}
|
||||
|
||||
|
@ -1274,36 +1249,15 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
{
|
||||
_attributes.clearAttributes();
|
||||
_attributes.addAll(attributes);
|
||||
Enumeration<String> e = _attributes.getAttributeNames();
|
||||
while (e.hasMoreElements())
|
||||
{
|
||||
String name = e.nextElement();
|
||||
checkManagedAttribute(name,attributes.getAttribute(name));
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public void clearAttributes()
|
||||
{
|
||||
Enumeration<String> e = _attributes.getAttributeNames();
|
||||
while (e.hasMoreElements())
|
||||
{
|
||||
String name = e.nextElement();
|
||||
checkManagedAttribute(name,null);
|
||||
}
|
||||
_attributes.clearAttributes();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void checkManagedAttribute(String name, Object value)
|
||||
{
|
||||
if (_managedAttributes != null && _managedAttributes.containsKey(name))
|
||||
{
|
||||
setManagedAttribute(name,value);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void setManagedAttribute(String name, Object value)
|
||||
{
|
||||
|
@ -2127,7 +2081,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
@Override
|
||||
public synchronized void setAttribute(String name, Object value)
|
||||
{
|
||||
checkManagedAttribute(name,value);
|
||||
Object old_value = super.getAttribute(name);
|
||||
|
||||
if (value == null)
|
||||
|
@ -2158,8 +2111,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
@Override
|
||||
public synchronized void removeAttribute(String name)
|
||||
{
|
||||
checkManagedAttribute(name,null);
|
||||
|
||||
Object old_value = super.getAttribute(name);
|
||||
super.removeAttribute(name);
|
||||
if (old_value != null &&!_contextAttributeListeners.isEmpty())
|
||||
|
@ -2298,7 +2249,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
return _extendedListenerTypes;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ClassLoader getClassLoader()
|
||||
{
|
||||
|
@ -2372,8 +2322,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
return _enabled;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public <T> T createInstance (Class<T> clazz) throws Exception
|
||||
{
|
||||
T o = clazz.newInstance();
|
||||
|
@ -2530,7 +2478,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean setInitParameter(String name, String value)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,107 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.server.handler;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletContextAttributeEvent;
|
||||
import javax.servlet.ServletContextAttributeListener;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Enable Jetty style JMX MBeans from within a Context
|
||||
*/
|
||||
public class ManagedAttributeListener implements ServletContextListener, ServletContextAttributeListener
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(ManagedAttributeListener.class);
|
||||
|
||||
final Set<String> _managedAttributes=new HashSet<>();
|
||||
final ContextHandler _context;
|
||||
|
||||
public ManagedAttributeListener(ContextHandler context,String... managedAttributes)
|
||||
{
|
||||
_context=context;
|
||||
|
||||
for (String attr:managedAttributes)
|
||||
_managedAttributes.add(attr);
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("managedAttributes {}",_managedAttributes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributeReplaced(ServletContextAttributeEvent event)
|
||||
{
|
||||
if (_managedAttributes.contains(event.getName()))
|
||||
updateBean(event.getName(),event.getValue(),event.getServletContext().getAttribute(event.getName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributeRemoved(ServletContextAttributeEvent event)
|
||||
{
|
||||
if (_managedAttributes.contains(event.getName()))
|
||||
updateBean(event.getName(),event.getValue(),null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attributeAdded(ServletContextAttributeEvent event)
|
||||
{
|
||||
if (_managedAttributes.contains(event.getName()))
|
||||
updateBean(event.getName(),null,event.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent event)
|
||||
{
|
||||
// Update existing attributes
|
||||
Enumeration<String> e = event.getServletContext().getAttributeNames();
|
||||
while (e.hasMoreElements())
|
||||
{
|
||||
String name = e.nextElement();
|
||||
if (_managedAttributes.contains(name))
|
||||
updateBean(name,null,event.getServletContext().getAttribute(name));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent event)
|
||||
{
|
||||
Enumeration<String> e = _context.getServletContext().getAttributeNames();
|
||||
while (e.hasMoreElements())
|
||||
{
|
||||
String name = e.nextElement();
|
||||
if (_managedAttributes.contains(name))
|
||||
updateBean(name,event.getServletContext().getAttribute(name),null);
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateBean(String name,Object oldBean,Object newBean)
|
||||
{
|
||||
LOG.info("update {} {}->{} on {}",name,oldBean,newBean,_context);
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("update {} {}->{} on {}",name,oldBean,newBean,_context);
|
||||
_context.updateBean(oldBean,newBean,false);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue