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

org.webswing.toolkit.WebswingMessagingApiImpl Maven / Gradle / Ivy

There is a newer version: 20.2.4
Show newest version
package org.webswing.toolkit;

import org.webswing.toolkit.api.WebswingMessagingApi;
import org.webswing.toolkit.api.messaging.WebswingMessage;
import org.webswing.toolkit.api.messaging.WebswingMessageListener;
import org.webswing.toolkit.api.messaging.WebswingTopic;
import org.webswing.toolkit.util.DeamonThreadFactory;
import org.webswing.toolkit.util.Logger;
import org.webswing.toolkit.util.Services;
import org.webswing.toolkit.util.Util;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WebswingMessagingApiImpl implements WebswingMessagingApi {
	private ExecutorService messageProcessor = Executors.newSingleThreadExecutor(DeamonThreadFactory.getInstance("Webswing Messaging API processor"));
	private Map listeners = new ConcurrentHashMap();

	@Override
	public  WebswingTopic getSharedTopic(final Class messageType) {
		return new WebswingTopic() {
			@Override
			public void publish(T message) throws IOException {
				if (message != null && message instanceof Serializable) {
					Services.getConnectionService().messageApiPublish((Serializable) message);
				} else {
					throw new IllegalArgumentException("Publishing failed. Message is not Serializable.");
				}
			}

			@Override
			public void subscribe(WebswingMessageListener listener) {
				listeners.put(listener, messageType);
			}

			@Override
			public void unsubscribe(WebswingMessageListener listener) {
				listeners.remove(listener);
			}
		};
	}

	public boolean hasListenerForClass(String msgtype) {
		ClassLoader cl =Util.getWebToolkit().getSwingClassLoader();
		Thread.currentThread().setContextClassLoader(cl);
		try {
			Class msgclass = cl.loadClass(msgtype);
			for (Class registeredclass : listeners.values()) {
				if (registeredclass.isAssignableFrom(msgclass)) {
					return true;
				}
			}
		} catch (ClassNotFoundException e) {
			Logger.warn("WebswingMessagingApiImpl: Failed to process Message. Message class " + msgtype + " not found.");
		}
		return false;
	}

	public void processMessage(final Serializable object) {
		for (WebswingMessageListener l : listeners.keySet()) {
			final WebswingMessageListener listener = l;
			Class regclass = listeners.get(l);
			if (regclass.isInstance(object)) {
				messageProcessor.submit(new Runnable() {
					@Override
					public void run() {
						listener.onMessage(new WebswingMessage() {
							@Override
							public Object getMessage() {
								return object;
							}
						});
					}
				});
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy