2
* Copyright (c) 2009 Red Hat, Inc.
4
* This software is licensed to you under the GNU General Public License,
5
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
6
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
7
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
8
* along with this software; if not, see
9
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11
* Red Hat trademarks are not licensed under GPLv2. No permission is
12
* granted to use or replicate Red Hat trademarks that are incorporated
13
* in this software or its documentation.
18
import com.redhat.rhn.common.util.StringUtil;
20
import org.apache.commons.lang.StringUtils;
22
import java.util.Arrays;
23
import java.util.Collection;
24
import java.util.Date;
25
import java.util.HashMap;
26
import java.util.HashSet;
27
import java.util.List;
33
* Base class has attributes common to
34
* distros, profiles, system records
38
public abstract class CobblerObject {
40
protected String handle;
41
protected Map<String, Object> dataMap = new HashMap<String, Object>();
42
protected CobblerConnection client;
45
* Helper method used by all cobbler objects to
46
* return a version of themselves by UID
47
* @see org.cobbler.Distro.lookupById for example usage..
49
* @param client the Cobbler Connection
50
* @param id the UID of the distro/profile/system record
51
* @param findMethod the find xmlrpc method, eg: find_distro
52
* @return true if the cobbler object was found.
55
protected static Map<String, Object> lookupDataMapById(CobblerConnection client,
56
String id, String findMethod) {
60
List<Map<String, Object>> objects = lookupDataMapsByCriteria(client,
62
if (!objects.isEmpty()) {
63
return objects.get(0);
70
* look up data maps by a certain criteria
71
* @param client the xmlrpc client
72
* @param critera (i.e. uid profile, etc..)
73
* @param value the value of the criteria
74
* @param findMethod the find method to use (find_system, find_profile)
75
* @return List of maps
78
protected static List<Map<String, Object>> lookupDataMapsByCriteria(
79
CobblerConnection client, String critera, String value, String findMethod) {
84
Map<String, String> criteria = new HashMap<String, String>();
85
criteria.put(critera, value);
86
List<Map<String, Object>> objects = (List<Map<String, Object>>)
87
client.invokeTokenMethod(findMethod, criteria);
94
* Helper method used by all cobbler objects to
95
* return a Map of themselves by name.
96
* @see org.cobbler.Distro.lookupByName for example usage..
97
* @param client the Cobbler Connection
98
* @param name the name of the cobbler object
99
* @param lookupMethod the name of the xmlrpc
100
* method to lookup: eg get_profile for profile
101
* @return the Cobbler Object Data Map or null
104
protected static Map <String, Object> lookupDataMapByName(CobblerConnection client,
105
String name, String lookupMethod) {
106
Map <String, Object> map = (Map<String, Object>)client.
107
invokeMethod(lookupMethod, name);
108
if (map == null || map.isEmpty()) {
114
protected abstract void invokeModify(String key, Object value);
115
protected abstract void invokeSave();
116
protected abstract boolean invokeRemove();
117
protected abstract String invokeGetHandle();
118
protected abstract void reload();
119
protected abstract void invokeRename(String newName);
121
protected String getHandle() {
122
if (isBlank(handle)) {
123
handle = invokeGetHandle();
128
protected void modify(String key, Object value) {
129
invokeModify(key, value);
130
dataMap.put(key, value);
134
* calls save object to complete the commit
142
* removes the kickstart object from cobbler.
143
* @return true if sucessfull
145
public boolean remove() {
146
return invokeRemove();
150
* @return the created (FIXME: note some datatype munging here)
152
public Date getCreated() {
153
Double time = (Double)dataMap.get(CTIME);
154
return new Date(time.longValue());
160
* @param createdIn the created to set
162
public void setCreated(Date createdIn) {
163
modify(CTIME, createdIn.getTime());
167
* @return the modified
169
public Date getModified() {
170
Double time = (Double)dataMap.get(MTIME);
171
return new Date(time.longValue());
175
* @param modifiedIn the modified to set
177
public void setModified(Date modifiedIn) {
178
modify(MTIME, modifiedIn.getTime());
184
public int getDepth() {
185
return (Integer)dataMap.get(DEPTH);
189
* @param depthIn the depth to set
191
public void setDepth(int depthIn) {
192
modify(DEPTH, depthIn);
196
* @return the kernelOptions
198
public Map<String, Object> getKernelOptions() {
199
return (Map<String, Object>)dataMap.get(KERNEL_OPTIONS);
203
* gets the kernel options in string form
206
public String getKernelOptionsString() {
207
return convertOptionsMap(getKernelOptions());
211
* gets the kernel post options in string form
214
public String getKernelPostOptionsString() {
215
return convertOptionsMap(getKernelPostOptions());
218
private String convertOptionsMap(Map<String, Object> map) {
219
StringBuilder string = new StringBuilder();
220
for (Object key : map.keySet()) {
221
if (StringUtils.isEmpty((String)map.get(key))) {
222
string.append(key + " ");
225
string.append(key + "=" + map.get(key) + " ");
228
return string.toString();
233
* @param kernelOptionsIn the kernelOptions to set
235
public void setKernelOptions(Map<String, Object> kernelOptionsIn) {
236
modify(SET_KERNEL_OPTIONS, kernelOptionsIn);
240
* @param kernelOptsIn the kernelOptions to set
242
public void setKernelOptions(String kernelOptsIn) {
243
setKernelOptions(parseKernelOpts(kernelOptsIn));
248
* @param kernelOptsIn the kernelOptions to set
250
public void setKernelPostOptions(String kernelOptsIn) {
251
setKernelPostOptions(parseKernelOpts(kernelOptsIn));
254
private Map<String, Object> parseKernelOpts(String kernelOpts) {
255
Map<String, Object> toRet = new HashMap<String, Object>();
257
if (StringUtils.isEmpty(kernelOpts)) {
261
String[] options = StringUtils.split(kernelOpts);
262
for (String option : options) {
263
String[] split = option.split("=");
264
if (split.length == 1) {
265
toRet.put(split[0], "");
267
else if (split.length == 2) {
268
toRet.put(split[0], split[1]);
276
* @return the kernelMeta
278
public Map<String, Object> getKsMeta() {
279
return (Map<String, Object>)dataMap.get(KS_META);
284
* @param kernelMetaIn the kernelMeta to set
286
public void setKsMeta(Map<String, ? extends Object> kernelMetaIn) {
287
modify(SET_KS_META, kernelMetaIn);
294
public String getName() {
295
return (String)dataMap.get(NAME);
299
* @param nameIn sets the new name
301
public void setName(String nameIn) {
302
invokeRename(nameIn);
303
dataMap.put(NAME, nameIn);
305
handle = getHandle();
311
* @return the kernelPostOptions
313
public Map<String, Object> getKernelPostOptions() {
314
return (Map<String, Object>)dataMap.get(KERNEL_OPTIONS_POST);
319
* @param kernelPostOptionsIn the kernelPostOptions to set
321
public void setKernelPostOptions(Map<String, Object> kernelPostOptionsIn) {
322
modify(SET_KERNEL_OPTIONS_POST, kernelPostOptionsIn);
325
protected void update() {
326
client.invokeTokenMethod("update");
329
protected boolean isBlank(String str) {
330
return str == null || str.trim().length() == 0;
337
public String toString() {
338
return "DataMap = " + dataMap;
342
* @param key the red hat activation key
344
public void setRedHatManagementKey(String key) {
345
modify(REDHAT_KEY, key);
349
* @param keys the red hat activation keys in a set
351
public void setRedHatManagementKey(Set<String> keys) {
352
modify(REDHAT_KEY, StringUtil.join(",", keys));
356
* get the red hat management key
357
* @return returns the red hat key as a string
359
public String getRedHatManagementKey() {
360
return (String) dataMap.get(REDHAT_KEY);
364
* get the redhate management key as a Set of keys
365
* @return returns the red hat key as a string
367
public Set<String> getRedHatManagementKeySet() {
368
String keys = StringUtils.defaultString(getRedHatManagementKey());
369
String[] sets = (keys).split(",");
370
Set set = new HashSet();
371
set.addAll(Arrays.asList(sets));
376
* remove the specified keys from the key set and add the specified set
377
* @param keysToRemove list of tokens to remove
378
* @param keysToAdd list of tokens to add
380
public void syncRedHatManagementKeys(Collection<String> keysToRemove,
381
Collection<String> keysToAdd) {
382
Set keySet = getRedHatManagementKeySet();
383
keySet.removeAll(keysToRemove);
384
keySet.addAll(keysToAdd);
385
setRedHatManagementKey(keySet);