head	1.12;
access;
symbols;
locks; strict;
comment	@# @;


1.12
date	2001.05.21.21.06.01;	author leonardr;	state dead;
branches;
next	1.11;

1.11
date	2001.04.11.10.02.15;	author dlr;	state Exp;
branches;
next	1.10;

1.10
date	2001.04.11.09.54.53;	author dlr;	state Exp;
branches;
next	1.9;

1.9
date	2001.04.10.00.57.53;	author dlr;	state Exp;
branches;
next	1.8;

1.8
date	2001.04.09.23.12.59;	author dlr;	state Exp;
branches;
next	1.7;

1.7
date	2001.04.09.21.57.03;	author dlr;	state Exp;
branches;
next	1.6;

1.6
date	2001.04.09.21.56.41;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2001.04.09.21.54.23;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2001.04.09.18.11.22;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.04.09.06.17.46;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.04.09.06.16.09;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.04.09.05.56.36;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.12
log
@Moved clustering features out of RMI and into the database.
@
text
@/* ================================================================
 * Copyright (c) 2001 CollabNet.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet <http://www.collab.net/>."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 *
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 *
 * 5. Products derived from this software may not use the "Tigris" or
 * "Anzu" names nor may "Tigris" or "Anzu" appear in their names without
 * prior written permission of CollabNet.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.tool.rmi;

import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.rmi.registry.LocateRegistry;
import java.rmi.Naming;
import java.rmi.Remote;

import org.apache.turbine.util.Log;
import org.apache.turbine.util.TurbineException;

import org.tigris.helm.tool.cluster.*;

/**
 * Provides RMI registration support for implementations of the {@@link
 * java.rmi.Remote} interface.
 *
 * @@author <a href="mailto:edk@@collab.net">Ed Korthof</a>
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 */
public class RemoteImplRegistrar
{
    /**
     * Creates a RMI registry and registers known implementations.
     */
    public static void main(String[] argv)
    {
        int rmiRegistryPort =
            Integer.getInteger("org.tigris.helm.RMIPORT",
                               ClusterService.getRMIRegistryPort()).intValue();
        createRMIRegistry(rmiRegistryPort);
        //registerBindings();
    }

    /**
     * Creates an RMI registry.
     */
    public static synchronized void createRMIRegistry(int rmiRegistryPort)
    {
        // Make sure the port isn't already bound.
        try
        {
            ServerSocket soc = new ServerSocket(rmiRegistryPort);
            soc.close();
        }
        catch (IOException e)
        {
            if (e instanceof BindException)
            {
                Log.info("Not creating RMI registry: Port " + rmiRegistryPort +
                         " already bound");
            }
            else
            {
                Log.warn("Error checking availability of RMI registry port " +
                         rmiRegistryPort);
            }

            // Problems touching port.
            return;
        }

        // If we get this far, it's safe to create a RMI registry on the
        // specified port.
        try
        {
            LocateRegistry.createRegistry(rmiRegistryPort);
            Log.info("Created RMI registry on port " + rmiRegistryPort);
        }
        catch (Exception e)
        {
            Log.warn("Unable to create RMI registry", e);
        }
    }

    /**
     * Create RMI bindings for {@@link java.rmi.Remote} implementations.
     */
    public static void registerBindings(String[] classKeys,
                                        String[] classNames)
        throws TurbineException
    {
        try
        {
            if (classKeys.length == classNames.length)
            {
                StringBuffer bindings = new StringBuffer();
                for (int i = 0; i < classKeys.length; i++)
                {
                    Class c = Class.forName(classNames[i]);
                    Remote remoteImpl = (Remote) c.newInstance();
                    String rmiURL = ClusterObject.getLookupURL(classKeys[i]);

                    //Log.debug("RMI registering Remote implementation " +
                    //          remoteImpl + " using " + rmiURL);
                    Naming.rebind(rmiURL, remoteImpl); 

                    bindings.append('\n').append(rmiURL).append(" => ")
                        .append(classNames[i]);
                }
                Log.debug("Registered RMI binding(s): " + bindings);
            }
            else
            {
                throw new IllegalArgumentException
                    ("RMI unequal number of class key/name bindings");
            }
        }
        catch (Throwable e)
        {
            throw new TurbineException("Unable to register Remote " +
                                       "implementations with RMI registry", e);
        }
    }
}
@


