001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.introspection;
018
019import java.io.BufferedInputStream;
020import java.io.DataInputStream;
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.FileNotFoundException;
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032
033public class BundleIdReader {
034
035    private static final Log log = LogFactory.getLog(BundleIdReader.class);
036
037    protected final Map<String, Long> ids = new HashMap<String, Long>();
038
039    @SuppressWarnings("unused")
040    private long count = 0;
041
042    public synchronized void load(File file) {
043        DataInputStream in = null;
044        try {
045            in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
046            count = in.readLong();
047            int size = in.readInt();
048            for (int i = 0; i < size; i++) {
049                String key = in.readUTF();
050                long id = in.readLong();
051                ids.put(key, Long.valueOf(id));
052            }
053        } catch (FileNotFoundException e) {
054            // do nothing - this is the first time the runtime is started
055        } catch (IOException e) {
056            log.error("The bundle.ids file is corrupted. Resetting bundle ids.");
057        } finally {
058            if (in != null) {
059                try {
060                    in.close();
061                } catch (IOException e) {
062                }
063            }
064        }
065    }
066
067    public List<String> getBundleNames() {
068        List<String> names = new ArrayList<String>();
069        names.addAll(ids.keySet());
070        return names;
071    }
072
073}