1 /*
   2  * Copyright (c) 2015, 2017, 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 c;
  24 
  25 import java.security.Provider;
  26 import java.security.Security;
  27 import java.util.Iterator;
  28 import java.util.ServiceLoader;
  29 
  30 /**
  31  * A Test client using different mechanism to search the custom security
  32  * provider. It uses ClassLoader, ServiceLoader and default mechanism to find
  33  * a provider registered through "java.security" extension file.
  34  */
  35 public class TestClient {
  36 
  37     public static void main(String[] args) throws Exception {
  38 
  39         Provider p = null;
  40         if (args != null && args.length > 1) {
  41             switch (args[0]) {
  42                 case "CL":
  43                     p = (Provider) Class.forName(args[1]).newInstance();
  44                     if (Security.addProvider(p) == -1) {
  45                         throw new RuntimeException("Failed to add provider");
  46                     }
  47                     break;
  48                 case "SL":
  49                     ServiceLoader<Provider> services
  50                             = ServiceLoader.load(java.security.Provider.class);
  51                     Iterator<Provider> iterator = services.iterator();
  52                     while (iterator.hasNext()) {
  53                         Provider spr = iterator.next();
  54                         if (spr.getName().equals(args[1])) {
  55                             p = spr;
  56                             if (Security.addProvider(p) == -1) {
  57                                 throw new RuntimeException(
  58                                         "Failed to add provider");
  59                             }
  60                             break;
  61                         }
  62                     }
  63                     break;
  64                 case "SPN":
  65                 case "SPT":
  66                     p = Security.getProvider(args[1]);
  67                     break;
  68                 default:
  69                     throw new RuntimeException("Invalid argument.");
  70             }
  71         }
  72         if (p == null) {
  73             throw new RuntimeException("Provider TestProvider not found");
  74         }
  75         System.out.printf("Client: found provider %s", p.getName());
  76     }
  77 }