Package org.jgroups.annotations

Examples of org.jgroups.annotations.ManagedAttribute


            }

            Method[] methods=this.getClass().getMethods();
            for(Method method:methods) {
                if(method.isAnnotationPresent(ManagedAttribute.class)) {
                    ManagedAttribute annotation=method.getAnnotation(ManagedAttribute.class);
                    if(!annotation.writable() && (method.getName().startsWith("is")
                       || method.getName().startsWith("get"))) {
                        Object value=null;
                        try {
                            value=method.invoke(this, new Object[0]);
                            String attributeName=methodNameToAttributeName(method.getName());
View Full Code Here


        List<Method> objectMethods = new ArrayList<Method>(Arrays.asList(Object.class.getMethods()));
        methods.removeAll(objectMethods);
              
        for(Method method:methods) {
            //does method have @ManagedAttribute annotation?
            ManagedAttribute attr=method.getAnnotation(ManagedAttribute.class);
            if(attr != null) {
                String methodName=method.getName();
                if(!methodName.startsWith("get") && !methodName.startsWith("set")
                   && !methodName.startsWith("is")) {
                    if(log.isWarnEnabled())
                        log.warn("method name " + methodName
                                 + " doesn't start with \"get\", \"set\", or \"is\""
                                 + ", but is annotated with @ManagedAttribute: will be ignored");
                }
                else {
                    MBeanAttributeInfo info=null;
                    //Is name field of @ManagedAttributed used?
                    String attributeName=attr.name().length()>0?attr.name().trim():null;
                    boolean writeAttribute=false;
                    if(isSetMethod(method)) { // setter
                        attributeName=(attributeName==null)?methodName.substring(3):attributeName;
                        info=new MBeanAttributeInfo(attributeName,
                                                    method.getParameterTypes()[0].getCanonicalName(),
                                                    attr.description(),
                                                    true,
                                                    true,
                                                    false);
                        writeAttribute=true;
                    }
                    else { // getter
                        if(method.getParameterTypes().length == 0 && method.getReturnType() != java.lang.Void.TYPE) {
                            boolean hasSetter=atts.containsKey(attributeName);
                            //we found is method
                            if(methodName.startsWith("is")) {
                                attributeName=(attributeName==null)?methodName.substring(2):attributeName;
                                info=new MBeanAttributeInfo(attributeName,
                                                            method.getReturnType().getCanonicalName(),
                                                            attr.description(),
                                                            true,
                                                            hasSetter,
                                                            true);
                            }
                            else {
                                //this has to be get
                                attributeName=(attributeName==null)?methodName.substring(3):attributeName;
                                info=new MBeanAttributeInfo(attributeName,
                                                            method.getReturnType().getCanonicalName(),
                                                            attr.description(),
                                                            true,
                                                            hasSetter,
                                                            false);
                            }
                        }
                        else {
                            if(log.isWarnEnabled()) {
                                log.warn("Method " + method.getName()
                                         + " must have a valid return type and zero parameters");
                            }
                            continue;
                        }
                    }                                     
                   
                    if(log.isDebugEnabled()) {
                        log.debug("@Attr found for method " + method.getName()
                                  + " and registered as "
                                  + attributeName);
                    }

                    AttributeEntry ae=atts.get(attributeName);
                    //is it a read method?
                    if(!writeAttribute) {
                        //we already have annotated field as read
                        if(ae instanceof FieldAttributeEntry && ae.getInfo().isReadable()) {
                            log.warn("not adding annotated method " + method
                                     + " since we already have read attribute");
                        }
                        //we already have annotated set method
                        else if(ae instanceof MethodAttributeEntry) {
                            MethodAttributeEntry mae=(MethodAttributeEntry)ae;
                            if(mae.hasSetMethod()) {
                                atts.put(attributeName,
                                         new MethodAttributeEntry(mae.getInfo(), mae.getSetMethod(), method));
                            }
                        } //we don't have such entry
                        else {
                            atts.put(attributeName, new MethodAttributeEntry(info, null, method));
                        }
                    }//is it a set method?
                    else {                       
                        if(ae instanceof FieldAttributeEntry) {
                            //we already have annotated field as write
                            if(ae.getInfo().isWritable()) {
                                log.warn("Not adding annotated method " + methodName
                                         + " since we already have writable attribute");
                            }
                            else {
                                //we already have annotated field as read
                                //lets make the field writable
                                Field f = ((FieldAttributeEntry)ae).getField();
                                MBeanAttributeInfo i=new MBeanAttributeInfo(ae.getInfo().getName(),
                                                                            f.getType().getCanonicalName(),
                                                                            attr.description(),
                                                                            true,
                                                                            Modifier.isFinal(f.getModifiers())? false: true,
                                                                            false);                              
                                atts.put(attributeName,new FieldAttributeEntry(i,f));
                            }
View Full Code Here

        //traverse class hierarchy and find all annotated fields
        for(Class<?> clazz=getObject().getClass();clazz != null; clazz=clazz.getSuperclass()) {

            Field[] fields=clazz.getDeclaredFields();
            for(Field field:fields) {
                ManagedAttribute attr=field.getAnnotation(ManagedAttribute.class);
                if(attr != null) {
                    String fieldName = renameToJavaCodingConvention(field.getName());
                    MBeanAttributeInfo info=new MBeanAttributeInfo(fieldName,
                                                                   field.getType().getCanonicalName(),
                                                                   attr.description(),
                                                                   true,
                                                                   Modifier.isFinal(field.getModifiers())? false: attr.writable(),
                                                                   false);

                    atts.put(fieldName, new FieldAttributeEntry(info, field));
                    if(log.isDebugEnabled()) {
                        log.debug("@Attr found for field " + field.getName());
View Full Code Here



    protected void exposeManagedAttribute(Method method) {
        String           methodName=method.getName();
        ManagedAttribute attr_annotation=method.getAnnotation(ManagedAttribute.class);
        Property         prop=method.getAnnotation(Property.class);

        boolean expose_prop=prop != null && prop.exposeAsManagedAttribute();
        boolean expose=attr_annotation != null || expose_prop;
        if(!expose)
            return;

        boolean writable=(prop != null && prop.writable()) || (attr_annotation != null && attr_annotation.writable());

        // Is name of @ManagedAttributed or @Property used?
        String attr_name=attr_annotation != null? attr_annotation.name() : prop != null? prop.name() : null;
        if(attr_name != null && !attr_name.trim().isEmpty())
            attr_name=attr_name.trim();
        else {
            // getFooBar() --> foo_bar
            attr_name=Util.methodNameToAttributeName(methodName);
            if(!atts.containsKey(attr_name)) {

                // hmm, maybe we need to look for an attribute fooBar
                String tmp=Util.methodNameToJavaAttributeName(methodName);
                if(atts.containsKey(tmp))
                    attr_name=tmp;
            }
        }

        String descr=attr_annotation != null ? attr_annotation.description() : prop != null? prop.description() : null;
        AttributeEntry attr=atts.get(attr_name);
        if(attr != null) {
            if(isSetMethod(method)) {
                if(attr.setter != null) {
                    if(log.isWarnEnabled())
View Full Code Here

        // traverse class hierarchy and find all annotated fields
        for(Class<?> clazz=obj.getClass(); clazz != null && clazz != Object.class; clazz=clazz.getSuperclass()) {

            Field[] fields=clazz.getDeclaredFields();
            for(Field field: fields) {
                ManagedAttribute attr=field.getAnnotation(ManagedAttribute.class);
                Property prop=field.getAnnotation(Property.class);
                boolean expose_prop=prop != null && prop.exposeAsManagedAttribute();
                boolean expose=attr != null || expose_prop;

                if(expose) {
                    String fieldName=attr != null? attr.name() : (prop != null? prop.name() : null);
                    if(fieldName != null && fieldName.trim().isEmpty())
                        fieldName=field.getName();

                    String descr=attr != null? attr.description() : prop.description();
                    boolean writable=attr != null? attr.writable() : prop.writable();

                    MBeanAttributeInfo info=new MBeanAttributeInfo(fieldName,
                                                                   field.getType().getCanonicalName(),
                                                                   descr,
                                                                   true,
View Full Code Here

            Field[] fields=clazz.getDeclaredFields();
            for(Field field: fields) {
                if(field.isAnnotationPresent(ManagedAttribute.class) ||
                  (field.isAnnotationPresent(Property.class) && field.getAnnotation(Property.class).exposeAsManagedAttribute())) {

                    ManagedAttribute attr_annotation=field.getAnnotation(ManagedAttribute.class);
                    Property         prop=field.getAnnotation(Property.class);
                    String attr_name=attr_annotation != null? attr_annotation.name() : prop != null? prop.name() : null;
                    if(attr_name != null && !attr_name.trim().isEmpty())
                        attr_name=attr_name.trim();
                    else
                        attr_name=field.getName();

                    try {
                        field.setAccessible(true);
                        Object value=field.get(this);
                        map.put(attr_name, value != null? value.toString() : null);
                    }
                    catch(Exception e) {
                        log.warn("Could not retrieve value of attribute (field) " + attr_name, e);
                    }
                }
            }

            Method[] methods=this.getClass().getMethods();
            for(Method method: methods) {
                if(method.isAnnotationPresent(ManagedAttribute.class) ||
                        (method.isAnnotationPresent(Property.class) && method.getAnnotation(Property.class).exposeAsManagedAttribute())) {

                    ManagedAttribute attr_annotation=method.getAnnotation(ManagedAttribute.class);
                    Property         prop=method.getAnnotation(Property.class);
                    String method_name=attr_annotation != null? attr_annotation.name() : prop != null? prop.name() : null;
                    if(method_name != null && !method_name.trim().isEmpty())
                        method_name=method_name.trim();
                    else {
                        String field_name=Util.methodNameToAttributeName(method.getName());
                        method_name=Util.attributeNameToMethodName(field_name);
View Full Code Here

            if(log.isWarnEnabled())
                log.warn("method name " + methodName + " doesn't start with \"get\", \"set\", or \"is\""
                        + ", but is annotated with @ManagedAttribute: will be ignored");
            return;
        }
        ManagedAttribute attr = method.getAnnotation(ManagedAttribute.class);
        Property prop=method.getAnnotation(Property.class);

        boolean expose_prop=prop != null && prop.exposeAsManagedAttribute();
        boolean expose=attr != null || expose_prop;
        if(!expose)
            return;

        // Is name field of @ManagedAttributed used?
        String attributeName=attr != null? attr.name() : null;
        if(attributeName != null && attributeName.trim().length() > 0)
            attributeName=attributeName.trim();
        else
            attributeName=null;
           
        String descr=attr != null ? attr.description() : prop != null? prop.description() : null;

        boolean writeAttribute=false;
        MBeanAttributeInfo info=null;
        if(isSetMethod(method)) { // setter
            attributeName=(attributeName==null)?methodName.substring(3):attributeName;
View Full Code Here

        //traverse class hierarchy and find all annotated fields
        for(Class<?> clazz=getObject().getClass();clazz != null; clazz=clazz.getSuperclass()) {

            Field[] fields=clazz.getDeclaredFields();
            for(Field field:fields) {
                ManagedAttribute attr=field.getAnnotation(ManagedAttribute.class);
                Property prop=field.getAnnotation(Property.class);
                boolean expose_prop=prop != null && prop.exposeAsManagedAttribute();
                boolean expose=attr != null || expose_prop;

                if(expose) {
                    String fieldName = Util.attributeNameToMethodName(field.getName());
                    String descr=attr != null? attr.description() : prop.description();
                    boolean writable=attr != null? attr.writable() : prop.writable();

                    MBeanAttributeInfo info=new MBeanAttributeInfo(fieldName,
                                                                   field.getType().getCanonicalName(),
                                                                   descr,
                                                                   true,
View Full Code Here

                log.warn("method name " + methodName
                         + " doesn't start with \"get\", \"set\", or \"is\""
                         + ", but is annotated with @ManagedAttribute: will be ignored");
        }
        else {
            ManagedAttribute attr = method.getAnnotation(ManagedAttribute.class);
            MBeanAttributeInfo info=null;
            //Is name field of @ManagedAttributed used?
            String attributeName=attr.name().length()>0?attr.name().trim():null;
            boolean writeAttribute=false;
            if(isSetMethod(method)) { // setter
                attributeName=(attributeName==null)?methodName.substring(3):attributeName;
                info=new MBeanAttributeInfo(attributeName,
                                            method.getParameterTypes()[0].getCanonicalName(),
                                            attr.description(),
                                            true,
                                            true,
                                            false);
                writeAttribute=true;
            }
            else { // getter
                if(method.getParameterTypes().length == 0 && method.getReturnType() != java.lang.Void.TYPE) {
                    boolean hasSetter=atts.containsKey(attributeName);
                    //we found is method
                    if(methodName.startsWith("is")) {
                        attributeName=(attributeName==null)?methodName.substring(2):attributeName;
                        info=new MBeanAttributeInfo(attributeName,
                                                    method.getReturnType().getCanonicalName(),
                                                    attr.description(),
                                                    true,
                                                    hasSetter,
                                                    true);
                    }
                    else {
                        //this has to be get
                        attributeName=(attributeName==null)?methodName.substring(3):attributeName;
                        info=new MBeanAttributeInfo(attributeName,
                                                    method.getReturnType().getCanonicalName(),
                                                    attr.description(),
                                                    true,
                                                    hasSetter,
                                                    false);
                    }
                }
                else {
                    if(log.isWarnEnabled()) {
                        log.warn("Method " + method.getName()
                                 + " must have a valid return type and zero parameters");
                    }
                    //silently skip this method
                    return;
                }
            }                                     
           
            if(log.isDebugEnabled()) {
                log.debug("@Attr found for method " + method.getName()
                          + " and registered as "
                          + attributeName);
            }

            AttributeEntry ae=atts.get(attributeName);
            //is it a read method?
            if(!writeAttribute) {
                //we already have annotated field as read
                if(ae instanceof FieldAttributeEntry && ae.getInfo().isReadable()) {
                    log.warn("not adding annotated method " + method
                             + " since we already have read attribute");
                }
                //we already have annotated set method
                else if(ae instanceof MethodAttributeEntry) {
                    MethodAttributeEntry mae=(MethodAttributeEntry)ae;
                    if(mae.hasSetMethod()) {
                        atts.put(attributeName,
                                 new MethodAttributeEntry(mae.getInfo(), mae.getSetMethod(), method));
                    }
                } //we don't have such entry
                else {
                    atts.put(attributeName, new MethodAttributeEntry(info, null, method));
                }
            }//is it a set method?
            else {                       
                if(ae instanceof FieldAttributeEntry) {
                    //we already have annotated field as write
                    if(ae.getInfo().isWritable()) {
                        log.warn("Not adding annotated method " + methodName
                                 + " since we already have writable attribute");
                    }
                    else {
                        //we already have annotated field as read
                        //lets make the field writable
                        Field f = ((FieldAttributeEntry)ae).getField();
                        MBeanAttributeInfo i=new MBeanAttributeInfo(ae.getInfo().getName(),
                                                                    f.getType().getCanonicalName(),
                                                                    attr.description(),
                                                                    true,
                                                                    Modifier.isFinal(f.getModifiers())? false: true,
                                                                    false);                              
                        atts.put(attributeName,new FieldAttributeEntry(i,f));
                    }
View Full Code Here

        //traverse class hierarchy and find all annotated fields
        for(Class<?> clazz=getObject().getClass();clazz != null; clazz=clazz.getSuperclass()) {

            Field[] fields=clazz.getDeclaredFields();
            for(Field field:fields) {
                ManagedAttribute attr=field.getAnnotation(ManagedAttribute.class);
                if(attr != null) {
                    String fieldName = renameToJavaCodingConvention(field.getName());
                    MBeanAttributeInfo info=new MBeanAttributeInfo(fieldName,
                                                                   field.getType().getCanonicalName(),
                                                                   attr.description(),
                                                                   true,
                                                                   Modifier.isFinal(field.getModifiers())? false: attr.writable(),
                                                                   false);

                    atts.put(fieldName, new FieldAttributeEntry(info, field));
                    if(log.isDebugEnabled()) {
                        log.debug("@Attr found for field " + field.getName());
View Full Code Here

TOP

Related Classes of org.jgroups.annotations.ManagedAttribute

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.