AbstractExchange.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.server.exchange;
022 
023 import javax.management.MalformedObjectNameException;
024 import javax.management.NotCompliantMBeanException;
025 import javax.management.ObjectName;
026 import javax.management.openmbean.OpenType;
027 import javax.management.openmbean.CompositeType;
028 import javax.management.openmbean.TabularType;
029 import javax.management.openmbean.TabularDataSupport;
030 import javax.management.openmbean.OpenDataException;
031 import javax.management.openmbean.SimpleType;
032 import javax.management.openmbean.ArrayType;
033 
034 import org.apache.qpid.AMQException;
035 import org.apache.qpid.framing.AMQShortString;
036 import org.apache.qpid.server.management.AMQManagedObject;
037 import org.apache.qpid.server.management.Managable;
038 import org.apache.qpid.server.management.ManagedObject;
039 import org.apache.qpid.server.management.ManagedObjectRegistry;
040 import org.apache.qpid.server.queue.QueueRegistry;
041 import org.apache.qpid.server.queue.AMQQueue;
042 import org.apache.qpid.server.registry.ApplicationRegistry;
043 import org.apache.qpid.server.virtualhost.VirtualHost;
044 
045 import java.util.List;
046 import java.util.Map;
047 
048 public abstract class AbstractExchange implements Exchange, Managable
049 {
050     private AMQShortString _name;
051 
052 
053 
054     protected boolean _durable;
055     protected String _exchangeType;
056     protected int _ticket;
057 
058     private VirtualHost _virtualHost;
059 
060     protected ExchangeMBean _exchangeMbean;
061 
062     /**
063      * Whether the exchange is automatically deleted once all queues have detached from it
064      */
065     protected boolean _autoDelete;
066 
067     /**
068      * Abstract MBean class. This has some of the methods implemented from
069      * management intrerface for exchanges. Any implementaion of an
070      * Exchange MBean should extend this class.
071      */
072     protected abstract class ExchangeMBean extends AMQManagedObject implements ManagedExchange
073     {
074         // open mbean data types for representing exchange bindings
075         protected String[] _bindingItemNames;
076         protected String[] _bindingItemIndexNames;
077         protected OpenType[] _bindingItemTypes;
078         protected CompositeType _bindingDataType;
079         protected TabularType _bindinglistDataType;
080         protected TabularDataSupport _bindingList;
081         
082         public ExchangeMBean() throws NotCompliantMBeanException
083         {
084             super(ManagedExchange.class, ManagedExchange.TYPE);
085         }
086 
087         protected void init() throws OpenDataException
088         {
089             _bindingItemNames = new String[]{"Binding Key""Queue Names"};
090             _bindingItemIndexNames = new String[]{_bindingItemNames[0]};
091             
092             _bindingItemTypes = new OpenType[2];
093             _bindingItemTypes[0= SimpleType.STRING;
094             _bindingItemTypes[1new ArrayType(1, SimpleType.STRING);
095             _bindingDataType = new CompositeType("Exchange Binding""Binding key and Queue names",
096                                                  _bindingItemNames, _bindingItemNames, _bindingItemTypes);
097             _bindinglistDataType = new TabularType("Exchange Bindings""Exchange Bindings for " + getName(),
098                                                    _bindingDataType, _bindingItemIndexNames);
099         }
100 
101         public ManagedObject getParentObject()
102         {
103             return _virtualHost.getManagedObject();
104         }
105 
106         public String getObjectInstanceName()
107         {
108             return _name.toString();
109         }
110 
111         public String getName()
112         {
113             return _name.toString();
114         }
115 
116         public String getExchangeType()
117         {
118             return _exchangeType;
119         }
120 
121         public Integer getTicketNo()
122         {
123             return _ticket;
124         }
125 
126         public boolean isDurable()
127         {
128             return _durable;
129         }
130 
131         public boolean isAutoDelete()
132         {
133             return _autoDelete;
134         }
135 
136         // Added exchangetype in the object name lets maangement apps to do any customization required
137         public ObjectName getObjectName() throws MalformedObjectNameException
138         {
139             String objNameString = super.getObjectName().toString();
140             objNameString = objNameString + ",ExchangeType=" + _exchangeType;
141             return new ObjectName(objNameString);
142         }
143 
144         protected ManagedObjectRegistry getManagedObjectRegistry()
145         {
146             return ApplicationRegistry.getInstance().getManagedObjectRegistry();
147         }
148     // End of MBean class
149 
150     public AMQShortString getName()
151     {
152         return _name;
153     }
154 
155     /**
156      * Concrete exchanges must implement this method in order to create the managed representation. This is
157      * called during initialisation (template method pattern).
158      @return the MBean
159      */
160     protected abstract ExchangeMBean createMBean() throws AMQException;
161 
162     public void initialise(VirtualHost host, AMQShortString name, boolean durable, int ticket, boolean autoDeletethrows AMQException
163     {
164         _virtualHost = host;
165         _name = name;
166         _durable = durable;
167         _autoDelete = autoDelete;
168         _ticket = ticket;
169         _exchangeMbean = createMBean();
170         _exchangeMbean.register();
171     }
172 
173     public boolean isDurable()
174     {
175         return _durable;
176     }
177 
178     public boolean isAutoDelete()
179     {
180         return _autoDelete;
181     }
182 
183     public int getTicket()
184     {
185         return _ticket;
186     }
187 
188     public void close() throws AMQException
189     {
190         if (_exchangeMbean != null)
191         {
192             _exchangeMbean.unregister();
193         }
194     }    
195 
196     public String toString()
197     {
198         return getClass().getName() "[" + getName() +"]";
199     }
200 
201     public ManagedObject getManagedObject()
202     {
203         return _exchangeMbean;
204     }
205 
206     public VirtualHost getVirtualHost()
207     {
208         return _virtualHost;
209     }
210 
211     public QueueRegistry getQueueRegistry()
212     {
213         return getVirtualHost().getQueueRegistry();
214     }
215 }