/************************************************
* Columbia University CS Department
* Systems and Security Group
*
* Open and Survivable Embedded Systems (OASES)
*
* Michael E. Locasto
*
* Protocol For Code Exchange
*  in Survivable Embedded Systems (PCXSES)
*
* This software is provided as is without any
* warranty or guarantee of merchantibility
* or fitness for a particular purpose. This
* software is released under the terms of the
* GNU General Public License (GPL).
*
* Last updated: November 19, 2002
*
***********************************************/

package locasto.pcxses.ac;

import locasto.logging.Logger;

import javax.naming.*;
import javax.naming.directory.*;

import java.io.*;
import java.util.*;


/**
 * The repositoryManager represents a lookup facility
 * and a collection of threads that can connect to the
 * filesystem and pump a class file over the network via
 * a DataoutputStream.
 *
 *
 *
 * env.put( Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory" );
 *
 *
 *
 *
 * @author locasto@cs.columbia.edu
*/
public class RepositoryManager{

	private DirContext dCtx;

	private AreaController parent;

	private static String repositoryBase;


	public RepositoryManager(){
	}


	public void configure( Hashtable env, AreaController ac ) throws NamingException{
		dCtx = new InitialDirContext( env );
		this.parent = ac;
		this.repositoryBase = AreaController.AC_HOME+
							File.separatorChar+"data"+
							File.separatorChar+"dev-src"+
							File.separatorChar;

	}

	void init(){
		log( "RepositoryManager started at contextBase "+repositoryBase+"..." );
	}

	synchronized void log( String message ){
		if( parent!=null )
			parent.log( message );
		else
			System.err.println( message );
	}

	public synchronized int getClassBytes( String classname ) throws NamingException{

		String objectID = classname.replace( '.', File.separatorChar );
		 objectID+=".class";
		 objectID = repositoryBase + objectID;

		File f = null;

		Object o = dCtx.lookup( objectID );

		try{
			f = ((File)o);
	    }catch( ClassCastException ccex ){
			return 0;
		}
		if( f.isDirectory() )
			return 0;
		else
			return ((int)f.length());


	}

	/** locate the class file along the fqcn path
	 * Return the object as a file
	 *
	 * com.mycompany.Hello
	 * com/mycompany/Hello.class
	 * AC_HOME/data/dev-src/com/mycompany/Hello.class
	*/
	public synchronized File lookup( String classname ) throws NamingException{

		String objectID = classname.replace( '.', File.separatorChar );
		objectID+=".class";
		objectID = repositoryBase + objectID;

		File f=null;
		 // Look up an object
		Object o = dCtx.lookup( objectID );

		try{
			f = (File)o;
		}catch( ClassCastException ccex ){
			f=null;
		}

		/*
		if( o instanceof File ){
			f = ((File)o);
			return f;
		}else{
			return null;
		}
		*/
		return f;
	}


	void shutDown(){

		try{

			dCtx.close();

		}catch( Exception e ){
			parent.log( "Trouble shutting down RepositoryManager"+e.getMessage() );
		}

	}
}
