001: /* ResourceLoader.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Aug 30 18:31:26 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.web.util.resource;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.FileNotFoundException;
024: import java.net.URL;
025:
026: import org.zkoss.lang.D;
027: import org.zkoss.util.resource.Loader;
028: import org.zkoss.util.logging.Log;
029:
030: /**
031: * A semi-implemented loader to used with {@link ResourceCaches#get}
032: * to retrieve servlet resources.
033: *
034: * @author tomyeh
035: */
036: abstract public class ResourceLoader implements Loader {
037: private static final Log log = Log.lookup(ResourceLoader.class);
038:
039: protected ResourceLoader() {
040: }
041:
042: /** Parses the specified file and returns the result which
043: * will be stored into the cache ({@link ResourceCaches#get}).
044: *
045: * <p>Deriving must override this method.
046: *
047: * @param extra the extra paramter passed from {@link ResourceCaches#get}.
048: */
049: abstract protected Object parse(String path, File file, Object extra)
050: throws Exception;
051:
052: /** Parses the specified URL and returns the result which
053: * will be stored into the cache ({@link ResourceCaches#get}).
054: *
055: * <p>Deriving must override this method.
056: *
057: * @param extra the extra paramter passed from {@link ResourceCaches#get}.
058: */
059: abstract protected Object parse(String path, URL url, Object extra)
060: throws Exception;
061:
062: public boolean shallCheck(Object src, long expiredMillis) {
063: return expiredMillis > 0;
064: //FUTURE: prolong if src.url's protocol is http, https or ftp
065: }
066:
067: public long getLastModified(Object src) {
068: final ResourceInfo si = (ResourceInfo) src;
069: if (si.url != null) {
070: //Due to round-trip, we don't retrieve last-modified
071: final String protocol = si.url.getProtocol().toLowerCase();
072: if (!"http".equals(protocol) && !"https".equals(protocol)
073: && !"ftp".equals(protocol)) {
074: try {
075: return si.url.openConnection().getLastModified();
076: } catch (IOException ex) {
077: return -1; //reload
078: }
079: }
080: return -1; //reload
081: }
082:
083: return si.file.lastModified();
084: }
085:
086: public Object load(Object src) throws Exception {
087: final ResourceInfo si = (ResourceInfo) src;
088: if (si.url != null)
089: return parse(si.path, si.url, si.extra);
090:
091: if (!si.file.exists()) {
092: if (D.ON && log.debugable())
093: log.debug("Not found: " + si.file);
094: return null; //File not found
095: }
096: if (D.ON && log.debugable())
097: log.debug("Loading " + si.file);
098: try {
099: return parse(si.path, si.file, si.extra);
100: } catch (FileNotFoundException ex) {
101: return null;
102: }
103: }
104: }
|