View Javadoc
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      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          this(DEFAULT_BASE_PACKAGE);
49      }
50  
51      public AbstractAnnotationConfigurationBuilder(String... basepackages)
52      {
53          this.classLoader = Thread.currentThread().getContextClassLoader();
54          this.basepackages = basepackages;
55      }
56  
57      public AbstractAnnotationConfigurationBuilder(ClassLoader classLoader)
58      {
59          this(classLoader, DEFAULT_BASE_PACKAGE);
60      }
61  
62      public AbstractAnnotationConfigurationBuilder(ClassLoader classLoader, String... basepackages)
63      {
64          this.classLoader = classLoader;
65          this.basepackages = basepackages;
66      }
67  
68      protected ClasspathScanner createClasspathScanner() throws IOException
69      {
70          if (Arrays.equals(DEFAULT_BASE_PACKAGE, basepackages))
71          {
72              basepackages = findPackages();
73          }
74  
75          String[] paths = convertPackagesToPaths(basepackages);
76          if(logger.isInfoEnabled())
77          {
78              logger.info("Scanning for annotations using the following paths: " + StringMessageUtils.toString(paths));
79          }
80          return new ClasspathScanner(classLoader, paths);
81      }
82  
83      protected abstract String getScanPackagesProperty();
84  
85      protected String[] convertPackagesToPaths(String[] packages)
86      {
87          String[] paths = new String[packages.length];
88          for (int i = 0; i < packages.length; i++)
89          {
90              paths[i] = packages[i].replaceAll("[.]", "/");
91          }
92          return paths;
93      }
94  
95      protected String[] findPackages() throws IOException
96      {
97          List<String> paths = new ArrayList<String>();
98          Properties p = new Properties();
99          Enumeration e = ClassUtils.getResources(IBEANS_PROPERTIES, getClass());
100         boolean scanAll = false;
101         while (e.hasMoreElements())
102         {
103             URL url = (URL) e.nextElement();
104 
105             if(logger.isInfoEnabled()) logger.info("reading packages from: " + url);
106             p.load(url.openStream());
107             String path = p.getProperty(getScanPackagesProperty());
108             if (path != null)
109             {
110                 for (StringTokenizer tokenizer = new StringTokenizer(path, ","); tokenizer.hasMoreTokens();)
111                 {
112                     String s = tokenizer.nextToken();
113                     if("*".equals(s)) {
114                         scanAll=true;
115                         break;
116                     }
117                     paths.add(s.trim());
118                 }
119             }
120         }
121 
122         if (paths.size() == 0 || scanAll)
123         {
124             return DEFAULT_BASE_PACKAGE;
125         }
126         return paths.toArray(new String[]{});
127     }
128 }