/*
 * 
 * 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 net.jini.lookup.entry;

import java.beans.Beans;
import java.io.IOException;
import net.jini.core.entry.Entry;

/**
 * Utility class for handling JavaBeans(TM) components that relate to Entry 
 * classes in the Jini(TM) Lookup service.
 * 
 * @author Sun Microsystems, Inc.
 */
public class EntryBeans {
    /**
     * Create a bean of the appropriate type for the given Entry
     * object, and link it to the object for immediate manipulation.
     *
     * @param ent the Entry for which to create and link a bean
     * @return a new bean of the right type, linked to the given Entry
     * @exception ClassNotFoundException no bean class of the
     * appropriate type could be found
     * @exception ClassCastException the bean class does not implement
     * the EntryBean interface
     * @exception IOException the JavaBeans component could not be instantiated
     */
    public static EntryBean createBean(Entry ent)
	throws ClassNotFoundException, IOException
    {
	String beanClass = ent.getClass().getName() + "Bean";
	Object obj = null;
	try {
	    obj = Beans.instantiate(ent.getClass().getClassLoader(),
				    beanClass);
	} catch (ClassNotFoundException e) {
	    // Ignore any ClassNotFoundException thrown here.
	} // Allow the instantiate method to throw an IOException.	
	if (obj == null)
	    obj = Beans.instantiate(null, beanClass);
	EntryBean entBean = (EntryBean)Beans.getInstanceOf(obj,
							   EntryBean.class);
	entBean.makeLink(ent);
	return entBean;
    }

    /**
     * Return the class of JavaBeans component that corresponds to a particular 
     * Entry class.  The class passed in as argument must implement the
     * Entry interface.
     *
     * @param c the class for which a JavaBeans component should be found
     * @return corresponding JavaBeans component class
     * @exception ClassNotFoundException no such class could be found
     * @exception ClassCastException the class does not implement
     * the EntryBean interface
     * @see EntryBean
     */
    public static Class getBeanClass(Class c) throws ClassNotFoundException {
	if (!Entry.class.isAssignableFrom(c))
	    throw new IllegalArgumentException("class does not implement net.jini.core.entry.Entry");
	String beanClassName = c.getName() + "Bean";
	Class beanClass = null;
	try {
	    beanClass = c.getClassLoader().loadClass(beanClassName);
	} catch (ClassNotFoundException e) {
	    // ignore
	}
	if (beanClass == null)
	    beanClass = Class.forName(beanClassName);
	if (!EntryBean.class.isAssignableFrom(beanClass))
	    throw new ClassCastException("JavaBeans component class does not implement EntryBean interface");
	return beanClass;
    }
}
