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.reggie.proxy;
19  
20  import java.io.IOException;
21  import java.io.InvalidObjectException;
22  import java.io.ObjectInput;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.lang.reflect.Proxy;
26  import java.rmi.MarshalledObject;
27  import java.rmi.RemoteException;
28  import java.util.logging.Level;
29  import java.util.logging.Logger;
30  import net.jini.core.lookup.ServiceEvent;
31  import net.jini.core.lookup.ServiceID;
32  import net.jini.core.lookup.ServiceItem;
33  import net.jini.export.ProxyAccessor;
34  import net.jini.lookup.ServiceProxyAccessor;
35  import net.jini.io.MarshalledInstance;
36  import org.apache.river.api.io.AtomicSerial;
37  import org.apache.river.api.io.AtomicSerial.GetArg;
38  import org.apache.river.api.io.AtomicSerial.ReadInput;
39  import org.apache.river.api.io.AtomicSerial.ReadObject;
40  
41  /**
42   * Concrete implementation class for abstract ServiceEvent.
43   *
44   * @author Sun Microsystems, Inc.
45   *
46   */
47  @AtomicSerial
48  public class RegistrarEvent extends ServiceEvent implements ProxyAccessor {
49  
50      private static final long serialVersionUID = 2L;
51  
52      /**
53       * The new state of the serviceItem, or null if the serviceItem has been
54       * deleted from the lookup service.  This is either a ServiceItem,
55       * an Item (to be converted to a ServiceItem when getServiceItem is called),
56       * or a bootstrapProxy;
57       *
58       * @serial
59       */
60      private volatile Object serviceItem;
61      /**
62       * The service ID of the serviceItem that triggered the event.  This field is used
63       * instead of the inherited serviceID field (which is set to null) and is
64       * written directly as a 128-bit value in order to avoid potential codebase
65       * annotation loss (see bug 4745728).
66       */
67      private transient ServiceID servID;
68      
69      private transient Proxy bootstrap;
70      
71      @ReadInput
72      private static ReadObject getRO(){
73  	return new RO();
74      }
75      
76      private static GetArg check(GetArg arg) throws IOException {
77  	Object serviceItem = arg.get("serviceItem", null);
78  	if (serviceItem == null ||
79  	    serviceItem instanceof ServiceItem ||
80  	    serviceItem instanceof Item )
81  	{
82  	    RO r = (RO) arg.getReader();
83  	    if (r.servID instanceof ServiceID) return arg;
84  	}
85  	throw new InvalidObjectException("Invariants weren't satisfied");
86      }
87      
88      public RegistrarEvent(GetArg arg) throws IOException {
89  	super(check(arg));
90  	serviceItem = arg.get("serviceItem", null);
91  	servID = ((RO) arg.getReader()).servID;
92      }
93  
94      /**
95       * Simple constructor.
96       *
97       * @param source the ServiceRegistrar that generated the event
98       * @param eventID the registration eventID
99       * @param seqNo the sequence number of this event
100      * @param handback the client handback
101      * @param serviceID the serviceID of the serviceItem that triggered the event
102      * @param transition the transition that triggered the event
103      * @param item the new state of the serviceItem, or null if deleted
104      */
105     @Deprecated
106     public RegistrarEvent(Object source,
107 			  long eventID,
108 			  long seqNo,
109 			  MarshalledObject handback,
110 			  ServiceID serviceID,
111 			  int transition,
112 			  Object item)
113     {
114 	super(source, eventID, seqNo, handback, null, transition);
115 	this.serviceItem = item;
116 	servID = serviceID;
117     }
118     
119     /**
120      * Simple constructor.
121      *
122      * @param source the ServiceRegistrar that generated the event
123      * @param eventID the registration eventID
124      * @param seqNo the sequence number of this event
125      * @param miHandback the client handback
126      * @param serviceID the serviceID of the serviceItem that triggered the event
127      * @param transition the transition that triggered the event
128      * @param item the new state of the serviceItem, or null if deleted
129      */
130     public RegistrarEvent(Object source,
131 			  long eventID,
132 			  long seqNo,
133 			  MarshalledInstance miHandback,
134 			  ServiceID serviceID,
135 			  int transition,
136 			  Object item)
137     {
138 	super(source, eventID, seqNo, miHandback, null, transition);
139 	this.serviceItem = item;
140 	servID = serviceID;
141     }
142 
143     /**
144      * Returns the new state of the serviceItem, or null if the serviceItem was deleted
145      * from the lookup service.
146      */
147     @Override
148     public ServiceItem getServiceItem() {
149 	if (serviceItem instanceof ServiceItem){
150 	    return ((ServiceItem) serviceItem).clone();
151 	} else if (serviceItem instanceof Item) {
152 	    bootstrap = ((Item)serviceItem).getProxy();
153 	    serviceItem = ((Item)serviceItem).get();
154 	    return ((ServiceItem) serviceItem).clone();
155 	} 
156 	return null;
157     }
158     
159     @Override
160     public Object getBootstrapProxy(){
161 	if (bootstrap != null) return bootstrap;
162 	if (serviceItem instanceof Item){
163 	    bootstrap = ((Item)serviceItem).getProxy();
164 	    return bootstrap;
165 	} 
166 	return null;
167     }
168 
169     // javadoc inherited from ServiceEvent
170     public ServiceID getServiceID() {
171 	return servID;
172     }
173 
174     /**
175      * Writes the default serializable field value for this instance, followed
176      * by the serviceItem's service ID encoded as specified by the
177      * ServiceID.writeBytes method.
178      */
179     private void writeObject(ObjectOutputStream out) throws IOException {
180 	out.defaultWriteObject();
181 	servID.writeBytes(out);
182     }
183 
184     /**
185      * Reads the default serializable field value for this instance, followed
186      * by the serviceItem's service ID encoded as specified by the
187      * ServiceID.writeBytes method. 
188      */
189     private void readObject(ObjectInputStream in)
190 	throws IOException, ClassNotFoundException
191     {
192 	in.defaultReadObject();
193 	servID = new ServiceID(in);
194     }
195 
196     public Object getProxy() {
197 	Object source = getSource();
198 	if (source instanceof ProxyAccessor) return ((ProxyAccessor)source).getProxy();
199 	throw new IllegalStateException("source wasn't a service registrar proxy");
200     }
201     
202     private static class RO implements ReadObject {
203 
204 	ServiceID servID;
205 	@Override
206 	public void read(ObjectInput in) throws IOException, ClassNotFoundException {
207 	    servID = new ServiceID(in);
208 }
209 	
210     }
211 }