~ubuntu-virt/cobbler/ubuntu

« back to all changes in this revision

Viewing changes to cobbler4j/CobblerObject.tmpl

  • Committer: Michael DeHaan
  • Date: 2009-10-08 15:37:00 UTC
  • Revision ID: git-v1:d20cad2dfa09821ba8025e08b8269f3d84f45bea
Moving directories around.  Further work on templating for bindings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright (c) 2009 Red Hat, Inc.
 
3
 *
 
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.
 
10
 *
 
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.
 
14
 */
 
15
 
 
16
package org.cobbler;
 
17
 
 
18
import com.redhat.rhn.common.util.StringUtil;
 
19
 
 
20
import org.apache.commons.lang.StringUtils;
 
21
 
 
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;
 
28
import java.util.Map;
 
29
import java.util.Set;
 
30
 
 
31
 
 
32
/**
 
33
 * Base class has attributes common to 
 
34
 * distros, profiles, system records
 
35
 * @author paji
 
36
 * @version $Rev$
 
37
 */
 
38
public abstract class CobblerObject {
 
39
    
 
40
    protected String handle;
 
41
    protected Map<String, Object> dataMap = new HashMap<String, Object>();
 
42
    protected CobblerConnection client;    
 
43
 
 
44
    /**
 
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..
 
48
     * 
 
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. 
 
53
     */
 
54
 
 
55
    protected static Map<String, Object> lookupDataMapById(CobblerConnection client, 
 
56
                             String id, String findMethod) {
 
57
        if (id == null) {
 
58
            return null;
 
59
        }
 
60
        List<Map<String, Object>> objects = lookupDataMapsByCriteria(client,
 
61
                                                            UID, id, findMethod);
 
62
        if (!objects.isEmpty()) {
 
63
            return objects.get(0);
 
64
        }
 
65
        return null;
 
66
 
 
67
    }
 
68
 
 
69
    /**
 
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
 
76
     */
 
77
 
 
78
    protected static List<Map<String, Object>> lookupDataMapsByCriteria(
 
79
            CobblerConnection client, String critera, String value, String findMethod) {
 
80
        if (value == null) {
 
81
            return null;
 
82
        }
 
83
 
 
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);
 
88
        return objects;
 
89
 
 
90
    }
 
91
    
 
92
 
 
93
    /**
 
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
 
102
     */
 
103
 
 
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()) {
 
109
            return null;
 
110
        }
 
111
        return map;
 
112
    }
 
113
    
 
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);
 
120
    
 
121
    protected String getHandle() {
 
122
        if (isBlank(handle)) {
 
123
            handle = invokeGetHandle();
 
124
        }
 
125
        return handle;
 
126
    }
 
127
    
 
128
    protected void modify(String key, Object value) {
 
129
        invokeModify(key, value);
 
130
        dataMap.put(key, value);
 
131
    }
 
132
    
 
133
    /**
 
134
     * calls save object to complete the commit
 
135
     */
 
136
    public void save() {
 
137
        invokeSave();
 
138
        update();
 
139
    }
 
140
 
 
141
    /**
 
142
     * removes the kickstart object from cobbler.
 
143
     * @return true if sucessfull
 
144
     */
 
145
    public boolean remove() {
 
146
        return invokeRemove();
 
147
    }
 
148
    
 
149
    /**
 
150
     * @return the created (FIXME: note some datatype munging here)
 
151
     */
 
152
    public Date getCreated() {
 
153
        Double time = (Double)dataMap.get(CTIME);
 
154
        return new Date(time.longValue());        
 
155
    }
 
156
   
 
157
    
 
158
 
 
159
    /**
 
160
     * @param createdIn the created to set
 
161
     */
 
162
    public void setCreated(Date createdIn) {
 
163
        modify(CTIME, createdIn.getTime());
 
164
    }
 
165
    
 
166
    /**
 
167
     * @return the modified
 
168
     */
 
169
    public Date getModified() {
 
170
        Double time = (Double)dataMap.get(MTIME);
 
171
        return new Date(time.longValue());
 
172
    }
 
173
    
 
174
    /**
 
175
     * @param modifiedIn the modified to set
 
176
     */
 
177
    public void setModified(Date modifiedIn) {
 
178
        modify(MTIME, modifiedIn.getTime());
 
179
    }
 
180
 
 
181
    /**
 
182
     * @return the depth
 
183
     */
 
184
    public int getDepth() {
 
185
        return (Integer)dataMap.get(DEPTH);
 
186
    }
 
187
    
 
188
    /**
 
189
     * @param depthIn the depth to set
 
190
     */
 
191
    public void setDepth(int depthIn) {
 
192
        modify(DEPTH, depthIn);
 
193
    }
 
194
 
 
195
    /**
 
196
     * @return the kernelOptions
 
197
     */
 
198
    public Map<String, Object> getKernelOptions() {
 
199
        return (Map<String, Object>)dataMap.get(KERNEL_OPTIONS);
 
200
    }
 
201
 
 
202
    /**
 
203
     * gets the kernel options in string form
 
204
     * @return the string
 
205
     */
 
206
    public String getKernelOptionsString() {
 
207
        return convertOptionsMap(getKernelOptions());
 
208
    }
 
