001    package com.khubla.pragmatach.framework.plugin;
002    
003    import java.io.InputStream;
004    import java.net.URL;
005    import java.util.HashMap;
006    import java.util.Map;
007    
008    import javax.servlet.ServletContext;
009    
010    import org.scannotation.ClasspathUrlFinder;
011    import org.scannotation.WarUrlFinder;
012    import org.scannotation.archiveiterator.IteratorFactory;
013    import org.scannotation.archiveiterator.StreamIterator;
014    import org.slf4j.Logger;
015    import org.slf4j.LoggerFactory;
016    
017    import com.khubla.pragmatach.framework.api.PragmatachException;
018    
019    /**
020     * @author tome
021     */
022    public class PluginDescriptors {
023       /**
024        * logger
025        */
026       private static final Logger logger = LoggerFactory.getLogger(PluginDescriptors.class);
027       /**
028        * plugin jars
029        */
030       private static Map<String, PluginDescriptor> plugins = new HashMap<String, PluginDescriptor>();
031    
032       public static Map<String, PluginDescriptor> getPlugins() {
033          return plugins;
034       }
035    
036       /**
037        * do the scan
038        */
039       public static void scan(ServletContext servletContext) throws PragmatachException {
040          scanURLs(ClasspathUrlFinder.findClassPaths(), servletContext);
041          scanURLs(WarUrlFinder.findWebInfLibClasspaths(servletContext), servletContext);
042          scanURLs(new URL[] { WarUrlFinder.findWebInfClassesPath(servletContext) }, servletContext);
043       }
044    
045       private static void scanURLs(URL[] urls, ServletContext servletContext) throws PragmatachException {
046          try {
047             if (null != servletContext) {
048                final PluginFilter pluginFilter = new PluginFilter();
049                if ((null != urls) && (urls.length > 0)) {
050                   for (final URL url : urls) {
051                      if (null != url) {
052                         final StreamIterator streamIterator = IteratorFactory.create(url, pluginFilter);
053                         InputStream inputStream;
054                         while (null != (inputStream = streamIterator.next())) {
055                            final PluginDescriptor plugin = new PluginDescriptor(url, inputStream, servletContext);
056                            logger.info("Found plugin: " + plugin.getName());
057                            plugins.put(plugin.getName(), plugin);
058                            inputStream.close();
059                         }
060                      }
061                   }
062                }
063             }
064          } catch (final Exception e) {
065             throw new PragmatachException("Exception in scanURLs", e);
066          }
067       }
068    }