Search in sources :

Example 1 with ManagedAttribute

use of org.eclipse.jetty.util.annotation.ManagedAttribute in project jetty.project by eclipse.

the class ContextHandler method getClassPath.

/* ------------------------------------------------------------ */
/**
     * Make best effort to extract a file classpath from the context classloader
     *
     * @return Returns the classLoader.
     */
@ManagedAttribute("The file classpath")
public String getClassPath() {
    if (_classLoader == null || !(_classLoader instanceof URLClassLoader))
        return null;
    URLClassLoader loader = (URLClassLoader) _classLoader;
    URL[] urls = loader.getURLs();
    StringBuilder classpath = new StringBuilder();
    for (int i = 0; i < urls.length; i++) {
        try {
            Resource resource = newResource(urls[i]);
            File file = resource.getFile();
            if (file != null && file.exists()) {
                if (classpath.length() > 0)
                    classpath.append(File.pathSeparatorChar);
                classpath.append(file.getAbsolutePath());
            }
        } catch (IOException e) {
            LOG.debug(e);
        }
    }
    if (classpath.length() == 0)
        return null;
    return classpath.toString();
}
Also used : URLClassLoader(java.net.URLClassLoader) Resource(org.eclipse.jetty.util.resource.Resource) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute)

Example 2 with ManagedAttribute

use of org.eclipse.jetty.util.annotation.ManagedAttribute in project jetty.project by eclipse.

the class ContextHandlerMBean method getContextAttributes.

@ManagedAttribute("Map of context attributes")
public Map<String, Object> getContextAttributes() {
    Map<String, Object> map = new HashMap<String, Object>();
    Attributes attrs = ((ContextHandler) _managed).getAttributes();
    Enumeration<String> en = attrs.getAttributeNames();
    while (en.hasMoreElements()) {
        String name = (String) en.nextElement();
        Object value = attrs.getAttribute(name);
        map.put(name, value);
    }
    return map;
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HashMap(java.util.HashMap) Attributes(org.eclipse.jetty.util.Attributes) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute)

Example 3 with ManagedAttribute

use of org.eclipse.jetty.util.annotation.ManagedAttribute in project jetty.project by eclipse.

the class ObjectMBean method getMBeanInfo.

public MBeanInfo getMBeanInfo() {
    try {
        if (_info == null) {
            // Start with blank lazy lists attributes etc.
            String desc = null;
            List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>();
            List<MBeanConstructorInfo> constructors = new ArrayList<MBeanConstructorInfo>();
            List<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>();
            List<MBeanNotificationInfo> notifications = new ArrayList<MBeanNotificationInfo>();
            // Find list of classes that can influence the mbean
            Class<?> o_class = _managed.getClass();
            List<Class<?>> influences = new ArrayList<Class<?>>();
            // always add MBean itself
            influences.add(this.getClass());
            influences = findInfluences(influences, _managed.getClass());
            if (LOG.isDebugEnabled())
                LOG.debug("Influence Count: {}", influences.size());
            // Process Type Annotations
            ManagedObject primary = o_class.getAnnotation(ManagedObject.class);
            if (primary != null) {
                desc = primary.value();
            } else {
                if (LOG.isDebugEnabled())
                    LOG.debug("No @ManagedObject declared on {}", _managed.getClass());
            }
            // For each influence
            for (int i = 0; i < influences.size(); i++) {
                Class<?> oClass = influences.get(i);
                ManagedObject typeAnnotation = oClass.getAnnotation(ManagedObject.class);
                if (LOG.isDebugEnabled())
                    LOG.debug("Influenced by: " + oClass.getCanonicalName());
                if (typeAnnotation == null) {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Annotations not found for: {}", oClass.getCanonicalName());
                    continue;
                }
                for (Method method : oClass.getDeclaredMethods()) {
                    ManagedAttribute methodAttributeAnnotation = method.getAnnotation(ManagedAttribute.class);
                    if (methodAttributeAnnotation != null) {
                        // TODO sort out how a proper name could get here, its a method name as an attribute at this point.
                        if (LOG.isDebugEnabled())
                            LOG.debug("Attribute Annotation found for: {}", method.getName());
                        MBeanAttributeInfo mai = defineAttribute(method, methodAttributeAnnotation);
                        if (mai != null) {
                            attributes.add(mai);
                        }
                    }
                    ManagedOperation methodOperationAnnotation = method.getAnnotation(ManagedOperation.class);
                    if (methodOperationAnnotation != null) {
                        if (LOG.isDebugEnabled())
                            LOG.debug("Method Annotation found for: {}", method.getName());
                        MBeanOperationInfo oi = defineOperation(method, methodOperationAnnotation);
                        if (oi != null) {
                            operations.add(oi);
                        }
                    }
                }
            }
            _info = new MBeanInfo(o_class.getName(), desc, (MBeanAttributeInfo[]) attributes.toArray(new MBeanAttributeInfo[attributes.size()]), (MBeanConstructorInfo[]) constructors.toArray(new MBeanConstructorInfo[constructors.size()]), (MBeanOperationInfo[]) operations.toArray(new MBeanOperationInfo[operations.size()]), (MBeanNotificationInfo[]) notifications.toArray(new MBeanNotificationInfo[notifications.size()]));
        }
    } catch (RuntimeException e) {
        LOG.warn(e);
        throw e;
    }
    return _info;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute) ManagedOperation(org.eclipse.jetty.util.annotation.ManagedOperation) MBeanConstructorInfo(javax.management.MBeanConstructorInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject)

Aggregations

ManagedAttribute (org.eclipse.jetty.util.annotation.ManagedAttribute)3 ManagedObject (org.eclipse.jetty.util.annotation.ManagedObject)2 File (java.io.File)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)1 MBeanInfo (javax.management.MBeanInfo)1 MBeanNotificationInfo (javax.management.MBeanNotificationInfo)1 MBeanOperationInfo (javax.management.MBeanOperationInfo)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1 Attributes (org.eclipse.jetty.util.Attributes)1 ManagedOperation (org.eclipse.jetty.util.annotation.ManagedOperation)1 Resource (org.eclipse.jetty.util.resource.Resource)1