Coverage Report - org.mule.module.guice.MuleSupportModule
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleSupportModule
0%
0/6
N/A
0
MuleSupportModule$1
0%
0/4
N/A
0
MuleSupportModule$MuleBindInjector
0%
0/16
0%
0/12
0
MuleSupportModule$MuleContextAwareInjector
0%
0/4
0%
0/2
0
 
 1  
 /*
 2  
  * $Id: MuleSupportModule.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.guice;
 11  
 
 12  
 import org.mule.api.MuleContext;
 13  
 import org.mule.api.MuleRuntimeException;
 14  
 import org.mule.api.NamedObject;
 15  
 import org.mule.api.agent.Agent;
 16  
 import org.mule.api.context.MuleContextAware;
 17  
 import org.mule.api.registry.RegistrationException;
 18  
 import org.mule.api.transport.Connector;
 19  
 import org.mule.config.i18n.CoreMessages;
 20  
 
 21  
 import com.google.inject.AbstractModule;
 22  
 import com.google.inject.MembersInjector;
 23  
 import com.google.inject.TypeLiteral;
 24  
 import com.google.inject.internal.ProviderMethod;
 25  
 import com.google.inject.matcher.Matchers;
 26  
 import com.google.inject.name.Named;
 27  
 import com.google.inject.spi.InjectionListener;
 28  
 import com.google.inject.spi.TypeEncounter;
 29  
 import com.google.inject.spi.TypeListener;
 30  
 
 31  
 /**
 32  
  * TODO
 33  
  */
 34  
 public class MuleSupportModule extends AbstractModule
 35  
 {
 36  
     protected MuleContext muleContext;
 37  
 
 38  
     public MuleSupportModule(MuleContext muleContext)
 39  0
     {
 40  0
         this.muleContext = muleContext;
 41  0
     }
 42  
 
 43  
     @Override
 44  
     protected final void configure()
 45  
     {
 46  
 
 47  0
         bindListener(Matchers.any(), new TypeListener()
 48  0
         {
 49  
             public <I> void hear(TypeLiteral<I> iTypeLiteral, TypeEncounter<I> iTypeEncounter)
 50  
             {
 51  
                 //iTypeEncounter.register(new MuleRegistryInjectionLister());
 52  0
                 iTypeEncounter.register(new MuleContextAwareInjector<I>());
 53  0
                 iTypeEncounter.register(new MuleBindInjector<I>());
 54  0
             }
 55  
         });
 56  0
         bind(MuleContext.class).toInstance(muleContext);
 57  0
     }
 58  
 
 59  
 
 60  0
     class MuleContextAwareInjector<I> implements MembersInjector<I>
 61  
     {
 62  
         public void injectMembers(I o)
 63  
         {
 64  0
             if(o instanceof MuleContextAware)
 65  
             {
 66  0
                 ((MuleContextAware)o).setMuleContext(muleContext);
 67  
             }
 68  0
         }
 69  
     }
 70  
 
 71  0
     class MuleBindInjector<I> implements InjectionListener<I>
 72  
     {
 73  
         public void afterInjection(I i)
 74  
         {
 75  0
             if(i instanceof ProviderMethod)
 76  
             {
 77  0
                 Class type = ((ProviderMethod)i).getKey().getTypeLiteral().getRawType();
 78  0
                 boolean bindRequired = (type.equals(Connector.class) || type.equals(Agent.class));
 79  
 
 80  0
                 Named bindTo = ((ProviderMethod)i).getMethod().getAnnotation(Named.class);
 81  0
                 if(bindTo!=null)
 82  
                 {
 83  
                     try
 84  
                     {
 85  0
                         Object o = ((ProviderMethod)i).get();
 86  0
                         if(o instanceof NamedObject)
 87  
                         {
 88  0
                             ((NamedObject)o).setName(bindTo.value());
 89  
                         }
 90  0
                         muleContext.getRegistry().registerObject(bindTo.value(), o);
 91  
                     }
 92  0
                     catch (RegistrationException e)
 93  
                     {
 94  0
                         throw new MuleRuntimeException(CoreMessages.createStaticMessage("failed to bind " + bindTo.value()));
 95  0
                     }
 96  
                 }
 97  0
                 else if(bindRequired)
 98  
                 {
 99  0
                     throw new RuntimeException("Provider object type: " + type + ", must have a @Named annotation so that the object can be bound in Mule");
 100  
                 }
 101  
             }
 102  0
         }
 103  
     }
 104  
 
 105  
     
 106  
 }