tn5250j/src/org/tn5250j/framework Tn5250jListener.java,NONE,1.1 Tn5250jController.java,NONE,1.1 Tn5250jSession.java,NONE,1.1 Tn5250jKeyEvents.java,NONE,1.1 Tn5250jEvent.java,NONE,1.1

Barry van Someren <[email protected]> Fri, 02 Jul 2004 13:46:01 +0000
Newsgroups gmane.comp.java.tn5250j.cvs
Message-ID <[email protected]>
Update of /cvsroot/tn5250j/tn5250j/src/org/tn5250j/framework
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32643/src/org/tn5250j/framework

Added Files:
	Tn5250jListener.java Tn5250jController.java 
	Tn5250jSession.java Tn5250jKeyEvents.java Tn5250jEvent.java 
Log Message:
The first Tn5250jFramework commit, please don't hurt me..
This release provides:
-Enabling JAR based plugins to Tn5250j
-Receiving screen data with plugins.
-Start sessions from plugins

--- NEW FILE: Tn5250jEvent.java ---
/**Copyright (C) 2004 Seagull Software
*
* 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*@author bvansomeren ([email protected])
*/
package org.tn5250j.framework;

import org.tn5250j.Screen5250;
import org.tn5250j.ScreenChar;
import org.tn5250j.ScreenFields;

public class Tn5250jEvent {
	
	private Screen5250 screen;
	private ScreenChar data[];
	private ScreenFields fields;
	
	public Tn5250jEvent() {
		screen = null;	
	}
	
	public Tn5250jEvent(Screen5250 newscreen) {
		screen = newscreen;
		Object[] original = (Object[])screen.getCharacters();
		data = new ScreenChar[original.length];
		System.arraycopy(original, 0, data, 0, original.length);
		this.fields = newscreen.getScreenFields();
	}
	
	public ScreenChar[] getData() {
		return data;
	}
	
	public Screen5250 getScreen() {
		return screen;
	}
	
	public ScreenFields getFields() {
		return this.fields;
	}
}

--- NEW FILE: Tn5250jKeyEvents.java ---
/**Copyright (C) 2004 Seagull Software
*
* 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*@author bvansomeren ([email protected])
*/
package org.tn5250j.framework;

import org.tn5250j.Screen5250;

public class Tn5250jKeyEvents extends Tn5250jEvent {
	private String keystrokes;
	
	public Tn5250jKeyEvents(Screen5250 screen, String strokes) {
		super(screen);
		this.keystrokes = strokes;
	}
	
	public String getKeystrokes() {
		return this.keystrokes;
	}

}

--- NEW FILE: Tn5250jSession.java ---
/**Copyright (C) 2004 Seagull Software
*
* 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*@author bvansomeren ([email protected])
*/
package org.tn5250j.framework;

import org.tn5250j.Screen5250;
import org.tn5250j.Session;
import org.tn5250j.tnvt;

public class Tn5250jSession {
	private Screen5250 sessionScreen;
	private tnvt SessionTNVT;
	private Session session;
	
	protected Tn5250jSession(Screen5250 screen, tnvt vt, Session ses) {
		sessionScreen=screen;
		SessionTNVT = vt;
		session=ses;
	}
	/**
	 * @return
	 */
	public Session getSession() {
		return session;
	}

	/**
	 * @return
	 */
	public Screen5250 getSessionScreen() {
		return sessionScreen;
	}

	/**
	 * @return
	 */
	public tnvt getSessionTNVT() {
		return SessionTNVT;
	}

}

--- NEW FILE: Tn5250jListener.java ---
/**Copyright (C) 2004 Seagull Software
*
* 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*@author bvansomeren ([email protected])
*/
package org.tn5250j.framework;

import java.io.File;
import java.util.Properties;

public abstract class Tn5250jListener {
	public abstract void actionPerformed(Tn5250jEvent event);
	
	public abstract void init(File fileDir, Properties config);
	
	public abstract void run();
	
	public abstract void destroy();
	
	public abstract String getName();
	
	public abstract void setController(Tn5250jController control);
	
	public abstract void sessionCreated(Tn5250jSession session);
}

--- NEW FILE: Tn5250jController.java ---
/**Copyright (C) 2004 Seagull Software
*
* 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*@author bvansomeren ([email protected])
*/
package org.tn5250j.framework;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.JFrame;

import org.tn5250j.GlobalConfigure;
import org.tn5250j.Screen5250;
import org.tn5250j.Session;
import org.tn5250j.SessionManager;
import org.tn5250j.Sessions;
import org.tn5250j.TN5250jConstants;
import org.tn5250j.tnvt;
import org.tn5250j.interfaces.ConfigureFactory;
import org.tn5250j.tools.logging.TN5250jLogFactory;
import org.tn5250j.tools.logging.TN5250jLogger;


public class Tn5250jController extends Thread implements TN5250jConstants {
	private File extensionDir;
	private TN5250jLogger log = TN5250jLogFactory.getLogger(this.getClass());
	//private URLClassLoader loader = new URLClassLoader(null, this.getClass().getClassLoader());
	private List eventList;
	private List listeners;
	private List sessions;
	private SessionManager manager;
	Properties sesprops;
	private static Tn5250jController current;

	private Tn5250jController() {
		String maindir = System.getProperty("user.dir");
		extensionDir = new File(maindir + File.separatorChar + "ext");
		log.info("plugin directory is: " + extensionDir.getAbsolutePath());
		if (!extensionDir.exists()) {
			extensionDir.mkdir();
		}
		this.setDaemon(true);
		eventList = new ArrayList();
		listeners = new ArrayList();
		Tn5250jController.current = this;
		log.info("Tn5250j plugin manager created");
		manager = SessionManager.instance();
		Sessions ses = manager.getSessions();
		log.debug("Sessions:" + ses.getCount());
		sesprops =
			((GlobalConfigure) ConfigureFactory.getInstance()).getProperties(
				GlobalConfigure.SESSIONS);
		log.debug("Session configuration: " + sesprops.toString());
		this.start();
	}

	private void loadExt() {
		File[] exts = extensionDir.listFiles();
		for (int x = 0; x < exts.length; x++) {
			if (exts[x].isDirectory()) {
				String jarName =
					exts[x].getAbsolutePath()
						+ File.separatorChar
						+ exts[x].getName()
						+ ".jar";
				File jarFile = new File(jarName);
				if (jarFile.exists()) {
					Properties config = loadConfig(jarFile);
					load(jarFile, config.getProperty("mainentry"), config);
				} else {
					log.warn(
						"extension could not be loaded as the jar was not found: "
							+ jarName);
				}
			}
		}
	}

	private void load(File jar, String name, Properties config) {
		URL[] urls = new URL[1];

		try {
			urls[0] = jar.toURL();
			log.info("Loading jar: " + jar.toURL());
		} catch (MalformedURLException e) {
			log.warn("The URL was malformed");
		}
		URLClassLoader loader = new URLClassLoader(urls);
		try {
			Class ext = loader.loadClass(name);
			try {
				Tn5250jListener module = (Tn5250jListener) ext.newInstance();
				listeners.add(module);
				ModuleThread thrd =
					new ModuleThread(jar.getParentFile(), module, config);
				thrd.start();

			} catch (InstantiationException e2) {
				log.warn("Error instantiating class " + name);
			} catch (IllegalAccessException e2) {
				log.warn(
					"The class "
						+ name
						+ " gives no access to the constructor");
			} catch (ClassCastException e2) {
				log.warn(
					"Main module class does not derive from Tn5250jListener");
			}

		} catch (ClassNotFoundException e1) {
			log.warn(
				"Extension could not be loaded, class: " + name + " not found");
		}
	}

	private Properties loadConfig(File jar) {
		JarFile jarfile = null;
		Properties config = null;
		try {
			jarfile = new JarFile(jar);
			JarEntry configfile = jarfile.getJarEntry("config.properties");
			InputStream stream = jarfile.getInputStream(configfile);
			config = new Properties();
			config.load(stream);
		} catch (IOException e) {
			log.warn("Failure trying to load configuration");
		}
		return config;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		log.info("Tn5250j plugin manager started");
		loadExt();
		while (true) { //keep on running forever as we are a daemon
			synchronized (eventList) {
				while (!eventList.isEmpty()) {
					final Tn5250jEvent event =
						(Tn5250jEvent) eventList.remove(0);
					broadcastEvent(event);
				}
				try {
					eventList.wait();
				} catch (InterruptedException e) {
					log.debug("Intertupted exception");
				}
			}
		}
		//log.info("Tn5250j plugin manager stopped");
	}

	private void broadcastEvent(final Tn5250jEvent event) {
		Iterator listenerIt = listeners.iterator();
		while (listenerIt.hasNext()) {
			Tn5250jListener listener = (Tn5250jListener) listenerIt.next();
			listener.actionPerformed(event);
		}
	}

	public void handleEvent(Tn5250jEvent e) {
		log.info("Received event: " + e.getClass().toString());
		if (e instanceof Tn5250jKeyEvents) {
			log.info("Keys: " + ((Tn5250jKeyEvents) e).getKeystrokes());
		}
		eventList.add(e);
		synchronized (eventList) {
			eventList.notify();
		}
	}

	public static Tn5250jController getCurrent() {
		if (current == null) {
			current = new Tn5250jController();
		}
		return current;
	}

	public void createSession(Screen5250 screen, tnvt vt, Session ses) {
		final Tn5250jSession session = new Tn5250jSession(screen, vt, ses);
		Iterator listenerIt = listeners.iterator();
		log.info("New session created and received");
		while (listenerIt.hasNext()) {
			Tn5250jListener listener = (Tn5250jListener) listenerIt.next();
			listener.sessionCreated(session);
		}
	}

	private class ModuleThread extends Thread {
		File dir;
		Tn5250jListener mod;
		Properties config;
		public ModuleThread(
			File directory,
			Tn5250jListener module,
			Properties config) {
			dir = directory;
			mod = module;
			this.config = config;
			this.setDaemon(true);
			this.setName(module.getName());
		}

		public void run() {
			mod.init(dir, config);
			mod.setController(Tn5250jController.getCurrent());
			log.info("module initialized");
			mod.run();
			log.info("module stopped");
			mod.destroy();
			log.info("module destroyed");
		}

	}

	protected Properties getPropertiesForSession(String session) {
		return null;
	}

	public Screen5250 startSession(String name) {
		JFrame frame = new JFrame();
		String args[] = new String[15];
		parseArgs((String) sesprops.get(name), args);
		Properties fin = convertToProps(args);
		Session newses = manager.openSession(fin, null, name);
		frame.getContentPane().add(newses);
		frame.setBounds(50, 50, 960, 700);
		frame.show();
		newses.connect();
		return newses.getScreen();
	}

	public List getSessions() {
		Enumeration e = sesprops.keys();
		ArrayList list = new ArrayList();
		String ses = null;
		//This has the nasty tendency to grab data it isn't suposed to grab.
		//please fix
		while (e.hasMoreElements()) {
			ses = (String) e.nextElement();
			if (!ses.startsWith("emul.")) {
				list.add(ses);
			}
		}
		log.error(list.toString());
		return list;
	}

	protected void parseArgs(String theStringList, String[] s) {
		int x = 0;
		StringTokenizer tokenizer = new StringTokenizer(theStringList, " ");
		while (tokenizer.hasMoreTokens()) {
			s[x++] = tokenizer.nextToken();
		}
	}

	protected Properties convertToProps(String args[]) {
		Properties sesProps = new Properties();

		String propFileName = null;
		String session = args[0];

		// Start loading properties
		sesProps.put(SESSION_HOST, session);

		if (isSpecified("-e", args))
			sesProps.put(SESSION_TN_ENHANCED, "1");

		if (isSpecified("-p", args)) {
			sesProps.put(SESSION_HOST_PORT, getParm("-p", args));
		}

		if (isSpecified("-f", args))
			propFileName = getParm("-f", args);

		if (isSpecified("-cp", args))
			sesProps.put(SESSION_CODE_PAGE, getParm("-cp", args));

		if (isSpecified("-gui", args))
			sesProps.put(SESSION_USE_GUI, "1");

		if (isSpecified("-132", args))
			sesProps.put(SESSION_SCREEN_SIZE, SCREEN_SIZE_27X132_STR);
		else
			sesProps.put(SESSION_SCREEN_SIZE, SCREEN_SIZE_24X80_STR);

		// are we to use a socks proxy
		if (isSpecified("-usp", args)) {

			// socks proxy host argument
			if (isSpecified("-sph", args)) {
				sesProps.put(SESSION_PROXY_HOST, getParm("-sph", args));
			}

			// socks proxy port argument
			if (isSpecified("-spp", args))
				sesProps.put(SESSION_PROXY_PORT, getParm("-spp", args));
		}

		// are we to use a ssl and if we are what type
		if (isSpecified("-sslType", args)) {

			sesProps.put(SSL_TYPE, getParm("-sslType", args));
		}

		// check if device name is specified
		if (isSpecified("-dn=hostname", args)) {
			String dnParam;

			// use IP address as device name
			try {
				dnParam = InetAddress.getLocalHost().getHostName();
			} catch (UnknownHostException uhe) {
				dnParam = "UNKNOWN_HOST";
			}

			sesProps.put(SESSION_DEVICE_NAME, dnParam);
		} else if (isSpecified("-dn", args)) {

			sesProps.put(SESSION_DEVICE_NAME, getParm("-dn", args));
		}

		if (isSpecified("-hb", args))
			sesProps.put(SESSION_HEART_BEAT, "1");

		return sesProps;
	}

	boolean isSpecified(String parm, String[] args) {

		if (args == null)
			return false;

		for (int x = 0; x < args.length; x++) {

			if (args[x] != null && args[x].equals(parm))
				return true;

		}
		return false;
	}

	private String getParm(String parm, String[] args) {

		for (int x = 0; x < args.length; x++) {

			if (args[x].equals(parm))
				return args[x + 1];

		}
		return null;
	}

}



-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - 
digital self defense, top technical experts, no vendor pitches, 
unmatched networking opportunities. Visit www.blackhat.com