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

com.predic8.membrane.core.exchangestore.MemoryExchangeStore Maven / Gradle / Ivy

There is a newer version: 5.6.0
Show newest version
/* Copyright 2009, 2011 predic8 GmbH, www.predic8.com

   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. */

package com.predic8.membrane.core.exchangestore;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.core.exchange.AbstractExchange;
import com.predic8.membrane.core.interceptor.Interceptor.Flow;
import com.predic8.membrane.core.model.IExchangesStoreListener;
import com.predic8.membrane.core.rules.Rule;
import com.predic8.membrane.core.rules.RuleKey;
import com.predic8.membrane.core.rules.StatisticCollector;

/**
 * TODO: thread-safety
 *
 * @description Stores all exchanges in-memory. The Java heap will overflow if this store is used to store too many
 *              Exchanges. Use for Membrane Monitor only.
 */
@MCElement(name="memoryExchangeStore")
public class MemoryExchangeStore extends AbstractExchangeStore {

	private Map> exchangesMap = new HashMap>();

	//for synchronization purposes choose Vector class
	private List totals = new Vector();

	public void snap(AbstractExchange exc, Flow flow) {
		// TODO: [fix me] this is for Membrane Monitor's legacy logic

		if (flow != Flow.REQUEST)
			return;

		if (isKeyInStore(exc)) {
			getKeyList(exc).add(exc);
		} else {
			List list = new Vector();
			list.add(exc);
			exchangesMap.put(exc.getRule().getKey(), list);
		}

		totals.add(exc);

		for (IExchangesStoreListener listener : exchangesStoreListeners) {
			exc.addExchangeStoreListener(listener);
			listener.addExchange(exc.getRule(), exc);
		}
	}

	public void remove(AbstractExchange exc) {
		removeWithoutNotify(exc);

		for (IExchangesStoreListener listener : exchangesStoreListeners) {
			listener.removeExchange(exc);
		}
	}

	public void removeAllExchanges(Rule rule) {
		AbstractExchange[] exchanges = getExchanges(rule.getKey());

		exchangesMap.remove(rule.getKey());
		totals.removeAll(Arrays.asList(exchanges));
		for (IExchangesStoreListener listener : exchangesStoreListeners) {
			listener.removeExchanges(rule, exchanges);
		}
	}

	public AbstractExchange[] getExchanges(RuleKey ruleKey) {
		List exchangesList = exchangesMap.get(ruleKey);
		if (exchangesList == null) {
			return new AbstractExchange[0];
		}
		return exchangesList.toArray(new AbstractExchange[exchangesList.size()]);
	}

	public int getNumberOfExchanges(RuleKey ruleKey) {
		if (!exchangesMap.containsKey(ruleKey)) {
			return 0;
		}

		return exchangesMap.get(ruleKey).size();
	}

	public StatisticCollector getStatistics(RuleKey key) {
		StatisticCollector statistics = new StatisticCollector(false);
		List exchangesList = exchangesMap.get(key);
		if (exchangesList == null || exchangesList.isEmpty())
			return statistics;

		for (int i = 0; i < exchangesList.size(); i++)
			statistics.collectFrom(exchangesList.get(i));

		return statistics;
	}

	public Object[] getAllExchanges() {
		if (totals.isEmpty())
			return null;

		return totals.toArray();
	}


	public List getAllExchangesAsList() {
		return totals;
	}


	public void removeAllExchanges(AbstractExchange[] exchanges) {
		for (AbstractExchange exc : exchanges) {
			removeWithoutNotify(exc);
		}
		for (IExchangesStoreListener listener : exchangesStoreListeners) {
			listener.removeExchanges(exchanges);
		}
	}

	private List getKeyList(AbstractExchange exc) {
		return exchangesMap.get(exc.getRule().getKey());
	}

	private boolean isKeyInStore(AbstractExchange exc) {
		return exchangesMap.containsKey(exc.getRule().getKey());
	}

	private void removeWithoutNotify(AbstractExchange exc) {
		if (!isKeyInStore(exc)) {
			return;
		}

		getKeyList(exc).remove(exc);
		if (getKeyList(exc).isEmpty()) {
			exchangesMap.remove(exc.getRule().getKey());
		}
		totals.remove(exc);
		exc.informExchangeViewerOnRemoval();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy