1   /*
2    * $Id: InOutState.java 7976 2007-08-21 14:26:13Z 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.providers.ftp.server;
12  
13  import java.util.Collection;
14  import java.util.HashSet;
15  
16  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
17  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
18  
19  public class InOutState implements ServerState
20  {
21  
22      private NamedPayload payload;
23      private CountDownLatch started = new CountDownLatch(1);
24      private CountDownLatch received = new CountDownLatch(1);
25  
26  
27      private boolean isPayloadAvailable()
28      {
29          return null != payload;
30      }
31  
32      public NamedPayload getDownload(String name)
33      {
34          if (isPayloadAvailable() && (name == null || name.equals(payload.getName())))
35          {
36              NamedPayload download = payload;
37              payload = null;
38              return download;
39          }
40          else
41          {
42              return null;
43          }
44      }
45  
46      public Collection getDownloadNames()
47      {
48          Collection names = new HashSet();
49          if (isPayloadAvailable())
50          {
51              names.add(payload.getName());
52          }
53          return names;
54      }
55  
56      public void pushLastUpload(NamedPayload payload)
57      {
58          this.payload = payload;
59          received.countDown();
60      }
61  
62      public void started()
63      {
64          started.countDown();
65      }
66  
67      public void awaitStart(long ms) throws InterruptedException
68      {
69          started.await(ms, TimeUnit.MILLISECONDS);
70      }
71  
72      public NamedPayload awaitUpload(long ms) throws InterruptedException
73      {
74          received.await(ms, TimeUnit.MILLISECONDS);
75          return payload;
76      }
77  
78  }