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  * Modular test for client using 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 TestSecurityProviderClient {
  37 
  38     private static final String CUSTOM_PROVIDER_NAME = "TEST";
  39     private static final String EXCEPTION_MESSAGE
  40             = "Unable to find Test Security Provider";
  41     private static final String SERVICE_LOADER = "SERVICE_LOADER";
  42     private static final String CLASS_LOADER = "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                 && SERVICE_LOADER.equals(args[0])) {
  49             System.out.println(
  50                     "Using service loader to find Security provider.");
  51             ServiceLoader<Provider> services
  52                     = ServiceLoader.load(java.security.Provider.class);
  53             Iterator<Provider> iterator = services.iterator();
  54             while (iterator.hasNext()) {
  55                 Provider p = iterator.next();
  56                 if (p.getName().equals(CUSTOM_PROVIDER_NAME)) {
  57                     provider = p;
  58                     break;
  59                 }
  60             }
  61         } else if (args != null && args.length > 0
  62                 && CLASS_LOADER.equals(args[0])) {
  63             System.out.println("Using class loader to find Security provider.");
  64             //Find the TEST provider loaded by ClassLoader.
  65             provider = new provider.TestSecurityProvider();
  66         } else {
  67             //Find the TEST provider configured through Security.getProvider().
  68             System.out.println("Finding Security provider through"
  69                     + " Security.getProvider().");
  70             provider = Security.getProvider(CUSTOM_PROVIDER_NAME);
  71         }
  72 
  73         if (provider != null) {
  74             System.out.format("%nTest Security provider named '%s' loaded "
  75                     + "successfully", CUSTOM_PROVIDER_NAME);
  76         } else {
  77             throw new RuntimeException(EXCEPTION_MESSAGE);
  78         }
  79     }
  80 }