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.MuleException;
11  import org.mule.api.construct.FlowConstruct;
12  import org.mule.module.ibeans.spi.MuleIBeansPlugin;
13  
14  import org.ibeans.impl.IBeansNotationHelper;
15  import org.ibeans.impl.view.TextView;
16  
17  /**
18   * Holds a reference to an iBeans class in the registry. An iBean instance can be created from this object as well as reporting
19   * its usage and short ID.
20   */
21  public class IBeanHolder implements Comparable
22  {
23      private Class ibean;
24      private String usage;
25  
26      public IBeanHolder(Class ibean)
27      {
28          this.ibean = ibean;
29      }
30  
31      public int compareTo(Object o)
32      {
33          IBeanHolder to = (IBeanHolder) o;
34          return getId().compareTo(to.getId());
35      }
36  
37      public Class getIbeanClass()
38      {
39          return ibean;
40      }
41  
42      public Object create(MuleContext muleContext, MuleIBeansPlugin plugin) throws MuleException
43      {
44          final String name = String.format("%s.%d", ibean.getSimpleName(), System.identityHashCode(this));
45          IBeanFlowConstruct flow = new IBeanFlowConstruct(name, muleContext);
46          muleContext.getRegistry().registerObject(flow.getName(), flow, FlowConstruct.class);
47  
48          IBeanBinding router = new IBeanBinding(flow, muleContext, plugin);
49          router.setInterface(ibean);
50          return router.createProxy(new Object());
51      }
52  
53      public String getId()
54      {
55          return IBeansNotationHelper.getIBeanShortID(ibean);
56      }
57  
58      public String getUsage()
59      {
60          if (usage == null)
61          {
62              TextView view = new TextView();
63              usage = view.createView(ibean);
64          }
65          return usage;
66      }
67  
68      @Override
69      public String toString()
70      {
71          return "IBean: " + getId() + " : " + ibean.getName();
72      }
73  }