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