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.api.MuleContext;
10  import org.mule.api.config.ConfigurationException;
11  import org.mule.util.scan.ClasspathScanner;
12  
13  import java.io.IOException;
14  import java.util.HashSet;
15  import java.util.Set;
16  
17  import org.ibeans.annotation.Call;
18  import org.ibeans.annotation.IBeanGroup;
19  import org.ibeans.annotation.Template;
20  import org.ibeans.impl.IBeansNotationHelper;
21  
22  /**
23   * A configuration builder that registers iBean objects on the classpath with the Mule registry.
24   * <p/>
25   * The registry can then be used to query available iBeans.
26   */
27  public class IBeanHolderConfigurationBuilder extends AbstractAnnotationConfigurationBuilder
28  {
29      public static final String IBEAN_HOLDER_PREFIX = "_ibeanHolder.";
30  
31      public IBeanHolderConfigurationBuilder()
32      {
33          super();
34      }
35  
36      public IBeanHolderConfigurationBuilder(String... basepackages)
37      {
38          super(basepackages);
39      }
40  
41      public IBeanHolderConfigurationBuilder(ClassLoader classLoader)
42      {
43          super(classLoader);
44      }
45  
46      public IBeanHolderConfigurationBuilder(ClassLoader classLoader, String... basepackages)
47      {
48          super(classLoader, basepackages);
49      }
50  
51      @Override
52      protected String getScanPackagesProperty()
53      {
54          return "ibeans.scan.packages";
55      }
56  
57      @SuppressWarnings("rawtypes")
58      @Override
59      protected void doConfigure(MuleContext muleContext) throws Exception
60      {
61          Set<Class> ibeanClasses = new HashSet<Class>();
62          ClasspathScanner scanner = createClasspathScanner();
63  
64          try
65          {
66              //There will be some overlap here but only
67              ibeanClasses.addAll(scanner.scanFor(Call.class, ClasspathScanner.INCLUDE_INTERFACE));
68              ibeanClasses.addAll(scanner.scanFor(Template.class, ClasspathScanner.INCLUDE_INTERFACE));
69              //Some ibeans will extend other iBeans but have not methods of there own
70              ibeanClasses.addAll(scanner.scanFor(IBeanGroup.class, ClasspathScanner.INCLUDE_INTERFACE));
71          }
72          catch (IOException e)
73          {
74              throw new ConfigurationException(e);
75          }
76  
77          for (Class<?> ibeanClass : ibeanClasses)
78          {
79              muleContext.getRegistry().registerObject(IBeansNotationHelper.getIBeanShortID(ibeanClass), new IBeanHolder(ibeanClass));
80          }
81      }
82  }