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

org.coos.messaging.transport.JvmTransport Maven / Gradle / Ivy

/**
 * COOS - Connected Objects Operating System (www.connectedobjects.org).
 *
 * Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 * You may also contact one of the following for additional information:
 * Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
 * Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 */
package org.coos.messaging.transport;

import org.coos.messaging.COOS;
import org.coos.messaging.COOSFactory;
import org.coos.messaging.Channel;
import org.coos.messaging.ChannelServer;
import org.coos.messaging.Message;
import org.coos.messaging.MessageContext;
import org.coos.messaging.Processor;
import org.coos.messaging.ProcessorException;
import org.coos.messaging.Transport;
import org.coos.messaging.impl.DefaultProcessor;

/**
 * The JVM transport is used between COOS instances and Plugins (COOS to COOS and COOS to/from
 * Plugin) residing in the same VM.
 * 
 * @author Knut Eilif Husa, Tellu AS
 */
public class JvmTransport extends DefaultProcessor implements Transport {

	private Channel channel;
	private ChannelServer channelServer;
	private static final String PROPERTY_COOS_INSTANCE_NAME = "COOSInstanceName";
	private static final String PROPERTY_CHANNEL_SERVER_NAME = "ChannelServerName";
	private InternalTransport intr = new InternalTransport();
	private Processor chainedProcessor;
	private boolean running = false;

	/**
	 * Processes the message
	 * 
	 * @param msg
	 *            the message to be processed
	 */
	public void processMessage(Message msg) throws ProcessorException {
		msg.setMessageContext(new MessageContext());
		intr.chainedProcessor.processMessage(msg);
	}

	/**
	 * Sets the Processor that this Processor will call after finished
	 * processing
	 * 
	 * @param chainedProcessor
	 */
	public void setChainedProcessor(Processor chainedProcessor) {
		this.chainedProcessor = chainedProcessor;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public void setChannelServer(ChannelServer channelServer) {
		this.channelServer = channelServer;
	}

	/**
	 * Starts the service
	 * 
	 * @throws Exception
	 *             Exception thrown if starting of service fails
	 */
	public void start() throws Exception {
		if (!running) {
			running = true;
			if (channelServer == null) {
				String coosInstanceName = (String) properties.get(PROPERTY_COOS_INSTANCE_NAME);
				COOS coos;
				if (coosInstanceName != null) {
					coos = COOSFactory.getCOOSInstance(coosInstanceName);
					if (coos == null) {
						throw new NullPointerException("COOS instance: " + coosInstanceName + " is not declared");
					}
				} else {
					coos = COOSFactory.getDefaultCOOSInstance();
					if (coos == null) {
						throw new NullPointerException("No COOS instance: defined in this vm!");
					}
				}
				String channelServerName = (String) properties.get(PROPERTY_CHANNEL_SERVER_NAME);
				if (channelServerName == null) {
					channelServerName = "default";
				}
				channelServer = coos.getChannelServer(channelServerName);
				if (channelServer == null) {
					throw new NullPointerException("ChannelServer: " + channelServerName
							+ " is not declared within COOS instance: " + coosInstanceName);
				}
			}

			intr.start();
			channelServer.initializeChannel(intr);
		}
	}

	/**
	 * Stops the service
	 * 
	 * @throws Exception
	 *             Exception thrown if stopping of service fails
	 */
	public void stop() throws Exception {
		if (running) {
			running = false;
			// channel.disconnect();
			intr.stop();
			channelServer = null;
		}
	}

	@Override
	public Processor copy() {
		JvmTransport transport = (JvmTransport) super.copy();
		transport.setChannel(channel);
		return transport;
	}

	private class InternalTransport extends DefaultProcessor implements Transport {

		private boolean running = false;
		Processor chainedProcessor;
		Channel channel;

		/**
		 * Processes the message
		 * 
		 * @param msg
		 *            the message to be processed
		 */
		public void processMessage(Message msg) throws ProcessorException {
			msg.setMessageContext(new MessageContext());
			JvmTransport.this.chainedProcessor.processMessage(msg);
		}

		/**
		 * Sets the Processor that this Processor will call after finished
		 * processing
		 * 
		 * @param chainedProcessor
		 */
		public void setChainedProcessor(Processor chainedProcessor) {
			this.chainedProcessor = chainedProcessor;
		}

		public void setChannel(Channel channel) {
			this.channel = channel;
		}

		/**
		 * Starts the service
		 * 
		 * @throws Exception
		 *             Exception thrown if starting of service fails
		 */
		public void start() throws Exception {
			if (!running) {
				running = true;
			}
		}

		/**
		 * Stops the service
		 * 
		 * @throws Exception
		 *             Exception thrown if stopping of service fails
		 */
		public void stop() throws Exception {
			if (running) {
				running = false;
				channel.disconnect();
				JvmTransport.this.stop();
			}
		}

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy