View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.modeler.mbeans;
18  
19  import java.util.HashMap;
20  
21  import javax.management.Attribute;
22  import javax.management.AttributeNotFoundException;
23  import javax.management.MBeanException;
24  import javax.management.ReflectionException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.commons.modeler.BaseModelMBean;
29  
30  /*** Use the same metadata, except that we replace the attribute
31   * get/set methods.
32   */
33  class MBeanProxy extends BaseModelMBean {
34      private static Log log = LogFactory.getLog(MBeanProxy.class);
35  
36      HashMap atts=new HashMap();
37  
38      SimpleRemoteConnector jkmx;
39  
40      public MBeanProxy(SimpleRemoteConnector jkmx, String code) throws Exception {
41          this.jkmx=jkmx;
42          initModelInfo(code);
43      }
44  
45      /*** Called by the connector - will update the value when a chunk of
46       * data is received
47       */
48      protected void update( String name, String val ) {
49          if( log.isTraceEnabled() )
50              log.trace( "Updating " + oname + " " + name + " " + val);
51          // XXX Conversions !!!
52          atts.put( name, val);
53      }
54  
55      public Object getAttribute(String name)
56          throws AttributeNotFoundException, MBeanException,
57              ReflectionException
58      {
59          // If we're stale - refresh values
60          jkmx.refresh();
61          return atts.get(name);
62      }
63  
64      public void setAttribute(Attribute attribute)
65          throws AttributeNotFoundException, MBeanException,
66          ReflectionException
67      {
68          try {
69              jkmx.setAttribute(oname, attribute);
70          } catch( Exception ex ) {
71              throw new MBeanException(ex);
72          }
73      }
74  
75      public Object invoke(String name, Object params[], String signature[])
76          throws MBeanException, ReflectionException
77      {
78          try {
79              jkmx.invoke(oname, name, params, signature);
80          } catch( Exception ex ) {
81              throw new MBeanException(ex);
82          }
83          return null;
84      }
85  }