1.11
log
@There might only be one RMI binding...
@
text
@@


1.10
log
@Reworked logging a bit.
@
text
@d148 1
a148 1
                Log.debug("Registered RMI bindings: " + bindings);
@


1.9
log
@Producing a little debugging trace on what's being RMI mapped.
@
text
@a136 7
                    if (i > 0)
                    {
                        bindings.append(", ");
                    }
                    bindings.append(classKeys[i]).append(" => ")
                        .append(classNames[i]);

d141 2
a142 2
                    Log.debug("RMI registering Remote implementation using " +
                              rmiURL);
d144 3
@


1.8
log
@Instantiate the named class, not its key.  Duh.
@
text
@d143 1
d146 5
a150 2
                    Naming.rebind(ClusterObject.getLookupURL(classKeys[i]),
                                  remoteImpl); 
@


1.7
log
@Removed done TODO item.
@
text
@d143 1
a143 1
                    Class c = Class.forName(classKeys[i]);
@


1.6
log
@Finished initial implementation of registerBindings() method.
@
text
@a124 3
     * <p>
     * TODO: Get a list of URL/Remote instance pairs to register from a
     * properties file.
@


1.5
log
@* Importing the Remote interface.

* Busted main() for now.  ;-\

* Added params to registerBindings() and almost finished implementing
the method.
@
text
@d146 2
a147 2
                    // TODO
                    Remote remoteImpl = (Remote) null;
@


1.4
log
@Added code to createRMIRegistry(int) to check the availablity of the
specified port before attempting to create the RMI registry.
@
text
@d54 1
d57 1
d76 2
a77 1
            Integer.getInteger("org.tigris.helm.RMIPORT", 1099).intValue();
d79 1
a79 1
        registerBindings();
d129 3
a131 1
    public static void registerBindings()
d135 23
a157 2
            //Naming.rebind(rmiURL, remoteImplementation);
            //Log.debug("Registered RMI bindings: " + list_of_bindings);
d161 2
a162 3
            Log.error("Unable to register Remote implementations with RMI " + 
                      "registry", e);
            System.exit(1);
@


1.3
log
@There really doesn't appear to be any need for an instance of this class.
@
text
@d49 3
d82 1
a82 1
    public static final void createRMIRegistry(int rmiRegistryPort)
d84 25
@


1.2
log
@Cleaned up and noted path to first implementation cut closure.
@
text
@d72 2
a73 3
        RemoteImplRegistrar registrar = new RemoteImplRegistrar();
        registrar.createRMIRegistry(rmiRegistryPort);
        registrar.registerBindings();
d79 1
a79 1
    private final void createRMIRegistry(int rmiRegistryPort)
d98 1
a98 1
    private void registerBindings()
@


1.1
log
@Lifted support for creating a RMI registry and the hook for registering
objects from Anzu.
@
text
@d70 2
a71 5
        int rmiPort =
            Integer.getInteger("org.tigris.anzu.RMIPORT", 1099).intValue();
        String rmiURL = System.getProperty("org.tigris.anzu.RMIURL",
                                           "org.tigris.anzu.mail");

d73 1
a73 1
        registrar.createRMIRegistry(rmiPort);
d80 1
a80 1
    private final void createRMIRegistry(int rmiPort)
d84 2
a85 1
            LocateRegistry.createRegistry(rmiPort);
d94 4
a97 1
     * TODO: Create RMI bindings for {@@link java.rmi.Remote} implementations.
a102 1
            Object remoteImplementation = null;
d104 1
a104 1
            Log.debug("Registered RMI bindings: " + remoteImplementation);
@

