當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。