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.MuleRuntimeException;
12  import org.mule.api.construct.FlowConstruct;
13  import org.mule.api.context.MuleContextAware;
14  import org.mule.api.registry.InjectProcessor;
15  import org.mule.module.ibeans.spi.MuleIBeansPlugin;
16  import org.mule.util.annotation.AnnotationMetaData;
17  import org.mule.util.annotation.AnnotationUtils;
18  
19  import java.lang.reflect.Field;
20  import java.util.Set;
21  
22  import org.ibeans.annotation.IntegrationBean;
23  
24  public class IntegrationBeanAnnotatedObjectProcessor implements InjectProcessor, MuleContextAware
25  {
26      private MuleContext muleContext;
27      private MuleIBeansPlugin plugin;
28  
29      public IntegrationBeanAnnotatedObjectProcessor()
30      {
31          super();
32      }
33  
34      public IntegrationBeanAnnotatedObjectProcessor(MuleContext muleContext)
35      {
36          this();
37          setMuleContext(muleContext);
38      }
39  
40      public void setMuleContext(MuleContext context)
41      {
42          this.muleContext = context;
43          this.plugin = new MuleIBeansPlugin(context);
44      }
45  
46      public Object process(Object object)
47      {
48          Set<AnnotationMetaData> annos = AnnotationUtils.getFieldAnnotationsForHierarchy(object.getClass(), IntegrationBean.class);
49  
50          for (AnnotationMetaData data : annos)
51          {
52              Field field = (Field) data.getMember();
53              field.setAccessible(true);
54              try
55              {
56                  if (field.get(object) != null)
57                  {
58                      continue;
59                  }
60              }
61              catch (IllegalAccessException e)
62              {
63                  continue;
64              }
65              IBeanBinding binding = createBinding(field.getType().getSimpleName());
66              binding.setInterface(field.getType());
67              Object proxy = binding.createProxy(new Object());
68              try
69              {
70                  field.set(object, proxy);
71              }
72              catch (IllegalAccessException e)
73              {
74                  throw new RuntimeException("Failed to create IntegrationBean proxy for: " + field.getType(), e);
75              }
76          }
77          return object;
78      }
79  
80      protected IBeanBinding createBinding(String name)
81      {
82          IBeanFlowConstruct flow = new IBeanFlowConstruct(name + ".ibean", muleContext);
83          try
84          {
85              muleContext.getRegistry().registerObject(flow.getName(), flow, FlowConstruct.class);
86          }
87          catch (MuleException e)
88          {
89              throw new MuleRuntimeException(e);
90          }
91          return new IBeanBinding(flow, muleContext, plugin);
92      }
93  }