001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.io.res.util;
020
021
022import java.util.Map;
023
024import lucee.commons.collection.LinkedHashMapMaxSize;
025import lucee.commons.io.res.Resource;
026import lucee.runtime.PageSource;
027
028public class ResourceSnippetsMap {
029
030        /* methods that access these Map objects should take care of synchronization */
031        private final Map<String, String> sources;
032        private final Map<String, ResourceSnippet> snippets;
033
034        public ResourceSnippetsMap( int maxSnippets, int maxSources ) {
035
036                sources  = new LinkedHashMapMaxSize<String, String>( maxSources );
037                snippets = new LinkedHashMapMaxSize<String, ResourceSnippet>( maxSnippets );
038        }
039
040        /**
041         * this method accesses the underlying Map(s) and is therefore synchronized
042         *
043         * @param ps
044         * @param startPos
045         * @param endPos
046         * @param charset
047         * @return
048         */
049        public synchronized ResourceSnippet getSnippet( PageSource ps, int startPos, int endPos, String charset ) {
050
051                String keySnp = calcKey( ps, startPos, endPos );
052
053                ResourceSnippet snippet = snippets.get( keySnp );
054
055                if ( snippet == null ) {
056
057                        Resource res = ps.getResource();
058                        String keyRes = calcKey( res );
059                        String src = sources.get( keyRes );
060
061                        if ( src == null ) {
062                                src = ResourceSnippet.getContents( res, charset );
063                                sources.put( keyRes, src );
064                        }
065
066                        snippet = ResourceSnippet.createResourceSnippet( src, startPos, endPos );
067                        snippets.put( keySnp, snippet );
068                }
069
070                return snippet;
071        }
072
073        public static String calcKey( Resource res ) {
074
075                return res.getAbsolutePath() + "@" + res.lastModified();
076        }
077
078        public static String calcKey( PageSource ps, int startPos, int endPos ) {
079
080                return ps.getDisplayPath()   + "@" + ps.getLastAccessTime() + ":" + startPos + "-" + endPos;
081        }
082}