001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.jmx;
019    
020    import javax.management.MBeanServer;
021    import javax.management.Notification;
022    import javax.management.ObjectName;
023    import javax.management.monitor.CounterMonitor;
024    import org.apache.camel.Consumer;
025    import org.apache.camel.Processor;
026    import org.apache.camel.Producer;
027    import org.apache.camel.impl.DefaultEndpoint;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    /**
032     * Creates a CounterMonitor for jmx attributes
033     *
034     * @version $Revision: 523016 $
035     */
036    public class JMXEndpoint extends DefaultEndpoint<JMXExchange> {
037    
038            private static final Log log=LogFactory.getLog(JMXEndpoint.class);
039            private String name;
040            private ObjectName ourName;
041            private String observedObjectName;
042            private String attributeName;
043            private long granularityPeriod=5000;
044            private Number threshold;
045            private Number offset;
046            private MBeanServer mbeanServer;
047            private CounterMonitor counterMonitor=new CounterMonitor();
048    
049            protected JMXEndpoint(String endpointUri,JMXComponent component){
050                    super(endpointUri,component);
051                    observedObjectName=endpointUri;
052            }
053    
054            /**
055             * @return a Producer
056             * @throws Exception
057             * @see org.apache.camel.Endpoint#createProducer()
058             */
059            public Producer<JMXExchange> createProducer() throws Exception{
060                    throw new RuntimeException("Not supported");
061            }
062    
063            /**
064             * @param proc
065             * @return a Consumer
066             * @throws Exception
067             * @see org.apache.camel.Endpoint#createConsumer(org.apache.camel.Processor)
068             */
069            public Consumer<JMXExchange> createConsumer(Processor proc)
070                    throws Exception{
071                    ObjectName observedName=new ObjectName(observedObjectName);
072                    if(name==null){
073                            String type=observedName.getKeyProperty("type");
074                            type=type!=null?type:"UNKNOWN";
075                            name=mbeanServer.getDefaultDomain()+":type=CounterMonitor_"+type;
076                    }
077                    JMXConsumer result=new JMXConsumer(this,proc);
078                    ourName=new ObjectName(name);
079                    counterMonitor.setNotify(true);
080                    counterMonitor.addObservedObject(observedName);
081                    counterMonitor.setObservedAttribute(attributeName);
082                    counterMonitor.setGranularityPeriod(granularityPeriod);
083                    counterMonitor.setDifferenceMode(false);
084                    counterMonitor.setInitThreshold(threshold);
085                    counterMonitor.setOffset(offset);
086                    mbeanServer.registerMBean(counterMonitor,ourName);
087                    mbeanServer.addNotificationListener(ourName,result,null,new Object());
088                    return result;
089            }
090    
091            public boolean isSingleton(){
092                    return true;
093            }
094    
095            public JMXExchange createExchange(Notification notification){
096                    return new JMXExchange(getContext(),notification);
097            }
098    
099            public JMXExchange createExchange(){
100                    return new JMXExchange(getContext(),null);
101            }
102    
103            
104        public String getAttributeName(){
105            return attributeName;
106        }
107    
108            
109        public void setAttributeName(String attributeName){
110            this.attributeName=attributeName;
111        }
112    
113            
114        public long getGranularityPeriod(){
115            return granularityPeriod;
116        }
117    
118            
119        public void setGranularityPeriod(long granularityPeriod){
120            this.granularityPeriod=granularityPeriod;
121        }
122    
123            
124        public String getName(){
125            return name;
126        }
127    
128            
129        public void setName(String name){
130            this.name=name;
131        }
132    
133            
134        public Number getOffset(){
135            return offset;
136        }
137    
138            
139        public void setOffset(Number offset){
140            this.offset=offset;
141        }
142    
143            
144        public Number getThreshold(){
145            return threshold;
146        }
147    
148            
149        public void setThreshold(Number threshold){
150            this.threshold=threshold;
151        }
152    
153            
154        public MBeanServer getMbeanServer(){
155            return mbeanServer;
156        }
157    
158            
159        public void setMbeanServer(MBeanServer mbeanServer){
160            this.mbeanServer=mbeanServer;
161        }
162    }