View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.river.outrigger.proxy;
19  
20  import java.rmi.RemoteException;
21  
22  import net.jini.core.entry.Entry;
23  import net.jini.core.entry.UnusableEntryException;
24  import net.jini.core.lease.Lease;
25  import net.jini.id.Uuid;
26  import net.jini.space.MatchSet;
27  
28  /**
29   * Outrigger's implementation of <code>MatchSet</code>.
30   * Outrigger's implementation of the <code>JavaSpace05.contents</code>
31   * method returns objects of this type. Created with an initial set of
32   * entries from the match set and supports pre-fetching whenever it
33   * needs additional entries. <code>RemoteException</code>s encountered
34   * when making a remote call to fetch the next batch of entries from
35   * the space generally do not invalidate the proxy or the match set.
36   * <p>
37   * Note, there is no way to serialize or otherwise copy instances of
38   * this class so the default equals implementation should suffice.
39   */
40  class MatchSetProxy implements MatchSet {
41      /** The remote server this proxy works with. */
42      final private OutriggerServer space;
43  
44      /** ID of the associated query (and lease) */
45      final private Uuid uuid;
46  
47      /** Lease assocated with this query */
48      final private Lease lease;
49  
50      /** Last batch fetched from server */
51      private volatile EntryRep[] reps;
52  
53      /** Last rep returned */
54      private volatile EntryRep lastRepReturned;
55  
56      /** Current index into reps */
57      private volatile int i;
58  
59      /** True if reps[i] could not be unpacked */
60      private volatile boolean unpackFailure = true; 
61  
62      MatchSetProxy(MatchSetData inital, SpaceProxy2 parent, OutriggerServer space) {
63  	uuid = inital.uuid;
64  	this.space = space;
65  	if (uuid != null) 
66  	    lease = parent.newLease(uuid, inital.intialLeaseDuration);
67  	else 
68  	    lease = null;
69  	reps = inital.reps;
70  
71  	i=0;
72      }
73  
74      public Lease getLease() {
75  	return lease;
76      }
77  
78      public Entry next() throws RemoteException, UnusableEntryException {
79  	if (i >= reps.length) {
80  	    // Fetch another batch	    
81  	    i = 0;
82  	    reps = space.nextBatch(uuid, lastRepReturned.id());	    
83  	}
84  
85  	if (reps[i] == null)
86  	    return null;
87  
88  	unpackFailure = true;
89  	lastRepReturned = reps[i++];
90  	final Entry rslt = lastRepReturned.entry();
91  	unpackFailure = false;
92  	return rslt;
93      }
94  
95      public Entry getSnapshot() {
96  	if (unpackFailure)
97  	    throw new IllegalStateException(
98  	        "getSnapshot - need successful next call first");
99  
100 	return new SnapshotRep(lastRepReturned);
101     }
102 
103     public String toString() {
104 	return getClass().getName() + " for " + uuid + 
105 	    " (through " + space + ")";
106     }
107 }