1 /*
2 * #%L
3 * Nuiton Utils
4 * %%
5 * Copyright (C) 2004 - 2010 CodeLutin
6 * %%
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Lesser Public License for more details.
16 *
17 * You should have received a copy of the GNU General Lesser Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20 * #L%
21 */
22
23 package org.nuiton.util;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import javax.xml.transform.Source;
29 import javax.xml.transform.stream.StreamSource;
30 import java.net.URL;
31
32 /**
33 * FasterCachedResourceResolver is a URIResolver using a optimized search time algorithm.
34 * <p>
35 * For a given href, will try to search the exact path in classp-ath in the exact directory META-INF/cache.
36 * </p>
37 * The fact of using META-INF directory is optimized (special jvm directory).
38 *
39 * The other fact of using an exact directory makes also decrease dramaticly time search.
40 *
41 * Finally, we spend ten times less time in searching; moreover using the path of
42 * searched resource resolve the probleme of name colision and this is also a good thing...
43 *
44 * Exemple :
45 * <pre>
46 * href = http://foo/bar/file.txt
47 * search resource : /META-INF/cache/foo/bar/file.txt
48 * </pre>
49 *
50 * @author chorlet
51 */
52 public class FasterCachedResourceResolver extends ResourceResolver {
53
54 /** log. */
55 private static final Log log = LogFactory.getLog(FasterCachedResourceResolver.class);
56
57 protected boolean offline;
58
59 public FasterCachedResourceResolver() {
60 this(null);
61 }
62
63 public FasterCachedResourceResolver(String base) {
64 super(base);
65 }
66
67 public void setOffline(boolean offline) {
68 this.offline = offline;
69 }
70
71 @Override
72 protected Source findHrefSource(String path) {
73 long t0 = System.nanoTime();
74 String resource = "META-INF/cache/" + path;
75
76 if (verbose) {
77 log.info("will discover " + resource);
78 }
79
80 URL url = cl.getResource(resource);
81
82 Source source = null;
83
84 if (url != null) {
85 if (verbose) {
86 log.info(url.toString());
87 }
88 source = new StreamSource(url.toString());
89 }
90 if (source == null) {
91 if (offline) {
92 throw new IllegalStateException("offline - could not find extact resource at location " + resource);
93 }
94 log.warn("could not find extact resource at location " + resource);
95 }
96 if (verbose) {
97 log.info("resolved in " + StringUtil.convertTime(System.nanoTime() - t0));
98 }
99 return source;
100 }
101 }