<%!

void watch(String name, AdminBean ninja) throws Exception {
    Set set = getWatches(ninja);
    set.add(new URI(name));
    writeWatches(set, ninja);
}

void unwatch(String name, AdminBean ninja) throws Exception {
    Set set = getWatches(ninja);
    set.remove(new URI(name));
    writeWatches(set, ninja);
}

void writeWatches(Set set, AdminBean ninja) throws Exception {
    StringBuffer buffer = new StringBuffer();
	for(Iterator i=set.iterator(); i.hasNext();) buffer.append(i.next()+",");
	if(buffer.length()>0) buffer.deleteCharAt(buffer.length()-1);
    ninja.setList(buffer+"");
}

Set getWatches(AdminBean ninja) throws Exception {
    final Set watchedServices = new HashSet();
    StringTokenizer tokenizer = new StringTokenizer(ninja.getList(), ",");
    for(;tokenizer.hasMoreTokens();) watchedServices.add(new URI(tokenizer.nextToken()));
    ninja.setFilter(new Filter(watchedServices));
    return watchedServices;
}

static class Filter {
    Set services;
    Filter(Set s) {
        services=new HashSet();
        services.addAll(s);
    }
    public boolean equals(Object in) {
        ServiceCacheEvent event = (ServiceCacheEvent )in;
        Entry[] entries = event.getServiceItem().attributeSets;
        try {
            for(int i=0; entries!=null && i<entries.length; i++) {
                if(!(entries[i] instanceof ApplianceInfo)) continue;
                ApplianceInfo entry = (ApplianceInfo )entries[i];
                URI uri = new URI("lpd://"+entry.hostName+"/public");
                return services.contains(uri);
            }
        }
        catch(Exception e) {}
        return false;
    }
}

%>