View Javadoc

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