Author: michiel
Date: 2010-01-22 00:23:46 +0100 (Fri, 22 Jan 2010)
New Revision: 40668
Added:
mmbase/trunk/core/src/main/java/org/mmbase/util/NodeURLStreamHandlerFactory.java
Modified:
mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceLoader.java
mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceWatcher.java
Log:
MMB-1917 Factored out bridge strategy to stand alone class
Added:
mmbase/trunk/core/src/main/java/org/mmbase/util/NodeURLStreamHandlerFactory.java
===================================================================
---
mmbase/trunk/core/src/main/java/org/mmbase/util/NodeURLStreamHandlerFactory.java
(rev 0)
+++
mmbase/trunk/core/src/main/java/org/mmbase/util/NodeURLStreamHandlerFactory.java
2010-01-21 23:23:46 UTC (rev 40668)
@@ -0,0 +1,306 @@
+/*
+
+This software is OSI Certified Open Source Software.
+OSI Certified is a certification mark of the Open Source Initiative.
+
+The license (Mozilla version 1.0) can be read at the MMBase site.
+See http://www.MMBase.org/license
+
+ */
+package org.mmbase.util;
+
+import java.io.*;
+import java.util.*;
+import java.util.regex.*;
+import java.net.*;
+
+// used for resolving in MMBase database
+import org.mmbase.bridge.*;
+import org.mmbase.bridge.util.Queries;
+import org.mmbase.storage.search.implementation.*;
+import org.mmbase.storage.search.*;
+
+import org.mmbase.util.logging.Logger;
+import org.mmbase.util.logging.Logging;
+
+
+/**
+ * @since MMBase-2.0
+ * @author Michiel Meeuwissen
+ */
+class NodeURLStreamHandlerFactory extends
ResourceLoader.URLStreamHandlerFactory {
+
+ private static Logger log =
Logging.getLoggerInstance(NodeURLStreamHandlerFactory.class);
+ // these should perhaps be configurable:
+ public static final String RESOURCENAME_FIELD = "name";
+ public static final String TYPE_FIELD = "type";
+ public static final String FILENAME_FIELD = "filename";
+ public static final String HANDLE_FIELD = "handle";
+ public static final String LASTMODIFIED_FIELD = "lastmodified";
+ public static final String DEFAULT_CONTEXT = "admin";
+
+ /**
+ * Protocol prefix used by URL objects in this class.
+ */
+ public static final URL NODE_URL_CONTEXT;
+ static {
+ URL temp = null;
+ try {
+ temp = new URL("http", "localhost", "/node/");
+ } catch (MalformedURLException mfue) {
+ assert false : mfue;
+ }
+ NODE_URL_CONTEXT = temp;
+ }
+
+
+
+ private static NodeManager resourceNodeManager;
+
+
+ /**
+ * @since MMBase-1.9.2
+ */
+ static NodeManager getResourceBuilder() {
+ if (ResourceLoader.resourceBuilder == null) return null;
+ if (resourceNodeManager == null) {
+ Cloud cloud =
ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null);
+ resourceNodeManager =
cloud.getNodeManager(ResourceLoader.resourceBuilder);
+ }
+ return resourceNodeManager;
+ }
+
+
+
+ public ResourceLoader.PathURLStreamHandler[]
createURLStreamHandler(ResourceLoader root, ResourceLoader.Type type) {
+ return new ResourceLoader.PathURLStreamHandler[] {new
NodeURLStreamHandler(root, type.ordinal())};
+ }
+ /**
+ * URLStreamHandler for NodeConnections.
+ */
+ protected static class NodeURLStreamHandler extends
ResourceLoader.PathURLStreamHandler {
+ private final int type;
+ NodeURLStreamHandler(ResourceLoader parent, int type) {
+ super(parent);
+ this.type = type;
+ }
+ public NodeURLStreamHandler createSubHandler(ResourceLoader parent) {
+ return new NodeURLStreamHandler(parent, type);
+ }
+
+ @Override
+ protected String getName(URL u) {
+ return u.getPath().substring(NODE_URL_CONTEXT.getPath().length());
+ }
+ @Override
+ public NodeConnection openConnection(String name) {
+ URL u;
+ while (name.startsWith("/")) {
+ name = name.substring(1);
+ }
+ try {
+ u = new URL(NODE_URL_CONTEXT, name, this);
+ } catch (MalformedURLException mfue) {
+ throw new AssertionError(mfue.getMessage());
+ }
+ return new NodeConnection(parent, u, name, type);
+ }
+ @Override
+ public Set<String> getPaths(final Set<String> results, final Pattern
pattern, final boolean recursive, final boolean directories) {
+ if (NodeURLStreamHandlerFactory.getResourceBuilder() != null) {
+ try {
+ NodeManager nm =
NodeURLStreamHandlerFactory.getResourceBuilder();
+ NodeQuery query = nm.createQuery();
+ Constraint typeConstraint =
Queries.createConstraint(query, TYPE_FIELD, Queries.getOperator("="), type);
+ Constraint nameConstraint =
Queries.createConstraint(query, RESOURCENAME_FIELD,
Queries.getOperator("LIKE"), parent.getContext().getPath().substring(1) + "%");
+
+ BasicCompositeConstraint constraint = new
BasicCompositeConstraint(CompositeConstraint.LOGICAL_AND);
+
+
constraint.addChild(typeConstraint).addChild(nameConstraint);
+
+ query.setConstraint(constraint);
+ for (Node node : nm.getList(query)) {
+ String url = node.getStringValue(RESOURCENAME_FIELD);
+ String subUrl =
url.substring(parent.getContext().getPath().length() - 1);
+ int pos = subUrl.indexOf('/');
+
+ if (directories) {
+ if (pos < 0) continue; // not a directory
+ do {
+ String u = subUrl.substring(0, pos);
+ if (pattern != null && !
pattern.matcher(u).matches()) {
+ continue;
+ }
+ results.add(u);
+ pos = subUrl.indexOf('/', pos + 1);
+ } while (pos > 0 && recursive);
+ } else {
+ if (pos > 0 && ! recursive) continue;
+ if (pattern != null && !
pattern.matcher(subUrl).matches()) {
+ continue;
+ }
+ results.add(subUrl);
+ }
+
+ }
+ } catch (BridgeException sqe) {
+ log.warn(sqe);
+ }
+ }
+ return results;
+ }
+ @Override
+ public Integer getResourceNode(String name) {
+ Node n = openConnection(name).getResourceNode();
+ return n == null ? null : n.getNumber();
+ }
+
+ @Override
+ public String toString() {
+ return "nodes of type " + type;
+ }
+
+ }
+
+ /**
+ * A URLConnection based on an MMBase node.
+ * @see FileConnection
+ */
+ private static class NodeConnection extends URLConnection {
+ Node node;
+ final String name;
+ final int type;
+ final ResourceLoader parent;
+ NodeConnection(ResourceLoader parent, URL url, String name, int t) {
+ super(url);
+ this.name = name;
+ this.type = t;
+ this.parent = parent;
+ }
+ @Override
+ public void connect() throws IOException {
+ if (NodeURLStreamHandlerFactory.getResourceBuilder() == null) {
+ throw new IOException("No resources builder available.");
+ }
+ connected = true;
+ }
+ /**
+ * Gets the Node associated with this URL if there is one.
+ * @return MMObjectNode or <code>null</code>
+ */
+ public Node getResourceNode() {
+ if (node != null) return node;
+ if (name.equals("")) return null;
+ String realName = (parent.getContext().getPath() +
name).substring(1);
+ if (NodeURLStreamHandlerFactory.getResourceBuilder() != null) {
+ try {
+ NodeManager nm =
NodeURLStreamHandlerFactory.getResourceBuilder();
+
+ NodeQuery query = nm.createQuery();
+ Constraint constraint1 = Queries.createConstraint(query,
RESOURCENAME_FIELD, Queries.getOperator("="), realName);
+ Constraint constraint2 = Queries.createConstraint(query,
TYPE_FIELD, Queries.getOperator("="), type);
+
+ BasicCompositeConstraint constraint = new
BasicCompositeConstraint(CompositeConstraint.LOGICAL_AND);
+ constraint.addChild(constraint1);
+ constraint.addChild(constraint2);
+
+ query.setConstraint(constraint);
+
+ Iterator<Node> i = nm.getList(query).iterator();
+ if (i.hasNext()) {
+ node = i.next();
+ return node;
+ }
+ } catch (BridgeException sqe) {
+ log.warn(sqe);
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public boolean getDoInput() {
+ return getResourceNode() != null;
+ }
+
+ @Override
+ public boolean getDoOutput() {
+ getResourceNode();
+ return
+ (node != null && node.mayWrite()) ||
+ (ResourceLoader.resourceBuilder != null &&
ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class",
null).getNodeManager(ResourceLoader.resourceBuilder).mayCreateNode());
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ getResourceNode();
+ if (node != null) {
+ return node.getInputStreamValue(HANDLE_FIELD);
+ } else {
+ throw new IOException("No such (node) resource for " + name);
+ }
+ }
+ @Override
+ public OutputStream getOutputStream() throws IOException {
+ if (getResourceNode() == null) {
+ if (ResourceLoader.resourceBuilder == null) return null;
+
+ Cloud cloud =
ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null);
+ NodeManager nm =
cloud.getNodeManager(ResourceLoader.resourceBuilder);
+ node = nm.createNode();
+ node.setContext(DEFAULT_CONTEXT);
+ String resourceName = (parent.getContext().getPath() +
name).substring(1);
+ node.setStringValue(RESOURCENAME_FIELD, resourceName);
+ node.setIntValue(TYPE_FIELD, type);
+ log.info("Creating node " + resourceName + " " + name + " " +
type);
+ node.commit();
+ }
+ return new OutputStream() {
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ @Override
+ public void close() throws IOException {
+ byte[] b = bytes.toByteArray();
+ node.setValue(HANDLE_FIELD, b);
+ String mimeType =
URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(b));
+ if (mimeType == null) {
+ URLConnection.guessContentTypeFromName(name);
+ }
+ node.setValue("mimetype", mimeType);
+ node.commit();
+ }
+ @Override
+ public void write(int b) {
+ bytes.write(b);
+ }
+ @Override
+ public void write(byte[] b) throws IOException {
+ if (b == null) {
+ node.delete();
+ node = null;
+ } else {
+ super.write(b);
+ }
+ }
+ };
+ }
+ @Override
+ public long getLastModified() {
+ getResourceNode();
+ if (node != null) {
+ Date lm = node.getDateValue(LASTMODIFIED_FIELD);
+ if (lm != null) {
+ return lm.getTime();
+ }
+ }
+ return -1;
+ }
+
+ @Override
+ public String toString() {
+ return "NodeConnection " + node;
+ }
+
+ }
+
+
+}
\ No newline at end of file
Modified: mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceLoader.java
===================================================================
--- mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceLoader.java
2010-01-21 22:43:44 UTC (rev 40667)
+++ mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceLoader.java
2010-01-21 23:23:46 UTC (rev 40668)
@@ -21,12 +21,6 @@
import javax.servlet.http.HttpServletRequest;
-// used for resolving in MMBase database
-import org.mmbase.bridge.*;
-import org.mmbase.bridge.util.Queries;
-import org.mmbase.storage.search.implementation.*;
-import org.mmbase.storage.search.*;
-
// XML stuff
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
@@ -144,22 +138,8 @@
*/
protected static final String CLASSLOADER_ROOT = "/org/mmbase/config";
- /**
- * Protocol prefix used by URL objects in this class.
- */
- public static final URL NODE_URL_CONTEXT;
- static {
- URL temp = null;
- try {
- temp = new URL("http", "localhost", "/node/");
- } catch (MalformedURLException mfue) {
- assert false : mfue;
- }
- NODE_URL_CONTEXT = temp;
- }
-
/**
* Used when using getResourcePaths for normal class-loaders.
*/
@@ -175,13 +155,6 @@
private static ServletContext servletContext = null;
- // these should perhaps be configurable:
- public static final String RESOURCENAME_FIELD = "name";
- public static final String TYPE_FIELD = "type";
- public static final String FILENAME_FIELD = "filename";
- public static final String HANDLE_FIELD = "handle";
- public static final String LASTMODIFIED_FIELD = "lastmodified";
- public static final String DEFAULT_CONTEXT = "admin";
/**
* The available ResourceLoaders wrapped into an enum, which makes them
available dynamicly
@@ -281,20 +254,7 @@
}
}
- private static NodeManager resourceNodeManager;
- /**
- * @since MMBase-1.9.2
- */
- static NodeManager getResourceBuilder() {
- if (resourceBuilder == null) return null;
- if (resourceNodeManager == null) {
- Cloud cloud =
ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null);
- resourceNodeManager =
cloud.getNodeManager(ResourceLoader.resourceBuilder);
- }
- return resourceNodeManager;
- }
-
/**
* Utility method to return the name part of a resource-name (removed
directory and 'extension').
* Used e.g. when loading builders in MMBase.
@@ -961,11 +921,10 @@
* @return A Node associated with the resource.
* Used by {...@link ResourceWatcher}.
*/
- Node getResourceNode(String name) {
+ Integer getResourceNode(String name) {
for (PathURLStreamHandler o : roots) {
- if (o instanceof NodeURLStreamHandler) {
- return ((NodeConnection)
o.openConnection(name)).getResourceNode();
- }
+ Integer n = o.getResourceNode(name);
+ if (n != null) return n;
}
return null;
}
@@ -1002,14 +961,9 @@
*/
URL shadowed(File f, String name) {
for (PathURLStreamHandler cf : roots) {
- if (cf instanceof NodeURLStreamHandler) {
- URLConnection con = cf.openConnection(name);
- if (con.getDoInput()) {
- return con.getURL();
- }
- } else if (cf instanceof AbstractFileURLStreamHandler) {
- URLConnection con = cf.openConnection(name);
- if (con instanceof FileConnection) { // could be NOT AVAILABLE
+ URLConnection con = cf.openConnection(name);
+ if (con.getDoInput()) {
+ if (cf instanceof AbstractFileURLStreamHandler) {
FileConnection fc = (FileConnection) con;
File file = fc.getFile();
if (file.equals(f)) {
@@ -1023,6 +977,8 @@
}
}
}
+ } else {
+ return con.getURL();
}
}
}
@@ -1152,6 +1108,12 @@
public int compareTo(PathURLStreamHandler o) {
return o.weight - weight;
}
+ /**
+ * @since MBase-2.0
+ */
+ public Integer getResourceNode(String name) {
+ return null;
+ }
}
@@ -1567,243 +1529,7 @@
}
}
-
//
================================================================================
- // Nodes
-
-
- /**
- * @since MMBase-2.0
- */
- protected static class NodeURLStreamHandlerFactory extends
URLStreamHandlerFactory {
- public PathURLStreamHandler[] createURLStreamHandler(ResourceLoader
root, Type type) {
- return new PathURLStreamHandler[] {new
NodeURLStreamHandler(configRoot, type.ordinal())};
- }
- }
- /**
- * URLStreamHandler for NodeConnections.
- */
- protected static class NodeURLStreamHandler extends PathURLStreamHandler {
- private final int type;
- NodeURLStreamHandler(ResourceLoader parent, int type) {
- super(parent);
- this.type = type;
- }
- public NodeURLStreamHandler createSubHandler(ResourceLoader parent) {
- return new NodeURLStreamHandler(parent, type);
- }
-
- @Override
- protected String getName(URL u) {
- return u.getPath().substring(NODE_URL_CONTEXT.getPath().length());
- }
- @Override
- public URLConnection openConnection(String name) {
- URL u;
- while (name.startsWith("/")) {
- name = name.substring(1);
- }
- try {
- u = new URL(NODE_URL_CONTEXT, name, this);
- } catch (MalformedURLException mfue) {
- throw new AssertionError(mfue.getMessage());
- }
- return new NodeConnection(parent, u, name, type);
- }
- @Override
- public Set<String> getPaths(final Set<String> results, final Pattern
pattern, final boolean recursive, final boolean directories) {
- if (ResourceLoader.getResourceBuilder() != null) {
- try {
- NodeManager nm = ResourceLoader.getResourceBuilder();
- NodeQuery query = nm.createQuery();
- Constraint typeConstraint =
Queries.createConstraint(query, TYPE_FIELD, Queries.getOperator("="), type);
- Constraint nameConstraint =
Queries.createConstraint(query, RESOURCENAME_FIELD,
Queries.getOperator("LIKE"), parent.context.getPath().substring(1) + "%");
-
- BasicCompositeConstraint constraint = new
BasicCompositeConstraint(CompositeConstraint.LOGICAL_AND);
-
-
constraint.addChild(typeConstraint).addChild(nameConstraint);
-
- query.setConstraint(constraint);
- for (Node node : nm.getList(query)) {
- String url = node.getStringValue(RESOURCENAME_FIELD);
- String subUrl =
url.substring(parent.context.getPath().length() - 1);
- int pos = subUrl.indexOf('/');
-
- if (directories) {
- if (pos < 0) continue; // not a directory
- do {
- String u = subUrl.substring(0, pos);
- if (pattern != null && !
pattern.matcher(u).matches()) {
- continue;
- }
- results.add(u);
- pos = subUrl.indexOf('/', pos + 1);
- } while (pos > 0 && recursive);
- } else {
- if (pos > 0 && ! recursive) continue;
- if (pattern != null && !
pattern.matcher(subUrl).matches()) {
- continue;
- }
- results.add(subUrl);
- }
-
- }
- } catch (BridgeException sqe) {
- log.warn(sqe);
- }
- }
- return results;
- }
- @Override
- public String toString() {
- return "nodes of type " + type;
- }
-
- }
-
- /**
- * A URLConnection based on an MMBase node.
- * @see FileConnection
- */
- private static class NodeConnection extends URLConnection {
- Node node;
- final String name;
- final int type;
- final ResourceLoader parent;
- NodeConnection(ResourceLoader parent, URL url, String name, int t) {
- super(url);
- this.name = name;
- this.type = t;
- this.parent = parent;
- }
- @Override
- public void connect() throws IOException {
- if (ResourceLoader.getResourceBuilder() == null) {
- throw new IOException("No resources builder available.");
- }
- connected = true;
- }
- /**
- * Gets the Node associated with this URL if there is one.
- * @return MMObjectNode or <code>null</code>
- */
- public Node getResourceNode() {
- if (node != null) return node;
- if (name.equals("")) return null;
- String realName = (parent.context.getPath() + name).substring(1);
- if (ResourceLoader.getResourceBuilder() != null) {
- try {
- NodeManager nm = ResourceLoader.getResourceBuilder();
-
- NodeQuery query = nm.createQuery();
- Constraint constraint1 = Queries.createConstraint(query,
RESOURCENAME_FIELD, Queries.getOperator("="), realName);
- Constraint constraint2 = Queries.createConstraint(query,
TYPE_FIELD, Queries.getOperator("="), type);
-
- BasicCompositeConstraint constraint = new
BasicCompositeConstraint(CompositeConstraint.LOGICAL_AND);
- constraint.addChild(constraint1);
- constraint.addChild(constraint2);
-
- query.setConstraint(constraint);
-
- Iterator<Node> i = nm.getList(query).iterator();
- if (i.hasNext()) {
- node = i.next();
- return node;
- }
- } catch (BridgeException sqe) {
- log.warn(sqe);
- }
- }
- return null;
- }
-
- @Override
- public boolean getDoInput() {
- return getResourceNode() != null;
- }
-
- @Override
- public boolean getDoOutput() {
- getResourceNode();
- return
- (node != null && node.mayWrite()) ||
- (ResourceLoader.resourceBuilder != null &&
ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class",
null).getNodeManager(ResourceLoader.resourceBuilder).mayCreateNode());
- }
-
- @Override
- public InputStream getInputStream() throws IOException {
- getResourceNode();
- if (node != null) {
- return node.getInputStreamValue(HANDLE_FIELD);
- } else {
- throw new IOException("No such (node) resource for " + name);
- }
- }
- @Override
- public OutputStream getOutputStream() throws IOException {
- if (getResourceNode() == null) {
- if (ResourceLoader.resourceBuilder == null) return null;
-
- Cloud cloud =
ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null);
- NodeManager nm = cloud.getNodeManager(resourceBuilder);
- node = nm.createNode();
- node.setContext(DEFAULT_CONTEXT);
- String resourceName = (parent.context.getPath() +
name).substring(1);
- node.setStringValue(RESOURCENAME_FIELD, resourceName);
- node.setIntValue(TYPE_FIELD, type);
- log.info("Creating node " + resourceName + " " + name + " " +
type);
- node.commit();
- }
- return new OutputStream() {
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- @Override
- public void close() throws IOException {
- byte[] b = bytes.toByteArray();
- node.setValue(HANDLE_FIELD, b);
- String mimeType =
URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(b));
- if (mimeType == null) {
- URLConnection.guessContentTypeFromName(name);
- }
- node.setValue("mimetype", mimeType);
- node.commit();
- }
- @Override
- public void write(int b) {
- bytes.write(b);
- }
- @Override
- public void write(byte[] b) throws IOException {
- if (b == null) {
- node.delete();
- node = null;
- } else {
- super.write(b);
- }
- }
- };
- }
- @Override
- public long getLastModified() {
- getResourceNode();
- if (node != null) {
- Date lm = node.getDateValue(LASTMODIFIED_FIELD);
- if (lm != null) {
- return lm.getTime();
- }
- }
- return -1;
- }
-
- @Override
- public String toString() {
- return "NodeConnection " + node;
- }
-
- }
-
-
-
- //
================================================================================
// ServletContext
protected static class ConfigServletResourceURLStreamHandlerFactory
extends URLStreamHandlerFactory {
Modified: mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceWatcher.java
===================================================================
--- mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceWatcher.java
2010-01-21 22:43:44 UTC (rev 40667)
+++ mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceWatcher.java
2010-01-21 23:23:46 UTC (rev 40668)
@@ -209,9 +209,9 @@
* @return Whether a Node as found to map.
*/
protected synchronized boolean mapNodeNumber(String resource) {
- Node node = resourceLoader.getResourceNode(resource);
+ Integer node = resourceLoader.getResourceNode(resource);
if (node != null) {
- nodeNumberToResourceName.put(node.getNumber(), resource);
+ nodeNumberToResourceName.put(node, resource);
return true;
} else {
return false;
@@ -238,9 +238,9 @@
break;
}
default: {
- Node node =
ResourceLoader.getResourceBuilder().getCloud().getNode(number);
+ Node node =
NodeURLStreamHandlerFactory.getResourceBuilder().getCloud().getNode(number);
int contextPrefix =
resourceLoader.getContext().getPath().length() - 1;
- String name =
node.getStringValue(ResourceLoader.RESOURCENAME_FIELD);
+ String name =
node.getStringValue(NodeURLStreamHandlerFactory.RESOURCENAME_FIELD);
if (name.length() > contextPrefix &&
getResources().contains(name.substring(contextPrefix))) {
log.service("Resource " + name + " changed (node added or
changed)");
nodeNumberToResourceName.put(number, name);
@@ -345,7 +345,7 @@
fw.exit();
i.remove();
}
- if (ResourceLoader.getResourceBuilder() != null) {
+ if (NodeURLStreamHandlerFactory.getResourceBuilder() != null) {
EventManager.getInstance().removeEventListener(this);
}
running = false;
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs