forked from jenkinsci/unique-id-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IdStore.java
110 lines (94 loc) · 3.23 KB
/
IdStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package org.jenkinsci.plugins.uniqueid;
import hudson.ExtensionPoint;
import jenkins.model.Jenkins;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import javax.annotation.Nullable;
import org.apache.commons.codec.binary.Base64;
/**
* An abstraction to persistently store and retrieve unique id's
* for various Jenkins model objects.
*
* These keys are guaranteed to be unique with a Jenkins
* and immutable across the lifetime of the given object.
*
* Implementations should not store the ID inside any specific item configuration as it is
* common for users top copy items either through the UI or manually and this will cause the
* IDs to become non-unique.
*
*
* @param <T>
*/
public abstract class IdStore<T> implements ExtensionPoint {
private final Class<T> type;
public IdStore (Class<T> forType) {
this.type = forType;
}
/**
* Creates an unique id for the given object.
* Subsequent calls are idempotent.
*
* @param object the object to make the id for.
*/
public abstract void make(T object);
/**
* Get the id for this given object.
* @param object
* @return the id or {@code null} if none assigned.
*/
@Nullable
public abstract String get(T object);
public boolean supports(Class clazz) {
return type.isAssignableFrom(clazz);
}
/**
* Retrieve an {@link IdStore} for the given type
* @param clazz
* @param <C>
* @return the store which supports the type, or null if none
*/
@Nullable
public static <C> IdStore<C> forClass(Class<C> clazz) {
for (IdStore store : Jenkins.getInstance().getExtensionList(IdStore.class)) {
if (store.supports(clazz)) {
return store;
}
}
return null;
}
/**
* Convenience method which makes the id for the given object.
*
* @throws java.lang.IllegalArgumentException if the type is not supported.
*/
public static void makeId(Object object) throws IllegalArgumentException {
IdStore store = forClass(object.getClass());
if (store == null) {
throw new IllegalArgumentException("Unsupported type: " + object.getClass().getName());
} else {
store.make(object);
}
}
/**
* Convenience method which retrieves the id for the given object.
*
* @throws java.lang.IllegalArgumentException if the type is not supported.
*/
public static String getId(Object object) throws IllegalArgumentException {
IdStore store = forClass(object.getClass());
if (store == null) {
throw new IllegalArgumentException("Unsupported type: " + object.getClass().getName());
} else {
return store.get(object);
}
}
/**
* Generates a new unique ID.
* Subclasses do not need to use this to create unique IDs and are free to create IDs by other methods.
* @return a string that should be unique against all jenkins instances.
*/
protected static String generateUniqueID() {
return Base64.encodeBase64String(UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8)).substring(0, 30);
}
}