KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24
25 import com.sslexplorer.agent.client.Agent;
26 import com.sslexplorer.agent.client.Messages;
27 import com.sslexplorer.agent.client.TaskProgress;
28 import com.sslexplorer.agent.client.util.ApplicationLauncherEvents;
29 import com.sslexplorer.agent.client.util.TunnelConfiguration;
30
31 /**
32  * Listens for events that occurs during an application launch inform the user
33  * (e.g. through status messages and progress bars).
34  *
35  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
36  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
37  */

38 public class ApplicationEventListener implements ApplicationLauncherEvents {
39
40     // #ifdef DEBUG
41
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(ApplicationEventListener.class);
42     // #endif
43

44     private static final String JavaDoc LABEL_TITLE = Messages.getString("VPNClient.error"); //$NON-NLS-1$
45
// Private instance variables
46
private Agent client;
47     private TaskProgress progress;
48     private String JavaDoc application;
49     private long totalNumBytes;
50
51     /**
52      * Constructor.
53      *
54      * @param client client
55      */

56     public ApplicationEventListener(Agent client) {
57         this.client = client;
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#startingLaunch(java.lang.String)
64      */

65     public void startingLaunch(String JavaDoc application) {
66         this.application = application;
67         progress = this.client.getGUI().createTaskProgress(
68             MessageFormat.format(Messages.getString("ApplicationEventListener.application"), //$NON-NLS-1$
69
new Object JavaDoc[] { application }), Messages.getString("ApplicationEventListener.title"), 100, false); //$NON-NLS-1$
70
progress.updateValue(7);
71
72     }
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#processingDescriptor()
78      */

79     public void processingDescriptor() {
80         progress.setMessage(MessageFormat.format(
81             Messages.getString("ApplicationEventListener.processing"), new Object JavaDoc[] { application })); //$NON-NLS-1$
82
progress.updateValue(14);
83
84     }
85
86     /*
87      * (non-Javadoc)
88      *
89      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#startDownload(long)
90      */

91     public void startDownload(long totalNumBytes) {
92         this.totalNumBytes = totalNumBytes;
93         progress.setMessage(Messages.getString("ApplicationEventListener.downloading")); //$NON-NLS-1$
94
progress.updateValue(20);
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#progressedDownload(long)
101      */

102     public void progressedDownload(long bytesSoFar) {
103         // The the file download takes up 70% of the total progress
104
int percent = (int) (((float) bytesSoFar / (float) totalNumBytes) * 70f) + 20;
105         progress.updateValue(percent);
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#completedDownload()
112      */

113     public void completedDownload() {
114         progress.setMessage(Messages.getString("ApplicationEventListener.downloadComplete")); //$NON-NLS-1$
115
progress.updateValue(90);
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#executingApplication(java.lang.String,
122      * java.lang.String)
123      */

124     public void executingApplication(String JavaDoc name, String JavaDoc cmdline) {
125         progress.setMessage(MessageFormat.format(Messages.getString("ApplicationEventListener.launching"), new Object JavaDoc[] { name })); //$NON-NLS-1$
126
progress.updateValue(95);
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#finishedLaunch()
133      */

134     public void finishedLaunch() {
135
136         Thread JavaDoc t = new Thread JavaDoc() {
137             public void run() {
138                 progress.updateValue(100);
139                 try {
140                     Thread.sleep(2000);
141                 } catch (InterruptedException JavaDoc ex) {
142                 }
143                 progress.dispose();
144             }
145         };
146
147         t.start();
148
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#createTunnel(java.lang.String,
155      * java.lang.String, int, boolean, boolean)
156      */

157     public TunnelConfiguration createTunnel(String JavaDoc name, String JavaDoc hostToConnect, int portToConnect, boolean usePreferredPort,
158                                boolean singleConnection, String JavaDoc sourceInterface)throws IOException JavaDoc {
159         try {
160             return client.getTunnelManager().startTemporaryLocalTunnel(name, hostToConnect, sourceInterface, portToConnect, usePreferredPort, singleConnection, null).getTunnel();
161         } catch (IOException JavaDoc e) {
162             // #ifdef DEBUG
163
log.error("Failed to start application tunnel " + name, e); //$NON-NLS-1$
164
// #endif
165
throw e;
166         }
167     }
168
169     /*
170      * (non-Javadoc)
171      *
172      * @see com.sslexplorer.vpn.util.ApplicationLauncherEvents#debug(java.lang.String)
173      */

174     public void debug(String JavaDoc msg) {
175         // #ifdef DEBUG
176
log.info(msg);
177         // #endif
178
}
179
180     public void error(String JavaDoc msg) {
181         // #ifdef DEBUG
182
log.error(msg);
183         // #endif
184
client.getGUI().popup(null, msg, LABEL_TITLE, "popup-error", -1);
185     }
186 }
Popular Tags