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  
19  package org.apache.river.phoenix.dl;
20  
21  import java.io.IOException;
22  import java.io.Serializable;
23  import java.rmi.Remote;
24  import java.rmi.RemoteException;
25  import java.rmi.UnmarshalException;
26  import java.rmi.activation.ActivationException;
27  import java.rmi.activation.ActivationID;
28  import java.rmi.server.UID;
29  import java.security.AccessController;
30  import java.security.PrivilegedAction;
31  import net.jini.activation.Resolve;
32  import net.jini.export.ProxyAccessor;
33  import org.apache.river.api.io.AtomicSerial;
34  import org.apache.river.api.io.AtomicSerial.GetArg;
35  import org.apache.river.proxy.MarshalledWrapper;
36  
37  /**
38   * @author Sun Microsystems, Inc.
39   * 
40   * @since 2.0
41   */
42  public class AID extends ActivationID {
43      private static final long serialVersionUID = 681896091039721074L;
44  
45      protected final Activator activator;
46      protected final UID uid;
47  
48      @AtomicSerial
49      static final class State implements Serializable, ProxyAccessor, Resolve {
50  	private static final long serialVersionUID = 4479839553358267720L;
51  
52  	private final Activator activator;
53  	private final UID uid;
54  
55  	State(Activator activator, UID uid) {
56  	    this.activator = activator;
57  	    this.uid = uid;
58  	}
59  	
60  	public State(GetArg arg) throws IOException, ClassNotFoundException{
61  	    this(arg.get("activator", null, Activator.class),
62  		 arg.get("uid", null, UID.class));
63  	}
64  
65  	public Object readResolve() {
66  	    return new AID(activator, uid);
67  	}
68  
69  	public Object getProxy() {
70  	    return activator;
71  	}
72      }
73  
74      public AID(Activator activator, UID uid) {
75  	super(null);
76  	this.activator = activator;
77  	this.uid = uid;
78      }
79  
80      /**
81       * Activate the object corresponding to this instance.
82       */
83      @Override
84      public Remote activate(boolean force)
85  	throws ActivationException, RemoteException
86      {
87   	try {
88   	    MarshalledWrapper marshalledProxy =
89  		activator.activate(this, force);
90  	    ClassLoader loader = classLoader();
91   	    return (Remote) marshalledProxy.get(loader, loader);
92   	} catch (RemoteException e) {
93   	    throw e;
94   	} catch (java.io.IOException e) {
95   	    throw new UnmarshalException("activation failed", e);
96   	} catch (java.lang.ClassNotFoundException e) {
97   	    throw new UnmarshalException("activation failed", e);
98  	}
99  	
100     }
101     
102     private ClassLoader classLoader(){
103 	return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>(){
104 
105 	    public ClassLoader run() {
106 		return AID.class.getClassLoader();
107 	    }
108 	    
109 	});
110     }
111     
112     public UID getUID() {
113 	return uid;
114     }
115 
116     @Override
117     public int hashCode() {
118 	return uid.hashCode();
119     }
120 
121     @Override
122     public boolean equals(Object obj) {
123 	if (obj != null && obj.getClass() == getClass()) {
124 	    AID id = (AID) obj;
125 	    return (uid.equals(id.uid) && activator.equals(id.activator));
126 	}
127 	return false;
128     }
129     
130     @Override
131     public String toString(){
132 	StringBuilder sb = new StringBuilder();
133 	sb.append("AID Activator ").append(activator).append(", UID ").append(uid);
134 	return sb.toString();
135     }
136 
137     private Object writeReplace() {
138 	return new State(activator, uid);
139     }
140 }