001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Victor A. Martynov
021: * @version $Revision: 1.1.2.4 $
022: */package org.apache.harmony.rmi.activation;
023:
024: import java.io.BufferedInputStream;
025: import java.lang.reflect.Constructor;
026: import java.rmi.MarshalledObject;
027: import java.rmi.RMISecurityManager;
028: import java.rmi.Remote;
029: import java.rmi.RemoteException;
030: import java.rmi.activation.Activatable;
031: import java.rmi.activation.ActivationDesc;
032: import java.rmi.activation.ActivationException;
033: import java.rmi.activation.ActivationGroup;
034: import java.rmi.activation.ActivationGroupDesc;
035: import java.rmi.activation.ActivationGroupID;
036: import java.rmi.activation.ActivationID;
037: import java.rmi.activation.UnknownObjectException;
038: import java.rmi.server.RMIClassLoader;
039: import java.security.AccessController;
040: import java.security.PrivilegedExceptionAction;
041: import java.util.Hashtable;
042:
043: import org.apache.harmony.rmi.common.RMILog;
044: import org.apache.harmony.rmi.internal.nls.Messages;
045: import org.apache.harmony.rmi.transport.RMIObjectInputStream;
046:
047: /**
048: * The implementation of ActivationGroup.
049: *
050: * @author Victor A. Martynov
051: * @version $Revision: 1.1.2.4 $
052: */
053: public class ActivationGroupImpl extends ActivationGroup {
054:
055: private static final long serialVersionUID = 5526311808915869570L;
056:
057: private ActivationGroupID groupID;
058:
059: private boolean isGroupActive = true;
060:
061: private static RMILog rlog = RMILog.getActivationLog();
062:
063: private Hashtable active_objects = new Hashtable();
064:
065: /**
066: *
067: */
068: private Class[] special_constructor_parameters = {
069: ActivationID.class, MarshalledObject.class };
070:
071: /**
072: * This main method is used to start new VMs for ActivationGroups. Four
073: * parameters needed to create ActivationGroup are: <br>
074: * ActivationGroupID <br>
075: * ActivationGroupDesc <br>
076: * incarnation The parameters needed to create ActivationGroup correctly are
077: * passed through the standard input stream in the following order: <br>
078: * ActivationGroupID -> ActivationGroupDesc -> incarnation
079: */
080: public static void main(String args[]) {
081: // rmi.log.4C=ActivationGroupImpl.main:
082: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.4C")); //$NON-NLS-1$
083: if (System.getSecurityManager() == null) {
084: System.setSecurityManager(new RMISecurityManager());
085: }
086:
087: try {
088: // rmi.log.4F=System.in.available = {0}
089: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.4F", //$NON-NLS-1$
090: System.in.available()));
091:
092: RMIObjectInputStream ois = new RMIObjectInputStream(
093: new BufferedInputStream(System.in));
094: // rmi.log.55=ois = {0}
095: rlog.log(RMILog.VERBOSE, Messages.getString(
096: "rmi.log.55", ois)); //$NON-NLS-1$
097: ActivationGroupID agid = (ActivationGroupID) ois
098: .readObject();
099: // rmi.log.57=agid = {0}
100: rlog.log(RMILog.VERBOSE, Messages.getString(
101: "rmi.log.57", agid)); //$NON-NLS-1$
102: ActivationGroupDesc agdesc = (ActivationGroupDesc) ois
103: .readObject();
104: // rmi.log.74=agdesc = {0}
105: rlog.log(RMILog.VERBOSE, Messages.getString(
106: "rmi.log.74", agdesc)); //$NON-NLS-1$
107: long incarnation = ois.readLong();
108: // rmi.log.7B=incarnation = {0}
109: rlog.log(RMILog.VERBOSE, Messages.getString(
110: "rmi.log.7B", incarnation)); //$NON-NLS-1$
111: ActivationGroup.createGroup(agid, agdesc, incarnation);
112: } catch (Throwable t) {
113: // rmi.log.7C=: Exception: {0}
114: rlog.log(RMILog.VERBOSE, Messages
115: .getString("rmi.log.7C", t)); //$NON-NLS-1$
116: t.printStackTrace();
117: }
118: }
119:
120: /**
121: * Method from ActivationInstantiator interface.
122: */
123: public MarshalledObject newInstance(final ActivationID aid,
124: final ActivationDesc adesc) throws ActivationException {
125:
126: // rmi.log.83=ActivationGroupImpl: ActivationGroupImpl.newInstance started.
127: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.83")); //$NON-NLS-1$
128: // Checking that we try to activate object in correct group.
129: if (!groupID.equals(adesc.getGroupID())) {
130: // rmi.36=Attempt to activate object from different group.
131: throw new ActivationException(Messages.getString("rmi.36")); //$NON-NLS-1$
132: }
133:
134: if (isGroupActive == false) {
135: // rmi.37=Attempt to activate object in inactive group.
136: throw new ActivationException(Messages.getString("rmi.37")); //$NON-NLS-1$
137: }
138:
139: /**
140: */
141:
142: ActiveObject ao = (ActiveObject) active_objects.get(aid);
143: // rmi.log.84=ActivationGroupImpl: active object = {0}
144: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.84", ao)); //$NON-NLS-1$
145:
146: if (ao != null) {
147: return ao.remote_object_stub;
148: }
149: try {
150:
151: // rmi.log.85=Ready to load active class: [location={0}; name={1}]
152: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.85", //$NON-NLS-1$
153: adesc.getLocation(), adesc.getClassName()));
154: final Class aclass = RMIClassLoader.loadClass(adesc
155: .getLocation(), adesc.getClassName());
156: // rmi.log.86=active class = {0}
157: rlog.log(RMILog.VERBOSE, Messages.getString(
158: "rmi.log.86", aclass)); //$NON-NLS-1$
159:
160: Remote rmt = (Remote) AccessController
161: .doPrivileged(new PrivilegedExceptionAction() {
162:
163: public Object run() throws Exception {
164: Constructor aconstructor = aclass
165: .getDeclaredConstructor(special_constructor_parameters);
166: // rmi.log.87=Activatable Constructor: {0}
167: rlog
168: .log(
169: RMILog.VERBOSE,
170: Messages
171: .getString(
172: "rmi.log.87", aconstructor)); //$NON-NLS-1$
173:
174: aconstructor.setAccessible(true);
175:
176: Object parameters[] = new Object[] { aid,
177: adesc.getData() };
178: return (Remote) aconstructor
179: .newInstance(parameters);
180: }
181: });
182:
183: // rmi.log.88=rmt.getClass = {0}
184: rlog.log(RMILog.VERBOSE, Messages.getString(
185: "rmi.log.88", rmt.getClass())); //$NON-NLS-1$
186:
187: // rmi.log.89=newInstance: Remote Object = {0}
188: rlog.log(RMILog.VERBOSE, Messages.getString(
189: "rmi.log.89", rmt)); //$NON-NLS-1$
190:
191: ao = new ActiveObject(rmt);
192: // rmi.log.91=active object = {0}
193: rlog.log(RMILog.VERBOSE, Messages.getString(
194: "rmi.log.91", ao)); //$NON-NLS-1$
195:
196: active_objects.put(aid, ao);
197: // rmi.log.8A=ao was put into Hashtable
198: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.8A")); //$NON-NLS-1$
199: // rmi.log.8B=calling newInstance of the superclass.
200: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.8B")); //$NON-NLS-1$
201:
202: super .activeObject(aid, ao.remote_object_stub);
203: return ao.remote_object_stub;
204: } catch (Throwable t) {
205: // rmi.log.44=Exception: {0}
206: rlog.log(RMILog.VERBOSE, Messages
207: .getString("rmi.log.44", t), t); //$NON-NLS-1$
208: return null;
209: }
210: }
211:
212: /**
213: * Constructor
214: */
215: public ActivationGroupImpl(ActivationGroupID agid,
216: MarshalledObject data) throws RemoteException,
217: ActivationException {
218: super (agid);
219: groupID = agid;
220: // rmi.log.8C=ActivationGroup was constructed.
221: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.8C")); //$NON-NLS-1$
222: }
223:
224: public boolean inactiveObject(ActivationID id)
225: throws ActivationException, UnknownObjectException,
226: RemoteException {
227: ActiveObject ao = (ActiveObject) active_objects.get(id);
228: if (ao == null) {
229: // rmi.93=Object was not registered or already deactivated.
230: throw new UnknownObjectException(Messages
231: .getString("rmi.93")); //$NON-NLS-1$
232: }
233:
234: Activatable.unexportObject(ao.getImpl(), false);
235: super .inactiveObject(id);
236: active_objects.remove(id);
237: return true;
238: }
239:
240: public void activeObject(ActivationID id, Remote obj)
241: throws ActivationException, UnknownObjectException,
242: RemoteException {
243: ActiveObject ao = new ActiveObject(obj);
244: active_objects.put(id, ao);
245: super .activeObject(id, ao.getStub());
246: }
247:
248: private class ActiveObject {
249: Remote remote_object_impl;
250:
251: MarshalledObject remote_object_stub;
252:
253: ActiveObject(Remote rmt) {
254:
255: // rmi.log.8E=ActiveObject: ActiveObject.<init>:
256: rlog.log(RMILog.VERBOSE, Messages.getString("rmi.log.8E")); //$NON-NLS-1$ //$NON-NLS-2$
257: remote_object_impl = rmt;
258: try {
259:
260: remote_object_stub = new MarshalledObject(rmt);
261: // rmi.log.8F=ActiveObject: remote_object_impl = {0}; remote_object_stub={1}
262: rlog.log(RMILog.VERBOSE, Messages.getString(
263: "rmi.log.8F", //$NON-NLS-1$
264: remote_object_impl, remote_object_stub));
265: } catch (Throwable t) {
266: // rmi.log.90=ActiveObject: Failed to marshal remote stub: {0}
267: rlog.log(RMILog.VERBOSE, Messages.getString(
268: "rmi.log.90", t)); //$NON-NLS-1$
269: t.printStackTrace();
270: }
271: }
272:
273: public Remote getImpl() {
274: return remote_object_impl;
275: }
276:
277: public MarshalledObject getStub() {
278: return remote_object_stub;
279: }
280: }
281: }
|