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