jdk/src/share/classes/sun/security/jgss/krb5/SubjectComber.java
changeset 10336 0bb1999251f8
parent 9499 f3115698a012
child 15006 10d6aacdd67f
equal deleted inserted replaced
10335:3c7eda3ab2f5 10336:0bb1999251f8
    54     }
    54     }
    55 
    55 
    56     static <T> T find(Subject subject, String serverPrincipal,
    56     static <T> T find(Subject subject, String serverPrincipal,
    57         String clientPrincipal, Class<T> credClass) {
    57         String clientPrincipal, Class<T> credClass) {
    58 
    58 
    59         return (T)findAux(subject, serverPrincipal, clientPrincipal, credClass,
    59         // findAux returns T if oneOnly.
    60             true);
    60         return credClass.cast(findAux(subject, serverPrincipal,
       
    61                                       clientPrincipal, credClass, true));
    61     }
    62     }
    62 
    63 
       
    64     @SuppressWarnings("unchecked") // findAux returns List<T> if !oneOnly.
    63     static <T> List<T> findMany(Subject subject, String serverPrincipal,
    65     static <T> List<T> findMany(Subject subject, String serverPrincipal,
    64         String clientPrincipal, Class<T> credClass) {
    66         String clientPrincipal, Class<T> credClass) {
    65 
    67 
    66         return (List<T>)findAux(subject, serverPrincipal, clientPrincipal, credClass,
    68         return (List<T>)findAux(subject, serverPrincipal, clientPrincipal,
    67             false);
    69             credClass, false);
    68     }
    70     }
    69 
    71 
    70     /**
    72     /**
    71      * Find private credentials for the specified client/server principals
    73      * Find private credentials for the specified client/server principals
    72      * in the subject. Returns null if the subject is null.
    74      * in the subject. Returns null if the subject is null.
    73      *
    75      *
    74      * @return the private credentials
    76      * @return the private credentials
    75      */
    77      */
       
    78     // Returns T if oneOnly and List<T> if !oneOnly.
    76     private static <T> Object findAux(Subject subject, String serverPrincipal,
    79     private static <T> Object findAux(Subject subject, String serverPrincipal,
    77         String clientPrincipal, Class<T> credClass, boolean oneOnly) {
    80         String clientPrincipal, Class<T> credClass, boolean oneOnly) {
    78 
    81 
    79         if (subject == null) {
    82         if (subject == null) {
    80             return null;
    83             return null;
    96                         answer.add(t);
    99                         answer.add(t);
    97                     }
   100                     }
    98                 }
   101                 }
    99             } else if (credClass == KerberosKey.class) {
   102             } else if (credClass == KerberosKey.class) {
   100                 // We are looking for credentials for the serverPrincipal
   103                 // We are looking for credentials for the serverPrincipal
   101                 Iterator<T> iterator =
   104                 Iterator<KerberosKey> iterator =
   102                     subject.getPrivateCredentials(credClass).iterator();
   105                     subject.getPrivateCredentials(KerberosKey.class).iterator();
   103                 while (iterator.hasNext()) {
   106                 while (iterator.hasNext()) {
   104                     T t = iterator.next();
   107                     KerberosKey t = iterator.next();
   105                     String name = ((KerberosKey)t).getPrincipal().getName();
   108                     String name = t.getPrincipal().getName();
   106                     if (serverPrincipal == null || serverPrincipal.equals(name)) {
   109                     if (serverPrincipal == null || serverPrincipal.equals(name)) {
   107                          if (DEBUG) {
   110                          if (DEBUG) {
   108                              System.out.println("Found " +
   111                              System.out.println("Found " +
   109                                      credClass.getSimpleName() + " for " + name);
   112                                      credClass.getSimpleName() + " for " + name);
   110                          }
   113                          }
   114                              if (serverPrincipal == null) {
   117                              if (serverPrincipal == null) {
   115                                  // Record name so that keys returned will all
   118                                  // Record name so that keys returned will all
   116                                  // belong to the same principal
   119                                  // belong to the same principal
   117                                  serverPrincipal = name;
   120                                  serverPrincipal = name;
   118                              }
   121                              }
   119                              answer.add(t);
   122                              answer.add(credClass.cast(t));
   120                          }
   123                          }
   121                     }
   124                     }
   122                 }
   125                 }
   123             } else if (credClass == KerberosTicket.class) {
   126             } else if (credClass == KerberosTicket.class) {
   124                 // we are looking for a KerberosTicket credentials
   127                 // we are looking for a KerberosTicket credentials
   127                 synchronized (pcs) {
   130                 synchronized (pcs) {
   128                     Iterator<Object> iterator = pcs.iterator();
   131                     Iterator<Object> iterator = pcs.iterator();
   129                     while (iterator.hasNext()) {
   132                     while (iterator.hasNext()) {
   130                         Object obj = iterator.next();
   133                         Object obj = iterator.next();
   131                         if (obj instanceof KerberosTicket) {
   134                         if (obj instanceof KerberosTicket) {
       
   135                             @SuppressWarnings("unchecked")
   132                             KerberosTicket ticket = (KerberosTicket)obj;
   136                             KerberosTicket ticket = (KerberosTicket)obj;
   133                             if (DEBUG) {
   137                             if (DEBUG) {
   134                                 System.out.println("Found ticket for "
   138                                 System.out.println("Found ticket for "
   135                                                     + ticket.getClient()
   139                                                     + ticket.getClient()
   136                                                     + " to go to "
   140                                                     + " to go to "
   178                                             }
   182                                             }
   179                                             if (serverPrincipal == null) {
   183                                             if (serverPrincipal == null) {
   180                                                 serverPrincipal =
   184                                                 serverPrincipal =
   181                                                 ticket.getServer().getName();
   185                                                 ticket.getServer().getName();
   182                                             }
   186                                             }
   183                                             answer.add((T)ticket);
   187                                             answer.add(credClass.cast(ticket));
   184                                         }
   188                                         }
   185                                     }
   189                                     }
   186                                 }
   190                                 }
   187                             }
   191                             }
   188                         }
   192                         }