001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.core;
011:
012: import java.util.Set;
013: import java.util.regex.Pattern;
014: import org.mmbase.util.*;
015: import org.mmbase.cache.Cache;
016: import org.mmbase.util.logging.*;
017: import org.mmbase.util.functions.Required;
018:
019: /**
020: * Returns the path to use for TREEPART, TREEFILE, LEAFPART and LEAFFILE.
021: * The system searches in a provided base path for a filename that matches the supplied number/alias of
022: * a node (possibly extended with a version number). See the documentation on the TREEPART SCAN command for more info.
023: *
024: * This class can be overriden to make an even smarter search possible.
025: *
026: * @since MMBase-1.8.5
027: * @version $Id: SmartPathFunction.java,v 1.11 2008/02/03 17:33:57 nklasens Exp $
028: */
029: public class SmartPathFunction {
030: private static final Logger log = Logging
031: .getLoggerInstance(SmartPathFunction.class);
032:
033: protected static Cache<String, String> smartPathCache = new Cache<String, String>(
034: 100) {
035: public String getName() {
036: return "SmartPathCache";
037: }
038:
039: public String getDescription() {
040: return "Caches the result of the 'smartpath' function";
041: }
042: };
043: static {
044: smartPathCache.putCache();
045: }
046:
047: protected final MMObjectBuilder parent;
048: protected String nodeNumber;
049: protected String version;
050: protected String path;
051: protected String documentRoot;
052: protected boolean backwardsCompatible = true;
053:
054: protected ResourceLoader webRoot = ResourceLoader.getWebRoot();
055:
056: public SmartPathFunction(MMObjectBuilder p) {
057: parent = p;
058: }
059:
060: /**
061: * The number or alias of the node to filter on
062: */
063: public void setNodeNumber(String nm) {
064: log.debug("Setting " + nodeNumber);
065: if (nm != null && !nm.equals("")) {
066: nodeNumber = nm;
067: }
068: }
069:
070: public void setNode(org.mmbase.bridge.Node n) {
071: if (nodeNumber == null || "".equals(nodeNumber)) {
072: nodeNumber = "" + n.getNumber();
073: }
074: }
075:
076: /**
077: * The version number (or <code>null</code> if not applicable) to filter on
078: */
079: public void setVersion(String v) {
080: version = v;
081: }
082:
083: /**
084: * the root of the path to search.
085: * @deprecated Use {@link #setLoader(ResourceLoder)}.
086: */
087: public void setRoot(String r) {
088: documentRoot = r;
089: }
090:
091: public void setLoader(ResourceLoader r) {
092: webRoot = r;
093: }
094:
095: public ResourceLoader getLoader() {
096: return webRoot;
097: }
098:
099: /**
100: * The subpath of the path to search
101: */
102: @Required
103: public void setPath(String p) {
104: path = p;
105: }
106:
107: public void setBackwardsCompatible(boolean b) {
108: backwardsCompatible = b;
109: }
110:
111: public boolean getBackwardsCompatible() {
112: return backwardsCompatible;
113: }
114:
115: public String smartKey() {
116: return path + '.' + nodeNumber + '.' + version;
117: }
118:
119: /**
120: * The found path as a <code>String</code>, or <code>null</code> if not found
121: */
122: public final String smartpath() {
123: String result;
124: String key = null;
125: if (smartPathCache.isActive()) {
126: key = smartKey();
127: result = smartPathCache.get(key);
128: if (result != null || smartPathCache.containsKey(key)) {
129: return result;
130: }
131: }
132: if (log.isDebugEnabled()) {
133: log.debug("Determining smartpath for node " + nodeNumber
134: + " " + parent.getTableName());
135: }
136: result = getSmartPath();
137:
138: if (key != null) {
139: smartPathCache.put(key, result);
140: }
141: return result;
142: }
143:
144: /**
145: * The found path as a <code>String</code>, or <code>null</code> if not found
146: */
147: protected String getSmartPath() {
148: log.debug("Determining smartpath for node " + nodeNumber + " "
149: + parent.getTableName());
150: if (backwardsCompatible) {
151: return parent.getSmartPath(documentRoot, path, nodeNumber,
152: version);
153: } else {
154: log.debug("Doing NEW way");
155: ResourceLoader child = webRoot.getChildResourceLoader(path);
156: String node = nodeNumber;
157: if (version != null)
158: node += "\\." + version;
159: Set s = child.getChildContexts(Pattern.compile(".*\\D"
160: + node + "\\D.*"), false);
161: if (s.size() == 0) {
162: return null;
163: } else {
164: return path + s.iterator().next() + "/";
165: }
166: }
167: }
168:
169: }
|