1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package client;
  24 
  25 import java.security.Provider;
  26 import java.security.Security;
  27 import java.util.Iterator;
  28 import java.util.ServiceLoader;
  29 
  30 /**
  31  * Test JCE client uses different mechanism to find the custom security
  32  * provider. It uses ServiceLoader and ClassLoader to find the TEST provider
  33  * available in classPath/modulePath. It also tries to find, if the provider is
  34  * configured through "java.security" file.
  35  */
  36 public class TestJCEClient {
  37 
  38     private static final String CUSTOM_PROVIDER_NAME = "TEST";
  39     private static final String EXCEPTION_MESSAGE
  40             = "Unable to find Test JCE Provider";
  41     private static final String USE_SERVICE_LOADER = "USE_SERVICE_LOADER";
  42     private static final String USE_CLASS_LOADER = "USE_CLASS_LOADER";
  43 
  44     public static void main(String[] args) {
  45         Provider provider = null;
  46         //Try to find the TEST provider loaded by ServiceLoader.
  47         if (args != null && args.length > 0
  48                 && USE_SERVICE_LOADER.equals(args[0])) {
  49             System.out.println("Using service loader to find JCE provider.");
  50             ServiceLoader<Provider> services
  51                     = ServiceLoader.load(java.security.Provider.class);
  52             Iterator<Provider> iterator = services.iterator();
  53             while (iterator.hasNext()) {
  54                 Provider p = iterator.next();
  55                 if (p.getName().equals(CUSTOM_PROVIDER_NAME)) {
  56                     provider = p;
  57                     break;
  58                 }
  59             }
  60         } else if (args != null && args.length > 0
  61                 && USE_CLASS_LOADER.equals(args[0])) {
  62             System.out.println("Using class loader to find JCE provider.");
  63             //Find the TEST provider loaded by ClassLoader.
  64             provider = new provider.TestJCEProvider();
  65         } else {
  66             //Find the TEST provider configured through Security.getProvider().
  67             System.out.println(
  68                     "Finding JCE provider from Security.getProvider() method.");
  69             provider = Security.getProvider(CUSTOM_PROVIDER_NAME);
  70         }
  71 
  72         if (provider != null) {
  73             System.out.printf("Test JCE provider named '%s' loaded "
  74                     + "successfully", CUSTOM_PROVIDER_NAME);
  75         } else {
  76             throw new RuntimeException(EXCEPTION_MESSAGE);
  77         }
  78     }
  79 }