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.io.IOException;
21  import java.io.InvalidObjectException;
22  import java.io.ObjectInputStream;
23  import java.io.Serializable;
24  import java.rmi.RemoteException;
25  import net.jini.core.discovery.LookupLocator;
26  import net.jini.core.entry.Entry;
27  import net.jini.core.transaction.Transaction;
28  import net.jini.core.transaction.TransactionException;
29  import net.jini.id.ReferentUuid;
30  import net.jini.id.ReferentUuids;
31  import net.jini.id.Uuid;
32  import net.jini.space.JavaSpace;
33  import org.apache.river.api.io.AtomicSerial;
34  import org.apache.river.api.io.AtomicSerial.GetArg;
35  import org.apache.river.admin.JavaSpaceAdmin;
36  import org.apache.river.admin.AdminIterator;
37  
38  /**
39   * <code>AdminProxy</code> objects are connected to particular
40   * <code>OutriggerServerImpl</code>s to implement the
41   * <code>JavaSpaceAdmin</code> interface for the server.
42   *
43   * @see JavaSpaceAdmin 
44   */
45  @AtomicSerial
46  public class AdminProxy implements JavaSpaceAdmin, ReferentUuid, Serializable {
47      private static final long serialVersionUID = 1L;
48      
49      /** Reference to the actual remote admin object. */
50      final OutriggerAdmin          admin;
51  
52      /** The <code>Uuid</code> that identifies the space this proxy is for */
53      final Uuid spaceUuid;
54   
55      private static final boolean DEBUG = false;
56  
57      /**
58       * Create an <code>AdminProxy</code> for the given remote admin
59       * objects.
60       * @param admin reference to remote server for the space.
61       * @param spaceUuid universal unique ID for the space.
62       * @throws NullPointerException if <code>admin</code> or
63       *         <code>spaceUuid</code> is <code>null</code>.     
64       */
65      public AdminProxy(OutriggerAdmin admin, Uuid spaceUuid) {
66  	this(check(admin,spaceUuid), admin, spaceUuid);
67      }
68      
69      AdminProxy(GetArg arg) throws IOException {
70  	this(serialCheck((OutriggerAdmin) arg.get("admin", null),
71  			(Uuid) arg.get("spaceUuid", null)),
72  		(OutriggerAdmin) arg.get("admin", null),
73  		(Uuid) arg.get("spaceUuid", null)
74  	);
75      }
76      
77      private AdminProxy(boolean check, OutriggerAdmin admin, Uuid spaceUuid){
78  	this.admin = admin;
79  	this.spaceUuid = spaceUuid;
80      }
81      
82      private static boolean serialCheck(OutriggerAdmin admin, Uuid spaceUuid) throws InvalidObjectException{
83  	try {
84  	    return check(admin, spaceUuid);
85  	} catch (NullPointerException ex){
86  	    InvalidObjectException e = new InvalidObjectException("Invariants not satisfied");
87  	    e.initCause(ex);
88  	    throw e;
89  	}
90      }
91      
92      private static boolean check(OutriggerAdmin admin, Uuid spaceUuid){
93  	if (admin == null)
94  	    throw new NullPointerException("admin must be non-null");
95  	if (spaceUuid == null) 
96  	    throw new NullPointerException("spaceUuid must be non-null");
97  	return true;
98      }
99  
100     /**
101      * Read this object back and validate state.
102      * @param in stream used to de-serialize.
103      * @throws IOException
104      * @throws ClassNotFoundException
105      */
106     private void readObject(ObjectInputStream in) 
107 	throws IOException, ClassNotFoundException
108     {
109 	in.defaultReadObject();
110 
111 	if (admin == null) 
112 	    throw new InvalidObjectException("null server reference");
113 	    
114 	if (spaceUuid == null)
115 	    throw new InvalidObjectException("null Uuid");
116     }
117 
118     /** 
119      * We should always have data in the stream, if this method
120      * gets called there is something wrong.
121      * @throws InvalidObjectException
122      */
123     private void readObjectNoData() throws InvalidObjectException {
124 	throw new 
125 	    InvalidObjectException("SpaceProxy should always have data");
126     }
127 
128     // inherit doc comment
129     public JavaSpace space() throws RemoteException {
130 	return admin.space();
131     }
132 
133     // inherit doc comment
134     public AdminIterator contents(Entry tmpl, Transaction tr)
135 	throws TransactionException, RemoteException
136     {
137 	return contents(tmpl, tr, USE_DEFAULT);
138     }
139 
140     // inherit doc comment
141     public AdminIterator contents(Entry tmpl, Transaction tr, int fetchSize)
142 	throws TransactionException, RemoteException
143     {
144 	return new IteratorProxy(
145             admin.contents(SpaceProxy2.repFor(tmpl), tr), admin, fetchSize);
146     }
147 
148     // inherit doc comment
149     public void destroy() throws RemoteException {
150 	admin.destroy();
151     }
152 
153     //              JoinAdmin
154     // --------------------------------------------------
155     // inherit doc comment
156     public Entry[] getLookupAttributes() throws RemoteException {
157 	return admin.getLookupAttributes();
158     }
159 
160     // inherit doc comment
161     public void addLookupAttributes(Entry[] attrSets) 
162 	throws RemoteException 
163     {
164 	admin.addLookupAttributes(attrSets);
165     }
166 
167     // inherit doc comment
168     public void modifyLookupAttributes(Entry[] attrSetTemplates, 
169 				       Entry[] attrSets)
170 	throws RemoteException
171     {
172 	admin.modifyLookupAttributes(attrSetTemplates, attrSets);
173     }
174 
175     // inherit doc comment
176     public String[] getLookupGroups() throws RemoteException {
177 	return admin.getLookupGroups();
178     }
179 
180     // inherit doc comment
181     public void addLookupGroups(String[] groups) throws RemoteException {
182 	admin.addLookupGroups(groups);
183     }
184     
185     // inherit doc comment
186     public void removeLookupGroups(String[] groups) 
187 	throws RemoteException 
188     {
189 	admin.removeLookupGroups(groups);
190     }
191 
192     // inherit doc comment
193     public void setLookupGroups(String[] groups) throws RemoteException {
194 	admin.setLookupGroups(groups);
195     }
196 
197     // inherit doc comment
198     public LookupLocator[] getLookupLocators() throws RemoteException {
199 	return admin.getLookupLocators();
200     }
201 
202     // inherit doc comment
203     public void addLookupLocators(LookupLocator[] locators) 
204 	throws RemoteException
205     {
206 	admin.addLookupLocators(locators);
207     }
208 
209     // inherit doc comment
210     public void removeLookupLocators(LookupLocator[] locators) 
211 	throws RemoteException
212     {
213 	admin.removeLookupLocators(locators);
214     }
215 
216     // inherit doc comment
217     public void setLookupLocators(LookupLocator[] locators) 
218 	throws RemoteException
219     {
220 	admin.setLookupLocators(locators);
221     }
222 
223     public String toString() {
224 	return getClass().getName() + " for " + spaceUuid + 
225 	    " (through " + admin + ")";
226     }
227 
228     public boolean equals(Object other) {
229 	return ReferentUuids.compare(this, other);
230     }
231 
232     public int hashCode() {
233 	return spaceUuid.hashCode();
234     }
235 
236     public Uuid getReferentUuid() {
237 	return spaceUuid;
238     }
239 }