View Javadoc

1   /*
2    * $Id: AbstractMuleGuiceModule.java 20321 2010-11-24 15:21:24Z dfeist $
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.MuleException;
14  import org.mule.api.endpoint.EndpointBuilder;
15  import org.mule.endpoint.DefaultEndpointFactory;
16  
17  import com.google.inject.AbstractModule;
18  
19  /**
20   * A mule specific Guice module that allows users to override the {@link #configureMuleContext(org.mule.api.MuleContext)} method
21   * to do any Mule configuration such as register notifications.  Most users will not need to override this method so the
22   * {@link com.google.inject.AbstractModule} can be used.
23   * <p/>
24   * Note that Mule objects such as Connectors and Agents can be registered in a Guice module too.  To do this create provider methods
25   * on a module and mark with the {@link com.google.inject.Provides} annotation.
26   * <p/>
27   * Its recommended that you put all your Mule configuration objects in a separate Guice module.
28   */
29  public abstract class AbstractMuleGuiceModule extends AbstractModule
30  {
31      protected MuleContext muleContext;
32  
33      void setMuleContext(MuleContext context)
34      {
35          muleContext = context;
36          configureMuleContext(muleContext);
37      }
38  
39      public void configureMuleContext(MuleContext muleContext)
40      {
41          // do nothing
42      }
43  
44      /**
45       * Creates an {@link org.mule.api.endpoint.EndpointBuilder} instance for the endpoint uri.  The builder can be used to add
46       * further configuration options and then used to create either {@link org.mule.api.endpoint.OutboundEndpoint} or
47       * {@link org.mule.api.endpoint.InboundEndpoint} instances.
48       *
49       * @param uri the address URI for the endpoint
50       * @return and EndpointBuilder instance that can be used to create endpoints
51       * @throws MuleException if the builder cannot be created for any reason
52       */
53      protected EndpointBuilder createEndpointBuilder(String uri) throws MuleException
54      {
55          DefaultEndpointFactory endpointFactory = new DefaultEndpointFactory();
56          endpointFactory.setMuleContext(muleContext);
57          return endpointFactory.getEndpointBuilder(uri);
58      }
59  }