001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.core.event.impl;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.event.Event;
026import org.nuxeo.ecm.core.event.EventContext;
027
028/**
029 * Base class to be used to create new context events.
030 * <p>
031 * This class handles context properties and event creation.
032 *
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public abstract class AbstractEventContext implements EventContext {
036
037    private static final long serialVersionUID = 1L;
038
039    protected static final Object[] EMPTY = new Object[0];
040
041    protected Object[] args;
042
043    protected Map<String, Serializable> properties;
044
045    protected String repositoryName;
046
047    /**
048     * Constructor to be used by derived classes
049     */
050    protected AbstractEventContext() {
051    }
052
053    protected AbstractEventContext(Object... args) {
054        this.args = args == null || (args.length == 1 && args[0] == null) ? EMPTY : args;
055    }
056
057    @Override
058    public Object[] getArguments() {
059        return args;
060    }
061
062    @Override
063    public Map<String, Serializable> getProperties() {
064        if (properties == null) {
065            properties = new HashMap<>();
066        }
067        return properties;
068    }
069
070    @Override
071    public void setProperties(Map<String, Serializable> properties) {
072        this.properties = properties;
073    }
074
075    @Override
076    public Serializable getProperty(String key) {
077        if (properties == null) {
078            properties = new HashMap<>();
079        }
080        return properties.get(key);
081    }
082
083    @Override
084    public boolean hasProperty(String key) {
085        if (properties == null) {
086            properties = new HashMap<>();
087        }
088        return properties.containsKey(key);
089    }
090
091    @Override
092    public void setProperty(String key, Serializable value) {
093        if (properties == null) {
094            properties = new HashMap<>();
095        }
096        properties.put(key, value);
097    }
098
099    @Override
100    public Event newEvent(String name) {
101        return new EventImpl(name, this);
102    }
103
104    @Override
105    public Event newEvent(String name, int flags) {
106        return new EventImpl(name, this, flags);
107    }
108
109    @Override
110    public String getRepositoryName() {
111        return repositoryName;
112    }
113
114    @Override
115    public void setRepositoryName(String repositoryName) {
116        this.repositoryName = repositoryName;
117    }
118
119}