001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.pgp;
019:
020: import java.util.Hashtable;
021: import java.util.Map;
022: import java.util.Properties;
023: import java.util.logging.Logger;
024:
025: import org.columba.api.plugin.IExtension;
026: import org.columba.api.plugin.IExtensionHandler;
027: import org.columba.api.plugin.PluginHandlerNotFoundException;
028: import org.columba.core.gui.externaltools.ExternalToolsManager;
029: import org.columba.core.logging.Logging;
030: import org.columba.core.plugin.PluginManager;
031: import org.columba.mail.config.MailConfig;
032: import org.columba.mail.config.SecurityItem;
033: import org.waffel.jscf.JSCFConnection;
034: import org.waffel.jscf.JSCFDriverManager;
035: import org.waffel.jscf.JSCFException;
036: import org.waffel.jscf.gpg.GPGDriver;
037:
038: /**
039: * The <code>JSCFController</code> controls JSCFDrivers and Connections. It
040: * chaches for each account the connection to JSCFDrivers. The
041: * <code>JSCFController</code> uses the "singleton pattern", which mean, that
042: * you should access it, using the <code>getInstcane</code> method.
043: *
044: * @author waffel
045: */
046: public class JSCFController {
047:
048: /** JDK 1.4+ logging framework logger, used for logging. */
049: private static final Logger LOG = Logger
050: .getLogger("org.columba.mail.pgp");
051:
052: private static JSCFController myInstance = null;
053:
054: private static Map connectionMap;
055:
056: /**
057: * Gives a instance of the <code>JSCFController</code> back. If no
058: * instance was created before, a new instance will be created.
059: *
060: * @return A Instance of <code>JSCFController</code>
061: */
062: public static JSCFController getInstance() {
063: if (myInstance == null) {
064: myInstance = new JSCFController();
065: registerDrivers();
066: connectionMap = new Hashtable();
067: }
068:
069: return myInstance;
070: }
071:
072: private static void registerDrivers() {
073: try {
074: // at the moment we are only supporting gpg. So let us code hard
075: // here the gpg driver
076: JSCFDriverManager.registerJSCFDriver(new GPGDriver());
077: } catch (JSCFException e) {
078:
079: e.printStackTrace();
080: }
081: }
082:
083: /**
084: * Creates a new Connection to the gpg driver, if the connection for the
085: * given <code>userID</code> are not exists. Properties for the connection
086: * are created by using the SecurityItem from the <code>AccountItem</code>.
087: * Properties like PATH and the GPG USERID are stored for the connection, if
088: * the connection are not exists.
089: *
090: * @param userID
091: * UserID from which the connection should give back
092: * @return a alrady etablished connection for this user or a newly created
093: * connection for this userID, if no connection exists for the
094: * userID
095: * @throws JSCFException
096: * If there are several Driver problems
097: */
098: public JSCFConnection getConnection(String userID)
099: throws JSCFException {
100: SecurityItem pgpItem = MailConfig.getInstance()
101: .getAccountList().getDefaultAccount().getPGPItem();
102: JSCFConnection con = (JSCFConnection) connectionMap.get(userID);
103:
104: if (con == null) {
105: LOG.fine("no connection for userID (" + userID
106: + ") found. Creating a new Connection.");
107: // let us hard coding the gpg for each connection. Later we should
108: // support also other variants (like smime)
109: con = JSCFDriverManager.getConnection("jscf:gpg:");
110:
111: // getting the path to gpg
112:
113: IExtensionHandler handler = null;
114: String path = null;
115: try {
116: LOG.fine("try to get the handler");
117: handler = PluginManager.getInstance()
118: .getExtensionHandler(
119: "org.columba.core.externaltools");
120: LOG.fine("recived Handler ... getting path from it");
121: path = ExternalToolsManager.getInstance()
122: .getLocationOfExternalTool("gpg").getPath();
123: LOG.fine("setting path: " + path);
124: } catch (PluginHandlerNotFoundException e) {
125: LOG.fine("PluginHandler not found" + e);
126: if (Logging.DEBUG) {
127: e.printStackTrace();
128: }
129: }
130:
131: /*
132: * if (path == null) { throw new ProgramNotFoundException("invalid
133: * path"); }
134: */
135: Properties props = con.getProperties();
136: if (path == null) {
137: throw new ProgramNotFoundException("invalid path");
138: }
139: props.put("PATH", path);
140: if (handler != null) {
141: IExtension extension = handler.getExtension("gpg");
142:
143: LOG.fine("gpg userId: "
144: + extension.getMetadata().getId());
145: }
146: LOG.info("gpg path: " + props.get("PATH"));
147: props.put("USERID", pgpItem.get("id"));
148: LOG.info("current gpg userID: " + props.get("USERID"));
149: con.setProperties(props);
150: connectionMap.put(userID, con);
151: }
152:
153: return con;
154: }
155:
156: /**
157: * Creates a new JSCFConnection for the current used Account. The current
158: * used Account is determind from the AccountItem. This method calls only
159: * {@link #getConnection(String)}with the <code>id</code> from the
160: * SecurityItem.
161: *
162: * @return a alrady etablished connection for the current account or a newly
163: * created connection for the current account, if no connection
164: * exists for the current account
165: * @throws JSCFException
166: * If there are several Driver problems
167: */
168: public JSCFConnection getConnection() throws JSCFException {
169: SecurityItem pgpItem = MailConfig.getInstance()
170: .getAccountList().getDefaultAccount().getPGPItem();
171:
172: return getConnection(pgpItem.get("id"));
173: }
174: }
|