/*
* $Id: ModuleCache.java,v 1.2 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;
import anvil.ForgingException;
import anvil.server.Address;
import anvil.server.Resource;
import anvil.server.Domain;
public class ModuleCache
{
protected Domain _domain;
protected TreeMap _cache = new TreeMap();
public ModuleCache(Domain domain)
{
_domain = domain;
}
protected ArrayList getMatching(String hostname, String pathinfo)
{
ArrayList list = new ArrayList();
Iterator iter = _cache.values().iterator();
while(iter.hasNext()) {
ModuleEnvelope env = (ModuleEnvelope)iter.next();
Address addr = env.getAddress();
if (addr.getHostname().equals(hostname)) {
if (addr.getPathinfo().startsWith(pathinfo)) {
list.add(env);
}
}
}
return list;
}
public synchronized ModuleEnvelope[] getContents(String hostname, String pathinfo)
{
ArrayList list = getMatching(hostname, pathinfo);
return (ModuleEnvelope[])list.toArray(new ModuleEnvelope[list.size()]);
}
public synchronized void purge(String hostname, String pathinfo)
{
ArrayList list = getMatching(hostname, pathinfo);
int n = list.size();
for(int i=0; i<n; i++) {
_cache.remove(((ModuleEnvelope)list.get(i)).getAddress());
}
}
public synchronized void purge(ModuleEnvelope envelope)
{
_cache.remove(envelope.getAddress());
}
public synchronized void purge(Address address)
{
_cache.remove(address);
}
public synchronized void put(ModuleEnvelope[] envelopes)
{
int n = envelopes.length;
for(int i=0; i<n; i++) {
put(envelopes[i]);
}
}
public synchronized void put(ModuleEnvelope envelope)
{
_cache.put(envelope.getAddress(), envelope);
}
public synchronized ModuleEnvelope get(Address address)
{
return (ModuleEnvelope)_cache.get(address);
}
public ModuleEnvelope load(Address address)
throws IOException, ForgingException
{
Smith smith = new Smith(this, address);
return smith.forge();
}
public synchronized void stop()
{
_cache.clear();
}
}