View Javadoc
1    /*
2     * $Id$
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.guiceyfruit.mule;
11   
12   import org.mule.api.MuleContext;
13   import org.mule.api.lifecycle.Initialisable;
14   
15   import com.google.inject.Guice;
16   import com.google.inject.Injector;
17   import com.google.inject.Module;
18   import com.google.inject.ProvisionException;
19   import com.google.inject.TypeLiteral;
20   import com.google.inject.internal.Iterables;
21   import com.google.inject.matcher.Matchers;
22   import com.google.inject.spi.InjectionListener;
23   import com.google.inject.spi.TypeEncounter;
24   import com.google.inject.spi.TypeListener;
25   
26   import java.util.Arrays;
27   import java.util.Collections;
28   
29   import org.guiceyfruit.jsr250.Jsr250Module;
30   import org.guiceyfruit.mule.support.DisposableCloser;
31   
32   /**
33    * TODO
34    */
35   public class MuleModule extends Jsr250Module
36   {
37       /**
38        * Returns a new Injector with support for
39        * Mule lifecycle support along with JSR 250 support included.
40        */
41       public static Injector createInjector(Module... modules)
42       {
43           Iterable<? extends Module> iterable = Iterables.concat(
44                   Collections.singletonList(new MuleModule()), Arrays.asList(modules));
45   
46           return Guice.createInjector(iterable);
47       }
48   
49       protected void configure()
50       {
51           super.configure();
52   
53           bindListener(Matchers.any(), new TypeListener()
54           {
55               public <I> void hear(TypeLiteral<I> injectableType, TypeEncounter<I> encounter)
56               {
57   
58                   encounter.register(new InjectionListener<I>()
59                   {
60                       public void afterInjection(I injectee)
61                       {
62                           if (injectee instanceof Initialisable && !(injectee instanceof MuleContext))
63                           {
64                               Initialisable initialisable = (Initialisable) injectee;
65                               try
66                               {
67                                   initialisable.initialise();
68                               }
69                               catch (Exception e)
70                               {
71                                   throw new ProvisionException("Failed to invoke initialise(): " + e, e);
72                               }
73                           }
74                       }
75                   });
76   
77               }
78           });
79   
80           bind(DisposableCloser.class);
81       }
82   
83   }