View Javadoc

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