View Javadoc

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      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()) logger.info("Scanning for annotations using the following paths: " + StringMessageUtils.toString(paths));
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      protected String[] findPackages() throws IOException
95      {
96          List<String> paths = new ArrayList<String>();
97          Properties p = new Properties();
98          Enumeration e = ClassUtils.getResources(IBEANS_PROPERTIES, getClass());
99          boolean scanAll = false;
100         while (e.hasMoreElements())
101         {
102             URL url = (URL) e.nextElement();
103 
104             if(logger.isInfoEnabled()) logger.info("reading packages from: " + url);
105             p.load(url.openStream());
106             String path = p.getProperty(getScanPackagesProperty());
107             if (path != null)
108             {
109                 for (StringTokenizer tokenizer = new StringTokenizer(path, ","); tokenizer.hasMoreTokens();)
110                 {
111                     String s = tokenizer.nextToken();
112                     if("*".equals(s)) {
113                         scanAll=true;
114                         break;
115                     }
116                     paths.add(s.trim());
117                 }
118             }
119         }
120 
121         if (paths.size() == 0 || scanAll)
122         {
123             return DEFAULT_BASE_PACKAGE;
124         }
125         return paths.toArray(new String[]{});
126     }
127 
128 
129 }