ApplicationRegistry.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.registry;
022 
023 import java.net.InetSocketAddress;
024 import java.util.HashMap;
025 import java.util.Map;
026 
027 import org.apache.log4j.Logger;
028 import org.apache.mina.common.IoAcceptor;
029 import org.apache.qpid.server.configuration.ServerConfiguration;
030 import org.apache.qpid.server.management.ManagedObjectRegistry;
031 import org.apache.qpid.server.plugins.PluginManager;
032 import org.apache.qpid.server.security.access.ACLManager;
033 import org.apache.qpid.server.security.auth.database.PrincipalDatabaseManager;
034 import org.apache.qpid.server.security.auth.manager.AuthenticationManager;
035 import org.apache.qpid.server.virtualhost.VirtualHost;
036 import org.apache.qpid.server.virtualhost.VirtualHostRegistry;
037 
038 /**
039  * An abstract application registry that provides access to configuration information and handles the
040  * construction and caching of configurable objects.
041  <p/>
042  * Subclasses should handle the construction of the "registered objects" such as the exchange registry.
043  */
044 public abstract class ApplicationRegistry implements IApplicationRegistry
045 {
046     protected static final Logger _logger = Logger.getLogger(ApplicationRegistry.class);
047 
048     private static Map<Integer, IApplicationRegistry> _instanceMap = new HashMap<Integer, IApplicationRegistry>();
049 
050     private final Map<Class<?>, Object> _configuredObjects = new HashMap<Class<?>, Object>();
051 
052     protected final ServerConfiguration _configuration;
053 
054     public static final int DEFAULT_INSTANCE = 1;
055     public static final String DEFAULT_APPLICATION_REGISTRY = "org.apache.qpid.server.util.NullApplicationRegistry";
056     public static String _APPLICATION_REGISTRY = DEFAULT_APPLICATION_REGISTRY;
057 
058     protected final Map<InetSocketAddress, IoAcceptor> _acceptors = new HashMap<InetSocketAddress, IoAcceptor>();
059 
060     protected ManagedObjectRegistry _managedObjectRegistry;
061 
062     protected AuthenticationManager _authenticationManager;
063 
064     protected VirtualHostRegistry _virtualHostRegistry;
065 
066     protected ACLManager _accessManager;
067 
068     protected PrincipalDatabaseManager _databaseManager;
069 
070     protected PluginManager _pluginManager;
071 
072     static
073     {
074         Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownService()));
075     }
076 
077     private static class ShutdownService implements Runnable
078     {
079         public void run()
080         {
081             removeAll();
082         }
083     }
084 
085     public static void initialise(IApplicationRegistry instancethrows Exception
086     {
087         initialise(instance, DEFAULT_INSTANCE);
088     }
089 
090     public static void initialise(IApplicationRegistry instance, int instanceIDthrows Exception
091     {
092         if (instance != null)
093         {
094             _logger.info("Initialising Application Registry:" + instanceID);
095             _instanceMap.put(instanceID, instance);
096 
097             try
098             {
099                 instance.initialise();
100             }
101             catch (Exception e)
102             {
103                 _instanceMap.remove(instanceID);
104                 throw e;
105             }
106         }
107         else
108         {
109             remove(instanceID);
110         }
111     }
112 
113     /**
114      * Method to cleanly shutdown specified registry running in this JVM
115      *
116      @param instanceID the instance to shutdown
117      */
118 
119     public static void remove(int instanceID)
120     {
121         try
122         {
123             IApplicationRegistry instance = _instanceMap.get(instanceID);
124             if (instance != null)
125             {
126                 if (_logger.isInfoEnabled())
127                 {
128                     _logger.info("Shuting down ApplicationRegistry(" + instanceID + "):" + instance);
129                 }
130                 instance.close();
131             }
132         }
133         catch (Exception e)
134         {
135             _logger.error("Error shutting down Application Registry(" + instanceID + "): " + e, e);
136         }
137         finally
138         {
139             _instanceMap.remove(instanceID);
140         }
141     }
142 
143     /** Method to cleanly shutdown all registries currently running in this JVM */
144     public static void removeAll()
145     {
146         Object[] keys = _instanceMap.keySet().toArray();
147         for (Object k : keys)
148         {
149             remove((Integerk);
150         }
151     }
152 
153     protected ApplicationRegistry(ServerConfiguration configuration)
154     {
155         _configuration = configuration;
156     }
157 
158     public static IApplicationRegistry getInstance()
159     {
160         return getInstance(DEFAULT_INSTANCE);
161     }
162 
163     public static IApplicationRegistry getInstance(int instanceID)
164     {
165         synchronized (IApplicationRegistry.class)
166         {
167             IApplicationRegistry instance = _instanceMap.get(instanceID);
168 
169             if (instance == null)
170             {
171                 try
172                 {
173                     _logger.info("Creating DEFAULT_APPLICATION_REGISTRY: " + _APPLICATION_REGISTRY + " : Instance:" + instanceID);
174                     IApplicationRegistry registry = (IApplicationRegistryClass.forName(_APPLICATION_REGISTRY).getConstructor((Class[]) null).newInstance((Object[]) null);
175                     ApplicationRegistry.initialise(registry, instanceID);
176                     _logger.info("Initialised Application Registry:" + instanceID);
177                     return registry;
178                 }
179                 catch (Exception e)
180                 {
181                     _logger.error("Error configuring application: " + e, e);
182                     //throw new AMQBrokerCreationException(instanceID, "Unable to create Application Registry instance " + instanceID);
183                     throw new RuntimeException("Unable to create Application Registry", e);
184                 }
185             }
186             else
187             {
188                 return instance;
189             }
190         }
191     }
192 
193     public void close() throws Exception
194     {
195         if (_logger.isInfoEnabled())
196         {
197             _logger.info("Shutting down ApplicationRegistry:"+this);
198         }
199 
200         //Stop incomming connections
201         unbind();
202 
203         //Shutdown virtualhosts
204         for (VirtualHost virtualHost : getVirtualHostRegistry().getVirtualHosts())
205         {
206             virtualHost.close();
207         }
208 
209         // Replace above with this
210 //        _virtualHostRegistry.close();
211 
212 //        _accessManager.close();
213 
214 //        _databaseManager.close();
215 
216         _authenticationManager.close();
217 
218 //        _databaseManager.close();
219 
220         // close the rmi registry(if any) started for management
221         if (_managedObjectRegistry != null)
222         {
223             _managedObjectRegistry.close();
224         }
225 
226 //        _pluginManager.close();
227     }
228 
229     private void unbind()
230     {
231         synchronized (_acceptors)
232         {
233             for (InetSocketAddress bindAddress : _acceptors.keySet())
234             {
235                 IoAcceptor acceptor = _acceptors.get(bindAddress);
236                 acceptor.unbind(bindAddress);
237             }
238         }
239     }
240 
241     public ServerConfiguration getConfiguration()
242     {
243         return _configuration;
244     }
245 
246     public void addAcceptor(InetSocketAddress bindAddress, IoAcceptor acceptor)
247     {
248         synchronized (_acceptors)
249         {
250             _acceptors.put(bindAddress, acceptor);
251         }
252     }
253 
254     public static void setDefaultApplicationRegistry(String clazz)
255     {
256         _APPLICATION_REGISTRY = clazz;
257     }
258 
259     public VirtualHostRegistry getVirtualHostRegistry()
260     {
261         return _virtualHostRegistry;
262     }
263 
264     public ACLManager getAccessManager()
265     {
266         return new ACLManager(_configuration.getSecurityConfiguration(), _pluginManager);
267     }
268 
269     public ManagedObjectRegistry getManagedObjectRegistry()
270     {
271         return _managedObjectRegistry;
272     }
273 
274     public PrincipalDatabaseManager getDatabaseManager()
275     {
276         return _databaseManager;
277     }
278 
279     public AuthenticationManager getAuthenticationManager()
280     {
281         return _authenticationManager;
282     }
283 
284     public PluginManager getPluginManager()
285     {
286         return _pluginManager;
287     }
288 
289 }