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.jmx;
20  
21  import java.util.Enumeration;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.eclipse.jetty.server.handler.ContextHandler;
26  import org.eclipse.jetty.util.Attributes;
27  import org.eclipse.jetty.util.annotation.ManagedAttribute;
28  import org.eclipse.jetty.util.annotation.ManagedObject;
29  import org.eclipse.jetty.util.annotation.ManagedOperation;
30  import org.eclipse.jetty.util.annotation.Name;
31  
32  @ManagedObject("ContextHandler mbean wrapper")
33  public class ContextHandlerMBean extends AbstractHandlerMBean
34  {
35      public ContextHandlerMBean(Object managedObject)
36      {
37          super(managedObject);
38      }
39  
40      @ManagedAttribute("Map of context attributes")
41      public Map<String,Object> getContextAttributes()
42      {
43          Map<String,Object> map = new HashMap<String,Object>();
44          Attributes attrs = ((ContextHandler)_managed).getAttributes();
45          Enumeration<String> en = attrs.getAttributeNames();
46          while (en.hasMoreElements())
47          {
48              String name = (String)en.nextElement();
49              Object value = attrs.getAttribute(name);
50              map.put(name,value);
51          }
52          return map;
53      }
54      
55      @ManagedOperation(value="Set context attribute", impact="ACTION")
56      public void setContextAttribute(@Name(value = "name", description="attribute name") String name, @Name(value = "value", description="attribute value") Object value)
57      {
58          Attributes attrs = ((ContextHandler)_managed).getAttributes();
59          attrs.setAttribute(name,value);
60      }
61      
62      @ManagedOperation(value="Set context attribute", impact="ACTION")
63      public void setContextAttribute(@Name(value = "name", description="attribute name") String name, @Name(value = "value", description="attribute value") String value)
64      {
65          Attributes attrs = ((ContextHandler)_managed).getAttributes();
66          attrs.setAttribute(name,value);
67      }
68      
69      @ManagedOperation(value="Remove context attribute", impact="ACTION")
70      public void removeContextAttribute(@Name(value = "name", description="attribute name") String name)
71      {
72          Attributes attrs = ((ContextHandler)_managed).getAttributes();
73          attrs.removeAttribute(name);
74      }
75  }