View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.security;
19  
20  import java.security.Provider;
21  import java.security.Security;
22  
23  import javax.activation.CommandMap;
24  import javax.activation.MailcapCommandMap;
25  
26  /***
27   * Security Providers initialization class. The first call of the init method
28   * will have the class loader do the job. This technique ensures proper
29   * initialization without the need of maintaining the
30   * <i>${java_home}/lib/security/java.security</i> file, that would otherwise
31   * need the addition of the following line:
32   * <code>security.provider.<i>n</i>=org.bouncycastle.jce.provider.BouncyCastleProvider</code>.
33   * 
34   * The call also registers to the javamail's MailcapCommandMap the content
35   * handlers that are needed to work with s/mime mails.
36   * 
37   */
38  public class InitJCE {
39      private static String bouncyCastleProviderClassName = "org.bouncycastle.jce.provider.BouncyCastleProvider";
40      private static boolean initialized = false;
41  
42      /***
43       * Method that registers the security provider BouncyCastle as a system
44       * security provider. The provider class is dinamically loaded on runtime so
45       * there is no need to include the bouncycastle jar in the James
46       * distribution. It can be downloaded and installed by the user if she needs
47       * it.
48       */        
49      public static void init() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
50          if (!initialized) {
51              Security.addProvider((Provider)Class.forName(bouncyCastleProviderClassName).newInstance());
52              
53              MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
54  
55              mailcap.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
56              mailcap.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
57              mailcap.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
58              mailcap.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
59              mailcap.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
60  
61              CommandMap.setDefaultCommandMap(mailcap);
62              
63              initialized = true;
64          } else return;
65      }
66  }