Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2008 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package groovy.jmx.builder;
 
 
 import java.util.List;
 import java.util.Map;
The JmxBuilderModelMBean is the MBean class that proxies exported POGO/POJO inside the MBeanServer. When JmxBuilder exports an object instance, an instance of this class is created and exported inside the MBeanServer.

Author(s):
Vladimir Vivien
 
 public class JmxBuilderModelMBean extends RequiredModelMBean implements NotificationListener {
     private List<StringmethodListeners = new ArrayList<String>(0);
     private Object managedObject;
 
         super.setManagedResource(objectRef"ObjectReference");
     }
 
         super();
     }
 
         super(mbi);
     }
 
     public synchronized void setManagedResource(Object obj) {
          = obj;
         try {
             super.setManagedResource(obj"ObjectReference");
         } catch (Exception ex) {
             throw new JmxBuilderException(ex);
         }
     }

    
Registers listeners for operation calls (i.e. method, getter, and setter calls) when invoked on this bean from the MBeanServer. Descriptor should contain a map with layout item -> [Map[methodListner:[target:"", tpe:"", callback:&Closure], ... ,]]

Parameters:
descriptor MetaMap descriptor containing description of operation call listeners
 
     public void addOperationCallListeners(Map<StringMapdescriptor) {
         if (descriptor == nullreturn;
         for (Map.Entry<StringMapitem : descriptor.entrySet()) {
             // setup method listeners (such as attributeListener and Operation Listners)
             // item -> [Map[methodListner:[target:"", tpe:"", callback:&Closure], ... ,]]
             if (item.getValue().containsKey("methodListener")) {
                 Map listener = (Mapitem.getValue().get("methodListener");
                 String target = (Stringlistener.get("target");
                 .add(target);
                 String listenerType = (Stringlistener.get("type");
                 listener.put("managedObject"this.);
                 // register an attribute change notification listener with model mbean
                 if (listenerType.equals("attributeChangeListener")) {
                     try {
                         this.addAttributeChangeNotificationListener(
                                 AttributeChangedListener.getListner(), (Stringlistener.get("attribute"), listener
                         );
                     } catch (MBeanException e) {
                         throw new JmxBuilderException(e);
                     }
                 }
                 if (listenerType.equals("operationCallListener")) {
                     String eventType = "jmx.operation.call." + target;
                     NotificationFilterSupport filter = new NotificationFilterSupport();
                     filter.enableType(eventType);
                     this.addNotificationListener(JmxEventListener.getListner(), filterlistener);
                 }
             }
         }
     }

    
Sets up event listeners for this MBean as described in the descriptor. The descriptor contains a map with layout {item -> Map[event:"...", from:ObjectName, callback:&Closure],...,}

Parameters:
server the MBeanServer is to be registered.
descriptor a map containing info about the event
    public void addEventListeners(MBeanServer serverMap<StringMapdescriptor) {
        for (Map.Entry<StringMapitem : descriptor.entrySet()) {
            Map<StringObjectlistener = item.getValue();
            // register with server
            ObjectName broadcaster = (ObjectNamelistener.get("from");
            try {
                String eventType = (Stringlistener.get("event");
                if (eventType != null) {
                    NotificationFilterSupport filter = new NotificationFilterSupport();
                    filter.enableType(eventType);
                    server.addNotificationListener(broadcaster, JmxEventListener.getListner(), filterlistener);
                } else {
                    server.addNotificationListener(broadcaster, JmxEventListener.getListner(), nulllistener);
                }
            } catch (InstanceNotFoundException e) {
                throw new JmxBuilderException(e);
            }
        }
    }
    @Override
    public Object invoke(String opNameObject[] opArgsString[] signaturethrows MBeanExceptionReflectionException {
        Object result = super.invoke(opNameopArgssignature);
        if (.contains(opName)) {
            this.sendNotification(buildCallListenerNotification(opName));
        }
        return result;
    }
    public void handleNotification(Notification noteObject handback) {
        //System.out.println("Received note!");
    }
        Notification note = new Notification(
                "jmx.operation.call." + target,
                this,
                NumberSequencer.getNextSequence(),
                System.currentTimeMillis()
        );
        return note;
    }
    private static class NumberSequencer {
        private static AtomicLong num;
        static {
             = new AtomicLong(0);
        }
        public static long getNextSequence() {
            return .incrementAndGet();
        }
    }

    
Internal class AttributeChangedListener provides hooks toa handle attribute-change events that occurs on registered MBeans.

Author(s):
Vladimir Vivien
See also:
JmxBuilderModelMBean
    private static class AttributeChangedListener implements NotificationListener {
        private static AttributeChangedListener listener;

        
Returns an instance of the AttributeChangedListener.

Returns:
        public static synchronized AttributeChangedListener getListner() {
            if ( == null) {
                 = new AttributeChangedListener();
            }
            return ;
        }
        private AttributeChangedListener() {
        }
        public void handleNotification(Notification notificationObject handback) {
            AttributeChangeNotification note = (AttributeChangeNotificationnotification;
            Map event = (Maphandback;
            if (event != null) {
                Object del = event.get("managedObject");
                Object callback = event.get("callback");
                if (callback != null && callback instanceof Closure) {
                    Closure closure = (Closurecallback;
                    closure.setDelegate(del);
                    if (closure.getMaximumNumberOfParameters() == 1)
                        closure.call(buildAttributeNotificationPacket(note));
                    else closure.call();
                }
            }
        }
        private static Map buildAttributeNotificationPacket(AttributeChangeNotification note) {
            Map<StringObjectresult = new HashMap<StringObject>();
            result.put("oldValue"note.getOldValue());
            result.put("newValue"note.getNewValue());
            result.put("attribute"note.getAttributeName());
            result.put("attributeType"note.getAttributeType());
            result.put("sequenceNumber"note.getSequenceNumber());
            result.put("timeStamp"note.getTimeStamp());
            return result;
        }
    }
New to GrepCode? Check out our FAQ X