001: // ExternalContainer.java
002: // $Id: ExternalContainer.java,v 1.6 2002/06/26 17:27:43 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.resources;
007:
008: import java.util.Enumeration;
009: import java.util.Hashtable;
010: import java.io.File;
011:
012: /**
013: * A Container which manage an external store, outside the space.
014: */
015: public abstract class ExternalContainer extends ContainerResource {
016:
017: /**
018: * Our transientFlag, is true that container must not be saved.
019: */
020: protected boolean transientFlag = false;
021:
022: /**
023: * Our external repository.
024: */
025: protected File repository = null;
026:
027: public ResourceReference createDefaultResource(String name) {
028: throw new RuntimeException("not extensible");
029: }
030:
031: /**
032: * Mark this resource as having been modified.
033: */
034: public void markModified() {
035: if (transientFlag) {
036: setValue(ATTR_LAST_MODIFIED, new Long(System
037: .currentTimeMillis()));
038: } else {
039: super .markModified();
040: }
041: }
042:
043: /**
044: * acquire children and notify space if we will be saved.
045: */
046: protected synchronized void acquireChildren() {
047: if (!acquired) {
048: ResourceSpace space = getSpace();
049: if (repository != null) {
050: space.acquireChildren(getChildrenSpaceEntry(),
051: repository, transientFlag);
052: } else {
053: // if we have been saved one time yet.
054: space.acquireChildren(getChildrenSpaceEntry());
055: }
056: acquired = true;
057: }
058: }
059:
060: /**
061: * Delete this Resource instance , and remove it from its store.
062: * This method will erase definitely this resource, for ever, by removing
063: * it from its resource store (when doable).
064: * @exception MultipleLockException if someone has locked this resource.
065: */
066:
067: public synchronized void delete() throws MultipleLockException {
068: if (transientFlag) {
069: // transient, so don't try to delete myself.
070: ResourceSpace space = getSpace();
071: if (space != null) {
072: acquireChildren();
073: // check for lock on children
074: Enumeration e = enumerateResourceIdentifiers();
075: ResourceReference rr = null;
076: Resource resource = null;
077: while (e.hasMoreElements()) {
078: rr = lookup((String) e.nextElement());
079: if (rr != null) {
080: try {
081: synchronized (rr) {
082: resource = rr.lock();
083: resource.delete();
084: }
085: } catch (InvalidResourceException ex) {
086: // nothing, remove invalid resource.
087: } finally {
088: rr.unlock();
089: }
090: }
091: }
092: space.deleteChildren(getChildrenSpaceEntry());
093: }
094: } else {
095: super .delete();
096: }
097: }
098:
099: /**
100: * Get The repository for this external container.
101: * Warning: called in the constructor!
102: * @param context The container context.
103: * @return A File instance
104: */
105: abstract public File getRepository(ResourceContext context);
106:
107: public void initialize(Object values[]) {
108: super .initialize(values);
109: if (repository == null)
110: repository = getRepository(getContext());
111: }
112:
113: /**
114: * @param id The identifier.
115: * @param context The default context.
116: * @param transientFlag The transient flag.
117: */
118:
119: public ExternalContainer(String identifier,
120: ResourceContext context, boolean transientFlag) {
121: Hashtable h = new Hashtable(3);
122: h.put(id, identifier);
123: h.put(co, context);
124: initialize(h);
125: this .acquired = false;
126: this .transientFlag = transientFlag;
127: if (transientFlag)
128: context.setResourceReference(new DummyResourceReference(
129: this ));
130: }
131:
132: public ExternalContainer() {
133: super ();
134: this .acquired = false;
135: this .transientFlag = false;
136: this.repository = null;
137: }
138: }
|