[MINOR: add to file hook the ability to be queried about the URL of the file that would be opened.
cadmium@x9c.fr**20080306190438] {
addfile ./src/fr/x9c/cadmium/kernel/BasicFileHook.java
hunk ./src/fr/x9c/cadmium/kernel/BasicFileHook.java 1
+/*
+ * This file is part of Cadmium.
+ * Copyright (C) 2007-2008 Xavier Clerc.
+ *
+ * Cadmium is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Cadmium is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see .
+ */
+
+package fr.x9c.cadmium.kernel;
+
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * This class implements a basic file hook that is used when embedded mode is
+ * activated.
+ *
+ * @author Xavier Clerc
+ * @version 1.0
+ * @since 1.0
+ */
+public final class BasicFileHook implements FileHook {
+
+ /** Class to be used as base for resource loading. */
+ private final Class base;
+
+ /**
+ * Constructs a file hook with given base.
+ * @param base class to be used as base for resource loading
+ * (if null this class with be used as base)
+ */
+ public BasicFileHook(final Class base) {
+ this.base = base;
+ } // end constructor (Class)
+
+ /**
+ * {@inheritDoc}
+ */
+ public InputStream getInputStream(final String file) {
+ return (this.base != null ? base : BasicFileHook.class).getResourceAsStream(file);
+ } // end method 'getInputStream(String)'
+
+ /**
+ * {@inheritDoc}
+ */
+ public URL getURL(final String file) {
+ return (this.base != null ? base : BasicFileHook.class).getResource(file);
+ } // end method 'getURL(String)'
+
+} // end class 'BasicFileHook'
hunk ./src/fr/x9c/cadmium/kernel/FileHook.java 37
- * @return an input stream over the passed file
+ * @return an input stream over the passed file,
+ * null if no such file can be opened
hunk ./src/fr/x9c/cadmium/kernel/FileHook.java 42
+ /**
+ * Tries to get the URL for a file.
+ * @param file file to be opened
+ * @return the URL of the file to be opened by {@link #getInputStream(java.lang.String)},
+ * null if no such file can be opened
+ */
+ URL getURL(final String file);
+
hunk ./src/fr/x9c/cadmium/support/TopLevelApplet.java 27
+import java.net.URL;
hunk ./src/fr/x9c/cadmium/support/TopLevelApplet.java 149
+ /**
+ * Data object modelling a file hook request.
+ */
+ private final class Request {
+
+ /** Whether an url is requested. */
+ private final boolean url;
+
+ /** File name for URL / input stream. */
+ private final String file;
+
+ /**
+ * Constructs a request.
+ * @param u whether an URL is requested
+ * @param f file name for URL / input stream
+ */
+ private Request(final boolean u, final String f) {
+ assert f != null : "null f";
+ this.url = u;
+ this.file = f;
+ } // end construtor (boolean, String)
+
+ } // end inner-class 'Request'
+
+ /**
+ * Data object modelling a file hook response.
+ */
+ private final class Response {
+
+ /** Returned input stream, if any. */
+ private final InputStream inputStream;
+
+ /** Returned url, if any. */
+ private final URL url;
+
+ /**
+ * Constructs a response from input stream,
+ * setting url to null.
+ * @param i input stream to be returned
+ */
+ private Response(final InputStream i) {
+ this.inputStream = i;
+ this.url = null;
+ } // end constructor (InputStream)
+
+ /**
+ * Constructs a response from URL,
+ * setting input stream to null.
+ * @param u URL to be returned
+ */
+ private Response(final URL u) {
+ this.inputStream = null;
+ this.url = u;
+ } // end constructor (URL)
+
+ } // end inner-class 'Response'
+
hunk ./src/fr/x9c/cadmium/support/TopLevelApplet.java 212
- private final BlockingQueue< AtomicReference > parameters =
- new LinkedBlockingQueue< AtomicReference >(1);
+ private final BlockingQueue< AtomicReference > parameters =
+ new LinkedBlockingQueue< AtomicReference >(1);
hunk ./src/fr/x9c/cadmium/support/TopLevelApplet.java 216
- private final BlockingQueue< AtomicReference > results =
- new LinkedBlockingQueue< AtomicReference >(1);
+ private final BlockingQueue< AtomicReference > results =
+ new LinkedBlockingQueue< AtomicReference >(1);
hunk ./src/fr/x9c/cadmium/support/TopLevelApplet.java 226
- final String s = this.parameters.take().get();
- this.results.put(new AtomicReference(getClass().getResourceAsStream(s)));
+ final Request req = this.parameters.take().get();
+ if (req.url) {
+ this.results.put(new AtomicReference(new Response(getClass().getResource(req.file))));
+ } else {
+ this.results.put(new AtomicReference(new Response(getClass().getResourceAsStream(req.file))));
+ } // end if/else
hunk ./src/fr/x9c/cadmium/support/TopLevelApplet.java 243
- this.parameters.put(new AtomicReference(file));
- return this.results.take().get();
+ this.parameters.put(new AtomicReference(new Request(false, file)));
+ return this.results.take().get().inputStream;
+ } catch (final InterruptedException ie) {
+ return null;
+ } // end try/catch
+ } // end method 'getInputStream(String)'
+
+ /**
+ * {@inheritDoc}
+ */
+ public URL getURL(final String file) {
+ try {
+ this.parameters.put(new AtomicReference(new Request(true, file)));
+ return this.results.take().get().url;
}