1 | package eu.clarin.cmdi.virtualcollectionregistry.model; |
---|
2 | |
---|
3 | import java.lang.reflect.Constructor; |
---|
4 | import java.net.URI; |
---|
5 | import java.util.Map; |
---|
6 | |
---|
7 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException; |
---|
8 | |
---|
9 | public abstract class PersistentIdentifierProvider { |
---|
10 | public static final String BASE_URI = "pid_provider.base_uri"; |
---|
11 | public static final String PROVIDER_CLASS= "pid_provider.class"; |
---|
12 | |
---|
13 | protected PersistentIdentifierProvider(Map<String, String> config) |
---|
14 | throws VirtualCollectionRegistryException { |
---|
15 | super(); |
---|
16 | } |
---|
17 | |
---|
18 | public abstract PersistentIdentifier createIdentifier(VirtualCollection vc) |
---|
19 | throws VirtualCollectionRegistryException; |
---|
20 | |
---|
21 | public abstract void updateIdentifier(String pid, URI target) |
---|
22 | throws VirtualCollectionRegistryException; |
---|
23 | |
---|
24 | public abstract void deleteIdentifier(String pid) |
---|
25 | throws VirtualCollectionRegistryException; |
---|
26 | |
---|
27 | protected PersistentIdentifier doCreate(VirtualCollection vc, |
---|
28 | PersistentIdentifier.Type type, |
---|
29 | String identifier) { |
---|
30 | return new PersistentIdentifier(vc, type, identifier); |
---|
31 | } |
---|
32 | |
---|
33 | protected static String getConfigParameter(Map<String, String> config, |
---|
34 | String parameter) throws VirtualCollectionRegistryException { |
---|
35 | String value = config.get(parameter); |
---|
36 | if (value == null) { |
---|
37 | throw new VirtualCollectionRegistryException("configuration " |
---|
38 | + "parameter \"" + parameter + "\" is not set"); |
---|
39 | } |
---|
40 | value = value.trim(); |
---|
41 | if (value.isEmpty()) { |
---|
42 | throw new VirtualCollectionRegistryException("configuration " |
---|
43 | + "parameter \"" + parameter + "\" is invalid"); |
---|
44 | } |
---|
45 | return value; |
---|
46 | } |
---|
47 | |
---|
48 | public static PersistentIdentifierProvider |
---|
49 | createProvider(Map<String, String> config) |
---|
50 | throws VirtualCollectionRegistryException { |
---|
51 | /* |
---|
52 | * XXX: instantiating the pid provider should probably done in a |
---|
53 | * different and less complicated way. |
---|
54 | */ |
---|
55 | String clazzName = getConfigParameter(config, PROVIDER_CLASS); |
---|
56 | try { |
---|
57 | Class<?> clazz = Class.forName(clazzName); |
---|
58 | if (PersistentIdentifierProvider.class.isAssignableFrom(clazz)) { |
---|
59 | Constructor<?> c = clazz.getConstructor(Map.class); |
---|
60 | return (PersistentIdentifierProvider) c.newInstance(config); |
---|
61 | } |
---|
62 | } catch (Exception e) { |
---|
63 | throw new VirtualCollectionRegistryException("error initalizing " + |
---|
64 | "persistent identifier provider", e); |
---|
65 | } |
---|
66 | throw new VirtualCollectionRegistryException("invalid persistent " + |
---|
67 | "identifier provider (" + clazzName + ")" ); |
---|
68 | } |
---|
69 | |
---|
70 | } // abstract class PersistentIdentifierProvider |
---|