001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.introspection;
020
021import java.io.BufferedInputStream;
022import java.io.DataInputStream;
023import java.io.File;
024import java.io.FileInputStream;
025import java.io.FileNotFoundException;
026import java.io.IOException;
027import java.util.ArrayList;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034
035public class BundleIdReader {
036
037    private static final Log log = LogFactory.getLog(BundleIdReader.class);
038
039    protected final Map<String, Long> ids = new HashMap<String, Long>();
040
041    @SuppressWarnings("unused")
042    private long count = 0;
043
044    public synchronized void load(File file) {
045        DataInputStream in = null;
046        try {
047            in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
048            count = in.readLong();
049            int size = in.readInt();
050            for (int i = 0; i < size; i++) {
051                String key = in.readUTF();
052                long id = in.readLong();
053                ids.put(key, Long.valueOf(id));
054            }
055        } catch (FileNotFoundException e) {
056            // do nothing - this is the first time the runtime is started
057        } catch (IOException e) {
058            log.error("The bundle.ids file is corrupted. Resetting bundle ids.");
059        } finally {
060            if (in != null) {
061                try {
062                    in.close();
063                } catch (IOException e) {
064                }
065            }
066        }
067    }
068
069    public List<String> getBundleNames() {
070        List<String> names = new ArrayList<String>();
071        names.addAll(ids.keySet());
072        return names;
073    }
074
075}