Coverage Report - org.mule.transport.stdio.PromptStdioConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
PromptStdioConnector
72%
38/53
50%
8/16
1.417
PromptStdioConnector$StdioMessageFactory
50%
1/2
N/A
1.417
 
 1  
 /*
 2  
  * $Id: PromptStdioConnector.java 10961 2008-02-22 19:01:02Z dfeist $
 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.transport.stdio;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.endpoint.ImmutableEndpoint;
 16  
 import org.mule.api.endpoint.InboundEndpoint;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.api.service.Service;
 19  
 import org.mule.api.transport.Connector;
 20  
 import org.mule.api.transport.MessageReceiver;
 21  
 import org.mule.config.i18n.MessageFactory;
 22  
 import org.mule.util.StringUtils;
 23  
 
 24  
 import java.io.InputStream;
 25  
 import java.io.OutputStream;
 26  
 
 27  
 /**
 28  
  * <code>PromptStdioConnector</code> connects to the System streams in and out by
 29  
  * default and add some basic fuctionality for writing out prompt messages.
 30  
  */
 31  
 public class PromptStdioConnector extends StdioConnector
 32  
 {
 33  
     private String promptMessage;
 34  36
     private String promptMessageCode = null;
 35  36
     private String resourceBundle = null;
 36  
     private String outputMessage;
 37  36
     private String outputMessageCode = null;
 38  36
     private long messageDelayTime = 3000;
 39  36
     private boolean firstTime = true;
 40  
 
 41  
     public PromptStdioConnector()
 42  
     {
 43  36
         super();
 44  
         
 45  36
         inputStream = System.in;
 46  36
         outputStream = System.out;
 47  36
     }
 48  
 
 49  
 
 50  
     protected void doInitialise() throws InitialisationException
 51  
     {
 52  
         // template method, nothing to do
 53  36
     }
 54  
 
 55  
     protected void doDispose()
 56  
     {
 57  
         // Override as a no-op.
 58  
         // The reason is System.in/out shouldn't be closed.
 59  
         // It is valid for them to remain open (consider, e.g. tail -F).
 60  
         // Trying to close System.in will result in I/O block, and
 61  
         // available() will always return 0 bytes for System.in.
 62  
 
 63  
         // There is a scheme to get a ref to System.in via NIO,
 64  
         // e.g. :
 65  
         // FileInputStream fis = new FileInputStream(FileDescriptor.in);
 66  
         // InputStream is = Channels.newInputStream(fis.getChannel);
 67  
         //
 68  
         // It is then possible to register a watchdog thread for the caller
 69  
         // which will interrupt this (now wrapped with NIO) read() call.
 70  
 
 71  
         // Well, it isn't absolutely required for the reasons stated above,
 72  
         // just following the KISS principle.
 73  52
     }
 74  
 
 75  
     protected void doConnect() throws Exception
 76  
     {
 77  
         // template method
 78  32
     }
 79  
 
 80  
     protected void doDisconnect() throws Exception
 81  
     {
 82  
         // template method
 83  32
     }
 84  
 
 85  
     public InputStream getInputStream()
 86  
     {
 87  6
         return inputStream;
 88  
     }
 89  
 
 90  
     public void doStart()
 91  
     {
 92  32
         firstTime = false;
 93  32
     }
 94  
 
 95  
     public OutputStream getOutputStream()
 96  
     {
 97  0
         return outputStream;
 98  
     }
 99  
 
 100  
     /**
 101  
      * @return Returns the promptMessage.
 102  
      */
 103  
     public String getPromptMessage()
 104  
     {
 105  4
         if (StringUtils.isNotBlank(resourceBundle) && StringUtils.isNotBlank(promptMessageCode))
 106  
         {
 107  2
             return StdioMessageFactory.getString(resourceBundle, promptMessageCode);
 108  
         }
 109  
 
 110  2
         return promptMessage;
 111  
     }
 112  
 
 113  
     /**
 114  
      * @param promptMessage The promptMessage to set.
 115  
      */
 116  
     public void setPromptMessage(String promptMessage)
 117  
     {
 118  12
         this.promptMessage = promptMessage;
 119  12
     }
 120  
 
 121  
     /**
 122  
      * @return Returns the promptMessageCode.
 123  
      */
 124  
     public String getPromptMessageCode()
 125  
     {
 126  2
         return promptMessageCode;
 127  
     }
 128  
 
 129  
     /**
 130  
      * @param promptMessageCode The promptMessageCode to set.
 131  
      */
 132  
     public void setPromptMessageCode(String promptMessageCode)
 133  
     {
 134  6
         this.promptMessageCode = promptMessageCode;
 135  6
     }
 136  
 
 137  
     /**
 138  
      * @return Returns the resourceBundle.
 139  
      */
 140  
     public String getResourceBundle()
 141  
     {
 142  2
         return resourceBundle;
 143  
     }
 144  
 
 145  
     /**
 146  
      * @param resourceBundle The resourceBundle to read the message from. This property is 
 147  
      * only needed in conjunction with promptMessageCode or outputMessageCode.
 148  
      */
 149  
     public void setResourceBundle(String resourceBundle)
 150  
     {
 151  6
         this.resourceBundle = resourceBundle;
 152  6
     }
 153  
 
 154  
     /**
 155  
      * @return Returns the outputMessage.
 156  
      */
 157  
     public String getOutputMessage()
 158  
     {
 159  4
         if (StringUtils.isNotBlank(resourceBundle) && StringUtils.isNotBlank(outputMessageCode))
 160  
         {
 161  0
             return StdioMessageFactory.getString(resourceBundle, outputMessageCode);
 162  
         }
 163  
 
 164  4
         return outputMessage;
 165  
     }
 166  
 
 167  
     /**
 168  
      * @param outputMessage The outputMessage to set.
 169  
      */
 170  
     public void setOutputMessage(String outputMessage)
 171  
     {
 172  12
         this.outputMessage = outputMessage;
 173  12
     }
 174  
 
 175  
     /**
 176  
      * @return Returns the outputMessageCode.
 177  
      */
 178  
     public String getOutputMessageCode()
 179  
     {
 180  0
         return outputMessageCode;
 181  
     }
 182  
 
 183  
     /**
 184  
      * @param outputMessageCode The outputMessageCode to set.
 185  
      */
 186  
     public void setOutputMessageCode(String outputMessageCode)
 187  
     {
 188  0
         this.outputMessageCode = outputMessageCode;
 189  0
     }
 190  
 
 191  
     public Connector getConnector()
 192  
     {
 193  0
         return this;
 194  
     }
 195  
 
 196  
     public MessageReceiver registerListener(Service service, InboundEndpoint endpoint) throws Exception
 197  
     {
 198  14
         if (receivers.size() > 0)
 199  
         {
 200  0
             throw new UnsupportedOperationException(
 201  
                 "You can only register one listener per system stream connector");
 202  
         }
 203  14
         MessageReceiver receiver = super.registerListener(service, endpoint);
 204  8
         return receiver;
 205  
     }
 206  
 
 207  
     public long getMessageDelayTime()
 208  
     {
 209  10
         if (firstTime)
 210  
         {
 211  0
             return messageDelayTime + 4000;
 212  
         }
 213  
         else
 214  
         {
 215  10
             return messageDelayTime;
 216  
         }
 217  
     }
 218  
 
 219  
     public void setMessageDelayTime(long messageDelayTime)
 220  
     {
 221  12
         this.messageDelayTime = messageDelayTime;
 222  12
     }
 223  
 
 224  
 
 225  
     public OutputStream getOutputStream(ImmutableEndpoint endpoint, MuleMessage message) throws MuleException
 226  
     {
 227  
         OutputStream out;
 228  0
         String streamName = endpoint.getEndpointURI().getAddress();
 229  
 
 230  0
         if (STREAM_SYSTEM_OUT.equalsIgnoreCase(streamName))
 231  
         {
 232  0
             out = System.out;
 233  
         }
 234  0
         else if (STREAM_SYSTEM_ERR.equalsIgnoreCase(streamName))
 235  
         {
 236  0
             out = System.err;
 237  
         }
 238  
         else
 239  
         {
 240  0
             out = getOutputStream();
 241  
         }
 242  0
         return out;
 243  
     }
 244  
     
 245  
     /**
 246  
      * {@link PromptStdioConnector} needs a way to access other modules' messages. The default
 247  
      * way to access messages is by using {@link MessageFactory} which itself is not meant to be used 
 248  
      * directly. In order not to soften this requiement this private subclass offers access to
 249  
      * {@link MessageFactory}'s methods.
 250  
      */
 251  0
     private static class StdioMessageFactory extends MessageFactory
 252  
     {
 253  
         protected static String getString(String bundlePath, String code)
 254  
         {
 255  2
             return MessageFactory.getString(bundlePath, Integer.parseInt(code));
 256  
         }
 257  
     }
 258  
 }