MUCRoomTask.java 4.26 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
7
 *
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
 */

package org.jivesoftware.openfire.muc.cluster;

23 24 25 26
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

27
import org.jivesoftware.openfire.XMPPServer;
28
import org.jivesoftware.openfire.cluster.ClusterManager;
29
import org.jivesoftware.openfire.muc.MultiUserChatService;
30
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
31 32
import org.jivesoftware.util.cache.ClusterTask;
import org.jivesoftware.util.cache.ExternalizableUtil;
33 34
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
35 36 37 38 39 40 41 42 43

/**
 * Task related to a room to be executed in a cluster node. This is a base
 * class to specific room tasks. The base class just keeps track of the room
 * related to the task.
 *
 * @author Gaston Dombiak
 */
public abstract class MUCRoomTask implements ClusterTask {
44 45 46
	
	private static final Logger Log = LoggerFactory.getLogger(MUCRoomTask.class);

47
    private boolean originator;
48 49
    private String roomName;
    private String subdomain;
50 51 52 53 54

    protected MUCRoomTask() {
    }

    protected MUCRoomTask(LocalMUCRoom room) {
55 56
        this.roomName = room.getName();
        this.subdomain = room.getMUCService().getServiceName();
57 58 59
    }

    public LocalMUCRoom getRoom() {
60 61 62 63 64 65 66 67
        MultiUserChatService mucService = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(subdomain);
        if (mucService == null) {
            throw new IllegalArgumentException("MUC service not found for subdomain: "+subdomain);
        }
        LocalMUCRoom room = (LocalMUCRoom) mucService.getChatRoom(roomName);
        if (room == null) {
            throw new IllegalArgumentException("Room not found: " + roomName);
        }
68 69 70
        return room;
    }

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    /**
     * Executes the requested task considering that this JVM may still be joining the cluster.
     * This means that events regarding rooms that were not loaded yet will be stored for later
     * processing. Once the JVM is done joining the cluster queued tasks will be processed.
     *
     * @param runnable the task to execute.
     */
    protected void execute(Runnable runnable) {
        // Check if we are joining a cluster
        boolean clusterStarting = ClusterManager.isClusteringStarting();
        try {
            // Check that the room exists
            getRoom();
            // Room was found so now execute the task
            runnable.run();
        }
        catch (IllegalArgumentException e) {
            // Room not found so check if we are still joining the cluster
            if (clusterStarting) {
                // Queue task in case the cluster
                QueuedTasksManager.getInstance().addTask(this);
            }
            else {
                // Task failed since room was not found
95
                Log.error(e.getMessage(), e);
96 97 98 99
            }
        }
    }

100 101 102 103 104 105 106 107 108 109
    public boolean isOriginator() {
        return originator;
    }

    public void setOriginator(boolean originator) {
        this.originator = originator;
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        ExternalizableUtil.getInstance().writeBoolean(out, originator);
110 111
        ExternalizableUtil.getInstance().writeSafeUTF(out, roomName);
        ExternalizableUtil.getInstance().writeSafeUTF(out, subdomain);
112 113 114 115
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        originator = ExternalizableUtil.getInstance().readBoolean(in);
116 117
        roomName = ExternalizableUtil.getInstance().readSafeUTF(in);
        subdomain = ExternalizableUtil.getInstance().readSafeUTF(in);
118 119
    }
}