MUCServicePropertyClusterEventTask.java 3.51 KB
Newer Older
1 2 3 4 5 6 7
/**
 * $RCSfile$
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2008 Jive Software. All rights reserved.
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
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
 */

package org.jivesoftware.openfire.muc.cluster;

import org.jivesoftware.util.cache.ClusterTask;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.jivesoftware.openfire.muc.spi.MUCPersistenceManager;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

/**
 * This task updates or deletes a property in a cluster node's muc service property map.
 * {@link org.jivesoftware.openfire.muc.spi.MUCServicePropertyEventListener} of each cluster node will be alerted of the event.
 *
 * @author Daniel Henninger
 */
public class MUCServicePropertyClusterEventTask implements ClusterTask {
    private Type event;
    private String service;
    private String key;
    private String value;

    public static MUCServicePropertyClusterEventTask createPutTask(String service, String key, String value) {
        MUCServicePropertyClusterEventTask task = new MUCServicePropertyClusterEventTask();
        task.event = Type.put;
        task.service = service;
        task.key = key;
        task.value = value;
        return task;
    }

    public static MUCServicePropertyClusterEventTask createDeleteTask(String service, String key) {
        MUCServicePropertyClusterEventTask task = new MUCServicePropertyClusterEventTask();
        task.event = Type.deleted;
        task.service = service;
        task.key = key;
        return task;
    }

    public Object getResult() {
        return null;
    }

    public void run() {
        if (Type.put == event) {
            MUCPersistenceManager.setLocalProperty(service, key, value);
        }
        else if (Type.deleted == event) {
            MUCPersistenceManager.deleteLocalProperty(service, key);
        }
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        ExternalizableUtil.getInstance().writeInt(out, event.ordinal());
        ExternalizableUtil.getInstance().writeSafeUTF(out, service);
        ExternalizableUtil.getInstance().writeSafeUTF(out, key);
        ExternalizableUtil.getInstance().writeBoolean(out, value != null);
        if (value != null) {
            ExternalizableUtil.getInstance().writeSafeUTF(out, value);
        }
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        event = Type.values()[ExternalizableUtil.getInstance().readInt(in)];
        service = ExternalizableUtil.getInstance().readSafeUTF(in);
        key = ExternalizableUtil.getInstance().readSafeUTF(in);
        if (ExternalizableUtil.getInstance().readBoolean(in)) {
            value = ExternalizableUtil.getInstance().readSafeUTF(in);
        }
    }

    private static enum Type {
        /**
         * Event triggered when a muc service property was added or updated in the system.
         */
        put,
        /**
         * Event triggered when a muc service property was deleted from the system.
         */
        deleted
    }
}