/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.apache.avalon.cornerstone.blocks.rmification.glue;

import org.apache.avalon.framework.logger.AbstractLoggable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.phoenix.Block;
import org.apache.avalon.cornerstone.services.rmification.RMIfication;

import java.util.Hashtable;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;


/**
 * Default implementation of RMIfication service.
 *
 * @author <a href="mailto:g-froehlich@gmx.de">Gerhard Froehlich</>
 */
public class Glue
    extends UnicastRemoteObject
    implements Block, RMIfication, Initializable, Startable, Disposable, Configurable {

    protected Configuration mConfiguration;
    protected String mBaseName;
    protected String mBindingAddress;

    public Glue() throws RemoteException {
        super();
    }

    public void initialize()
    {
    }

    public void dispose()
    {
    }

    public void start() {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
        }
        String name = "//" + mBindingAddress + "/" + mBaseName;
        try {
            Naming.rebind(name, this);
            System.out.println("rmi service bound");
        } catch (Exception e) {
            
        }
    }

    public void stop() {}

    public void configure( Configuration configuration ) throws ConfigurationException {
        mConfiguration = configuration;
        mBindingAddress = configuration.getChild("binding-address").getValue( "127.0.0.1" );
        mBaseName = configuration.getChild("base-name").getValue( "rmi" );
    }

    /**
     * Publish 
     *
     * @param obj the object to publish
     * @param publicationName The name to publish it as.
     */
    public void publish( Object obj, String publicationName ) throws RemoteException {
        //TODO implementation
    }

    /**
     * Publish 
     *
     * @param obj the object to publish
     * @param publicationName The name to publish it as.
     */
    public void publish( Object obj, String publicationName, Class[] interfacesToExpose ) throws RemoteException {
        //TODO implementation
    }

    public void publish( Object obj, String publicationName, Class interfaceToExpose ) throws RemoteException {
        publish(obj, publicationName, new Class[] {interfaceToExpose});        
    }

    public void unpublish( String publicationName ) throws RemoteException {
        //TODO implementation
    }
}


