1   /* $Id: GeneratorFactory.java 4417 2006-08-02 01:12:00Z stack-sf $
2    *
3    * Created on July 27th, 2006
4    *
5    * Copyright (C) 2006 Internet Archive.
6    *
7    * This file is part of the Heritrix web crawler (crawler.archive.org).
8    *
9    * Heritrix is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser Public License as published by
11   * the Free Software Foundation; either version 2.1 of the License, or
12   * any later version.
13   *
14   * Heritrix is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser Public License
20   * along with Heritrix; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22   */
23  package org.archive.uid;
24  
25  import java.net.URI;
26  import java.net.URISyntaxException;
27  import java.util.Map;
28  
29  /***
30   * Factory that generates uids.
31   * Singleton.  Default implementation is {@link UUIDGenerator}. To
32   * change, specify alternate implementation of {@link Generator} with
33   * {@link #SYSTEM_PROPERTY_GENERATOR_KEY} system property.
34   * @author stack
35   * @version $Revision: 4417 $ $Date: 2006-08-02 01:12:00 +0000 (Wed, 02 Aug 2006) $
36   */
37  public class GeneratorFactory implements Generator {
38  	public final String SYSTEM_PROPERTY_GENERATOR_KEY =
39  		this.getClass().toString() + ".generator";
40  	private static final String DEFAULT_GENERATOR =
41  		"org.archive.uid.UUIDGenerator";
42  	private static final GeneratorFactory factory = new GeneratorFactory();
43  	private final Generator generator;
44  	
45  	private GeneratorFactory() {
46  		super();
47  		String className = System.getProperty(SYSTEM_PROPERTY_GENERATOR_KEY,
48  			DEFAULT_GENERATOR);
49  		Generator ridg = null;
50  		try {
51  			Class c = Class.forName(className);
52  			ridg = (Generator) c.newInstance();
53  		} catch (Exception e) {
54  			e.printStackTrace();
55  		}
56  		this.generator = ridg;
57  	}
58  	
59  	public URI getRecordID() throws URISyntaxException {
60  		return this.generator.getRecordID();
61  	}
62  	
63  	public URI getQualifiedRecordID(Map<String, String> qualifiers)
64  	throws URISyntaxException {
65  		return this.generator.getQualifiedRecordID(qualifiers);
66  	}
67  	
68  	public URI getQualifiedRecordID(String key, String value)
69  	throws URISyntaxException {
70  		return this.generator.getQualifiedRecordID(key, value);
71  	}
72  	
73  	public URI qualifyRecordID(final URI uri,
74  			final Map<String, String> qualifiers)
75  	throws URISyntaxException {
76  		return this.generator.qualifyRecordID(uri, qualifiers);
77  	}
78  
79  	public static GeneratorFactory getFactory() {
80  		return factory;
81  	}
82  }