MBeanCapability.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.wsdm.capabilities;
022 
023 import java.lang.management.ManagementFactory;
024 
025 import javax.management.Attribute;
026 import javax.management.AttributeNotFoundException;
027 import javax.management.InstanceNotFoundException;
028 import javax.management.MBeanException;
029 import javax.management.MBeanServer;
030 import javax.management.ObjectName;
031 
032 import org.apache.muse.ws.resource.impl.AbstractWsResourceCapability;
033 import org.apache.qpid.management.Messages;
034 import org.apache.qpid.management.domain.handler.impl.InvocationResult;
035 import org.apache.qpid.management.domain.services.MethodInvocationException;
036 import org.apache.qpid.management.wsdm.common.EntityInstanceNotFoundFault;
037 import org.apache.qpid.management.wsdm.common.MethodInvocationFault;
038 import org.apache.qpid.management.wsdm.common.NoSuchAttributeFault;
039 import org.apache.qpid.management.wsdm.common.QManFault;
040 import org.apache.qpid.transport.util.Logger;
041 
042 /**
043  * Abstract capability used for centralize common 
044  * behaviour of the QMan resource(s) related capabilities.
045  
046  @author Andrea Gazzarini
047  */
048 public abstract class MBeanCapability extends AbstractWsResourceCapability 
049 {
050   private static final Logger LOGGER = Logger.get(MBeanCapability.class);
051 
052   protected final MBeanServer _mxServer;
053   protected ObjectName _objectName;
054   
055   /**
056    * Builds a new capability related to the given object name.
057    
058    @param objectName the name of the target object of this capability.
059    */
060   public MBeanCapability() 
061   {
062     _mxServer = ManagementFactory.getPlatformMBeanServer();
063   }
064 
065   /**
066    * Injects on this capability the object name of the target mbean.
067    
068    @param objectName the object name of the target mbean.
069    */
070   void setResourceObjectName(ObjectName objectName
071   {
072     this._objectName = objectName;
073   }
074 
075   /**
076    * Returns the attribute value of a QMan managed object instance.
077    
078    @param attributeName the name of the attribute to be requested.
079    @return the value for the requested attribute.
080    @throws NoSuchAttributeFault when the requested attribute cannot be found 
081    *       on the given entity instance.
082    @throws EntityInstanceNotFoundFault when the requested entity instance cannot 
083    *       be found.
084    @throws QManFault in case of internal system failure.
085    */
086   Object getAttribute(String attributeNamethrows NoSuchAttributeFault, EntityInstanceNotFoundFault, QManFault 
087   {
088     try 
089     {
090       return _mxServer.getAttribute(_objectName, attributeName);
091     catch (AttributeNotFoundException exception
092     {
093       throw new NoSuchAttributeFault(
094           getWsResource().getEndpointReference()
095           attributeName);
096     catch (InstanceNotFoundException exception
097     {
098       throw new EntityInstanceNotFoundFault(
099           getWsResource().getEndpointReference()
100           _objectName);
101     catch (Exception exception
102     {
103       LOGGER.error(
104           Messages.QMAN_100035_GET_ATTRIBUTE_FAILURE,
105           attributeName,
106           _objectName);      
107       throw new QManFault(
108           getWsResource().getEndpointReference(),
109           exception);
110     }
111   }
112 
113   /**
114    * Sets the value for the given attribute on this MBean (proxy).
115    
116    @param objectName
117    *            the object name of the target instance (excluding the domain
118    *            name).
119    @param attributeName
120    *            the name of the attribute to be requested.
121    @param value
122    *            the value for the requested attribute.
123    @throws NoSuchAttributeFault
124    *             when the requested attribute cannot be found on the given
125    *             entity instance.
126    @throws EntityInstanceNotFoundFault
127    *             when the requested entity instance cannot be found.
128    @throws QManFault
129    *             in case of internal system failure.
130    */
131   void setAttribute(String attributeName, Object valuethrows NoSuchAttributeFault, EntityInstanceNotFoundFault, QManFault 
132   {
133     try 
134     {
135       _mxServer.setAttribute(_objectName, new Attribute(attributeName,value));
136     catch (AttributeNotFoundException exception
137     {
138       throw new NoSuchAttributeFault(
139           getWsResource().getEndpointReference()
140           attributeName);
141     catch (InstanceNotFoundException exception
142     {
143       throw new EntityInstanceNotFoundFault(
144           getWsResource().getEndpointReference()
145           _objectName);
146     catch (Exception exception
147     {
148       LOGGER.error(
149           Messages.QMAN_100036_SET_ATTRIBUTE_FAILURE,
150           attributeName,
151           _objectName);      
152       throw new QManFault(
153           getWsResource().getEndpointReference(),
154           exception);
155     }
156   }
157 
158   /**
159    * Invokes the requested operation on target JMX resource.
160    
161    @param operationName the name of the operation to be invoked.
162    @param params parameters used for operation invocation.
163    @param signature the operation / method signature.
164    @throws EntityInstanceNotFoundFault 
165    *     when the target MBean doesn't exist on Management server.
166    @throws MethodInvocationFault 
167    *     when the invocation of the requested operation raises an exception.
168    @throws QManFault 
169    *     in case of not-well known failure.
170    */
171   Result invoke(String operationName, Object [] params, String [] signaturethrows EntityInstanceNotFoundFault, MethodInvocationFault,QManFault 
172   {
173     try
174     {
175       InvocationResult output =  (InvocationResult_mxServer
176         .invoke(
177             _objectName, 
178             operationName, 
179             params, 
180             signature);
181       
182       Result result = new Result(
183           output.getReturnCode(),
184           output.getStatusText(),
185           output.getOutputSection());
186       
187       return result;
188     catch (InstanceNotFoundException exception)
189     {
190       throw new EntityInstanceNotFoundFault(
191           getWsResource().getEndpointReference()
192           _objectName);
193     catch (MBeanException exception)
194     {
195       if (exception.getTargetException() instanceof MethodInvocationException)
196       {
197         MethodInvocationException failure = (MethodInvocationExceptionexception.getTargetException();
198         throw new MethodInvocationFault(
199             getWsResource().getEndpointReference(),
200             operationName,
201             failure.getStatusText(),
202             failure.getReturnCode());        
203       else {
204         LOGGER.error(
205             Messages.QMAN_100037_INVOKE_OPERATION_FAILURE,
206             operationName,
207             _objectName);      
208         throw new QManFault(
209             getWsResource().getEndpointReference(),
210             exception);              
211       }
212     }catch(Exception exception)
213     {
214       LOGGER.error(
215           Messages.QMAN_100037_INVOKE_OPERATION_FAILURE,
216           operationName,
217           _objectName);      
218       throw new QManFault(
219           getWsResource().getEndpointReference(),
220           exception);      
221     }
222   }
223 }