package org.w3c.tools.resources ;
import java.util.* ;
import java.io.* ;
import org.w3c.tools.resources.event.*;
public class FramedResource extends Resource
implements FrameEventListener
{
class FrameReference implements ResourceReference {
Class frameClass = null;
ResourceReference framedr = null;
int lockCount = 0;
public void updateContext(ResourceContext ctxt) {
}
public int nbLock() {
return lockCount;
}
@return
public Resource lock()
throws InvalidResourceException
{
FramedResource res = (FramedResource)framedr.lock();
lockCount++;
return res.getFrame(frameClass);
}
public void unlock() {
framedr.unlock();
lockCount--;
}
public boolean isLocked() {
return lockCount != 0;
}
FrameReference (ResourceFrame rframe, ResourceReference framedr) {
this.frameClass = rframe.getClass();
this.framedr = framedr;
}
}
protected transient Hashtable framesRef = null;
protected transient AttributeChangedListener attrListener = null;
protected transient StructureChangedListener structListener = null;
protected transient boolean debugEvent = false;
protected transient boolean event_disabled = false;
protected void disableEvent() {
event_disabled = true;
}
protected void enableEvent() {
event_disabled = false;
}
protected static int ATTR_OID = -1;
static {
Attribute a = null ;
Class cls = null ;
try {
cls = Class.forName("org.w3c.tools.resources.FramedResource") ;
} catch (Exception ex) {
ex.printStackTrace() ;
System.exit(1) ;
}
a = new IntegerAttribute("oid",
null,
Attribute.COMPUTED);
ATTR_OID = AttributeRegistry.registerAttribute(cls, a);
}
public Object getClone(Object values[]) {
FramedResource clone = (FramedResource) super.getClone(values);
clone.framesRef = new Hashtable(3);
return clone;
}
@return
public ServerInterface getServer() {
return ((ResourceContext) getValue(ATTR_CONTEXT, null)).getServer();
}
@return
public int getOid() {
int oid = getInt(ATTR_OID, -1);
if ( oid == -1 ) {
double d = Math.random() * ((double) Integer.MAX_VALUE);
setInt(ATTR_OID, oid = (int) d);
}
return oid;
}
protected void displayEvent(FramedResource fr, EventObject evt) {
if (debugEvent) {
System.out.println(">>> ["+fr.getIdentifier()+
"] has receive "+evt);
}
}
@param evt
public void frameAdded(FrameEvent evt) {
displayEvent( this, evt );
markModified();
}
@param evt
public void frameModified(FrameEvent evt) {
displayEvent( this, evt );
markModified();
}
@param evt
public void frameRemoved(FrameEvent evt) {
displayEvent( this, evt );
markModified();
}
@param frame@param defs
public void registerFrame(ResourceFrame frame, Hashtable defs) {
super.registerFrame(frame,defs);
frame.addFrameEventListener(this);
addAttributeChangedListener(frame);
frame.registerResource(this);
}
@param classname@param identifier@exception ClassNotFoundException@exception IllegalAccessException@exception InstantiationException@exception ClassCastException
protected void registerFrameIfNone(String classname, String identifier)
throws ClassNotFoundException,
IllegalAccessException,
InstantiationException,
ClassCastException
{
Class frameclass =
Class.forName(classname);
ResourceFrame frame = getFrame(frameclass);
if (frame == null) {
Hashtable defs = new Hashtable(3);
defs.put("identifier" , identifier);
registerFrame( (ResourceFrame)frameclass.newInstance() , defs );
}
}
@param frame
public synchronized void unregisterFrame(ResourceFrame frame) {
super.unregisterFrame(frame);
frame.unregisterResource(this);
frame.removeFrameEventListener(this);
removeAttributeChangedListener(frame);
}
private ResourceReference[] getReferenceArray(ResourceFrame[] frames) {
if (frames == null)
return null;
ResourceReference[] refs = new ResourceReference[frames.length];
ResourceReference rr = null;
for (int i=0 ; i < frames.length ; i++) {
rr = (ResourceReference)framesRef.get(frames[i]);
if (rr == null) {
rr = (ResourceReference)
new FrameReference(frames[i],
getResourceReference());
framesRef.put(frames[i],rr);
}
refs[i] = rr;
}
return refs;
}
@return
public synchronized ResourceReference[] getFramesReference() {
return getReferenceArray(getFrames());
}
@param cls@return
public synchronized ResourceReference[] collectFramesReference(Class c) {
return getReferenceArray(collectFrames(c));
}
@param cls@return
public synchronized ResourceReference getFrameReference(Class c) {
ResourceFrame frame = getFrame(c);
if (frame == null)
return null;
ResourceReference rr = (ResourceReference)framesRef.get(frame);
if (rr == null) {
rr = (ResourceReference)
new FrameReference(frame,
getResourceReference());
framesRef.put(frame,rr);
}
return rr;
}
@param frame@return
public synchronized
ResourceReference getFrameReference(ResourceFrame frame) {
ResourceReference rr = (ResourceReference)framesRef.get(frame);
if (rr == null) {
rr = (ResourceReference)
new FrameReference(frame,
getResourceReference());
framesRef.put(frame,rr);
}
return rr;
}
@param evt
public void processEvent(ResourceEvent evt) {
if (evt instanceof StructureChangedEvent) {
fireStructureChangedEvent((StructureChangedEvent)evt);
} else if (evt instanceof AttributeChangedEvent) {
fireAttributeChangeEvent((AttributeChangedEvent)evt);
}
}
@param evt
public void postEvent(ResourceEvent evt) {
if (event_disabled)
return;
ResourceSpace space = getSpace();
if (space != null)
space.getEventQueue().sendEvent(evt);
}
@param l
public void addAttributeChangedListener(AttributeChangedListener l) {
attrListener = ResourceEventMulticaster.add(attrListener, l);
}
@param l
public void removeAttributeChangedListener(AttributeChangedListener l) {
attrListener = ResourceEventMulticaster.remove(attrListener, l);
}
@param idx@param newvalue
protected void postAttributeChangeEvent(int idx, Object newvalue) {
if (( attrListener != null ) && (getResourceReference() != null)) {
AttributeChangedEvent evt =
new AttributeChangedEvent(getResourceReference(),
attributes[idx],
newvalue);
fireAttributeChangeEvent(evt);
}
}
@param evt
protected void fireAttributeChangeEvent(AttributeChangedEvent evt) {
if ( attrListener != null )
attrListener.attributeChanged(evt);
}
@param l
public void addStructureChangedListener(StructureChangedListener l) {
structListener = ResourceEventMulticaster.add(structListener, l);
}
@param l
public void removeStructureChangedListener(StructureChangedListener l) {
structListener = ResourceEventMulticaster.remove(structListener, l);
}
@param rr@param type
protected void postStructureChangedEvent(ResourceReference rr, int type) {
if ((structListener != null) && (rr != null)) {
StructureChangedEvent evt =
new StructureChangedEvent(rr, type);
postEvent(evt);
}
}
@param type
protected void postStructureChangedEvent(int type) {
if ((structListener != null) && (getResourceReference() != null)) {
StructureChangedEvent evt =
new StructureChangedEvent(getResourceReference(), type);
postEvent(evt);
}
}
@param type
protected void fireStructureChangedEvent(int type) {
if ((structListener != null) && (getResourceReference() != null)) {
StructureChangedEvent evt =
new StructureChangedEvent(getResourceReference(), type);
fireStructureChangedEvent(evt);
}
}
@param evt
protected void fireStructureChangedEvent(StructureChangedEvent evt) {
if (structListener != null) {
int type = evt.getID();
switch (type) {
case Events.RESOURCE_MODIFIED :
structListener.resourceModified(evt);
break;
case Events.RESOURCE_CREATED :
structListener.resourceCreated(evt);
break;
case Events.RESOURCE_REMOVED :
structListener.resourceRemoved(evt);
break;
case Events.RESOURCE_UNLOADED :
structListener.resourceUnloaded(evt);
break;
}
}
}
public void notifyUnload() {
fireStructureChangedEvent(Events.RESOURCE_UNLOADED);
super.notifyUnload();
}
public synchronized void delete()
throws MultipleLockException
{
disableEvent();
fireStructureChangedEvent(Events.RESOURCE_REMOVED);
ResourceFrame frames[] = getFrames();
if ( frames != null ) {
for (int i = 0 ; i < frames.length ; i++) {
if ( frames[i] == null )
continue;
frames[i].removeFrameEventListener(this);
this.removeAttributeChangedListener(frames[i]);
frames[i].unregisterResource(this);
}
}
try {
super.delete();
} catch (MultipleLockException ex) {
enableEvent();
throw ex;
}
}
public void markModified() {
super.markModified();
postStructureChangedEvent(Events.RESOURCE_MODIFIED);
}
public synchronized void setValue(int idx, Object value) {
super.setValue(idx, value) ;
if (idx != ATTR_LAST_MODIFIED) {
postAttributeChangeEvent(idx, value);
postStructureChangedEvent(Events.RESOURCE_MODIFIED);
}
}
public boolean lookup(LookupState ls, LookupResult lr)
throws ProtocolException
{
ResourceFrame frames[] = getFrames();
if (frames != null) {
for (int i = 0 ; i < frames.length ; i++) {
if (frames[i] == null)
continue;
if (frames[i].lookup(ls,lr))
return true;
}
}
if ( ls.hasMoreComponents() ) {
lr.setTarget(null);
return false;
} else {
lr.setTarget(getResourceReference());
return true;
}
}
@param request@return
protected ReplyInterface performFrames(RequestInterface request)
throws ProtocolException, NotAProtocolException
{
ResourceFrame frames[] = getFrames();
if (frames != null) {
for (int i = 0 ; i < frames.length ; i++) {
if (frames[i] == null)
continue;
ReplyInterface reply = frames[i].perform(request);
if (reply != null)
return reply;
}
}
return null;
}
public ReplyInterface perform(RequestInterface request)
throws ProtocolException, NotAProtocolException
{
return performFrames(request);
}
@param values
public void initialize(Object values[]) {
this.attrListener = null;
this.structListener = null;
disableEvent();
super.initialize(values);
ResourceFrame frames[] = getFrames();
if ( frames != null ) {
this.framesRef = new Hashtable(frames.length);
Hashtable defs = new Hashtable(3);
for (int i = 0 ; i < frames.length ; i++) {
if ( frames[i] == null )
continue;
frames[i].registerResource(this);
frames[i].initialize(defs);
frames[i].addFrameEventListener(this);
this.addAttributeChangedListener(frames[i]);
}
} else {
this.framesRef = new Hashtable(3);
}
enableEvent();
}
}