View Javadoc

1   /*
2    * $Id: SimpleServiceBuilder.java 21683 2011-04-14 22:07:56Z ddossot $
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  
11  package org.mule.construct.builder;
12  
13  import java.util.Arrays;
14  
15  import org.mule.MessageExchangePattern;
16  import org.mule.api.MuleContext;
17  import org.mule.api.MuleException;
18  import org.mule.api.component.Component;
19  import org.mule.api.component.JavaComponent;
20  import org.mule.api.lifecycle.Callable;
21  import org.mule.api.model.EntryPointResolverSet;
22  import org.mule.api.object.ObjectFactory;
23  import org.mule.api.processor.MessageProcessor;
24  import org.mule.api.transformer.Transformer;
25  import org.mule.component.DefaultJavaComponent;
26  import org.mule.component.SimpleCallableJavaComponent;
27  import org.mule.construct.SimpleService;
28  import org.mule.construct.SimpleService.Type;
29  import org.mule.model.resolvers.LegacyEntryPointResolverSet;
30  import org.mule.object.PrototypeObjectFactory;
31  import org.mule.object.SingletonObjectFactory;
32  
33  /**
34   * Fluent API for the creation of a SimpleService.
35   */
36  public class SimpleServiceBuilder extends
37      AbstractFlowConstructWithSingleInboundEndpointBuilder<SimpleServiceBuilder, SimpleService>
38  {
39      protected Type type = SimpleService.Type.DIRECT;
40      protected Component component;
41  
42      @Override
43      protected MessageExchangePattern getInboundMessageExchangePattern()
44      {
45          return MessageExchangePattern.REQUEST_RESPONSE;
46      }
47  
48      public SimpleServiceBuilder transformers(Transformer... transformers)
49      {
50          this.transformers = Arrays.asList((MessageProcessor[]) transformers);
51          return this;
52      }
53  
54      public SimpleServiceBuilder responseTransformers(Transformer... responseTransformers)
55      {
56          this.responseTransformers = Arrays.asList((MessageProcessor[]) responseTransformers);
57          return this;
58      }
59  
60      public SimpleServiceBuilder component(Class<?> componentClass)
61      {
62          return component(new PrototypeObjectFactory(componentClass));
63      }
64  
65      public SimpleServiceBuilder component(ObjectFactory objectFactory)
66      {
67          return component(new DefaultJavaComponent(objectFactory));
68      }
69  
70      public SimpleServiceBuilder component(Callable callable)
71      {
72          return component(new SimpleCallableJavaComponent(callable));
73      }
74  
75      public SimpleServiceBuilder component(Object o)
76      {
77          return component(new SingletonObjectFactory(o));
78      }
79  
80      public SimpleServiceBuilder type(Type type)
81      {
82          this.type = type;
83          return this;
84      }
85  
86      public SimpleServiceBuilder component(Component component)
87      {
88          if (component instanceof JavaComponent)
89          {
90              final JavaComponent javaComponent = (JavaComponent) component;
91  
92              if (javaComponent.getEntryPointResolverSet() == null)
93              {
94                  javaComponent.setEntryPointResolverSet(createEntryPointResolverSet());
95              }
96          }
97  
98          this.component = component;
99          return this;
100     }
101 
102     @Override
103     protected SimpleService buildFlowConstruct(MuleContext muleContext) throws MuleException
104     {
105         return new SimpleService(name, muleContext, getOrBuildInboundEndpoint(muleContext), transformers,
106             responseTransformers, component, type);
107     }
108 
109     protected EntryPointResolverSet createEntryPointResolverSet()
110     {
111         return new LegacyEntryPointResolverSet();
112     }
113 }