tn5250j/src/org/tn5250j/framework Tn5250jController.java,1.4,1.5 Tn5250jEvent.java,1.4,1.5 Tn5250jKeyEvents.java,1.2,1.3 Tn5250jSession.java,1.3,1.4
"Kenneth J. Pouncey" <[email protected]> Fri, 30 Jul 2004 05:05:46 +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-serv343 Modified Files: Tn5250jController.java Tn5250jEvent.java Tn5250jKeyEvents.java Tn5250jSession.java Log Message: use new framework packages Index: Tn5250jEvent.java =================================================================== RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/framework/Tn5250jEvent.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Tn5250jEvent.java 26 Jul 2004 19:23:00 -0000 1.4 --- Tn5250jEvent.java 30 Jul 2004 05:05:44 -0000 1.5 *************** *** 1,62 **** ! /**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.framework.tn5250.Screen5250; ! import org.tn5250j.framework.tn5250.ScreenFields; ! //import org.tn5250j.ScreenFields; ! ! public class Tn5250jEvent { ! ! private Screen5250 screen; ! private char[] 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); ! ! // changed by Kenneth - This should be replaced with a call to ! // getPlane method of screen object when they are implemented. These ! // new methods will also do the array copy. ! char[] original = screen.getCharacters(); ! data = new char[original.length]; ! System.arraycopy(original, 0, data, 0, original.length); ! this.fields = newscreen.getScreenFields(); ! } ! ! public char[] getData() { ! return data; ! } ! ! public Screen5250 getScreen() { ! return screen; ! } ! ! public ScreenFields getFields() { ! return this.fields; ! } ! } --- 1,62 ---- ! /**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.framework.tn5250.Screen5250; ! import org.tn5250j.framework.tn5250.ScreenFields; ! //import org.tn5250j.ScreenFields; ! ! public class Tn5250jEvent { ! ! private Screen5250 screen; ! private char[] 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); ! ! // changed by Kenneth - This should be replaced with a call to ! // getPlane method of screen object when they are implemented. These ! // new methods will also do the array copy. ! char[] original = screen.getCharacters(); ! data = new char[original.length]; ! System.arraycopy(original, 0, data, 0, original.length); ! this.fields = newscreen.getScreenFields(); ! } ! ! public char[] getData() { ! return data; ! } ! ! public Screen5250 getScreen() { ! return screen; ! } ! ! public ScreenFields getFields() { ! return this.fields; ! } ! } Index: Tn5250jKeyEvents.java =================================================================== RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/framework/Tn5250jKeyEvents.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Tn5250jKeyEvents.java 26 Jul 2004 19:23:00 -0000 1.2 --- Tn5250jKeyEvents.java 30 Jul 2004 05:05:44 -0000 1.3 *************** *** 1,36 **** ! /**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.framework.tn5250.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; ! } ! ! } --- 1,36 ---- ! /**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.framework.tn5250.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; ! } ! ! } Index: Tn5250jSession.java =================================================================== RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/framework/Tn5250jSession.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Tn5250jSession.java 26 Jul 2004 19:23:00 -0000 1.3 --- Tn5250jSession.java 30 Jul 2004 05:05:44 -0000 1.4 *************** *** 1,57 **** ! /**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.framework.tn5250.Screen5250; ! //import org.tn5250j.Screen5250; ! import org.tn5250j.SessionGUI; ! import org.tn5250j.framework.tn5250.tnvt; ! ! public class Tn5250jSession { ! private Screen5250 sessionScreen; ! private tnvt SessionTNVT; ! private SessionGUI session; ! ! protected Tn5250jSession(Screen5250 screen, tnvt vt, SessionGUI ses) { ! sessionScreen=screen; ! SessionTNVT = vt; ! session=ses; ! } ! /** ! * @return ! */ ! public SessionGUI getSession() { ! return session; ! } ! ! /** ! * @return ! */ ! public Screen5250 getSessionScreen() { ! return sessionScreen; ! } ! ! /** ! * @return ! */ ! public tnvt getSessionTNVT() { ! return SessionTNVT; ! } ! ! } --- 1,57 ---- ! /**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.framework.tn5250.Screen5250; ! //import org.tn5250j.Screen5250; ! import org.tn5250j.SessionGUI; ! import org.tn5250j.framework.tn5250.tnvt; ! ! public class Tn5250jSession { ! private Screen5250 sessionScreen; ! private tnvt SessionTNVT; ! private SessionGUI session; ! ! protected Tn5250jSession(Screen5250 screen, tnvt vt, SessionGUI ses) { ! sessionScreen=screen; ! SessionTNVT = vt; ! session=ses; ! } ! /** ! * @return ! */ ! public SessionGUI getSession() { ! return session; ! } ! ! /** ! * @return ! */ ! public Screen5250 getSessionScreen() { ! return sessionScreen; ! } ! ! /** ! * @return ! */ ! public tnvt getSessionTNVT() { ! return SessionTNVT; ! } ! ! } Index: Tn5250jController.java =================================================================== RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/framework/Tn5250jController.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Tn5250jController.java 26 Jul 2004 19:23:00 -0000 1.4 --- Tn5250jController.java 30 Jul 2004 05:05:44 -0000 1.5 *************** *** 1,385 **** ! /**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.framework.tn5250.Screen5250; ! import org.tn5250j.SessionGUI; ! import org.tn5250j.Session5250; ! import org.tn5250j.SessionManager; ! import org.tn5250j.Sessions; ! import org.tn5250j.TN5250jConstants; ! import org.tn5250j.framework.tn5250.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.debug("Received event: " + e.getClass().toString()); ! if (e instanceof Tn5250jKeyEvents) { ! log.debug("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, SessionGUI 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); ! Session5250 newses = manager.openSession(fin, null, name); ! SessionGUI newGui = new SessionGUI(newses); ! frame.getContentPane().add(newGui); ! 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; ! } ! ! } --- 1,385 ---- ! /**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.framework.tn5250.Screen5250; ! import org.tn5250j.SessionGUI; ! import org.tn5250j.Session5250; ! import org.tn5250j.framework.common.SessionManager; ! import org.tn5250j.TN5250jConstants; ! import org.tn5250j.framework.tn5250.tnvt; ! import org.tn5250j.interfaces.ConfigureFactory; ! import org.tn5250j.tools.logging.TN5250jLogFactory; ! import org.tn5250j.tools.logging.TN5250jLogger; ! import org.tn5250j.framework.common.Sessions; ! ! ! 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.debug("Received event: " + e.getClass().toString()); ! if (e instanceof Tn5250jKeyEvents) { ! log.debug("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, SessionGUI 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); ! Session5250 newses = manager.openSession(fin, null, name); ! SessionGUI newGui = new SessionGUI(newses); ! frame.getContentPane().add(newGui); ! 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 is sponsored by OSTG. Have you noticed the changes on Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now, one more big change to announce. We are now OSTG- Open Source Technology Group. Come see the changes on the new OSTG site. www.ostg.com