Provider getService() and getServices() method in Java

getService( String type, String algorithm)

The getService() method of java.security.Provider class is used to get the service describing this Provider’s implementation of the specified type of this algorithm or alias.

If no such implementation exists, this method returns null. If there are two matching services, one added to this provider using putService() and one added via put(), the service added via putService() is returned.

Syntax:

public Provider.Service getService(String type, String algorithm)

Parameters: This method takes the following argument as a parameter.

  • type– the type of service requested.
  • algorithm– the case insensitive algorithm name (or alternate alias) of the service requested.

Return Value: This method returns the service describing this Provider’s matching service or null if no such service exists.



Exception: This method throws NullPointerException if type or algorithm is null.

Below are the examples to illustrate the getService() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getService() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
            // creating the object of  SecureRandom
            Signature sr = Signature.getInstance("SHA1withDSA", "SUN");
  
            // getting the Provider of the SecureRandom sr
            // by using method getProvider()
            Provider provider = sr.getProvider();
  
            // getting the service of the provider using getServices() method
            Provider.Service service1 = provider
                                            .getService("Signature", sr.getAlgorithm());
  
            // printing the service
            System.out.println("Provider service : " + service1);
        }
  
        catch (NoSuchAlgorithmException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Provider service : SUN: Signature.SHA1withDSA -> sun.security.provider.DSA$SHA1withDSA
  aliases: [DSA, DSS, SHA/DSA, SHA-1/DSA, SHA1/DSA, SHAwithDSA, DSAWithSHA1, OID.1.2.840.10040.4.3, 1.2.840.10040.4.3, 1.3.14.3.2.13, 1.3.14.3.2.27]
  attributes: {ImplementedIn=Software, KeySize=1024, SupportedKeyClasses=java.security.interfaces.DSAPublicKey|java.security.interfaces.DSAPrivateKey}

Example 2: To show NullPointerException thrown by getService() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getService() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
            // creating the object of  SecureRandom
            Signature sr = Signature.getInstance("SHA1withDSA", "SUN");
  
            // getting the Provider of the SecureRandom sr
            // by using method getProvider()
            Provider provider = sr.getProvider();
  
            // getting the service of the provider using getServices() method
            Provider.Service service1 = provider
                                            .getService(null, sr.getAlgorithm());
  
            // printing the service
            System.out.println("Provider service : " + service1);
        }
  
        catch (NoSuchAlgorithmException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Exception thrown : java.lang.NullPointerException

getServices()

The getServices() method of java.security.Provider class is used to get an unmodifiable Set of all services supported by this Provider.

Syntax:

public Set<Provider.Service> getServices()

Return Value: This method returns an unmodifiable Set of all services supported by this Provider.

Below are the examples to illustrate the getServices() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getService() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        // Declaring int variable
        int i = 5;
  
        try {
            // creating the object of  SecureRandom
            Signature sr = Signature.getInstance("SHA1withDSA", "SUN");
  
            // getting the Provider of the SecureRandom sr
            // by using method getProvider()
            Provider provider = sr.getProvider();
  
            // Declaring the variable of set<Map> type
            Set<Provider.Service> servicelist;
  
            // getting the service of the provider using getServices() method
            servicelist = provider.getServices();
  
            // Creating the object of iterator to iterate set
            Iterator<Provider.Service> iter = servicelist.iterator();
  
            // printing the set elements
            System.out.println("Provider servicelist : \n ");
            while (i > 0) {
                System.out.println("Value is : " + iter.next());
                i--;
            }
        }
  
        catch (NoSuchAlgorithmException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Provider servicelist : 
 
Value is : SUN: SecureRandom.NativePRNG -> sun.security.provider.NativePRNG

Value is : SUN: SecureRandom.SHA1PRNG -> sun.security.provider.SecureRandom
  attributes: {ImplementedIn=Software}

Value is : SUN: SecureRandom.NativePRNGBlocking -> sun.security.provider.NativePRNG$Blocking

Value is : SUN: SecureRandom.NativePRNGNonBlocking -> sun.security.provider.NativePRNG$NonBlocking

Value is : SUN: Signature.SHA1withDSA -> sun.security.provider.DSA$SHA1withDSA
  aliases: [DSA, DSS, SHA/DSA, SHA-1/DSA, SHA1/DSA, SHAwithDSA, DSAWithSHA1, OID.1.2.840.10040.4.3, 1.2.840.10040.4.3, 1.3.14.3.2.13, 1.3.14.3.2.27]
  attributes: {ImplementedIn=Software, KeySize=1024, SupportedKeyClasses=java.security.interfaces.DSAPublicKey|java.security.interfaces.DSAPrivateKey}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai