View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.tags.jmx;
18  
19  import javax.management.InstanceAlreadyExistsException;
20  import javax.management.MBeanRegistrationException;
21  import javax.management.MBeanServer;
22  import javax.management.NotCompliantMBeanException;
23  import javax.management.ObjectName;
24  
25  import org.apache.commons.jelly.JellyTagException;
26  import org.apache.commons.jelly.MissingAttributeException;
27  import org.apache.commons.jelly.TagSupport;
28  import org.apache.commons.jelly.XMLOutput;
29  import org.apache.commons.jelly.impl.CollectionTag;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /***
34   * Registers a JavaBean or JMX MBean with a server..
35   *
36   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
37   * @version $Revision: 155420 $
38   */
39  public class RegisterTag extends TagSupport implements CollectionTag {
40  
41      /*** The Log to which logging calls will be made. */
42      private static final Log log = LogFactory.getLog(RegisterTag.class);
43  
44      private ObjectName name;
45      private MBeanServer server;
46  
47      public RegisterTag() {
48      }
49  
50  
51      // CollectionTag interface
52      //-------------------------------------------------------------------------
53      public void addItem(Object bean) throws JellyTagException {
54          try {
55              register(server, bean);
56          }
57          catch (Exception e) {
58              throw new JellyTagException("Failed to register bean: " + bean, e);
59          }
60      }
61  
62      // Tag interface
63      //-------------------------------------------------------------------------
64      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
65          if (name == null) {
66              throw new MissingAttributeException("name");
67          }
68          if (server == null) {
69              ServerTag serverTag = (ServerTag) findAncestorWithClass(ServerTag.class);
70              if (serverTag == null) {
71                  throw new JellyTagException("This class must be nested inside a <server> tag");
72              }
73              server = serverTag.getServer();
74          }
75          invokeBody(output);
76      }
77  
78  
79      // Properties
80      //-------------------------------------------------------------------------
81  
82  
83      /***
84       * @return ObjectName
85       */
86      public ObjectName getName() {
87          return name;
88      }
89  
90      /***
91       * Sets the name.
92       * @param name The name to set
93       */
94      public void setName(ObjectName name) {
95          this.name = name;
96      }
97  
98      /***
99       * @return MBeanServer
100      */
101     public MBeanServer getServer() {
102         return server;
103     }
104 
105     /***
106      * Sets the MBeanServer. If this attribute is not supplied then the parent &lt;server&gt; tag
107      * is used to get the MBeanServer instance to use.
108      *
109      * @param server The MBeanServer to register the mbeans with.
110      */
111     public void setServer(MBeanServer server) {
112         this.server = server;
113     }
114 
115     // Implementation methods
116     //-------------------------------------------------------------------------
117 
118     /***
119      * Registers the given bean with the MBeanServer
120      */
121     protected void register(MBeanServer server, Object bean) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
122         server.registerMBean(bean, getName());
123     }
124 
125 }