1   // ========================================================================
2   // Copyright 2006 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at 
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.cxf.demo;
16  
17  
18  import java.util.concurrent.Future;
19  import java.util.List;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  
23  import ebay.apis.eblbasecomponents.*;
24  
25  import javax.servlet.ServletRequest;
26  import javax.xml.ws.BindingProvider;
27  
28  /* ------------------------------------------------------------ */
29  
30  public class EbayFindItemAsync
31  {
32      ServletRequest _request;
33      private long _accessed;
34      private long _startTime; // purely for test purposes
35  
36      ShoppingInterface _shoppingPort;
37  
38      private List<EbayFindItemAsyncHandler> _handlers = new ArrayList<EbayFindItemAsyncHandler>();
39      private int _todo;
40  
41      /* ------------------------------------------------------------ */
42  
43      public EbayFindItemAsync(ShoppingInterface port, ServletRequest request)
44      {
45          _startTime = System.currentTimeMillis();
46          _request=request;
47          _shoppingPort=port;
48      }
49  
50      /* ------------------------------------------------------------ */
51      public void search(String item)
52      {
53          FindItemsRequestType req = new FindItemsRequestType();
54          req.setQueryKeywords(item);
55          req.setMaxEntries(4);
56  
57          synchronized (this)
58          {
59              _todo++;
60              EbayFindItemAsyncHandler handler = new EbayFindItemAsyncHandler(this);
61              _shoppingPort.findItemsAsync(req, handler);
62              _handlers.add(handler);
63          }
64      }
65  
66      /* ------------------------------------------------------------ */
67      public void done()
68      {
69          synchronized (this)
70          {
71              System.err.println("done "+_todo);
72              if (_todo>0)
73              {
74                  if (--_todo==0)
75                  {
76                      System.err.println("Resuming request");
77                      _request.resume();
78                  }
79              }
80          }
81      }
82  
83      /* ------------------------------------------------------------ */
84      public List<EbayFindItemAsyncHandler> getPayload()
85      {
86          return _handlers;
87      }
88  
89      /* ------------------------------------------------------------ */
90      public void access()
91      {
92          synchronized (this)
93          {
94              // distribute access time in cluster
95              _accessed = System.currentTimeMillis();
96          }
97      }
98  
99      /* ------------------------------------------------------------ */
100     public synchronized long lastAccessed()
101     {
102         return _accessed;
103     }
104 
105     /* ------------------------------------------------------------ */
106     public synchronized long getStartTime()
107     {
108         return _startTime;
109     }
110 
111 }