All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.event.global.GlobalEvent Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * @(#) $Id: GlobalEvent.java 9056 2012-03-01 11:46:47Z dschwarz $
 *
 * 
 * 
 * 
 *
 */
package at.spardat.xma.event.global;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * A GlobalEvent is created and sent by GlobalEventManager. Sent events can than
 * be polled (by GlobalEventManager) and be transmitted to server side or client
 * side registered GlobalEventListener
 *
 * @author s3460
 * @since version_number
 */
public class GlobalEvent implements Serializable {


    /**
     * The event is sent only to a client identified by its session ID.
     * Can be '|' concatinated with the other constants.
     */
    public static final int RECIPIENT_BY_ID = 0;

    /**
     * The event is sent to all servers in the cluster.
     * Can be '|' concatinated with the other constants.
     */
    public static final int RECIPIENT_ALL_SERVERS = 1;

    /**
     * The event is sent to all clients of all servers in the cluster.
     * Can be '|' concatinated with the other constants.
     */
    public static final int RECIPIENT_ALL_CLIENTS = 2;

    /**
     * The event is sent to all clients and servers in the cluster.
     * Concat of RECIPIENT_ALL_SERVERS | RECIPIENT_ALL_CLIENTS.
     */
    public static final int RECIPIENT_ALL_CLIENTS_ALL_SERVERS = (RECIPIENT_ALL_SERVERS | RECIPIENT_ALL_CLIENTS);


    /**
     * Key for member name for toMap() method
     */
    private static final String KEY_NAME ="name_";

    /**
     * Key for member count for toMap() method
     */
    private static final String KEY_COUNT ="count_";

    /**
     * Key for member recipient for toMap() method
     */
    private static final String KEY_SERVERNAME ="servername_";

    /**
     * Key for member recipient for toMap() method
     */
    private static final String KEY_RECIPIENT ="recipient_";

    /**
     * Key for member expiresMilliSec for toMap() method
     */
    private static final String KEY_EXPIRES_MILLISEC ="expiresMilliSec_";

    /**
     * Key for member properties for toMap() method
     */
    private static final String KEY_SESSIONID ="sessionid_";

    /**
     * Key for member properties for toMap() method
     */
    private static final String KEY_PROPERTIES ="properties_";

    /**
     * type of event
     */
    private String name;

    /*
     * unique id
     */
    private int count;

    /**
     * server on which this event was created
     */
    private String serverName;

    /**
     * type of recipient
     */
    private int recipient = 0;

    /**
     * expires millisec after 1970
     */
    private long expiresMilliSec;

    /**
     * key - value pairs
     *
     */
    private Properties properties = new  Properties();

    /**
     * Session ID of recepient (usually creator).
     */
    private String sessionId;

    /**
     * @param name
     *            type of GlobalEvent
     * @param recipient
     *            type of recipient: RECIPIENT_ALL_SERVERS,
     *            RECIPIENT_ALL_CLIENTS, RECIPIENT_ALL_CLIENTS_ALL_SERVERS
     * @param serverName
     *            name of server on which is event is created
     * @param expiresMilliSec
     *            milli seconds after 1970
     *
     */
    GlobalEvent(String name, int recipient, String serverName, long expiresMilliSec,String sessionId) {
        super();
        this.name = name;
        this.recipient = recipient;
        this.serverName = serverName;
        this.expiresMilliSec = expiresMilliSec;
        this.sessionId = sessionId;
    }

    /**
     * creates a new GlobalEvent instance from a map created by toMap().
     * @param map - must be a map created by GlobalEvent.toMap().
     */
    GlobalEvent(Map map) {
        super();
        if(map!=null){
            this.name = (String) map.get(KEY_NAME);
            if(map.get(KEY_COUNT) != null) //bugfix 27.09.2007 - old maps may not have a count at JNDI - @todo this if can be deleted in future versions!
                this.count = ((Integer)map.get(KEY_COUNT)).intValue();
            this.recipient = ((Integer)map.get(KEY_RECIPIENT)).intValue();
            this.serverName = (String) map.get(KEY_SERVERNAME);
            this.expiresMilliSec = ((Long)map.get(KEY_EXPIRES_MILLISEC)).longValue();
            this.sessionId = (String) map.get(KEY_SESSIONID);
            this.properties = (Properties) map.get(KEY_PROPERTIES);
        }else{
            throw new IllegalArgumentException("Failed to use constructor: GlobalEvent(Map map) - map is null ");
        }
    }

    /**
     * Transforms this class to a map object. All members of the class are stored in the map.
     * This method is used to store intances of this classes as map in JNDI trees.
     * @return
     * @since version_number
     * @author s3460
     */
    Map toMap(){
        Map map = new HashMap();
        map.put(KEY_NAME, name);
        map.put(KEY_COUNT, new Integer(count));
        map.put(KEY_RECIPIENT, new Integer(recipient));
        map.put(KEY_SERVERNAME, serverName);
        map.put(KEY_EXPIRES_MILLISEC, new Long(expiresMilliSec));
        map.put(KEY_SESSIONID, sessionId);
        map.put(KEY_PROPERTIES, properties);

        return map;
    }

    /**
     * @return Returns the expires.
     */
    public long getExpiresMilliSec() {
        return expiresMilliSec;
    }

    /**
     * @return Returns the id.
     */
    public int getCount() {
        return count;
    }

    /**
     * @param count
     *            The count to set.
     */
    void setCount(int count) {
        this.count = count;
    }

    /**
     * @return Returns the name.
     */
    public String getName() {
        return name;
    }

    /**
     * @return Returns the properties.
     */
    public Properties getProperties() {
        return properties;
    }

    /**
     *
     * @return unique ID
     * @since version_number
     * @author s3460
     */
    public String getId() {
        return serverName + "_" + count;
    }

    /**
     *
     * @param name
     * @return @since version_number
     * @author s3460
     */
    public String getProperty(String name) {
        return properties != null ? properties.getProperty(name) : null;
    }

    /**
     *
     * @param name
     * @param defaultValue
     * @return the named property transmitted from xma caller
     * @since version_number
     * @author s3460
     */
    public String getProperty(String name, String defaultValue) {
        return properties != null ? properties.getProperty(name, defaultValue) : null;
    }

    /**
     * @param key
     * @param value
     * @return @since version_number
     * @author s3460
     */
    public Object setProperty(String key, String value) {
        return properties.setProperty(key, value);
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return getId() + ": " + properties.toString();
    }

    /**
     * Returns true if this event is valid for the type of recipient of
     * aRecipient.
     *
     * @param aRecipient
     * @return @since version_number
     * @author s3460
     */
    public boolean filterRecipient(int aRecipient) {
        return (recipient & aRecipient) > 0;
    }

    /**
     * is recipient RECIPIENT_ALL_SERVERS
     *
     * @return @since version_number
     * @author s3460
     */
    public boolean isEventForServer() {
        return (recipient & RECIPIENT_ALL_SERVERS) > 0;
    }

    /**
     * is recipient RECIPIENT_ALL_CLIENTS
     *
     * @return @since version_number
     * @author s3460
     */
    public boolean isEventForClient() {
        return (recipient & RECIPIENT_ALL_CLIENTS) > 0;
    }

    /**
     *
     * @param idSession
     * @return true if it is an event for a client with the session id idSession.
     * @since version_number
     * @author s3460
     */
    public boolean isEventForClient(String idSession) {
        return this.sessionId != null ? this.sessionId.equals(idSession) : false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy