View Javadoc

1   /*
2    * $Id: IBeanHolderConfigurationBuilder.java 22231 2011-06-21 09:55:51Z 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.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  
24  /**
25   * A configuration builder that registers iBean objects on the classpath with the Mule registry.
26   * <p/>
27   * The registry can then be used to query available iBeans.
28   */
29  public class IBeanHolderConfigurationBuilder extends AbstractAnnotationConfigurationBuilder
30  {
31      public static final String IBEAN_HOLDER_PREFIX = "_ibeanHolder.";
32  
33      public IBeanHolderConfigurationBuilder()
34      {
35          super();
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      @Override
54      protected String getScanPackagesProperty()
55      {
56          return "ibeans.scan.packages";
57      }
58  
59      @Override
60      protected void doConfigure(MuleContext muleContext) throws Exception
61      {
62          Set<Class<?>> ibeanClasses = new HashSet<Class<?>>();
63          ClasspathScanner scanner = createClasspathScanner();
64  
65          try
66          {
67              //There will be some overlap here but only
68              ibeanClasses.addAll(scanner.scanFor(Call.class, ClasspathScanner.INCLUDE_INTERFACE));
69              ibeanClasses.addAll(scanner.scanFor(Template.class, ClasspathScanner.INCLUDE_INTERFACE));
70              //Some ibeans will extend other iBeans but have not methods of there own
71              ibeanClasses.addAll(scanner.scanFor(IBeanGroup.class, ClasspathScanner.INCLUDE_INTERFACE));
72          }
73          catch (IOException e)
74          {
75              throw new ConfigurationException(e);
76          }
77  
78          for (Class<?> ibeanClass : ibeanClasses)
79          {
80              muleContext.getRegistry().registerObject(IBeanHolder.getId(ibeanClass), new IBeanHolder(ibeanClass));
81          }
82      }
83  }