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

org.coos.messaging.PluginParser 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;

import java.io.DataInputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Random;
import java.util.Stack;
import java.util.Vector;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.coos.messaging.COContainer;
import org.coos.messaging.Channel;
import org.coos.messaging.Endpoint;
import org.coos.messaging.Plugin;
import org.coos.messaging.Processor;
import org.coos.messaging.Transport;
import org.coos.messaging.util.URIHelper;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.midp.io.Properties;

public class PluginParser extends DefaultHandler {

	private Vector plugins = new Vector();
	private Vector channels = new Vector();
	private Vector transports = new Vector();
	private Vector processors = new Vector();

	private Stack tagStack = new Stack();

	// Tags
	public static final String APPLICATION = "application";

	public static final String PLUGIN = "plugin";
	public static final String CHANNEL = "channel";
	public static final String TRANSPORT = "transport";

	public static final String PROPERTY = "property";

	public static final String INBOUND = "inBound";
	public static final String OUTBOUND = "outBound";
	public static final String FILTER = "filter";

	public static final String PROCESSOR = "processor";

	public static final String JVM_TRANSPORT_CLASS = "no.tellu.messaging.cldc.transport.TCPTransport";

	PluginBundle pb;
	ChannelBundle ch;
	TransportBundle tb;
	ProcessorBundle prb;

	Hashtable ht_transports;
	Hashtable ht_channels;
	Hashtable ht_plugins;
	Hashtable ht_processors;
	Hashtable ht_shared_processors;

	boolean inbound = false;

	COContainer cl;

	public PluginParser(COContainer cl) {
		this.cl = cl;
	}

	public void startDocument() throws SAXException {
	}

	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (PLUGIN.equals(qName)) {
			pb = new PluginBundle(attributes.getValue("name"), attributes.getValue("channel"), attributes
					.getValue("class"), new Hashtable());
		} else if (TRANSPORT.equals(qName)) {
			tb = new TransportBundle(attributes.getValue("name"), attributes.getValue("class"), new Hashtable());
		} else if (PROPERTY.equals(qName)) {
			if (pb != null) {
				pb.properties.put(attributes.getValue("name"), attributes.getValue("value"));
			} else if (tb != null) {
				tb.properties.put(attributes.getValue("name"), attributes.getValue("value"));
			} else if (prb != null) {
				prb.properties.put(attributes.getValue("name"), attributes.getValue("value"));
			}
		} else if (PROCESSOR.equals(qName)) {
			prb = new ProcessorBundle(attributes.getValue("name"), attributes.getValue("class"), attributes
					.getValue("shared"), new Hashtable());
		} else if (INBOUND.equals(qName)) {
			inbound = true;
		} else if (FILTER.equals(qName)) {
			if (inbound)
				ch.inBound.addElement(new String(attributes.getValue("processor")));
			else
				ch.outBound.addElement(new String(attributes.getValue("processor")));
		} else if (CHANNEL.equals(qName)) {
			ch = new ChannelBundle(attributes.getValue("name"), attributes.getValue("class"), attributes
					.getValue("segment"), attributes.getValue("transport"), new Hashtable());
		}
		tagStack.push(qName);

	}

	public void characters(char[] ch, int start, int length) throws SAXException {
		// String chars = new String(ch, start, length).trim();

		/*
		 * if (chars.length() > 0) { String qName =
		 * (String)phones.lastElement(); if(qName.equals("name")){
		 * currentPhone.setName(chars); } else if(qName.equals("colour")){
		 * currentPhone.setColour(chars); } }
		 */
	}

	public void endElement(String uri, String localName, String qName) throws SAXException {

		if (PLUGIN.equals(qName)) {
			plugins.addElement(new PluginBundle(pb.name, pb.channel, pb.className, pb.properties));
			pb = null;
		} else if (CHANNEL.equals(qName)) {
			channels.addElement(new ChannelBundle(ch.name, ch.className, ch.segment, ch.transport, ch.properties));
			ch = null;
		} else if (TRANSPORT.equals(qName)) {
			transports.addElement(new TransportBundle(tb.name, tb.className, tb.properties));
			tb = null;
		} else if (INBOUND.equals(qName)) {
			inbound = false;
		} else if (PROCESSOR.equals(qName)) {
			processors.addElement(new ProcessorBundle(prb.name, prb.className, prb.shared, prb.properties));
			prb = null;
		}
		tagStack.pop();
	}

	public void endDocument() throws SAXException {
		/*
		 * StringBuffer result = new StringBuffer(); for (int i = 0; i <
		 * phones.size(); i++){ Phone currentPhone = (Phone)phones.elementAt(i);
		 * result.append(currentPhone.getName() + " is available in " +
		 * currentPhone.getColour() + "\n"); }
		 */
		instanciatePlugins();
	}

	public void instanciatePlugins() {
		try {
			ht_processors = new Hashtable();

			for (int i = 0; i < processors.size(); i++) {
				prb = (ProcessorBundle) processors.elementAt(i);

				String name = prb.name;

				ht_processors.put(name, prb.className);

				Processor processor = instantiateProcessor(prb, cl);

				if (processor.isShared()) {
					if (ht_shared_processors == null) {
						ht_shared_processors = new Hashtable();
					}
					ht_shared_processors.put(name, prb);
				}
			}

			// create the plugins
			ht_transports = new Hashtable();
			for (int i = 0; i < transports.size(); i++) {
				tb = (TransportBundle) transports.elementAt(i);

				// TransportType transportType = model.getTransportArray()[i];
				// String name = transportType.getName();
				// String className = transportType.getClass1();
				// Map props = new HashMap();
				/*
				 * for (int j = 0; j < transportType.getPropertyArray().length;
				 * j++) { PropertyType propertyType =
				 * transportType.getPropertyArray()[j];
				 * props.put(propertyType.getName(), propertyType.getValue()); }
				 */
				String className = tb.className;
				String name = tb.name;

				Class transportClass = Class.forName(className);
				Transport transport = (Transport) transportClass.newInstance();

				transport.setProperties(tb.properties);
				ht_transports.put(name, transport);
			}

			ht_channels = new Hashtable();
			for (int i = 0; i < channels.size(); i++) {
				ch = (ChannelBundle) channels.elementAt(i);
				String className = ch.className;
				String name = ch.name;
				Class channelClass = Class.forName(className);
				Channel channel = (Channel) channelClass.newInstance();

				String transportType = ch.transport;

				if (transportType != null) {
					boolean found = false;
					for (Enumeration e = ht_transports.keys(); e.hasMoreElements();) {
						if (e.nextElement().equals(transportType)) {
							found = true;
							break;
						}
					}
					if (!found) {
						throw new Exception("Transport " + transportType + " is not declared.");
					}

					channel.setTransport((Transport) ((Transport) ht_transports.get(transportType)).copy());
					channel.setInit(true); // The Plugin shall always initiate a
											// connection
				} else {
					Class transportClass = Class.forName(JVM_TRANSPORT_CLASS);
					Transport transport = (Transport) transportClass.newInstance();
					channel.setTransport(transport);
					channel.setInit(true); // The Plugin shall always initiate a
											// connection
				}

				String segment = ch.segment;// pluginType.getSegment();
				if (segment == null) {
					segment = "";
				}
				channel.setSegment(segment);

				Vector outBound = ch.outBound;
				if (outBound != null) {
					for (int j = 0; j < outBound.size(); j++) {
						String processor = (String) outBound.elementAt(j);

						ProcessorBundle procType = (ProcessorBundle) ht_processors.get(processor);

						if (procType == null) {
							throw new Exception("Processor " + processor + " is not declared.");
						}
						if (procType.shared.equals("true")) {
							channel.getOutLink().addFilterProcessor((Processor) ht_shared_processors.get(processor));
						} else {
							channel.getOutLink().addFilterProcessor(instantiateProcessor(procType, cl));
						}
					}
				}

				Vector inBound = ch.inBound;
				if (inBound != null) {
					for (int j = 0; j < inBound.size(); j++) {
						String processor = (String) inBound.elementAt(j);
						ProcessorBundle procType = (ProcessorBundle) ht_processors.get(processor);

						if (procType == null) {
							throw new Exception("Processor " + processor + " is not declared.");
						}
						if (procType.shared.equals("true")) {
							channel.getInLink().addFilterProcessor((Processor) ht_shared_processors.get(processor));
						} else {
							channel.getInLink().addFilterProcessor(instantiateProcessor(procType, cl));
						}
					}
				}

				ht_channels.put(name, channel);
			}

			ht_plugins = new Hashtable();
			for (int i = 0; i < plugins.size(); i++) {
				pb = (PluginBundle) plugins.elementAt(i);

				Plugin plugin = new Plugin();

				// PluginType pluginType = model.getPluginArray()[i];
				String name = pb.name;

				String className = pb.className;

				String channel = pb.channel;

				Class pluginClass = Class.forName(className);

				Endpoint endpoint = (Endpoint) pluginClass.newInstance();
				endpoint.setCoContainer(cl);
				endpoint.setEndpointUri("coos://" + name);

				URIHelper helper = new URIHelper(endpoint.getEndpointUri());
				if (helper.isEndpointUuid()) {
					endpoint.setEndpointUuid(name);
				}

				// Map props = new HashMap();
				/*
				 * for (int k = 0; k < pluginType.getPropertyArray().length;
				 * k++) { PropertyType propertyType =
				 * pluginType.getPropertyArray()[k];
				 * props.put(propertyType.getName(), propertyType.getValue()); }
				 */
				endpoint.setProperties(pb.properties);

				plugin.setEndpoint(endpoint);

				plugin.addChannel((Channel) ht_channels.get(channel));

				ht_plugins.put(pb.name != null ? pb.name : "plugin" + new Random().nextInt() % 100, plugin);
			}
		} catch (ClassNotFoundException err) {
			err.printStackTrace();
		} catch (Exception err) {
			err.printStackTrace();
		}
	}

	class PluginBundle {
		public String name, channel, className;
		public Hashtable properties;// / = new Hashtable();

		public PluginBundle(String name, String channel, String className, Hashtable properties) {
			this.name = name;
			this.channel = channel;
			this.className = className;
			this.properties = properties;
		}
	}

	class ChannelBundle {
		public String name, className, segment, transport;
		public Hashtable properties;// = new Hashtable();
		public Vector inBound = new Vector();
		public Vector outBound = new Vector();

		public ChannelBundle(String name, String className, String segment, String transport, Hashtable properties) {
			super();
			this.name = name;
			this.className = className;
			this.segment = segment;
			this.transport = transport;
			this.properties = properties;

		}

	}

	class TransportBundle {
		public String name, className;
		public Hashtable properties;// = new Hashtable();

		public TransportBundle(String name, String className, Hashtable properties) {
			this.name = name;
			this.className = className;
			this.properties = properties;
		}
	}

	class ProcessorBundle {
		public String name, className, shared;
		Hashtable properties;// = new Hashtable();

		public ProcessorBundle(String name, String className, String shared, Hashtable properties) {
			this.name = name;
			this.className = className;
			this.shared = shared;
			this.properties = properties;
		}
	}

	private static Processor instantiateProcessor(ProcessorBundle processor, COContainer cl)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		String className = processor.className;
		boolean isShared = processor.name.equals("true");
		/*
		 * Map props = new HashMap(); for (int j
		 * = 0; j < processorType.getPropertyArray().length; j++) { PropertyType
		 * propertyType = processorType.getPropertyArray()[j];
		 * props.put(propertyType.getName(), propertyType.getValue()); }
		 */

		Class procClass = cl.loadClass(className);
		Processor p = (Processor) procClass.newInstance();
		p.setProperties(processor.properties);
		p.setShared(new Boolean(isShared));
		return p;
	}

	public Plugin[] getPlugins() {
		Plugin[] retp = new Plugin[ht_plugins.size()];
		int counter = 0;
		for (Enumeration e = ht_plugins.keys(); e.hasMoreElements();) {
			retp[counter++] = (Plugin) ht_plugins.get(e.nextElement());
		}
		return retp;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy