001/* 002 * (C) Copyright 2006-2011 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 * Nuxeo - initial API and implementation 018 * $Id$ 019 */ 020 021package org.nuxeo.osgi; 022 023import java.io.BufferedInputStream; 024import java.io.BufferedOutputStream; 025import java.io.DataInputStream; 026import java.io.DataOutputStream; 027import java.io.File; 028import java.io.FileInputStream; 029import java.io.FileNotFoundException; 030import java.io.FileOutputStream; 031import java.io.IOException; 032import java.util.HashMap; 033import java.util.Map; 034 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037 038/** 039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 040 */ 041public class BundleIdGenerator { 042 043 private static final Log log = LogFactory.getLog(BundleIdGenerator.class); 044 045 private final Map<String, Long> ids = new HashMap<String, Long>(); 046 047 private long count = 0; 048 049 public synchronized void load(File file) { 050 DataInputStream in = null; 051 try { 052 in = new DataInputStream(new BufferedInputStream(new FileInputStream(file))); 053 count = in.readLong(); 054 int size = in.readInt(); 055 for (int i = 0; i < size; i++) { 056 String key = in.readUTF(); 057 long id = in.readLong(); 058 ids.put(key, id); 059 } 060 } catch (FileNotFoundException e) { 061 // do nothing - this is the first time the runtime is started 062 } catch (IOException e) { 063 // may be the file is corrupted 064 file.delete(); 065 log.error("The bundle.ids file is corrupted. reseting bundle ids."); 066 } finally { 067 if (in != null) { 068 try { 069 in.close(); 070 } catch (IOException e) { 071 } 072 } 073 } 074 } 075 076 public synchronized void store(File file) throws IOException { 077 DataOutputStream out = null; 078 try { 079 out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); 080 out.writeLong(count); 081 int size = ids.size(); 082 out.writeInt(size); 083 for (Map.Entry<String, Long> entry : ids.entrySet()) { 084 out.writeUTF(entry.getKey()); 085 out.writeLong(entry.getValue()); 086 } 087 } finally { 088 if (out != null) { 089 out.close(); 090 } 091 } 092 } 093 094 public synchronized long getBundleId(String name) { 095 Long id = ids.get(name); 096 if (id == null) { 097 id = count++; 098 ids.put(name, id); 099 } 100 return id; 101 } 102 103 public synchronized long addBundle(String name) { 104 long id = count++; 105 ids.put(name, id); 106 return id; 107 } 108 109 public synchronized boolean contains(String name) { 110 return ids.containsKey(name); 111 } 112 113}