KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > server > ServerApplicationLauncher


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.applications.server;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import com.sslexplorer.applications.ApplicationShortcut;
34 import com.sslexplorer.boot.Util;
35 import com.sslexplorer.extensions.store.ExtensionStore;
36 import com.sslexplorer.security.SessionInfo;
37
38
39 /**
40  * Launches a server application.
41  *
42  * @author Sebastien Belin <a HREF="mailto: seb@3sp.com">&lt;seb@3sp.com&gt;</a>
43  */

44 public class ServerApplicationLauncher extends Thread JavaDoc {
45     
46     
47     final static Log log = LogFactory.getLog(ServerApplicationLauncher.class);
48     
49     Map JavaDoc<String JavaDoc, String JavaDoc> parameters;
50     boolean running = false;
51     String JavaDoc name;
52     ServerLauncher launcher;
53     String JavaDoc exitMessage = null;
54
55     /**
56      * Constructor.
57      *
58      * @param parameters
59      * @param id
60      * @param session
61      * @param shortcut
62      * @throws IOException
63      */

64     public ServerApplicationLauncher(Map JavaDoc<String JavaDoc, String JavaDoc> parameters, String JavaDoc id, SessionInfo session, ApplicationShortcut shortcut) throws IOException JavaDoc {
65         super(id + " launcher");
66         this.name = id;
67         this.parameters = parameters;
68         running = true;
69         try {
70             launcher = new ServerLauncher(ExtensionStore.getInstance().getExtensionDescriptor(id), session, shortcut, parameters);
71             launcher.prepare();
72             launcher.start();
73             exitMessage = launcher.exitMessage.equals("") ? "OK" : launcher.exitMessage;
74         } catch (Throwable JavaDoc t) {
75             if (t instanceof IOException JavaDoc) {
76                 throw (IOException JavaDoc) t;
77             } else {
78                 /* DEBUG */log.error("Failed to launch. ", t);
79                 throw new IOException JavaDoc("Failed to launch. " + t.getMessage());
80             }
81         }
82     }
83
84     public void run() {
85         FileOutputStream JavaDoc out = null;
86         FileInputStream JavaDoc in = null;
87         File JavaDoc tmp = null;
88         try {
89             tmp = File.createTempFile("montitor", ".out");
90             out = new FileOutputStream JavaDoc(tmp);
91             ProcessMonitor monitor = launcher.getApplicationType().getProcessMonitor();
92             if (monitor != null) {
93                 monitor.watch(out, out);
94             }
95             
96           
97             if(log.isInfoEnabled()) {
98                 String JavaDoc line;
99                 BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in = new FileInputStream JavaDoc(tmp)));
100                 while((line = reader.readLine())!=null) {
101                     log.info(line);
102                 }
103             }
104         } catch (Throwable JavaDoc ex) {
105             log.error("Exception during process monitoring.", ex);
106         } finally {
107             
108             Util.closeStream(out);
109             Util.closeStream(in);
110             Util.delTree(tmp);
111             Util.delTree(launcher.getInstallDir());
112         }
113         running = false;
114
115     }
116 }
Popular Tags