Update of /var/cvs/src/org/mmbase/util
In directory james.mmbase.org:/tmp/cvs-serv10476
Modified Files:
Tag: MMBase-1_8
ResourceLoader.java ApplicationContextReader.java
Log Message:
backported MMB-1598
See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/util
See also: http://www.mmbase.org/jira/browse/MMB-1598
Index: ResourceLoader.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/util/ResourceLoader.java,v
retrieving revision 1.39.2.6
retrieving revision 1.39.2.7
diff -u -b -r1.39.2.6 -r1.39.2.7
--- ResourceLoader.java 23 Jun 2008 12:42:25 -0000 1.39.2.6
+++ ResourceLoader.java 14 Jul 2008 15:22:32 -0000 1.39.2.7
@@ -97,7 +97,7 @@
* <p>For property-files, the java-unicode-escaping is undone on loading, and
applied on saving, so there is no need to think of that.</p>
* @author Michiel Meeuwissen
* @since MMBase-1.8
- * @version $Id: ResourceLoader.java,v 1.39.2.6 2008/06/23 12:42:25 michiel
Exp $
+ * @version $Id: ResourceLoader.java,v 1.39.2.7 2008/07/14 15:22:32 michiel
Exp $
*/
public class ResourceLoader extends ClassLoader {
@@ -334,6 +334,8 @@
log.warn("mmbase.config system property is masked by
mmbase.config servlet context parameter");
}
+ configRoot.roots.add(configRoot.new
ApplicationContextFileURLStreamHandler());
+
if (configPath != null) {
if (servletContext != null) {
// take into account that configpath can start at
webrootdir
@@ -345,6 +347,7 @@
configRoot.roots.add(configRoot.new FileURLStreamHandler(new
File(configPath), true));
}
+
if (servletContext != null) {
String s = servletContext.getRealPath(RESOURCE_ROOT);
if (s != null) {
@@ -475,6 +478,8 @@
Object o = i.next();
if (o instanceof FileURLStreamHandler) {
roots.add(new FileURLStreamHandler((FileURLStreamHandler) o));
+ } else if (o instanceof ApplicationContextFileURLStreamHandler) {
+ roots.add(new ApplicationContextFileURLStreamHandler());
} else if (o instanceof NodeURLStreamHandler) {
roots.add(new NodeURLStreamHandler((NodeURLStreamHandler) o));
} else if (o instanceof ServletResourceURLStreamHandler) {
@@ -911,8 +916,11 @@
Iterator i = roots.iterator();
while (i.hasNext()) {
Object o = i.next();
- if (o instanceof FileURLStreamHandler) {
- result.add(((FileURLStreamHandler) o).getFile(name));
+ if (o instanceof AbstractFileURLStreamHandler) {
+ File f = ((AbstractFileURLStreamHandler) o).getFile(name);
+ if (f != null) {
+ result.add(((AbstractFileURLStreamHandler)
o).getFile(name));
+ }
}
}
return result;
@@ -1090,46 +1098,24 @@
abstract Set getPaths(Set results, Pattern pattern, boolean
recursive, boolean directories);
}
+ //
================================================================================
+ // Files
- protected class FileURLStreamHandler extends PathURLStreamHandler {
- private File fileRoot;
- private boolean writeable;
- FileURLStreamHandler(File root, boolean w) {
- fileRoot = root;
+ protected abstract class AbstractFileURLStreamHandler extends
PathURLStreamHandler {
+ protected final boolean writeable;
+ AbstractFileURLStreamHandler(boolean w) {
writeable = w;
-
- }
- FileURLStreamHandler(FileURLStreamHandler f) {
- fileRoot = f.fileRoot;
- writeable = f.writeable;
}
- public File getFile(String name) {
- if (name != null && name.startsWith("file:")) {
- try {
- return new File(new URI(name)); // hff, how cumbersome, to
translate an URL to a File
- } catch (URISyntaxException use) {
- log.warn(use);
- }
- }
- String fileName = fileRoot + ResourceLoader.this.context.getPath()
+ (name == null ? "" : name);
- if (! File.separator.equals("/")) { // windows compatibility
- fileName = fileName.replace('/', File.separator.charAt(0)); //
er
- }
- return new File(fileName);
- }
- public String getName(URL u) {
- int l = (fileRoot +
ResourceLoader.this.context.getPath()).length();
- String path = u.getPath();
- return l < path.length() ? path.substring(l) : path;
- }
public URLConnection openConnection(String name) {
URL u;
try {
if (name.startsWith("file:")) {
u = new URL(null, name, this);
} else {
- u = new URL(null, "file:" + getFile(name), this);
+ File file = getFile(name);
+ if (file == null) return
NOT_AVAILABLE_URLSTREAM_HANDLER.openConnection(name);
+ u = new URL(null, "file:" + file, this);
}
} catch (MalformedURLException mfue) {
throw new AssertionError(mfue.getMessage());
@@ -1148,16 +1134,17 @@
};
File f = getFile(recursive);
- if (f.isDirectory()) { // should always be true
+ if (f != null && f.isDirectory()) { // should always be true
File [] files = f.listFiles(filter);
if (files == null) return results;
- for (int j = 0; j < files.length; j++) {
- if (files[j].getName().equals("")) continue;
- if (recursive != null && files[j].isDirectory()) {
- getPaths(results, pattern, recursive +
files[j].getName() + "/", directories);
+ for (int i = 0; i < files.length; i++) {
+ File element = files[i];
+ if (element.getName().equals("")) continue;
+ if (recursive != null && element.isDirectory()) {
+ getPaths(results, pattern, recursive +
element.getName() + "/", directories);
}
- if (files[j].canRead() && (directories ==
files[j].isDirectory())) {
- results.add((recursive == null ? "" : recursive) +
files[j].getName());
+ if (element.canRead() && (directories ==
element.isDirectory())) {
+ results.add((recursive == null ? "" : recursive) +
element.getName());
}
}
@@ -1165,6 +1152,41 @@
return results;
}
+ abstract public File getFile(String name);
+ }
+
+ protected class FileURLStreamHandler extends AbstractFileURLStreamHandler
{
+ private final File fileRoot;
+ FileURLStreamHandler(File root, boolean w) {
+ super(w);
+ fileRoot = root;
+
+ }
+ FileURLStreamHandler(FileURLStreamHandler f) {
+ super(f.writeable);
+ fileRoot = f.fileRoot;
+ }
+
+ public File getFile(String name) {
+ if (name != null && name.startsWith("file:")) {
+ try {
+ return new File(new URI(name)); // hff, how cumbersome, to
translate an URL to a File
+ } catch (URISyntaxException use) {
+ log.warn(use);
+ }
+ }
+ String fileName = fileRoot + ResourceLoader.this.context.getPath()
+ (name == null ? "" : name);
+ if (! File.separator.equals("/")) { // windows compatibility
+ fileName = fileName.replace('/', File.separator.charAt(0)); //
er
+ }
+ return new File(fileName);
+ }
+ public String getName(URL u) {
+ int l = (fileRoot +
ResourceLoader.this.context.getPath()).length();
+ String path = u.getPath();
+ return l < path.length() ? path.substring(l) : path;
+ }
+
public String toString() {
return fileRoot.toString();
}
@@ -1180,7 +1202,6 @@
}
-
/**
* A URLConnection for connecting to a File. Of course SUN ships an
implementation as well
* (File.getURL), but Sun's implementation sucks. You can't use it for
writing a file, and
@@ -1280,6 +1301,66 @@
}
+
+ //
================================================================================
+ // ApplicationContext
+
+ protected class ApplicationContextFileURLStreamHandler extends
AbstractFileURLStreamHandler {
+ private Map FILES;
+ ApplicationContextFileURLStreamHandler() {
+ super(true);
+ try {
+ FILES = ApplicationContextReader.getProperties("mmbase-config"
+ ResourceLoader.this.context.getPath());
+ } catch (javax.naming.NameNotFoundException nnfe) {
+ // never mind
+ log.debug(nnfe);
+ FILES = new HashMap();
+ } catch (javax.naming.NamingException ne) {
+ log.error(ne);
+ FILES = new HashMap();
+ }
+ }
+
+ protected File getFileFromString(String s) {
+ if (s == null) return null;
+ if (s.startsWith("file:")) {
+ try {
+ return new File(new URI(s)); // hff, how cumbersome, to
translate an URL to a File
+ } catch (URISyntaxException use) {
+ log.warn("" + s + " : " + use.getMessage() , use);
+ return null;
+ }
+ } else {
+ return new File(s);
+ }
+ }
+
+ public File getFile(final String in) {
+ return getFileFromString((String) FILES.get(in));
+ }
+ public String getName(URL u) {
+ Iterator i = FILES.entrySet().iterator();
+ while(i.hasNext()) {
+ Map.Entry entry = (Map.Entry) i.next();
+ try {
+ File file = getFileFromString((String) entry.getValue());
+ if (file != null) {
+ if (file.toURL().sameFile(u)) {
+ return (String) entry.getKey();
+ }
+ }
+ } catch (MalformedURLException mfue) {
+ }
+ }
+ return null;
+ }
+ public String toString() {
+ return "" + FILES;
+ }
+ }
+
+
+
/**
* URLStreamHandler for NodeConnections.
*/
Index: ApplicationContextReader.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/util/ApplicationContextReader.java,v
retrieving revision 1.2
retrieving revision 1.2.2.1
diff -u -b -r1.2 -r1.2.2.1
--- ApplicationContextReader.java 6 Jul 2006 14:34:22 -0000 1.2
+++ ApplicationContextReader.java 14 Jul 2008 15:22:33 -0000 1.2.2.1
@@ -20,7 +20,7 @@
*
* @author Nico Klasens
* @since MMBase 1.8.1
- * @version $Id: ApplicationContextReader.java,v 1.2 2006/07/06 14:34:22
pierre Exp $
+ * @version $Id: ApplicationContextReader.java,v 1.2.2.1 2008/07/14 15:22:33
michiel Exp $
*/
public class ApplicationContextReader {
@@ -42,7 +42,16 @@
String contextName = element.getName();
String lookupName = env.composeName(contextName, path);
Object value = env.lookup(lookupName);
- properties.put(contextName, value);
+ if (value instanceof Context) {
+ Map subProps = getProperties(path + "/" + contextName);
+ Iterator i = subProps.entrySet().iterator();
+ while (i.hasNext()) {
+ Map.Entry entry = (Map.Entry) i.next();
+ properties.put(contextName + "/" + entry.getKey(),
entry.getValue());
+ }
+ } else {
+ properties.put(contextName, value.toString());
+ }
}
}
return properties;
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs