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.runtime.cache.legacy;
020
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.Iterator;
026import java.util.List;
027import java.util.Map;
028import java.util.Map.Entry;
029
030import lucee.commons.io.res.Resource;
031import lucee.commons.io.res.util.WildCardFilter;
032import lucee.commons.lang.ExceptionUtil;
033import lucee.runtime.converter.JavaConverter;
034
035import org.apache.oro.text.regex.MalformedPatternException;
036
037public class MetaData implements Serializable {
038        
039        private static Map<String,MetaData> instances=new HashMap<String,MetaData>();
040        
041        private HashMap<String,String> data=new HashMap<String,String>();
042        private Resource file;
043        
044        private MetaData(Resource file) {
045                this.file=file;
046                data=new HashMap<String,String>();
047        }
048        
049        public MetaData(Resource file,HashMap<String,String> data) {
050                this.file=file;
051                this.data=data;
052        }
053
054        public static MetaData getInstance(Resource directory) {
055                MetaData instance=instances.get(directory.getAbsolutePath());
056                
057                if(instance==null) {
058                        Resource file = directory.getRealResource("meta");
059                        if(file.exists()){
060                                try {
061                                        instance= new MetaData(file,(HashMap)JavaConverter.deserialize(file));
062                                }
063                                catch (Throwable t) {
064                        ExceptionUtil.rethrowIfNecessary(t);
065                    }
066                        }
067                        if(instance==null) instance=new MetaData(file);
068                        instances.put(directory.getAbsolutePath(), instance);
069                }
070                return instance;
071        }
072        
073        public synchronized void add(String name, String raw) throws IOException {
074                synchronized (data) {
075                        data.put(name, raw);
076                        JavaConverter.serialize(data, file);
077                }
078        }
079        
080        public synchronized List<String> get(String wildcard) throws MalformedPatternException, IOException {
081                synchronized (data) {
082                        List<String> list=new ArrayList<String>();
083                        Iterator<Entry<String, String>> it = data.entrySet().iterator();
084                        WildCardFilter filter=new WildCardFilter( wildcard);
085                        Entry<String, String> entry;
086                        String value;
087                        while(it.hasNext()) {
088                                entry = it.next();
089                                value= entry.getValue();
090                                if(filter.accept(value)){
091                                        list.add(entry.getKey());
092                                        it.remove();
093                                }
094                        }
095                        if(list.size()>0)JavaConverter.serialize(data, file);
096                        return list;
097                }
098        }
099        
100}