1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.common.component;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import net.jcip.annotations.Immutable;
32 import org.modeshape.common.CommonI18n;
33 import org.modeshape.common.annotation.Category;
34 import org.modeshape.common.annotation.Description;
35 import org.modeshape.common.annotation.Label;
36 import org.modeshape.common.util.CheckArg;
37 import org.modeshape.common.util.ClassUtil;
38
39
40
41
42 @Immutable
43 public class ComponentConfig implements Comparable<ComponentConfig> {
44
45 @Description( i18n = CommonI18n.class, value = "componentConfigNamePropertyDescription" )
46 @Label( i18n = CommonI18n.class, value = "componentConfigNamePropertyLabel" )
47 @Category( i18n = CommonI18n.class, value = "componentConfigNamePropertyCategory" )
48 private final String name;
49
50 @Description( i18n = CommonI18n.class, value = "componentConfigDescriptionPropertyDescription" )
51 @Label( i18n = CommonI18n.class, value = "componentConfigDescriptionPropertyLabel" )
52 @Category( i18n = CommonI18n.class, value = "componentConfigDescriptionPropertyCategory" )
53 private final String description;
54
55 @Description( i18n = CommonI18n.class, value = "componentConfigClassnamePropertyDescription" )
56 @Label( i18n = CommonI18n.class, value = "componentConfigClassnamePropertyLabel" )
57 @Category( i18n = CommonI18n.class, value = "componentConfigClassnamePropertyCategory" )
58 private final String componentClassname;
59
60
61
62
63 private final List<String> classpath;
64
65 private final Map<String, Object> properties;
66 private final long timestamp;
67
68
69
70
71
72
73
74
75
76
77
78 public ComponentConfig( String name,
79 String description,
80 String classname,
81 String... classpath ) {
82 this(name, description, System.currentTimeMillis(), Collections.<String, Object>emptyMap(), classname, classpath);
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public ComponentConfig( String name,
98 String description,
99 Map<String, Object> properties,
100 String classname,
101 String... classpath ) {
102 this(name, description, System.currentTimeMillis(), properties, classname, classpath);
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public ComponentConfig( String name,
119 String description,
120 long timestamp,
121 Map<String, Object> properties,
122 String classname,
123 String... classpath ) {
124 CheckArg.isNotEmpty(name, "name");
125 this.name = name.trim();
126 this.description = description != null ? description.trim() : "";
127 this.componentClassname = classname;
128 this.classpath = buildList(classpath);
129 this.timestamp = timestamp;
130 this.properties = properties != null ? Collections.unmodifiableMap(new HashMap<String, Object>(properties)) : Collections.<String, Object>emptyMap();
131
132
133 if (!ClassUtil.isFullyQualifiedClassname(classname)) {
134 throw new IllegalArgumentException(CommonI18n.componentClassnameNotValid.text(classname, name));
135 }
136 }
137
138
139 List<String> classpath = null;
140 if (classpathElements != null) {
141 classpath = new ArrayList<String>();
142 for (String classpathElement : classpathElements) {
143 if (!classpath.contains(classpathElement)) classpath.add(classpathElement);
144 }
145 classpath = Collections.unmodifiableList(classpath);
146 } else {
147 classpath = Collections.emptyList();
148 }
149 return classpath;
150 }
151
152
153
154
155
156
157 public String getName() {
158 return this.name;
159 }
160
161
162
163
164
165
166 public String getDescription() {
167 return this.description;
168 }
169
170
171
172
173
174
175 public String getComponentClassname() {
176 return this.componentClassname;
177 }
178
179
180
181
182
183
184 public List<String> getComponentClasspath() {
185 return this.classpath;
186 }
187
188
189
190
191
192
193 public String[] getComponentClasspathArray() {
194 return this.classpath.toArray(new String[this.classpath.size()]);
195 }
196
197
198
199
200
201
202 public long getTimestamp() {
203 return this.timestamp;
204 }
205
206
207
208
209
210
211 public Map<String, Object> getProperties() {
212 return this.properties;
213 }
214
215
216
217
218 public int compareTo( ComponentConfig that ) {
219 if (that == this) return 0;
220 int diff = this.getName().compareToIgnoreCase(that.getName());
221 if (diff != 0) return diff;
222 diff = (int)(this.getTimestamp() - that.getTimestamp());
223 return diff;
224 }
225
226
227
228
229 @Override
230 public int hashCode() {
231 return this.getName().hashCode();
232 }
233
234
235
236
237 @Override
238 public boolean equals( Object obj ) {
239 if (obj == this) return true;
240 if (obj instanceof ComponentConfig) {
241 ComponentConfig that = (ComponentConfig)obj;
242 if (!this.getClass().equals(that.getClass())) return false;
243 return this.getName().equalsIgnoreCase(that.getName());
244 }
245 return false;
246 }
247
248
249
250
251
252
253
254
255
256
257 public boolean hasChanged( ComponentConfig component ) {
258 CheckArg.isNotNull(component, "component");
259 CheckArg.isInstanceOf(component, this.getClass(), "component");
260 if (!this.getName().equalsIgnoreCase(component.getName())) return true;
261 if (!this.getDescription().equals(component.getDescription())) return true;
262 if (!this.getComponentClassname().equals(component.getComponentClassname())) return true;
263 if (!this.getComponentClasspath().equals(component.getComponentClasspath())) return true;
264 return false;
265 }
266
267 }