KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > applications > AgentApplicationLauncher


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client.applications;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import com.maverick.multiplex.ChannelOpenException;
30 import com.sslexplorer.agent.client.Agent;
31 import com.sslexplorer.agent.client.WinRegistry;
32 import com.sslexplorer.agent.client.util.AbstractApplicationLauncher;
33 import com.sslexplorer.agent.client.util.ApplicationLauncherEvents;
34 import com.sslexplorer.agent.client.util.Messages;
35 import com.sslexplorer.agent.client.util.TunnelConfiguration;
36 import com.sslexplorer.agent.client.util.XMLElement;
37
38 /**
39  * Extension of {@link AbstractApplicationLauncher} for use with the SSL-Explorer Agent.
40  *
41  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
42  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
43  */

44 public class AgentApplicationLauncher extends AbstractApplicationLauncher {
45
46     // #ifdef DEBUG
47
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(AgentApplicationLauncher.class);
48     // #endif
49

50     private Agent agent;
51     private String JavaDoc name;
52     private String JavaDoc descriptor;
53     private Vector JavaDoc tunnels = new Vector JavaDoc();
54     
55     /**
56      * Constructor.
57      *
58      * @param vpn agent
59      * @param parameters parameters
60      * @param events events callback
61      */

62     public AgentApplicationLauncher(Agent vpn, String JavaDoc name, Hashtable JavaDoc parameters, String JavaDoc descriptor, ApplicationLauncherEvents events) {
63         super(vpn.getConfiguration().getCacheDir(), "https", vpn.getUsername(), vpn.getSSLExplorerHost(), vpn.getSSLExplorerPort(), parameters, events);
64         this.agent = vpn;
65         this.name = name;
66         this.descriptor = descriptor;
67     }
68     
69     public Agent getAgent() {
70         return agent;
71     }
72
73     protected void createTunnel(XMLElement e) throws IOException JavaDoc {
74
75         String JavaDoc hostname = (String JavaDoc) e.getAttribute("hostname"); //$NON-NLS-1$
76
String JavaDoc name = (String JavaDoc) e.getAttribute("name"); //$NON-NLS-1$
77
int port = Integer.parseInt((String JavaDoc) e.getAttribute("port")); //$NON-NLS-1$
78
boolean usePreferredPort = ("true".equals(e.getAttribute("usePreferredPort"))); //$NON-NLS-1$ //$NON-NLS-2$
79
boolean singleConnection = !("false".equals(e.getAttribute("singleConnection"))); //$NON-NLS-1$ //$NON-NLS-2$
80
boolean localhostWorkaround = "true".equals(e.getAttribute("localhostWorkaround")); //$NON-NLS-1$ //$NON-NLS-2$
81
if (events != null) {
82             TunnelConfiguration listeningSocketConfiguration = events.createTunnel(name,
83                 hostname,
84                 port,
85                 usePreferredPort,
86                 singleConnection,
87                 localhostWorkaround ? "127.0.0.2" : agent.getConfiguration().getLocalhostAddress());
88             tunnels.addElement(listeningSocketConfiguration);
89         } else
90             throw new IOException JavaDoc(Messages.getString("ApplicationLauncher.tunnelRequiredButNoEventHandler")); //$NON-NLS-1$
91

92     }
93     
94     /* (non-Javadoc)
95      * @see com.sslexplorer.agent.client.util.AbstractApplicationLauncher#replaceTokens(java.lang.String)
96      */

97     public String JavaDoc replaceTokens(String JavaDoc str) {
98         str = super.replaceTokens(str);
99         for (Enumeration JavaDoc e = tunnels.elements(); e.hasMoreElements();) {
100             TunnelConfiguration listeningSocketConfiguration = (TunnelConfiguration) e.nextElement();
101             String JavaDoc paramHost = "${tunnel:" + listeningSocketConfiguration.getName() + ".hostname}"; //$NON-NLS-1$ //$NON-NLS-2$
102
String JavaDoc paramPort = "${tunnel:" + listeningSocketConfiguration.getName() + ".port}"; //$NON-NLS-1$ //$NON-NLS-2$
103

104             str = replaceAllTokens(str, paramHost, listeningSocketConfiguration.getSourceInterface()); //$NON-NLS-1$
105
str = replaceAllTokens(str, paramPort, String.valueOf(listeningSocketConfiguration.getSourcePort()));
106         }
107         return str;
108     }
109
110     /**
111      * Get a list of {@link TunnelConfiguration} objects that this
112      * application launcher requires.
113      *
114      * @return tunnels
115      */

116     public Vector JavaDoc getTunnels() {
117         return tunnels;
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see com.sslexplorer.vpn.util.ApplicationLauncher#processLauncherElement(com.sslexplorer.vpn.util.XMLElement)
124      */

125     protected boolean processLauncherElement(XMLElement element) {
126
127         if (element.getName().equalsIgnoreCase("registry")) {
128             Enumeration JavaDoc e = element.enumerateChildren();
129
130             while (e.hasMoreElements()) {
131                 XMLElement el = (XMLElement) e.nextElement();
132
133                 processRegistryElements(el);
134             }
135             return true;
136         } else
137             return false;
138     }
139
140     private void processRegistryElements(XMLElement el) {
141         if (el.getName().equalsIgnoreCase("get")) {
142             String JavaDoc scope = el.getStringAttribute("scope");
143             String JavaDoc key = replaceTokens(el.getStringAttribute("key"));
144             String JavaDoc value = replaceTokens(el.getStringAttribute("value"));
145             String JavaDoc param = replaceTokens(el.getStringAttribute("parameter"));
146             String JavaDoc defaultValue = replaceTokens(el.getStringAttribute("default"));
147
148             addParameter(param, WinRegistry.getRegistryValue(scope, key, value, defaultValue == null ? "" : defaultValue));
149         } else if (el.getName().equalsIgnoreCase("set")) {
150             String JavaDoc scope = el.getStringAttribute("scope");
151             String JavaDoc key = replaceTokens(el.getStringAttribute("key"));
152             String JavaDoc value = replaceTokens(el.getStringAttribute("value"));
153             String JavaDoc arg = replaceTokens(el.getStringAttribute("arg"));
154
155             WinRegistry.setRegistryValue(scope, key, value, arg);
156
157         } else if (el.getName().equalsIgnoreCase("if")) {
158
159             String JavaDoc scope = el.getStringAttribute("scope");
160             String JavaDoc key = replaceTokens(el.getStringAttribute("key"));
161             String JavaDoc value = replaceTokens(el.getStringAttribute("value"));
162             String JavaDoc notAttr = el.getStringAttribute("not");
163             String JavaDoc existsAttr = el.getStringAttribute("exists");
164             String JavaDoc equalsAttr = el.getStringAttribute("equals");
165
166             if (existsAttr != null) {
167                 boolean exists = Boolean.getBoolean(existsAttr);
168                 String JavaDoc v = WinRegistry.getRegistryValue(scope, key, value, "DEFAULT_VALUE");
169                 if (v.equals("DEFAULT_VALUE") && !exists) {
170                     processRegistryElements(el);
171                 } else if (!v.equals("DEFAULT_VALUE") && exists) {
172                     processRegistryElements(el);
173                 }
174             } else if (notAttr != null) {
175                 boolean not = Boolean.getBoolean(notAttr == null ? "false" : notAttr);
176                 String JavaDoc v = WinRegistry.getRegistryValue(scope, key, value, "");
177
178                 if (equalsAttr.equals(v) && !not) {
179                     processRegistryElements(el);
180                 } else if (!equalsAttr.equals(v) && not) {
181                     processRegistryElements(el);
182                 }
183             }
184
185         }
186     }
187
188     /* (non-Javadoc)
189      * @see com.sslexplorer.vpn.util.ApplicationLauncher#getApplicationDescriptor()
190      */

191     public InputStream JavaDoc getApplicationDescriptor() throws IOException JavaDoc {
192
193         if (events != null)
194             events.debug("Getting application descriptor using Maverick HTTP client");
195
196         return new ByteArrayInputStream JavaDoc(descriptor.getBytes());
197     }
198
199     /**
200      * Get download file.
201      *
202      * @param name name
203      * @param ticket ticket
204      * @param filename filename
205      * @return stream
206      * @throws IOException on any error
207      */

208     public InputStream JavaDoc getDownloadFile(String JavaDoc name, String JavaDoc ticket, String JavaDoc filename) throws IOException JavaDoc {
209
210         if (events != null)
211             events.debug("Downloading application file " + filename);
212
213         try {
214             ApplicationFileChannel channel = new ApplicationFileChannel(name, ticket, filename);
215             agent.getConnection().openChannel(channel);
216             return channel.getInputStream();
217         } catch (ChannelOpenException e) {
218             throw new IOException JavaDoc(e.getMessage());
219         }
220         
221     }
222 }
223
Popular Tags