209
 
 
210
    /**
 
211
     * gets the kernel post options in string form
 
212
     * @return the string
 
213
     */
 
214
    public String getKernelPostOptionsString() {
 
215
        return convertOptionsMap(getKernelPostOptions());
 
216
    }
 
217
 
 
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 + " ");
 
223
            }
 
224
            else {
 
225
                string.append(key + "=" + map.get(key) + " ");
 
226
            }
 
227
        }
 
228
        return string.toString();
 
229
    }
 
230
 
 
231
    
 
232
    /**
 
233
     * @param kernelOptionsIn the kernelOptions to set
 
234
     */
 
235
    public void setKernelOptions(Map<String, Object> kernelOptionsIn) {
 
236
        modify(SET_KERNEL_OPTIONS, kernelOptionsIn);
 
237
    }
 
238
 
 
239
    /**
 
240
     * @param kernelOptsIn the kernelOptions to set
 
241
     */
 
242
    public void setKernelOptions(String kernelOptsIn) {
 
243
        setKernelOptions(parseKernelOpts(kernelOptsIn));
 
244
    }
 
245
 
 
246
 
 
247
    /**
 
248
     * @param kernelOptsIn the kernelOptions to set
 
249
     */
 
250
    public void setKernelPostOptions(String kernelOptsIn) {
 
251
        setKernelPostOptions(parseKernelOpts(kernelOptsIn));
 
252
    }
 
253
 
 
254
    private Map<String, Object> parseKernelOpts(String kernelOpts) {
 
255
        Map<String, Object> toRet = new HashMap<String, Object>();
 
256
 
 
257
        if (StringUtils.isEmpty(kernelOpts)) {
 
258
            return toRet;
 
259
        }
 
260
 
 
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], "");
 
266
            }
 
267
            else if (split.length == 2) {
 
268
                toRet.put(split[0], split[1]);
 
269
            }
 
270
        }
 
271
        return toRet;
 
272
    }
 
273
 
 
274
    
 
275
    /**
 
276
     * @return the kernelMeta
 
277
     */
 
278
    public Map<String, Object> getKsMeta() {
 
279
        return (Map<String, Object>)dataMap.get(KS_META);
 
280
    }
 
281
 
 
282
    
 
283
    /**
 
284
     * @param kernelMetaIn the kernelMeta to set
 
285
     */
 
286
    public void setKsMeta(Map<String, ? extends Object> kernelMetaIn) {
 
287
        modify(SET_KS_META, kernelMetaIn);
 
288
    }
 
289
 
 
290
    
 
291
    /**
 
292
     * @return the name
 
293
     */
 
294
    public String getName() {
 
295
        return (String)dataMap.get(NAME);
 
296
    }
 
297
 
 
298
    /**
 
299
     * @param nameIn sets the new name
 
300
     */
 
301
    public void setName(String nameIn) {
 
302
        invokeRename(nameIn);
 
303
        dataMap.put(NAME, nameIn);
 
304
        handle = null;
 
305
        handle = getHandle();
 
306
        reload();
 
307
    }
 
308
    
 
309
    
 
310
    /**
 
311
     * @return the kernelPostOptions
 
312
     */
 
313
    public Map<String, Object> getKernelPostOptions() {
 
314
        return (Map<String, Object>)dataMap.get(KERNEL_OPTIONS_POST);
 
315
    }
 
316
 
 
317
    
 
318
    /**
 
319
     * @param kernelPostOptionsIn the kernelPostOptions to set
 
320
     */
 
321
    public void setKernelPostOptions(Map<String, Object> kernelPostOptionsIn) {
 
322
        modify(SET_KERNEL_OPTIONS_POST, kernelPostOptionsIn);
 
323
    }
 
324
    
 
325
    protected void update() {
 
326
        client.invokeTokenMethod("update");            
 
327
    }
 
328
    
 
329
    protected boolean isBlank(String str) {
 
330
        return str == null || str.trim().length() == 0;
 
331
    }
 
332
    
 
333
    /**
 
334
     * {@inheritDoc} 
 
335
     */
 
336
    @Override
 
337
    public String toString() {
 
338
        return "DataMap = " + dataMap;
 
339
    }
 
340
 
 
341
    /**
 
342
     * @param key the red hat activation key
 
343
     */
 
344
    public void setRedHatManagementKey(String key) {
 
345
        modify(REDHAT_KEY, key);
 
346
    }
 
347
 
 
348
    /**
 
349
     * @param keys the red hat activation keys in a set
 
350
     */
 
351
    public void setRedHatManagementKey(Set<String> keys) {
 
352
        modify(REDHAT_KEY, StringUtil.join(",", keys));
 
353
    }
 
354
 
 
355
    /**
 
356
     * get the red hat management key
 
357
     * @return returns the red hat key as a string
 
358
     */
 
359
    public String getRedHatManagementKey() {
 
360
        return (String) dataMap.get(REDHAT_KEY);
 
361
    }
 
362
 
 
363
    /**
 
364
     * get the redhate management key as a Set of keys
 
365
     * @return returns the red hat key as a string
 
366
     */
 
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));
 
372
        return set;
 
373
    }
 
374
 
 
375
    /**
 
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
 
379
     */
 
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);
 
386
    }
 
387
 
 
388
}