DefaultExchangeFactory.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 java.util.Collection;
024 import java.util.HashMap;
025 import java.util.Map;
026 
027 import org.apache.log4j.Logger;
028 
029 import org.apache.qpid.AMQException;
030 import org.apache.qpid.AMQUnknownExchangeType;
031 import org.apache.qpid.framing.AMQShortString;
032 import org.apache.qpid.server.configuration.VirtualHostConfiguration;
033 import org.apache.qpid.server.registry.ApplicationRegistry;
034 import org.apache.qpid.server.virtualhost.VirtualHost;
035 
036 public class DefaultExchangeFactory implements ExchangeFactory
037 {
038     private static final Logger _logger = Logger.getLogger(DefaultExchangeFactory.class);
039 
040     private Map<AMQShortString, ExchangeType<? extends Exchange>> _exchangeClassMap = new HashMap<AMQShortString, ExchangeType<? extends Exchange>>();
041     private final VirtualHost _host;
042 
043     public DefaultExchangeFactory(VirtualHost host)
044     {
045         _host = host;
046         registerExchangeType(DirectExchange.TYPE);
047         registerExchangeType(TopicExchange.TYPE);
048         registerExchangeType(HeadersExchange.TYPE);
049         registerExchangeType(FanoutExchange.TYPE);
050     }
051 
052     public void registerExchangeType(ExchangeType<? extends Exchange> type)
053     {
054         _exchangeClassMap.put(type.getName(), type);
055     }
056 
057     public Collection<ExchangeType<? extends Exchange>> getRegisteredTypes()
058     {
059         return _exchangeClassMap.values();
060     }
061 
062     public Exchange createExchange(AMQShortString exchange, AMQShortString type, boolean durable, boolean autoDelete,
063                                    int ticket)
064             throws AMQException
065     {
066         ExchangeType<? extends Exchange> exchType = _exchangeClassMap.get(type);
067         if (exchType == null)
068         {
069 
070             throw new AMQUnknownExchangeType("Unknown exchange type: " + type,null);
071         }
072         Exchange e = exchType.newInstance(_host, exchange, durable, ticket, autoDelete);
073         return e;
074     }
075 
076     @Override
077     public void initialise(VirtualHostConfiguration hostConfig)
078     {
079 
080         if (hostConfig == null)
081         {
082             return;
083         }
084 
085         for(Object className : hostConfig.getCustomExchanges())
086         {
087             try
088             {
089                 ExchangeType<?> exchangeType = ApplicationRegistry.getInstance().getPluginManager().getExchanges().get(String.valueOf(className));
090                 if (exchangeType == null)
091                 {
092                     _logger.error("No such custom exchange class found: \""+String.valueOf(className)+"\"");
093                     return;
094                 }
095                 Class<? extends ExchangeType> exchangeTypeClass = exchangeType.getClass();
096                 ExchangeType type = exchangeTypeClass.newInstance();
097                 registerExchangeType(type);
098             }
099             catch (ClassCastException classCastEx)
100             {
101                 _logger.error("No custom exchange class: \""+String.valueOf(className)+"\" cannot be registered as it does not extend class \""+ExchangeType.class+"\"");
102             }
103             catch (IllegalAccessException e)
104             {
105                 _logger.error("Cannot create custom exchange class: \""+String.valueOf(className)+"\"",e);
106             }
107             catch (InstantiationException e)
108             {
109                 _logger.error("Cannot create custom exchange class: \""+String.valueOf(className)+"\"",e);
110             }
111         }
112 
113     }
114 }