001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.core.event.impl;
013
014import java.io.Serializable;
015import java.util.HashMap;
016import java.util.Map;
017
018import org.nuxeo.ecm.core.event.Event;
019import org.nuxeo.ecm.core.event.EventContext;
020
021/**
022 * Base class to be used to create new context events.
023 * <p>
024 * This class handles context properties and event creation.
025 *
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028public abstract class AbstractEventContext implements EventContext {
029
030    private static final long serialVersionUID = 1L;
031
032    protected static final Object[] EMPTY = new Object[0];
033
034    protected Object[] args;
035
036    protected Map<String, Serializable> properties;
037
038    protected String repositoryName;
039
040    /**
041     * Constructor to be used by derived classes
042     */
043    protected AbstractEventContext() {
044    }
045
046    protected AbstractEventContext(Object... args) {
047        this.args = args == null || (args.length == 1 && args[0] == null) ? EMPTY : args;
048    }
049
050    @Override
051    public Object[] getArguments() {
052        return args;
053    }
054
055    @Override
056    public Map<String, Serializable> getProperties() {
057        if (properties == null) {
058            properties = new HashMap<String, Serializable>();
059        }
060        return properties;
061    }
062
063    @Override
064    public void setProperties(Map<String, Serializable> properties) {
065        this.properties = properties;
066    }
067
068    @Override
069    public Serializable getProperty(String key) {
070        if (properties == null) {
071            properties = new HashMap<String, Serializable>();
072        }
073        return properties.get(key);
074    }
075
076    @Override
077    public boolean hasProperty(String key) {
078        if (properties == null) {
079            properties = new HashMap<String, Serializable>();
080        }
081        return properties.containsKey(key);
082    }
083
084    @Override
085    public void setProperty(String key, Serializable value) {
086        if (properties == null) {
087            properties = new HashMap<String, Serializable>();
088        }
089        properties.put(key, value);
090    }
091
092    @Override
093    public Event newEvent(String name) {
094        return new EventImpl(name, this);
095    }
096
097    @Override
098    public Event newEvent(String name, int flags) {
099        return new EventImpl(name, this, flags);
100    }
101
102    @Override
103    public String getRepositoryName() {
104        return repositoryName;
105    }
106
107    @Override
108    public void setRepositoryName(String repositoryName) {
109        this.repositoryName = repositoryName;
110    }
111
112}