PluginManager.java
001 /*
002  * Licensed to the Apache Software Foundation (ASF) under one
003  * or more contributor license agreements.  See the NOTICE file
004  * distributed with this work for additional information
005  * regarding copyright ownership.  The ASF licenses this file
006  * to you under the Apache License, Version 2.0 (the
007  * "License"); you may not use this file except in compliance
008  * with the License.  You may obtain a copy of the License at
009  *
010  *   http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing,
013  * software distributed under the License is distributed on an
014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015  * KIND, either express or implied.  See the License for the
016  * specific language governing permissions and limitations
017  * under the License.
018  */
019 
020 package org.apache.qpid.server.plugins;
021 
022 import java.io.File;
023 import java.util.ArrayList;
024 import java.util.HashMap;
025 import java.util.List;
026 import java.util.Map;
027 
028 import org.apache.felix.framework.Felix;
029 import org.apache.felix.framework.cache.BundleCache;
030 import org.apache.felix.framework.util.FelixConstants;
031 import org.apache.felix.framework.util.StringMap;
032 import org.apache.qpid.server.exchange.ExchangeType;
033 import org.apache.qpid.server.security.access.ACLPlugin;
034 import org.apache.qpid.server.security.access.ACLPluginFactory;
035 import org.apache.qpid.server.security.access.plugins.AllowAll;
036 import org.apache.qpid.server.security.access.plugins.DenyAll;
037 import org.apache.qpid.server.security.access.plugins.SimpleXML;
038 import org.osgi.framework.BundleActivator;
039 import org.osgi.framework.BundleException;
040 import org.osgi.util.tracker.ServiceTracker;
041 
042 /**
043  
044  @author aidan
045  *
046  * Provides access to pluggable elements, such as exchanges
047  */
048 
049 public class PluginManager
050 {
051 
052     private Felix _felix = null;
053     private ServiceTracker _exchangeTracker = null;
054     private ServiceTracker _securityTracker = null;
055     private Activator _activator = null;
056     private boolean _empty;
057     private Map<String, ACLPluginFactory> _securityPlugins;
058 
059     public PluginManager(String plugindirthrows Exception
060     {
061         StringMap configMap = new StringMap(false);
062 
063         // Tell felix it's being embedded
064         configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
065         // Add the bundle provided service interface package and the core OSGi
066         // packages to be exported from the class path via the system bundle.
067         configMap.put(FelixConstants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.3.0,"
068                 "org.osgi.service.packageadmin; version=1.2.0," 
069                 "org.osgi.service.startlevel; version=1.0.0," 
070                 "org.osgi.service.url; version=1.0.0," 
071                 "org.apache.qpid.framing; version=0.2.1," +
072                 "org.apache.qpid.server.exchange; version=0.2.1," +
073                 "org.apache.qpid.server.management; version=0.2.1,"+
074                 "org.apache.qpid.protocol; version=0.2.1,"+
075                 "org.apache.qpid.server.virtualhost; version=0.2.1," +
076                 "org.apache.qpid; version=0.2.1," +
077                 "org.apache.qpid.server.queue; version=0.2.1," +
078                 "javax.management.openmbean; version=1.0.0,"+
079                 "javax.management; version=1.0.0,"+
080                 "org.apache.qpid.junit.extensions.util; version=0.6.1,"
081                 );
082         
083         if (plugindir == null)
084         {
085           _empty = true;
086             return;
087         }
088         
089         // Set the list of bundles to load
090         File dir = new File(plugindir);
091         if (!dir.exists())
092         {
093           _empty = true;
094             return;
095         }
096         StringBuffer pluginJars = new StringBuffer();
097         
098         if (dir.isDirectory())
099         {
100             for (String child : dir.list())
101             {
102                 if (child.endsWith("jar"))
103                 {
104                     pluginJars.append(String.format(" file:%s%s%s", plugindir,File.separator,child));
105                 }
106             }
107         }
108         if (pluginJars.length() == 0)
109         {
110             _empty = true;
111             return;
112         }
113             
114         configMap.put(FelixConstants.AUTO_START_PROP + ".1", pluginJars.toString());
115         configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, plugindir);
116         
117         List<BundleActivator> activators = new ArrayList<BundleActivator>();
118         _activator = new Activator();
119         activators.add(_activator);
120 
121         _felix = new Felix(configMap, activators);
122         try
123         {
124             _felix.start();
125             
126             _exchangeTracker = new ServiceTracker(_activator.getContext(), ExchangeType.class.getName()null);
127             _exchangeTracker.open();
128             
129             _securityTracker = new ServiceTracker(_activator.getContext(), ACLPlugin.class.getName()null);
130             _exchangeTracker.open();
131             
132         }
133         catch (BundleException e)
134         {
135             throw new Exception("Could not create bundle");
136         }
137     }
138 
139     private <type> Map<String, type> getServices(ServiceTracker tracker)
140     {   
141         Map<String, type>exchanges = new HashMap<String, type>();
142         
143         if (tracker != null)
144         {
145             for (Object service : tracker.getServices())
146             {
147                 exchanges.put(service.getClass().getName()(typeservice);
148             }
149         }
150         
151         return exchanges;
152     }
153     
154     public Map<String, ExchangeType<?>> getExchanges()
155     {
156         return getServices(_exchangeTracker);
157     }
158     
159     public Map<String, ACLPluginFactory> getSecurityPlugins()
160     {
161         if (_securityPlugins == null)
162         {
163             _securityPlugins = getServices(_securityTracker);
164             // A little gross that we have to add them here, but not all the plugins are OSGIfied
165             _securityPlugins.put(SimpleXML.class.getName(), SimpleXML.FACTORY);
166             _securityPlugins.put(AllowAll.class.getName(), AllowAll.FACTORY);
167             _securityPlugins.put(DenyAll.class.getName(), DenyAll.FACTORY);
168         }
169         return _securityPlugins;
170     }
171 
172 }