001: // ControlResource.java
002: // $Id: ControlResource.java,v 1.10 2002/06/20 11:28:34 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.jigsaw.http;
007:
008: import java.util.Enumeration;
009: import java.util.Hashtable;
010: import java.util.NoSuchElementException;
011: import java.util.Properties;
012:
013: import java.io.File;
014: import java.io.FileOutputStream;
015: import java.io.IOException;
016:
017: import java.net.URL;
018:
019: import org.w3c.util.ObservableProperties;
020:
021: import org.w3c.tools.resources.AbstractContainer;
022: import org.w3c.tools.resources.AttributeHolder;
023: import org.w3c.tools.resources.DummyResourceReference;
024: import org.w3c.tools.resources.Resource;
025: import org.w3c.tools.resources.ResourceReference;
026: import org.w3c.tools.resources.ServerInterface;
027:
028: class ControlResourceEnumeration implements Enumeration {
029: private static final String ids[] = { "checkpoint", "save",
030: "restart", "shutdown" };
031: int idx = 0;
032:
033: public boolean hasMoreElements() {
034: return false;
035: }
036:
037: public Object nextElement() {
038: // if ( idx >= ids.length )
039: throw new NoSuchElementException("control resource enumeration");
040: // return ids[idx++];
041: }
042:
043: ControlResourceEnumeration() {
044: this .idx = 0;
045: }
046: }
047:
048: public class ControlResource extends AbstractContainer {
049: protected httpd server = null;
050:
051: protected ResourceReference self = null;
052:
053: public void registerResource(String n, Resource c, Hashtable d) {
054: throw new RuntimeException("static container");
055: }
056:
057: public void delete(String name) {
058: throw new RuntimeException("static container");
059: }
060:
061: public ResourceReference createDefaultResource(String name) {
062: throw new RuntimeException("static container");
063: }
064:
065: protected void saveProperties() {
066: ObservableProperties props = server.getProperties();
067: File propfile = new File(props.getString(httpd.PROPS_P, null));
068: // Did we guessed were the place to save the property file ?
069: if (propfile == null) {
070: throw new RuntimeException(
071: "Unable to save properties: property "
072: + httpd.PROPS_P + " undefined.");
073: } else {
074: try {
075: FileOutputStream fout = new FileOutputStream(propfile);
076: server.getProperties().store(fout, "Jigsaw written");
077: fout.close();
078: } catch (IOException ex) {
079: // FIXME
080: }
081: }
082: server.errlog("Properties " + propfile + " have been saved.");
083: }
084:
085: public ResourceReference lookup(String name) {
086: if (name.equalsIgnoreCase("checkpoint")) {
087: server.startCheckpoint();
088: } else if (name.equalsIgnoreCase("save")) {
089: saveProperties();
090: server.checkpoint();
091: } else if (name.equalsIgnoreCase("restart")) {
092: server.restart();
093: } else if (name.equalsIgnoreCase("stop")) {
094: server.shutdown();
095: }
096: if (self == null)
097: self = new DummyResourceReference(this );
098: return self;
099: }
100:
101: /**
102: * Get the server this resource is served by.
103: * @return The first instance of Jigsaw this resource was attached to.
104: */
105: public ServerInterface getServer() {
106: return server;
107: }
108:
109: private String computeHelpUrl() {
110: try {
111: URL url = new URL(getServer().getDocumentationURL());
112: URL docurl = new URL(url.getProtocol(), url.getHost(), url
113: .getPort(), "/Doc/Overview.html");
114: return docurl.toExternalForm();
115: } catch (Exception ex) {
116: ex.printStackTrace();
117: return null;
118: }
119: }
120:
121: synchronized public Object getValue(int idx, Object def) {
122: if ((idx == ATTR_HELP_URL) && (values[ATTR_HELP_URL] == null))
123: values[ATTR_HELP_URL] = computeHelpUrl();
124: return super .getValue(idx, def);
125: }
126:
127: public Enumeration enumerateResourceIdentifiers(boolean all) {
128: return new ControlResourceEnumeration();
129: }
130:
131: public ControlResource(httpd server) {
132: this.server = server;
133: }
134:
135: }
|