/* PointerBlock.java * * Copyright (c) 2002, Benja Fallenstein * * This file is part of Storm. * * Storm is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Storm is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Storm; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein */ package org.nongnu.storm.pointers; import org.nongnu.storm.*; import java.util.*; import com.bitzi.util.*; import java.io.*; import java.security.*; public class PointerIndex { public static final String uri = "http://fenfire.org/2003/05/pointer-index-0.1"; public static final IndexType type = new IndexType(); protected IndexedPool pool; protected IndexedPool.DB db; public BlockId get(PointerId id) throws IOException, GeneralSecurityException { Collector c = db.get(id.toString()).block(); long maxstamp = 0; BlockId result = null; for(Iterator i=c.iterator(); i.hasNext();) { IndexedPool.Mapping m = (IndexedPool.Mapping)i.next(); long timestamp; try { timestamp = Long.parseLong(m.value); } catch(NumberFormatException _) { // malformed entry continue; } if(timestamp > maxstamp) { PointerBlock p; try { p = new PointerBlock(pool.get(m.block)); } catch(Throwable _) { System.out.println("Couldn't use '"+m.block+"' because: "+_); continue; } if(p.getPointer().equals(id) && p.getTimestamp() == timestamp) { result = p.getTarget(); maxstamp = timestamp; } } } if(result == null) throw new FileNotFoundException(); return result; } public void set(PointerId id, BlockId target, PrivateKey key) throws IOException, GeneralSecurityException { // XXX this assumes that the computer clock // is always set correctly: if there is an existing // pointer block with a later timestamp (because // a clock was set wrongly), this may not // actually change the pointer... long timestamp = System.currentTimeMillis(); String data = id.toString() + "\n" + timestamp + "\n" + target.toString(); Signature s = Signature.getInstance("SHA1withDSA"); s.initSign(key); s.update(data.getBytes("US-ASCII")); byte[] signature = s.sign(); BlockOutputStream bos = pool.getBlockOutputStream("text/plain"); String header = PointerBlock.COOKIE + "\n" + Base32.encode(signature); bos.write(header.getBytes("US-ASCII")); bos.write(data.getBytes("US-ASCII")); bos.close(); } protected static class IndexType implements IndexedPool.IndexType { public Set getMappings(Block block) throws IOException { PointerBlock p; try { p = new PointerBlock(block); } catch(Throwable _) { return Collections.EMPTY_SET; } return Collections.singleton (new IndexedPool.Mapping(block.getId(), p.getPointer().toString(), ""+p.getTimestamp())); } public Object createIndex(IndexedPool pool, IndexedPool.DB db) { return new PointerIndex(pool, db); } public String getIndexTypeURI() { return uri; } public String getHumanReadableName() { return ("An index of 0.1 pointer blocks by pointer URN."); } } public PointerIndex(IndexedPool pool, IndexedPool.DB db) { this.pool = pool; this.db = db; } }