/*
 * Filename: JiniLookup.java
 *
 * Author: Joe Wang
 *
 * Copyright (c) 2003-2005 
 * The Trustees of Columbia University in the City of New York
 * Columbia University Information Technology
 *
 * This file is part of NINJa.
 *
 * NINJa is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * NINJa is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NINJa; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

package edu.columbia.print.ninja.prix.service;

import com.millipad.util.*;
import com.millipad.util.*;
import java.util.*;
import javax.print.*;
import javax.print.attribute.*;
import net.jini.discovery.*;
import net.jini.core.lookup.*;
import net.jini.core.discovery.*;

/**
 * The JiniLookup allows PrintServices to be found via a
 * Jini Registrar.
 * @author Joe Wang <jw168@columbia.edu>
 * @since 1.0
 * @see <a href="http://jini.org/">Jini</a>
 */
public class JiniLookup extends PrintServiceLookup {
	long timeout;
	String[] groups;
	LookupLocator[] locators;
	transient LookupDiscoveryManager dm;

	public JiniLookup() {

		ResourceBundle bundle = ResourceBundle.getBundle(
			getClass().getPackage().getName()+".resource"
			);

		StringTokenizer t = new StringTokenizer(bundle.getString("jini.groups"));
		if(t.countTokens()!=0) {
			groups = new String[t.countTokens()];
			for(int i=0; t.hasMoreTokens(); i++) {
				groups[i] = t.nextToken();
				debug("group: "+groups[i]);
				}
			}
		t = new StringTokenizer(bundle.getString("jini.lookups"));
		if(t.countTokens()!=0) {
			locators = new LookupLocator[t.countTokens()];
			for(int i=0; t.hasMoreTokens(); i++) {
				try {locators[i] = new LookupLocator(t.nextToken());}
				catch(Exception e) {
					debug(e);
					}
				debug(locators[i]);
				}
			}

		}

	/**
	 * @return an array of groups in which to search for the PrintService.
	 * @see net.jini.discovery.DiscoveryGroupManagement
	 */
	public String[] getGroups() {return groups;}

	/**
	 * @return an array of locators in which to search for the PrintService.
	 * @see net.jini.discovery.DiscoveryLocatorManagement
	 */
	public LookupLocator[] getLocators() {return locators;}

	/**
	 * Set the timeout window in which PrintServices may be found.
	 * The window is the time between the contruction of a LookupDiscoveryManager
	 * and LookupDiscoveryManager.getRegistrars is called.
	 * @param the timeout in milliseconds
	 * @see net.jini.discovery.LookupDiscoveryManager
	 */
	public void setTimeout(long timeout) {this.timeout=timeout;}

	private void debug(Object in) {Debug.print(JiniLookup.class,in);}

	public PrintService getDefaultPrintService() {return null;}

	public MultiDocPrintService[] getMultiDocPrintServices(DocFlavor[] flavor, AttributeSet set) {
		return null;
		}

	/**
	 * Return the PrintServices found in the Jini Registrars.
	 */
	public PrintService[] getPrintServices() {return getPrintServices(null,null);}

	public PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes) {
		try {
			if(dm==null) dm = new LookupDiscoveryManager(groups, locators, null);
			ServiceRegistrar[] regs = dm.getRegistrars();
			if(regs==null || regs.length==0) {
				synchronized(dm) {
					dm.wait(timeout);
					regs = dm.getRegistrars();
					}
				}
			Set services = new HashSet();
			for(int i=regs.length-1; i>=0; i--) {
				ServiceMatches matches = regs[i].lookup(new ServiceTemplate(
					null, new Class[] {PrintService.class}, null
					), 1000);
				for(int j=matches.totalMatches-1; j>=0; j--) {
					Object value = matches.items[j].service;
					if(value!=null) services.add(value);
					}
				}
			return (PrintService[] )services.toArray(new PrintService[services.size()]);
			}
		catch(Exception e) {
			e.printStackTrace();
			}
		return null;
		}

	class Notifier implements DiscoveryListener {
		Object lock;
		Notifier(Object lock) {
			this.lock = lock;
			}
		public void discarded(DiscoveryEvent event) {}
		public void discovered(DiscoveryEvent event) {
			lock.notify();
			}
		}
	}

