View Javadoc

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