01: /**
02: * $RCSfile$
03: * $Revision: $
04: * $Date: $
05: *
06: * Copyright (C) 2007 Jive Software. All rights reserved.
07: *
08: * This software is published under the terms of the GNU Public License (GPL),
09: * a copy of which is included in this distribution.
10: */package org.jivesoftware.util;
11:
12: import org.jivesoftware.util.cache.ClusterTask;
13: import org.jivesoftware.util.cache.ExternalizableUtil;
14:
15: import java.io.IOException;
16: import java.io.ObjectInput;
17: import java.io.ObjectOutput;
18:
19: /**
20: * This task updates or deletes a property in a cluster node's property map.
21: * {@link PropertyEventListener} of each cluster node will be alerted of the event.
22: *
23: * @author Gaston Dombiak
24: */
25: public class PropertyClusterEventTask implements ClusterTask {
26: private Type event;
27: private String key;
28: private String value;
29:
30: public static PropertyClusterEventTask createPutTask(String key,
31: String value) {
32: PropertyClusterEventTask task = new PropertyClusterEventTask();
33: task.event = Type.put;
34: task.key = key;
35: task.value = value;
36: return task;
37: }
38:
39: public static PropertyClusterEventTask createDeteleTask(String key) {
40: PropertyClusterEventTask task = new PropertyClusterEventTask();
41: task.event = Type.deleted;
42: task.key = key;
43: return task;
44: }
45:
46: public Object getResult() {
47: return null;
48: }
49:
50: public void run() {
51: if (Type.put == event) {
52: JiveProperties.getInstance().localPut(key, value);
53: } else if (Type.deleted == event) {
54: JiveProperties.getInstance().localRemove(key);
55: }
56: }
57:
58: public void writeExternal(ObjectOutput out) throws IOException {
59: ExternalizableUtil.getInstance().writeInt(out, event.ordinal());
60: ExternalizableUtil.getInstance().writeSafeUTF(out, key);
61: ExternalizableUtil.getInstance().writeBoolean(out,
62: value != null);
63: if (value != null) {
64: ExternalizableUtil.getInstance().writeSafeUTF(out, value);
65: }
66: }
67:
68: public void readExternal(ObjectInput in) throws IOException,
69: ClassNotFoundException {
70: event = Type.values()[ExternalizableUtil.getInstance().readInt(
71: in)];
72: key = ExternalizableUtil.getInstance().readSafeUTF(in);
73: if (ExternalizableUtil.getInstance().readBoolean(in)) {
74: value = ExternalizableUtil.getInstance().readSafeUTF(in);
75: }
76: }
77:
78: private static enum Type {
79: /**
80: * Event triggered when a system property was added or updated in the system.
81: */
82: put,
83: /**
84: * Event triggered when a system property was deleted from the system.
85: */
86: deleted
87: }
88: }
|