KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > pgp > JSCFController


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.pgp;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24
25 import org.columba.api.plugin.IExtension;
26 import org.columba.api.plugin.IExtensionHandler;
27 import org.columba.api.plugin.PluginHandlerNotFoundException;
28 import org.columba.core.gui.externaltools.ExternalToolsManager;
29 import org.columba.core.logging.Logging;
30 import org.columba.core.plugin.PluginManager;
31 import org.columba.mail.config.MailConfig;
32 import org.columba.mail.config.SecurityItem;
33 import org.waffel.jscf.JSCFConnection;
34 import org.waffel.jscf.JSCFDriverManager;
35 import org.waffel.jscf.JSCFException;
36 import org.waffel.jscf.gpg.GPGDriver;
37
38 /**
39  * The <code>JSCFController</code> controls JSCFDrivers and Connections. It
40  * chaches for each account the connection to JSCFDrivers. The
41  * <code>JSCFController</code> uses the "singleton pattern", which mean, that
42  * you should access it, using the <code>getInstcane</code> method.
43  *
44  * @author waffel
45  */

46 public class JSCFController {
47
48     /** JDK 1.4+ logging framework logger, used for logging. */
49     private static final Logger JavaDoc LOG = Logger.getLogger("org.columba.mail.pgp");
50
51     private static JSCFController myInstance = null;
52
53     private static Map JavaDoc connectionMap;
54
55     /**
56      * Gives a instance of the <code>JSCFController</code> back. If no
57      * instance was created before, a new instance will be created.
58      *
59      * @return A Instance of <code>JSCFController</code>
60      */

61     public static JSCFController getInstance() {
62         if (myInstance == null) {
63             myInstance = new JSCFController();
64             registerDrivers();
65             connectionMap = new Hashtable JavaDoc();
66         }
67
68         return myInstance;
69     }
70
71     private static void registerDrivers() {
72         try {
73             // at the moment we are only supporting gpg. So let us code hard
74
// here the gpg driver
75
JSCFDriverManager.registerJSCFDriver(new GPGDriver());
76         } catch (JSCFException e) {
77
78             e.printStackTrace();
79         }
80     }
81
82     /**
83      * Creates a new Connection to the gpg driver, if the connection for the
84      * given <code>userID</code> are not exists. Properties for the connection
85      * are created by using the SecurityItem from the <code>AccountItem</code>.
86      * Properties like PATH and the GPG USERID are stored for the connection, if
87      * the connection are not exists.
88      *
89      * @param userID
90      * UserID from which the connection should give back
91      * @return a alrady etablished connection for this user or a newly created
92      * connection for this userID, if no connection exists for the
93      * userID
94      * @throws JSCFException
95      * If there are several Driver problems
96      */

97     public JSCFConnection getConnection(String JavaDoc userID) throws JSCFException {
98         SecurityItem pgpItem = MailConfig.getInstance().getAccountList()
99                 .getDefaultAccount().getPGPItem();
100         JSCFConnection con = (JSCFConnection) connectionMap.get(userID);
101
102         if (con == null) {
103             LOG.fine("no connection for userID (" + userID
104                     + ") found. Creating a new Connection.");
105             // let us hard coding the gpg for each connection. Later we should
106
// support also other variants (like smime)
107
con = JSCFDriverManager.getConnection("jscf:gpg:");
108
109             // getting the path to gpg
110

111             IExtensionHandler handler = null;
112             String JavaDoc path = null;
113             try {
114                 LOG.fine("try to get the handler");
115                 handler = PluginManager
116                         .getInstance().getExtensionHandler(
117                                 "org.columba.core.externaltools");
118                 LOG.fine("recived Handler ... getting path from it");
119                 path = ExternalToolsManager.getInstance()
120                         .getLocationOfExternalTool("gpg").getPath();
121                 LOG.fine("setting path: " + path);
122             } catch (PluginHandlerNotFoundException e) {
123                 LOG.fine("PluginHandler not found" + e);
124                 if (Logging.DEBUG) {
125                     e.printStackTrace();
126                 }
127             }
128
129             /*
130              * if (path == null) { throw new ProgramNotFoundException("invalid
131              * path"); }
132              */

133             Properties JavaDoc props = con.getProperties();
134             if (path == null) {
135                 throw new ProgramNotFoundException("invalid path");
136             }
137             props.put("PATH", path);
138             if (handler != null) {
139                 IExtension extension = handler.getExtension("gpg");
140
141                 LOG.fine("gpg userId: " + extension.getMetadata().getId());
142             }
143             LOG.info("gpg path: " + props.get("PATH"));
144             props.put("USERID", pgpItem.get("id"));
145             LOG.info("current gpg userID: " + props.get("USERID"));
146             con.setProperties(props);
147             connectionMap.put(userID, con);
148         }
149
150         return con;
151     }
152
153     /**
154      * Creates a new JSCFConnection for the current used Account. The current
155      * used Account is determind from the AccountItem. This method calls only
156      * {@link #getConnection(String)}with the <code>id</code> from the
157      * SecurityItem.
158      *
159      * @return a alrady etablished connection for the current account or a newly
160      * created connection for the current account, if no connection
161      * exists for the current account
162      * @throws JSCFException
163      * If there are several Driver problems
164      */

165     public JSCFConnection getConnection() throws JSCFException {
166         SecurityItem pgpItem = MailConfig.getInstance().getAccountList()
167                 .getDefaultAccount().getPGPItem();
168
169         return getConnection(pgpItem.get("id"));
170     }
171 }
Popular Tags