Source Code Cross Referenced for CachingInputStreamSource.java in  » Web-Framework » RSF » uk » org » ponder » springutil » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » RSF » uk.org.ponder.springutil 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on 16-Mar-2006
003:         */
004:        package uk.org.ponder.springutil;
005:
006:        import java.io.ByteArrayInputStream;
007:        import java.io.File;
008:        import java.io.InputStream;
009:        import java.util.HashMap;
010:        import java.util.Map;
011:
012:        import org.springframework.core.io.ClassPathResource;
013:        import org.springframework.core.io.Resource;
014:        import org.springframework.core.io.ResourceLoader;
015:
016:        import uk.org.ponder.fileutil.StalenessEntry;
017:        import uk.org.ponder.streamutil.StreamResolver;
018:        import uk.org.ponder.util.UniversalRuntimeException;
019:
020:        /**
021:         * A "caching" source of InputStreams that will only poll the filesystem for
022:         * changes after a specified lag. Currently any non-filesystem resources are
023:         * assumed to be ALWAYS STALE, that is, they will always have their streams
024:         * returned rather than the marker.
025:         * 
026:         * @author Antranig Basman (antranig@caret.cam.ac.uk)
027:         */
028:
029:        public class CachingInputStreamSource implements  StreamResolver {
030:            /**
031:             * If the stream is considered up to date, either through actually being up to
032:             * date or through having been polled within the last
033:             * <code>cacheSeconds</code>, this marker value is returned from
034:             * getInputStream. Do not attempt to use any methods of this object!
035:             */
036:            public static final InputStream UP_TO_DATE = new ByteArrayInputStream(
037:                    new byte[0]);
038:
039:            private int cachesecs;
040:
041:            public static final int ALWAYS_STALE = 0;
042:
043:            public static final long NEVER_STALE_MODTIME = Long.MAX_VALUE;
044:
045:            /**
046:             * Sets the lag after which the filesystem will be checked again for change of
047:             * datestamp. At a value of ALWAYS_STALE (0) the resource will always be
048:             * reloaded.
049:             */
050:
051:            public void setCacheSeconds(int cachesecs) {
052:                this .cachesecs = cachesecs;
053:            }
054:
055:            private ResourceLoader resourceloader;
056:
057:            private StreamResolver baseresolver;
058:
059:            private Map stalenesses = new HashMap();
060:
061:            // The first argument here is typically the ApplicationContext - note that
062:            // it will stubbornly interpret ALL paths as relative to the ServletContext,
063:            // whether they begin with slash or no - @see ServletContextResource
064:            public CachingInputStreamSource(ResourceLoader resourceloader,
065:                    int cachesecs) {
066:                this .resourceloader = resourceloader;
067:                this .cachesecs = cachesecs;
068:                init();
069:            }
070:
071:            public void init() {
072:                baseresolver = new SpringStreamResolver(resourceloader);
073:            }
074:
075:            public StreamResolver getNonCachingResolver() {
076:                return baseresolver;
077:            }
078:
079:            public InputStream openStream(String fullpath) {
080:                StalenessEntry staleness = (StalenessEntry) stalenesses
081:                        .get(fullpath);
082:                boolean isnew = false;
083:                if (staleness == null) {
084:                    isnew = true;
085:                    staleness = new StalenessEntry();
086:                }
087:                long now = System.currentTimeMillis();
088:                boolean isstale = false;
089:
090:                Resource res = null;
091:                try {
092:                    if (staleness.modtime != NEVER_STALE_MODTIME
093:                            && now > staleness.lastchecked + cachesecs * 1000) {
094:                        res = resourceloader.getResource(fullpath);
095:                        if (res == null || !res.exists())
096:                            return null;
097:                        try {
098:                            if (res instanceof  ClassPathResource) {
099:                                staleness.modtime = NEVER_STALE_MODTIME;
100:                            } else {
101:                                // Logger.log.debug("Trying to load from path " + fullpath);
102:                                File f = res.getFile(); // throws IOException
103:                                long modtime = f.lastModified();
104:                                if (modtime > staleness.modtime) {
105:                                    staleness.modtime = modtime;
106:                                    isstale = true;
107:                                }
108:                            }
109:                            if (isnew) {
110:                                stalenesses.put(fullpath, staleness);
111:                            }
112:                        } catch (Exception e) {
113:                            // If it's not a file, it's always stale.
114:                            return res.getInputStream();
115:                        }
116:                    }
117:                    staleness.lastchecked = now;
118:
119:                    return isstale ? res.getInputStream() : UP_TO_DATE;
120:                } catch (Exception e) {
121:                    throw UniversalRuntimeException.accumulate(e,
122:                            "Error opening stream for resource " + fullpath);
123:                }
124:            }
125:
126:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.