Coverage Report - org.mule.module.ibeans.config.AbstractAnnotationConfigurationBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAnnotationConfigurationBuilder
0%
0/44
0%
0/20
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.ibeans.config;
 8  
 
 9  
 import org.mule.config.AnnotationsConfigurationBuilder;
 10  
 import org.mule.util.ClassUtils;
 11  
 import org.mule.util.StringMessageUtils;
 12  
 import org.mule.util.scan.ClasspathScanner;
 13  
 
 14  
 import java.io.IOException;
 15  
 import java.net.URL;
 16  
 import java.util.ArrayList;
 17  
 import java.util.Arrays;
 18  
 import java.util.Enumeration;
 19  
 import java.util.List;
 20  
 import java.util.Properties;
 21  
 import java.util.StringTokenizer;
 22  
 
 23  
 /**
 24  
  * Provides implementation support for configuration builders that configure Mule by scanning annotations on the
 25  
  * classpath.
 26  
  *
 27  
  * One of more scan packages can be used to locate classes with annotations. The packages to be scanned cn be configured
 28  
  * in two ways:
 29  
  *
 30  
  * 1) Pass one or more comma-separated packages into the constructor of this builder
 31  
  * 2) if no packages are set via the constructor or the {@link #DEFAULT_BASE_PACKAGE} value is used, the classpath will be
 32  
  * scanned for <code>META-INF/ibeans.properties</code>. Zero or more of these will be loaded and the package names defined in
 33  
  * either the 'ibeans.scan.packages' or 'annotations.scan.packages' will be scanned.  This allows users to configure
 34  
  * specific packages to scan in their application.
 35  
  */
 36  
 public abstract class AbstractAnnotationConfigurationBuilder extends AnnotationsConfigurationBuilder
 37  
 {
 38  
     public static final String IBEANS_PROPERTIES = "META-INF/ibeans-app.properties";
 39  
 
 40  0
     public static final String[] DEFAULT_BASE_PACKAGE = new String[]{""};
 41  
 
 42  
 
 43  
     protected ClassLoader classLoader;
 44  
     protected String[] basepackages;
 45  
 
 46  
     public AbstractAnnotationConfigurationBuilder()
 47  
     {
 48  0
         this(DEFAULT_BASE_PACKAGE);
 49  0
     }
 50  
 
 51  
     public AbstractAnnotationConfigurationBuilder(String... basepackages)
 52  0
     {
 53  0
         this.classLoader = Thread.currentThread().getContextClassLoader();
 54  0
         this.basepackages = basepackages;
 55  0
     }
 56  
 
 57  
     public AbstractAnnotationConfigurationBuilder(ClassLoader classLoader)
 58  
     {
 59  0
         this(classLoader, DEFAULT_BASE_PACKAGE);
 60  0
     }
 61  
 
 62  
     public AbstractAnnotationConfigurationBuilder(ClassLoader classLoader, String... basepackages)
 63  0
     {
 64  0
         this.classLoader = classLoader;
 65  0
         this.basepackages = basepackages;
 66  0
     }
 67  
 
 68  
     protected ClasspathScanner createClasspathScanner() throws IOException
 69  
     {
 70  0
         if (Arrays.equals(DEFAULT_BASE_PACKAGE, basepackages))
 71  
         {
 72  0
             basepackages = findPackages();
 73  
         }
 74  
 
 75  0
         String[] paths = convertPackagesToPaths(basepackages);
 76  0
         if(logger.isInfoEnabled())
 77  
         {
 78  0
             logger.info("Scanning for annotations using the following paths: " + StringMessageUtils.toString(paths));
 79  
         }
 80  0
         return new ClasspathScanner(classLoader, paths);
 81  
     }
 82  
 
 83  
     protected abstract String getScanPackagesProperty();
 84  
 
 85  
     protected String[] convertPackagesToPaths(String[] packages)
 86  
     {
 87  0
         String[] paths = new String[packages.length];
 88  0
         for (int i = 0; i < packages.length; i++)
 89  
         {
 90  0
             paths[i] = packages[i].replaceAll("[.]", "/");
 91  
         }
 92  0
         return paths;
 93  
     }
 94  
 
 95  
     protected String[] findPackages() throws IOException
 96  
     {
 97  0
         List<String> paths = new ArrayList<String>();
 98  0
         Properties p = new Properties();
 99  0
         Enumeration e = ClassUtils.getResources(IBEANS_PROPERTIES, getClass());
 100  0
         boolean scanAll = false;
 101  0
         while (e.hasMoreElements())
 102  
         {
 103  0
             URL url = (URL) e.nextElement();
 104  
 
 105  0
             if(logger.isInfoEnabled()) logger.info("reading packages from: " + url);
 106  0
             p.load(url.openStream());
 107  0
             String path = p.getProperty(getScanPackagesProperty());
 108  0
             if (path != null)
 109  
             {
 110  0
                 for (StringTokenizer tokenizer = new StringTokenizer(path, ","); tokenizer.hasMoreTokens();)
 111  
                 {
 112  0
                     String s = tokenizer.nextToken();
 113  0
                     if("*".equals(s)) {
 114  0
                         scanAll=true;
 115  0
                         break;
 116  
                     }
 117  0
                     paths.add(s.trim());
 118  0
                 }
 119  
             }
 120  0
         }
 121  
 
 122  0
         if (paths.size() == 0 || scanAll)
 123  
         {
 124  0
             return DEFAULT_BASE_PACKAGE;
 125  
         }
 126  0
         return paths.toArray(new String[]{});
 127  
     }
 128  
 }