Provider getService() and getServices() method in Java
Last Updated :
12 Jun, 2019
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:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
Signature sr = Signature.getInstance( "SHA1withDSA" , "SUN" );
Provider provider = sr.getProvider();
Provider.Service service1 = provider
.getService( "Signature" , sr.getAlgorithm());
System.out.println( "Provider service : " + service1);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
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.
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
Signature sr = Signature.getInstance( "SHA1withDSA" , "SUN" );
Provider provider = sr.getProvider();
Provider.Service service1 = provider
.getService( null , sr.getAlgorithm());
System.out.println( "Provider service : " + service1);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
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:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
int i = 5 ;
try {
Signature sr = Signature.getInstance( "SHA1withDSA" , "SUN" );
Provider provider = sr.getProvider();
Set<Provider.Service> servicelist;
servicelist = provider.getServices();
Iterator<Provider.Service> iter = servicelist.iterator();
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);
}
}
}
|
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}
Share your thoughts in the comments
Please Login to comment...