001: // AutoLookupDirectory.java
002: // $Id: AutoLookupDirectory.java,v 1.11 2000/08/16 21:37:33 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.jigedit.resources;
007:
008: import java.io.File;
009:
010: import org.w3c.cvs.CVS;
011: import org.w3c.cvs.CvsDirectory;
012: import org.w3c.cvs.CvsException;
013:
014: import org.w3c.tools.resources.Attribute;
015: import org.w3c.tools.resources.AttributeHolder;
016: import org.w3c.tools.resources.AttributeRegistry;
017: import org.w3c.tools.resources.BooleanAttribute;
018: import org.w3c.tools.resources.LookupResult;
019: import org.w3c.tools.resources.LookupState;
020: import org.w3c.tools.resources.ProtocolException;
021: import org.w3c.tools.resources.Resource;
022: import org.w3c.tools.resources.ResourceReference;
023: import org.w3c.tools.resources.ServerInterface;
024:
025: import org.w3c.www.http.HttpRequestMessage;
026:
027: import org.w3c.jigsaw.http.Request;
028:
029: import org.w3c.jigedit.cvs.CvsModule;
030: import org.w3c.jigedit.cvs.CvsRootDirectory;
031:
032: /**
033: * A special version of DirectoryResource that can fetch a file
034: * from CVS directly if it is not already here.
035: * It can alos do an automatic update, depending on a flag
036: */
037:
038: public class AutoLookupDirectory extends CvsRootDirectory {
039:
040: private CvsDirectory cvs = null;
041:
042: /**
043: * Attribute index, tell if we must update the resource everytime it is
044: * acceded (not recommended as it generates many cvs commands)
045: */
046:
047: private static int ATTR_AUTOUPDATE = -1;
048:
049: /**
050: * Attribute index, tell if we must add into cvs new puted directories.
051: */
052: private static int ATTR_EXTENSIBLE = -1;
053:
054: static {
055: Attribute a = null;
056: Class cls = null;
057:
058: try {
059: cls = Class
060: .forName("org.w3c.jigedit.resources.AutoLookupDirectory");
061: } catch (Exception ex) {
062: ex.printStackTrace();
063: System.exit(1);
064: }
065: // The browsable flag:
066: a = new BooleanAttribute("autoupdate", Boolean.FALSE,
067: Attribute.EDITABLE);
068: ATTR_AUTOUPDATE = AttributeRegistry.registerAttribute(cls, a);
069: // The browsable flag:
070: a = new BooleanAttribute("cvs-extensible", Boolean.FALSE,
071: Attribute.EDITABLE);
072: ATTR_EXTENSIBLE = AttributeRegistry.registerAttribute(cls, a);
073: }
074:
075: /**
076: * Get the appropriate CVS manager for the directory we handle.
077: * @return A CvsDirectory instance.
078: * @exception CvsException If we couldn't get the manager.
079: */
080:
081: protected synchronized CvsDirectory getCvsManager()
082: throws CvsException {
083: if (cvs == null) {
084: cvs = CvsModule.getCvsManager(getDirectory(), getContext(),
085: getServer().getProperties());
086: }
087: return cvs;
088: }
089:
090: /**
091: * tell if we must always do an update.
092: */
093:
094: public boolean isAutoUpdatable() {
095: return getBoolean(ATTR_AUTOUPDATE, false);
096: }
097:
098: /**
099: * tell if we must add in cvs the new puted documents.
100: */
101: public boolean isCvsExtensible() {
102: return getBoolean(ATTR_EXTENSIBLE, false);
103: }
104:
105: /**
106: * Create a DirectoryResource and the physical directory too.
107: * Add the new directory in the CVS repository.
108: * @param name the name of the resource.
109: * @return A ResourceReference instance.
110: */
111: public ResourceReference createDirectoryResource(String name) {
112: ResourceReference newdir = super .createDirectoryResource(name);
113: if ((newdir != null) && isCvsExtensible()) {
114: String names[] = new String[1];
115: names[0] = name;
116: try {
117: getCvsManager().add(names);
118: } catch (CvsException ex) {
119: getServer().errlog(this , ex.getMessage());
120: }
121: }
122: return newdir;
123: }
124:
125: /**
126: * Lookup the next component of this lookup state in here.
127: * @param ls The current lookup state.
128: * @param lr The lookup result under construction.
129: * @exception ProtocolException If an error occurs.
130: * @return A boolean, <strong>true</strong> if lookup has completed,
131: * <strong>false</strong> if it should be continued by the caller.
132: */
133: public boolean lookup(LookupState ls, LookupResult lr)
134: throws ProtocolException {
135: Request request = (Request) ls.getRequest();
136: if ((request != null) && (request.getMethod().equals("PUT"))) {
137: if (ls.hasMoreComponents()) {
138: String name = null;
139: try {
140: name = ls.peekNextComponent();
141: File dir = new File(getDirectory(), name);
142: if (dir.isDirectory()) {
143: CvsDirectory cvs = getCvsManager();
144: if (cvs.getDirectoryStatus(name, false) == CVS.DIR_Q) {
145: String names[] = { name };
146: cvs.add(names);
147: }
148: }
149: } catch (CvsException ex) {
150: String msg = "cvs add \"" + name + "\" failed.";
151: getServer().errlog(this , msg);
152: }
153: }
154: }
155: return super .lookup(ls, lr);
156: }
157:
158: /**
159: * Lookup the resource having the given name in this directory.
160: * if the resource is not present, it will try to fetch it from
161: * the Cvs repository.
162: * @param name The name of the resource.
163: * @return A ResourceReference instance, or <strong>null</strong>.
164: */
165:
166: public ResourceReference lookup(String name) {
167: ResourceReference rr = super .lookup(name);
168: if (rr == null) {
169: // This may be an unchecked out directory:
170: try {
171: CvsDirectory cvs = getCvsManager();
172: if (cvs.getDirectoryStatus(name) == CVS.DIR_NCO)
173: cvs.updateDirectory(name);
174: } catch (CvsException ex) {
175: String msg = "cvs update -d \"" + name + "\" failed.";
176: getServer().errlog(this , msg);
177: return null;
178: }
179: // Checking out the directory succeeded, retry lookup:
180: return super.lookup(name);
181: } else {
182: return rr;
183: }
184: }
185:
186: }
|