View Javadoc

1   /*
2    * $Id: StreamingComponent.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.impl.model.streaming;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.impl.MuleDescriptor;
15  import org.mule.impl.RequestContext;
16  import org.mule.impl.model.AbstractComponent;
17  import org.mule.umo.ComponentException;
18  import org.mule.umo.UMOEvent;
19  import org.mule.umo.UMOException;
20  import org.mule.umo.UMOMessage;
21  import org.mule.umo.endpoint.UMOEndpoint;
22  import org.mule.umo.lifecycle.Disposable;
23  import org.mule.umo.lifecycle.Initialisable;
24  import org.mule.umo.lifecycle.InitialisationException;
25  import org.mule.umo.model.UMOEntryPoint;
26  import org.mule.umo.model.UMOModel;
27  
28  import java.util.Iterator;
29  
30  /**
31   * TODO
32   */
33  public class StreamingComponent extends AbstractComponent
34  {
35  
36      private static final long serialVersionUID = 2967438446264425730L;
37  
38      protected Object component;
39  
40      protected UMOEntryPoint entryPoint;
41  
42      public StreamingComponent(MuleDescriptor descriptor, UMOModel model)
43      {
44          super(descriptor, model);
45      }
46  
47      protected void doInitialise() throws InitialisationException
48      {
49          try
50          {
51              component = lookupComponent();
52          }
53          catch (UMOException e)
54          {
55              throw new InitialisationException(e, this);
56          }
57  
58          // Validate the component
59          // Right now we do not support transformers on the Streaming model
60          for (Iterator iterator = descriptor.getInboundRouter().getEndpoints().iterator(); iterator.hasNext();)
61          {
62              //Enforce streaming
63              UMOEndpoint ep = (UMOEndpoint) iterator.next();
64              ep.setStreaming(true);
65  //            if (!ep.isStreaming())
66  //            {
67  //                throw new InitialisationException(
68  //                    CoreMessages.streamingEndpointsMustBeUsedWithStreamingModel(), this);
69  //            }
70              // TODO RM*: This restriction could be lifted in future
71              if (ep.getTransformer() != null)
72              {
73                  throw new InitialisationException(
74                      CoreMessages.streamingEndpointsDoNotSupportTransformers(), this);
75              }
76          }
77          if (component instanceof Initialisable)
78          {
79              ((Initialisable) component).initialise();
80          }
81  
82          try
83          {
84              entryPoint = model.getEntryPointResolver().resolveEntryPoint(descriptor);
85          }
86          catch (Exception e)
87          {
88              throw new InitialisationException(e, this);
89          }
90  
91      }
92  
93      protected UMOMessage doSend(UMOEvent event) throws UMOException
94      {
95          doDispatch(event);
96          return null;
97      }
98  
99      protected void doDispatch(UMOEvent event) throws UMOException
100     {
101 
102         try
103         {
104             entryPoint.invoke(component, RequestContext.getEventContext());
105         }
106         catch (UMOException e)
107         {
108             throw e;
109         }
110         catch (Exception e)
111         {
112             throw new ComponentException(event.getMessage(), this, e);
113         }
114     }
115 
116     protected void doDispose()
117     {
118         if (component instanceof Disposable)
119         {
120             ((Disposable) component).dispose();
121         }
122     }
123 
124 }