001: // PassDirectory.java
002: // $Id: PassDirectory.java,v 1.3 2000/08/16 21:37:53 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.io.File;
009:
010: /**
011: * A directory resource able to pass (jump) into some non-related directory.
012: * This resource provides something similar to the PASSS rule of the CERN
013: * server: it allows you to export a directory that is not strictly under
014: * the server physicall space (e.g. none of its parent directory is the server
015: * space directory).
016: * <p>Using this resource means that your exported space is no more
017: * restrained to the server space directory, which can be considered as
018: * a security problem.
019: */
020: public class PassDirectory extends DirectoryResource {
021:
022: /**
023: * Attribute index - The target physicall directory of this resource.
024: */
025: protected static int ATTR_PASSTARGET = -1;
026:
027: static {
028: Attribute a = null;
029: Class cls = null;
030:
031: // Get a pointer to our class.
032: try {
033: cls = Class
034: .forName("org.w3c.tools.resources.PassDirectory");
035: } catch (Exception ex) {
036: ex.printStackTrace();
037: System.exit(1);
038: }
039: // The directory attribute.
040: a = new FileAttribute("pass-target", null, Attribute.EDITABLE);
041: ATTR_PASSTARGET = AttributeRegistry.registerAttribute(cls, a);
042: }
043:
044: /**
045: * Catch side-effects on pass-target, to absolutize it.
046: * @param idx The attribute to set.
047: * @param value The new value.
048: */
049:
050: public void setValue(int idx, Object value) {
051: if ((idx == ATTR_IDENTIFIER) || (idx == ATTR_PASSTARGET)) {
052: try {
053: deleteChildren();
054: } catch (MultipleLockException ex) {
055: //nothing to do
056: }
057: }
058: super .setValue(idx, value);
059: if (idx == ATTR_IDENTIFIER) {
060: ResourceReference rr = getParent();
061: if (rr != null) {
062: try {
063: Resource parent = rr.lock();
064: if (parent.definesAttribute("directory")) {
065: File pdir = (File) parent.getValue("directory",
066: null);
067: if (pdir != null) {
068: // Compute and set our directory attribute:
069: File dir = new File(pdir, getIdentifier());
070: super .setValue(ATTR_DIRECTORY, dir);
071: }
072: }
073: } catch (InvalidResourceException ex) {
074:
075: } finally {
076: rr.unlock();
077: }
078: }
079: values[ATTR_PASSTARGET] = null;
080: values[ATTR_DIRSTAMP] = new Long(-1);
081: } else if (idx == ATTR_PASSTARGET) {
082: File file = (File) value;
083: if (!file.isAbsolute()) {
084: // Make it absolute, relative to the server space.
085: File abs = new File(getServer().getRootDirectory(),
086: file.toString());
087: values[ATTR_PASSTARGET] = abs;
088: values[ATTR_DIRECTORY] = abs;
089: } else {
090: values[ATTR_PASSTARGET] = value;
091: values[ATTR_DIRECTORY] = value;
092: }
093: values[ATTR_DIRSTAMP] = new Long(-1);
094: }
095: }
096:
097: /**
098: * The getDirectory method now returns the pass-directory.
099: * @return The pass target location.
100: */
101:
102: public File getDirectory() {
103: File dir = (File) getValue(ATTR_PASSTARGET, null);
104: if (dir == null)
105: dir = super .getDirectory();
106: return dir;
107: }
108:
109: /**
110: * Make the directory attribute default to the target location.
111: * This is required for classes that rely on the directory attribute to
112: * compute their own attributes.
113: * @param values The values we should initialized from.
114: */
115:
116: public void initialize(Object values[]) {
117: super .initialize(values);
118: disableEvent();
119: File target = getDirectory();
120: if (target != null)
121: setValue(ATTR_DIRECTORY, target);
122: enableEvent();
123: }
124:
125: }
|