/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */

package com.sun.jini.phoenix;

import java.lang.reflect.Method;
import java.rmi.activation.ActivationGroup;
import java.rmi.activation.ActivationGroupDesc;
import java.rmi.activation.ActivationGroupID;
import java.rmi.server.RMIClassLoader;
import java.util.Collections;
import net.jini.io.MarshalInputStream;

/**
 * This is the bootstrap code to start a virtual machine (VM) executing an
 * activation group.
 *
 * The activator spawns (as a child process) an activation group as needed
 * and directs activation requests to the appropriate activation
 * group. After spawning the VM, the activator passes some
 * information to the bootstrap code via its stdin: <p>
 * <ul>
 * <li> the activation group's id, 
 * <li> the activation group's descriptor (an instance of the class
 *    java.rmi.activation.ActivationGroupDesc) for the group, and
 * <li> the group's incarnation number.
 * </ul><p>
 *
 * When the bootstrap VM starts executing, it reads group id and
 * descriptor from its stdin so that it can create the activation
 * group for the VM.
 *
 * @author Sun Microsystems, Inc.
 * 
 * @since 2.0
 */
class ActivationGroupInit {
    private ActivationGroupInit() {}

    /**
     * Main program to start a VM for an activation group.
     */
    public static void main(String args[])
    {
	try {
	    if (System.getSecurityManager() == null) {
		System.setSecurityManager(new SecurityManager());
	    }
	    MarshalInputStream in =
		new MarshalInputStream(
				   System.in,
				   ActivationGroupInit.class.getClassLoader(),
				   false, null, Collections.EMPTY_LIST);
	    in.useCodebaseAnnotations();
	    ActivationGroupID id  = (ActivationGroupID)in.readObject();
	    ActivationGroupDesc desc = (ActivationGroupDesc)in.readObject();
	    long incarnation = in.readLong();
	    Class cl = RMIClassLoader.loadClass(desc.getLocation(),
						desc.getClassName());
	    try {
		Method create =
		    cl.getMethod("createGroup",
				 new Class[]{ActivationGroupID.class,
					     ActivationGroupDesc.class,
					     long.class});
		create.invoke(null, new Object[]{id, desc,
						 new Long(incarnation)});
	    } catch (NoSuchMethodException e) {
		ActivationGroup.createGroup(id, desc, incarnation);
	    }
	} catch (Exception e) {
	    System.err.println("Exception in starting ActivationGroupInit:");
	    e.printStackTrace();
	} finally {
	    try {
		System.in.close();
		// note: system out/err shouldn't be closed
		// since the parent may want to read them.
	    } catch (Exception ex) {
		// ignore exceptions
	    }
	}
    }
}
