ingo@219: /* ingo@219: * Copyright (c) 2010, 2011 by Intevation GmbH ingo@219: * ingo@219: * This program is free software under the LGPL (>=v2.1) ingo@219: * Read the file LGPL.txt coming with the software for details ingo@219: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@219: */ ingo@219: package de.intevation.artifactdatabase; ingo@219: felix@362: import org.apache.log4j.Logger; felix@362: felix@353: import java.util.ArrayList; felix@353: import java.util.List; felix@362: import java.util.Map; ingo@219: import java.util.HashMap; ingo@219: ingo@219: import de.intevation.artifacts.ArtifactDatabase; ingo@219: import de.intevation.artifacts.CallContext; ingo@219: import de.intevation.artifacts.CallMeta; felix@353: import de.intevation.artifacts.DataProvider; ingo@219: ingo@219: ingo@219: /** ingo@219: * Abstract class that implements some basic methods of a CallContext. ingo@219: * ingo@219: * @author Sascha L. Teichmann ingo@219: * @author Ingo Weinzierl ingo@219: */ ingo@219: public abstract class AbstractCallContext implements CallContext { ingo@219: felix@362: Logger logger = Logger.getLogger(AbstractCallContext.class); felix@362: ingo@219: /** ingo@219: * The ArtifactDatabase instance. ingo@219: */ ingo@219: protected ArtifactDatabaseImpl database; ingo@219: ingo@219: /** ingo@219: * The action to be performed after the artifacts or collections calls. ingo@219: */ ingo@219: protected int action; ingo@219: ingo@219: /** ingo@219: * The meta information of the concrete call (preferred languages et. al.) ingo@219: */ ingo@219: protected CallMeta callMeta; ingo@219: ingo@219: /** ingo@219: * Map to act like a clipboard when nesting calls like a proxy artifact. ingo@219: */ sascha@366: protected Map customValues; ingo@219: felix@353: /** felix@353: * Map to act like a clipboard when nesting calls like a proxy artifact. felix@353: */ sascha@366: protected Map> dataProviders; felix@353: ingo@219: ingo@219: /** ingo@219: * The default constructor of this abstract CallContext. ingo@219: * ingo@219: * @param action The action. ingo@219: * @param callMeta The CallMeta object. ingo@219: * @param context The global context. ingo@219: */ ingo@219: public AbstractCallContext( ingo@219: ArtifactDatabaseImpl artifactDatabase, ingo@219: int action, sascha@246: CallMeta callMeta sascha@246: ) { ingo@219: this.database = artifactDatabase; ingo@219: this.action = action; ingo@219: this.callMeta = callMeta; sascha@246: sascha@246: database.initCallContext(this); ingo@219: } ingo@219: ingo@219: sascha@246: public void postCall() { sascha@246: database.closeCallContext(this); sascha@246: } ingo@219: ingo@219: public abstract void afterCall(int action); ingo@219: ingo@219: public abstract Long getTimeToLive(); ingo@219: ingo@219: public abstract void afterBackground(int action); ingo@219: ingo@219: ingo@219: public Object globalContext() { sascha@246: return database.context; ingo@219: } ingo@219: ingo@219: ingo@219: public ArtifactDatabase getDatabase() { ingo@219: return database; ingo@219: } ingo@219: ingo@219: ingo@219: public CallMeta getMeta() { ingo@219: return callMeta; ingo@219: } ingo@219: ingo@219: ingo@219: public Object getContextValue(Object key) { ingo@219: return customValues != null ingo@219: ? customValues.get(key) ingo@219: : null; ingo@219: } ingo@219: ingo@219: public Object putContextValue(Object key, Object value) { ingo@219: if (customValues == null) { ingo@219: customValues = new HashMap(); ingo@219: } ingo@219: return customValues.put(key, value); ingo@219: } felix@353: felix@353: /** felix@353: * Get list of DataProviders that registered for given key. felix@353: * @return list (empty list if none found, never null). felix@353: */ felix@353: public List getDataProvider(Object key) { felix@362: if (dataProviders != null) { felix@362: List list = dataProviders.get(key); felix@362: return list != null felix@362: ? list felix@362: : java.util.Collections.emptyList(); felix@362: } felix@362: return java.util.Collections.emptyList(); felix@353: } felix@353: felix@362: felix@353: /** felix@353: * Let a DataProvider register itself with given key. felix@353: * Multiple DataProvider can register under the same key. felix@353: */ felix@353: public Object registerDataProvider(Object key, DataProvider value) { felix@353: List providers = null; felix@353: if (dataProviders == null) { felix@353: dataProviders = new HashMap(); felix@353: providers = new ArrayList(); felix@353: providers.add(value); felix@353: return dataProviders.put(key, providers); felix@353: } felix@353: providers = dataProviders.get(key); felix@353: felix@353: if (providers == null) { felix@353: providers = new ArrayList(); felix@353: } felix@353: providers.add(value); felix@353: return dataProviders.put(key, providers); felix@353: } ingo@219: } ingo@219: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :