View Javadoc

1   /*
2       Jagacy plug-in - A TN3270 plug-in for Jameleon.
3       Copyright (C) 2005 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.plugin.jagacy;
20  
21  import net.sf.jameleon.SessionTag;
22  import net.sf.jameleon.function.ContextHelper;
23  import net.sf.jameleon.util.Configurator;
24  
25  import com.jagacy.Session3270;
26  import com.jagacy.util.JagacyException;
27  
28  /***
29   * A Session tag for the Jagacy 3270 plug-in.
30   *
31   * An example of its use might:
32   *
33   * <pre><source>
34   * &lt;testcase xmlns="jelly:jameleon"&gt;
35   *     &lt;jagacy-session host="locis.loc.gov" beginSession="true"&gt;
36   *       &lt;jagacy-is-text-present
37   *           functionId="Check that page has 'the' in it."
38   *           text="the"/&gt;
39   *     &lt;/jagacy-session&gt;
40   * &lt;/testcase&gt;
41   * </source></pre>
42   * @jameleon.function name="jagacy-session"
43   */
44  public class JagacySessionTag extends SessionTag {
45  
46      protected final static String SESSION_NAME = "jagacy-plugin";
47      protected final static String HOST_PROP_NAME = "jagacy.host";
48      protected final static String HOST_CONTEXT_NAME = "jagacyHost";
49      protected String host;
50      protected Boolean visible;
51  
52      /***
53       * The 3270 session handle
54       */
55      protected Session3270 session;
56  
57      public void setUpSession() {
58          System.setProperty(SESSION_NAME+".window", getVisible()+"");
59          try{
60              if (isStringNotNull(getHost())) {
61                  System.setProperty(HOST_PROP_NAME, getHost());
62              }
63              session = new Session3270(SESSION_NAME);
64          }catch (JagacyException je){
65              throw new RuntimeException("Could not set up Session3270", je);
66          }
67      }
68  
69      public void startApplication() {
70          if (isStringNotNull(getHost())) {
71              try{
72                  session.open();
73              }catch(JagacyException je){
74                  throw new RuntimeException("Could not connect to host '"+host+"'",je );
75              }
76          }else{
77              throw new RuntimeException("'host' is a required attribute when beginSession is true!");
78          }
79      }
80  
81      public String getHost(){
82          if (host == null || host.trim().length() == 0) {
83              host = ContextHelper.getVariableAsString(context, HOST_CONTEXT_NAME);
84          }
85          return host;
86      }
87  
88      public Session3270 getSession3270(){
89          return session;
90      }
91  
92      /***
93       * @jameleon.attribute
94       */
95      public void setHost(String host){
96          this.host = host;
97      }
98  
99  
100     public Boolean getVisible(){
101         String val = Configurator.getInstance().getValue("jagacy-plugin.visible");
102         if (isStringNotNull(val)) {
103             visible = new Boolean(val);
104         }else{
105             visible = new Boolean(false);
106         }
107         return visible;
108     }
109 
110     /***
111      * @jameleon.attribute
112      */
113     public void setVisible(boolean visible){
114         this.visible = new Boolean(visible);
115     }
116 
117 
118     protected void tearDownSession(){
119         if (session != null) {
120             try {
121                 session.close();
122             } catch (JagacyException e) {
123                 System.err.println("Trouble closing Session3270 connection");
124                 e.printStackTrace();
125             }
126         }
127     }
128 
129     protected boolean isStringNotNull(String str){
130         boolean isNotNull = false;
131         if (str != null && str.trim().length() > 0 ) {
132             isNotNull = true;
133         }
134         return isNotNull;
135     }
136 
137 }