当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Provider getService() and getServices()用法及代码示例


getService( String type, String algorithm)

java.security.Provider类的getService()方法用于获取描述此Provider指定类型的算法或别名实现的服务。

如果不存在这样的实现,则此方法返回null。如果有两个匹配的服务,一个使用putService()添加到此提供程序,另一个通过put()添加,则返回通过putService()添加的服务。

用法:


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

参数:此方法将以下参数作为参数。

  • type-请求的服务类型。
  • algorithm-请求的服务的大小写不敏感算法名称(或备用别名)。

返回值:此方法返回描述此提供者的匹配服务的服务;如果不存在此服务,则返回null。

异常:如果type或algorithm为null,则此方法引发NullPointerException。

下面是说明getService()方法的示例:

范例1:

// 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); 
        } 
    } 
}
输出:
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}

范例2:显示由getService()方法引发的NullPointerException。

// 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); 
        } 
    } 
}
输出:
Exception thrown:java.lang.NullPointerException

getServices()

java.security.Provider类的getServices()方法用于获取此Provider支持的所有服务的不可修改的Set。

用法:

public Set<Provider.Service> getServices()

返回值:此方法返回此提供程序支持的所有服务的不可修改的Set。

下面是说明getServices()方法的示例:

程序1:

// 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); 
        } 
    } 
}
输出:
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}


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Provider getService() and getServices() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。