001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
029: import com.sshtools.j2ssh.io.IOUtil;
030: import com.sshtools.j2ssh.util.ExtensionClassLoader;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import java.io.IOException;
036: import java.io.InputStream;
037:
038: import java.net.URL;
039:
040: import java.util.Enumeration;
041: import java.util.HashMap;
042: import java.util.List;
043: import java.util.Properties;
044: import java.util.Vector;
045:
046: /**
047: *
048: * <p>This class is responsible for dynamically loading all the installed
049: * session providers. A session provider can be used with
050: * <code>SessionProviderFrame</code> to integrate an ssh service such as
051: * a terminal window or sftp window within another application.</p>
052: *
053: * <p>To install a session provider you should provide a special properties
054: * file resource at the root of your source tree called 'session.provider'.</p>
055: *
056: * <blockquote><pre>
057: * This properties file should have the following properties defined:
058: *
059: * provider.id= [The unique name of the provider e.g 'sshterm']
060: * provider.name= [The descriptive name of the provider e.g. 'Terminal Session']
061: * provider.class= [Fully qualified classname of the provider implementation]
062: * provider.shortdesc= [A short description of the provider]
063: * provider.smallicon= [The providers small icon, must be filename only and be
064: * placed in the same package as the provider class implementation]
065: * provider.largeicon= [The providers large icon, must be filename only and be
066: * placed in the same package as the provider class implementation]
067: * provider.mnemonic= [The mnemonic character]
068: * provider.options= [The options panel implementation, must implement
069: * com.sshtools.common.ui.OptionsTab]
070: * property.page.1= [An number of property page panels, must implement
071: * com.sshtools.common.ui.SshToolsConnectionTab]
072: * property.page.2= [More property pages added like this]
073: * provider.weight= [Weight setting, used to order providers]
074: * </pre></blockquote>
075: *
076: * </p>
077: * @author Lee David Painter
078: * @version $Id: SessionProviderFactory.java,v 1.12 2003/09/22 15:57:57 martianx Exp $
079: */
080: public class SessionProviderFactory {
081: private static Log log = LogFactory
082: .getLog(SessionProviderFactory.class);
083: private static SessionProviderFactory instance;
084: HashMap providers = new HashMap();
085:
086: SessionProviderFactory() {
087: ExtensionClassLoader classloader = ConfigurationLoader
088: .getExtensionClassLoader();
089:
090: try {
091: Enumeration en = classloader
092: .getResources("session.provider");
093: URL url = null;
094: Properties properties;
095: InputStream in;
096: SessionProvider provider;
097: String name;
098: String id;
099:
100: while (en.hasMoreElements()) {
101: try {
102: url = (URL) en.nextElement();
103: in = url.openStream();
104: properties = new Properties();
105: properties.load(in);
106: IOUtil.closeStream(in);
107:
108: if (properties.containsKey("provider.class")
109: && properties.containsKey("provider.name")) {
110: Class cls = classloader.loadClass(properties
111: .getProperty("provider.class"));
112: String optionsClassName = properties
113: .getProperty("provider.options");
114: Class optionsClass = ((optionsClassName == null) || optionsClassName
115: .equals("")) ? null : classloader
116: .loadClass(optionsClassName);
117: String pageclass;
118: int num = 1;
119: Vector pages = new Vector();
120:
121: do {
122: pageclass = properties
123: .getProperty("property.page."
124: + String.valueOf(num), null);
125:
126: if (pageclass != null) {
127: pages.add(classloader
128: .loadClass(pageclass));
129: num++;
130: }
131: } while (pageclass != null);
132:
133: Class[] propertypages = new Class[pages.size()];
134: pages.toArray(propertypages);
135: name = properties.getProperty("provider.name");
136:
137: int weight = Integer.parseInt(properties
138: .getProperty("provider.weight"));
139: id = properties
140: .getProperty("provider.id", name);
141: provider = new SessionProvider(
142: id,
143: name,
144: cls,
145: properties
146: .getProperty("provider.shortdesc"),
147: properties
148: .getProperty("provider.mnemonic"),
149: properties
150: .getProperty("provider.smallicon"),
151: properties
152: .getProperty("provider.largeicon"),
153: optionsClass, propertypages, weight);
154: providers.put(id, provider);
155: log.info("Installed " + provider.getName()
156: + " session provider");
157: }
158: } catch (ClassNotFoundException ex) {
159: log.warn("Session provider class not found", ex);
160: } catch (IOException ex) {
161: log.warn("Failed to read " + url.toExternalForm(),
162: ex);
163: }
164: }
165: } catch (IOException ex) {
166: }
167: }
168:
169: /**
170: * Get all the installed SessionProvider's.
171: *
172: * @return A list containing instances of <code>SessionProvider</code>
173: */
174: public List getSessionProviders() {
175: return new Vector(providers.values());
176: }
177:
178: /**
179: * <p>Get a <code>SessionProvider</code> by its id. The id is defined by the
180: * provider.id property in the providers 'session.provider' resource file.</p>
181: *
182: * <p>Session providers that are currently defined within the SSHTools source
183: * tree are:<br>
184: * <blockquote><pre>
185: * sshterm - Terminal session provider
186: * shift - SFTP session provider
187: * tunneling - Secure port forwarding provider
188: * sshvnc - VNC session provider
189: * </pre></blockquote>
190: *
191: * @param id the id of the SessionProvider.
192: * @return
193: */
194: public SessionProvider getProvider(String id) {
195: return (SessionProvider) providers.get(id);
196: }
197:
198: /**
199: * Get the one time instance of the factory.
200: * @return
201: */
202: public static SessionProviderFactory getInstance() {
203: return (instance == null) ? (instance = new SessionProviderFactory())
204: : instance;
205: }
206: }
|