1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.ui;
27
28 import com.sshtools.j2ssh.configuration.ConfigurationLoader;
29 import com.sshtools.j2ssh.io.IOUtil;
30 import com.sshtools.j2ssh.util.ExtensionClassLoader;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import java.io.IOException;
36 import java.io.InputStream;
37
38 import java.net.URL;
39
40 import java.util.Enumeration;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Properties;
44 import java.util.Vector;
45
46
47 /**
48 *
49 * <p>This class is responsible for dynamically loading all the installed
50 * session providers. A session provider can be used with
51 * <code>SessionProviderFrame</code> to integrate an ssh service such as
52 * a terminal window or sftp window within another application.</p>
53 *
54 * <p>To install a session provider you should provide a special properties
55 * file resource at the root of your source tree called 'session.provider'.</p>
56 *
57 * <blockquote><pre>
58 * This properties file should have the following properties defined:
59 *
60 * provider.id= [The unique name of the provider e.g 'sshterm']
61 * provider.name= [The descriptive name of the provider e.g. 'Terminal Session']
62 * provider.class= [Fully qualified classname of the provider implementation]
63 * provider.shortdesc= [A short description of the provider]
64 * provider.smallicon= [The providers small icon, must be filename only and be
65 * placed in the same package as the provider class implementation]
66 * provider.largeicon= [The providers large icon, must be filename only and be
67 * placed in the same package as the provider class implementation]
68 * provider.mnemonic= [The mnemonic character]
69 * provider.options= [The options panel implementation, must implement
70 * com.sshtools.common.ui.OptionsTab]
71 * property.page.1= [An number of property page panels, must implement
72 * com.sshtools.common.ui.SshToolsConnectionTab]
73 * property.page.2= [More property pages added like this]
74 * provider.weight= [Weight setting, used to order providers]
75 * </pre></blockquote>
76 *
77 * </p>
78 * @author Lee David Painter
79 * @version $Id: SessionProviderFactory.java,v 1.12 2003/09/22 15:57:57 martianx Exp $
80 */
81 public class SessionProviderFactory {
82 private static Log log = LogFactory.getLog(SessionProviderFactory.class);
83 private static SessionProviderFactory instance;
84 HashMap providers = new HashMap();
85
86 SessionProviderFactory() {
87 ExtensionClassLoader classloader = ConfigurationLoader.getExtensionClassLoader();
88
89 try {
90 Enumeration en = classloader.getResources("session.provider");
91 URL url = null;
92 Properties properties;
93 InputStream in;
94 SessionProvider provider;
95 String name;
96 String id;
97
98 while (en.hasMoreElements()) {
99 try {
100 url = (URL) en.nextElement();
101 in = url.openStream();
102 properties = new Properties();
103 properties.load(in);
104 IOUtil.closeStream(in);
105
106 if (properties.containsKey("provider.class") &&
107 properties.containsKey("provider.name")) {
108 Class cls = classloader.loadClass(properties.getProperty(
109 "provider.class"));
110 String optionsClassName = properties.getProperty(
111 "provider.options");
112 Class optionsClass = ((optionsClassName == null) ||
113 optionsClassName.equals("")) ? null
114 : classloader.loadClass(optionsClassName);
115 String pageclass;
116 int num = 1;
117 Vector pages = new Vector();
118
119 do {
120 pageclass = properties.getProperty("property.page." +
121 String.valueOf(num), null);
122
123 if (pageclass != null) {
124 pages.add(classloader.loadClass(pageclass));
125 num++;
126 }
127 } while (pageclass != null);
128
129 Class[] propertypages = new Class[pages.size()];
130 pages.toArray(propertypages);
131 name = properties.getProperty("provider.name");
132
133 int weight = Integer.parseInt(properties.getProperty(
134 "provider.weight"));
135 id = properties.getProperty("provider.id", name);
136 provider = new SessionProvider(id, name, cls,
137 properties.getProperty("provider.shortdesc"),
138 properties.getProperty("provider.mnemonic"),
139 properties.getProperty("provider.smallicon"),
140 properties.getProperty("provider.largeicon"),
141 optionsClass, propertypages, weight);
142 providers.put(id, provider);
143 log.info("Installed " + provider.getName() +
144 " session provider");
145 }
146 } catch (ClassNotFoundException ex) {
147 log.warn("Session provider class not found", ex);
148 } catch (IOException ex) {
149 log.warn("Failed to read " + url.toExternalForm(), ex);
150 }
151 }
152 } catch (IOException ex) {
153 }
154 }
155
156 /**
157 * Get all the installed SessionProvider's.
158 *
159 * @return A list containing instances of <code>SessionProvider</code>
160 */
161 public List getSessionProviders() {
162 return new Vector(providers.values());
163 }
164
165 /**
166 * <p>Get a <code>SessionProvider</code> by its id. The id is defined by the
167 * provider.id property in the providers 'session.provider' resource file.</p>
168 *
169 * <p>Session providers that are currently defined within the SSHTools source
170 * tree are:<br>
171 * <blockquote><pre>
172 * sshterm - Terminal session provider
173 * shift - SFTP session provider
174 * tunneling - Secure port forwarding provider
175 * sshvnc - VNC session provider
176 * </pre></blockquote>
177 *
178 * @param id the id of the SessionProvider.
179 * @return
180 */
181 public SessionProvider getProvider(String id) {
182 return (SessionProvider) providers.get(id);
183 }
184
185 /**
186 * Get the one time instance of the factory.
187 * @return
188 */
189 public static SessionProviderFactory getInstance() {
190 return (instance == null) ? (instance = new SessionProviderFactory())
191 : instance;
192 }
193 }