View Javadoc

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