QEmu.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;
022 
023 import javax.management.MBeanRegistration;
024 import javax.management.MBeanServer;
025 import javax.management.NotificationBroadcasterSupport;
026 import javax.management.ObjectName;
027 
028 import org.apache.qpid.management.Names;
029 import org.apache.qpid.management.domain.handler.impl.QpidDomainObject;
030 import org.apache.qpid.management.domain.handler.impl.QpidDomainObjectMBean;
031 import org.apache.qpid.management.jmx.EntityLifecycleNotification;
032 
033 /**
034  * QEmu is basically an instance creator that is installed separately 
035  * as part of QMan test cases & examples.
036  * Reason for that is to emulate object creation (queues, exchanges, etc...) 
037  * without having Qpid broker connected and therefore controlling the 
038  * total number of the instances that are created.
039  
040  @author Andrea Gazzarini
041  */
042 public class QEmu extends NotificationBroadcasterSupport implements QEmuMBean, MBeanRegistration{
043    
044   private MBeanServer _mxServer;
045   private final static String PACKAGE_NAME= "org.apache.qpid";
046   private final static String QUEUE = "queue";
047   
048   /**
049    * Unregisters a Queue MBean with MBeanServer.
050    
051    @param objectName the name of the MBean that must unregistered.
052    @throws Exception when the creation or the registration fails.
053    */
054   public void unregister(ObjectName objectNamethrows Exception 
055   {
056     _mxServer.unregisterMBean(objectName);
057     
058     sendNotification(
059         EntityLifecycleNotification.INSTANCE_REMOVED_NOTIFICATION_TYPE,
060         objectName);
061   }
062 
063   /**
064    * Creates and registers a Queue MBean with MBeanServer.
065    
066    @param objectName the name of the queue MBean.
067    @throws Exception when the creation or the registration fails.
068    */
069   public void createQueue(ObjectName objectNamethrows Exception 
070   {
071     QpidDomainObjectMBean queue = new QpidDomainObject();
072     _mxServer.registerMBean(queue, objectName);
073     
074     sendNotification(
075         EntityLifecycleNotification.INSTANCE_ADDED_NOTIFICATION_TYPE,
076         objectName);
077   }
078 
079   /**
080    * Sends a notification about a lifecycle event of the mbean associated 
081    * with the given object.
082    
083    @param type the event (notification) type.
084    @param name the name of the event source.
085    */
086   private void sendNotification(String type,ObjectName name)
087   {
088     sendNotification(
089         new EntityLifecycleNotification(
090             type,
091             PACKAGE_NAME, 
092             QUEUE, 
093             Names.CLASS,
094             name));
095   }
096   
097   /**
098    * Not implemented for this class.
099    */
100   public void postDeregister()
101   {
102     // N.A.
103   }
104 
105   /**
106    * Not implemented for this class.
107    */
108   public void postRegister(Boolean registrationDone)
109   {
110     // N.A.
111   }
112 
113   /**
114    * Not implemented for this class.
115    */
116   public void preDeregister()
117   {
118     // N.A.
119   }
120 
121   /**
122    * MBean server callback.
123    * Stores the value of the owner MBeanServer.
124    */
125   public ObjectName preRegister(MBeanServer server, ObjectName name
126   {
127     this._mxServer = server;
128     return name;
129   }
130 }