package org.w3c.tools.resources ;
import java.util.*;
import org.w3c.tools.resources.event.*;
public class ContainerResource extends AbstractContainer {
protected static int ATTR_KEY = -1;
static {
Attribute a = null ;
Class cls = null ;
try {
cls = Class.forName("org.w3c.tools.resources.ContainerResource") ;
} catch (Exception ex) {
ex.printStackTrace() ;
System.exit(1) ;
}
a = new IntegerAttribute("key",
null,
Attribute.COMPUTED);
ATTR_KEY = AttributeRegistry.registerAttribute(cls, a);
}
public Object getClone(Object values[]) {
values[ATTR_KEY] = null;
return super.getClone(values);
}
@return
public Integer getKey() {
Integer key = (Integer) getValue(ATTR_KEY, null);
if (key == null) {
key = new Integer(getIdentifier().hashCode() ^
(new Date().hashCode()));
setValue(ATTR_KEY, key);
}
return key;
}
protected SpaceEntry getSpaceEntry() {
ResourceReference rr = getParent();
if (rr == null) return new SpaceEntryImpl(this);
try {
ContainerResource cont = (ContainerResource) rr.lock();
return new SpaceEntryImpl(cont);
} catch (InvalidResourceException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
return null;
} finally {
rr.unlock();
}
}
@return
protected SpaceEntry getChildrenSpaceEntry() {
return new SpaceEntryImpl( this );
}
@param evt
public void resourceModified(StructureChangedEvent evt) {
super.resourceModified(evt);
}
@param evt
public void resourceCreated(StructureChangedEvent evt) {
super.resourceCreated(evt);
}
@param evt
public void resourceRemoved(StructureChangedEvent evt) {
super.resourceRemoved(evt);
}
protected ResourceContext updateDefaultChildAttributes(Hashtable attrs) {
ResourceContext context = super.updateDefaultChildAttributes(attrs);
if (context == null) {
context = new ResourceContext(getContext());
attrs.put("context", context) ;
}
String name = (String) attrs.get("identifier");
if ( name != null )
attrs.put("url", getURLPath()+name);
return context;
}
@param all@return
public synchronized Enumeration enumerateResourceIdentifiers(boolean all) {
ResourceSpace space = getSpace();
acquireChildren();
return space.enumerateResourceIdentifiers( getChildrenSpaceEntry() );
}
@param name
public ResourceReference createDefaultResource(String name) {
return null;
}
@param name
public ResourceReference lookup(String name) {
acquireChildren();
SpaceEntry sp = getChildrenSpaceEntry();
ResourceSpace space = getSpace();
ResourceReference rr = space.lookupResource(sp, name);
if (rr != null)
return rr;
Hashtable defs = new Hashtable(5) ;
defs.put("identifier", name);
ResourceContext context = updateDefaultChildAttributes(defs);
rr = space.loadResource(sp, name, defs);
if (rr != null) {
context.setResourceReference(rr);
try {
Resource resource = rr.lock();
if (resource instanceof FramedResource) {
FramedResource fres = (FramedResource) resource;
fres.addStructureChangedListener(this);
}
} catch (InvalidResourceException ex) {
} finally {
rr.unlock();
}
}
return rr;
}
@param ls@param lr@exception ProtocolException@return
public boolean lookup(LookupState ls, LookupResult lr)
throws ProtocolException
{
if ( super.lookup(ls, lr) )
return true;
String name = ls.getNextComponent() ;
ResourceReference rr = null;
rr = lookup(name);
if (rr == null) {
lr.setTarget(null);
return false;
}
try {
lr.setTarget(rr);
FramedResource resource = (FramedResource) rr.lock();
return (resource != null ) ? resource.lookup(ls, lr) : false;
} catch (InvalidResourceException ex) {
return false;
} finally {
rr.unlock();
}
}
@param name@exception MultipleLockException
public void delete(String name)
throws MultipleLockException
{
ResourceReference rr = null;
rr = lookup(name);
if (rr != null) {
try {
synchronized (rr) {
Resource resource = rr.lock();
if (resource instanceof FramedResource)
((FramedResource)resource).
removeStructureChangedListener(this);
resource.delete();
}
} catch (InvalidResourceException ex) {
} finally {
rr.unlock();
}
}
}
@exception MultipleLockException
public synchronized void replace(DirectoryResource newdir)
throws MultipleLockException
{
Enumeration e = enumerateResourceIdentifiers();
ResourceReference rr = null;
Resource resource = null;
while (e.hasMoreElements()) {
rr = lookup((String) e.nextElement());
if (rr != null) {
try {
resource = rr.lock();
ResourceContext ctxt = new ResourceContext(
newdir.getContext());
resource.setContext(ctxt, true);
if (resource instanceof FramedResource) {
((FramedResource)resource).
removeStructureChangedListener(this);
((FramedResource)resource).
addStructureChangedListener(newdir);
}
} catch (InvalidResourceException ex) {
} finally {
rr.unlock();
}
}
}
super.delete();
}
@exception MultipleLockException
public synchronized void delete()
throws MultipleLockException
{
disableEvent();
ResourceSpace space = getSpace();
acquireChildren();
Enumeration e = enumerateResourceIdentifiers();
ResourceReference rr = null;
Resource resource = null;
while (e.hasMoreElements())
delete((String)e.nextElement());
SpaceEntry sentry = getChildrenSpaceEntry();
super.delete();
space.deleteChildren(sentry);
}
public void notifyUnload() {
super.notifyUnload();
}
protected boolean acquired = false;
protected synchronized void acquireChildren() {
if (!acquired) {
ResourceSpace space = getSpace();
space.acquireChildren( getChildrenSpaceEntry() );
acquired = true;
}
}
@param resource
protected synchronized ResourceReference addResource(Resource resource,
Hashtable defs) {
acquireChildren();
ResourceReference rr = getSpace().addResource(getChildrenSpaceEntry() ,
resource,
defs);
resource.getContext().setResourceReference(rr);
if (resource instanceof FramedResource) {
FramedResource fres = (FramedResource) resource;
fres.addStructureChangedListener(this);
}
markModified() ;
postStructureChangedEvent(rr, Events.RESOURCE_CREATED);
return rr;
}
@param name@param resource@param defs@exception InvalidResourceException
public void registerResource(String name,
Resource resource,
Hashtable defs)
throws InvalidResourceException
{
acquireChildren();
if ( defs == null )
defs = new Hashtable(11) ;
defs.put("identifier", name);
ResourceContext context = updateDefaultChildAttributes(defs);
if (context != null) {
resource.initialize(defs);
ResourceReference rr;
rr = getSpace().addResource(getChildrenSpaceEntry(),
resource,
defs);
context.setResourceReference(rr);
if (resource instanceof FramedResource) {
FramedResource fres = (FramedResource) resource;
fres.addStructureChangedListener(this);
}
markModified();
postStructureChangedEvent(rr, Events.RESOURCE_CREATED);
} else {
throw new InvalidResourceException(getIdentifier(),
name,
"unable to get context");
}
}
@param values
public void initialize(Object values[]) {
super.initialize(values);
disableEvent();
String url = getURLPath() ;
if ((url != null) && ! url.endsWith("/") )
setValue(ATTR_URL, url+"/") ;
acquired = false;
enableEvent();
}
}