KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > demo > sas > KerberosClient


1 package org.jacorb.demo.sas;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileReader JavaDoc;
6 import java.security.Principal JavaDoc;
7 import java.security.PrivilegedAction JavaDoc;
8
9 import javax.security.auth.Subject JavaDoc;
10 import javax.security.auth.login.LoginContext JavaDoc;
11 import javax.security.auth.login.LoginException JavaDoc;
12
13 import org.omg.CORBA.ORB JavaDoc;
14
15 /**
16  * This is the client side of the sas demo. It just calls the single
17  * operation "printCert()" of the server. As you can see, sas is fully
18  * transparent.
19  *
20  * @author Nicolas Noffke
21  * @version $Id: KerberosClient.java,v 1.2 2004/02/05 10:49:54 nick.cross Exp $
22  */

23
24 public class KerberosClient {
25     private static Principal JavaDoc myPrincipal = null;
26     private static Subject JavaDoc mySubject = null;
27     private static ORB JavaDoc orb = null;
28
29     public KerberosClient(String JavaDoc args[]) {
30
31         try {
32             // initialize the ORB.
33
orb = ORB.init(args, null);
34
35             // get the server
36
File JavaDoc f = new File JavaDoc(args[0]);
37             if (!f.exists()) {
38                 System.out.println("File " + args[0] + " does not exist.");
39                 System.exit(-1);
40             }
41             if (f.isDirectory()) {
42                 System.out.println("File " + args[0] + " is a directory.");
43                 System.exit(-1);
44             }
45             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
46             org.omg.CORBA.Object JavaDoc obj = orb.string_to_object(br.readLine());
47             br.close();
48             SASDemo demo = SASDemoHelper.narrow(obj);
49
50             //call single operation
51
demo.printSAS();
52             demo.printSAS();
53             demo.printSAS();
54
55             System.out.println("Call to server succeeded");
56         } catch (Exception JavaDoc ex) {
57             ex.printStackTrace();
58         }
59     }
60
61     public static void main(String JavaDoc args[]) {
62         if (args.length != 3) {
63             System.out.println("Usage: java demo.sas.KerberosClient <ior_file> <username> <password>");
64             System.exit(1);
65         }
66
67         // login - with Kerberos
68
LoginContext JavaDoc loginContext = null;
69         try {
70             JaasTxtCalbackHandler txtHandler = new JaasTxtCalbackHandler();
71             txtHandler.setMyUsername(args[1]);
72             txtHandler.setMyPassword(args[2].toCharArray());
73             loginContext = new LoginContext JavaDoc("KerberosClient", txtHandler);
74             loginContext.login();
75         } catch (LoginException JavaDoc le) {
76             System.out.println("Login error: " + le);
77             System.exit(1);
78         }
79         mySubject = loginContext.getSubject();
80         myPrincipal = (Principal JavaDoc) mySubject.getPrincipals().iterator().next();
81         System.out.println("Found principal " + myPrincipal.getName());
82
83         // run in privileged mode
84
final String JavaDoc[] finalArgs = args;
85         try {
86             Subject.doAs(mySubject, new PrivilegedAction JavaDoc() {
87                 public Object JavaDoc run() {
88                     try {
89                         KerberosClient client = new KerberosClient(finalArgs);
90                         orb.run();
91                     } catch (Exception JavaDoc e) {
92                         System.out.println("Error running program: "+e);
93                     }
94                     System.out.println("Exiting privileged operation");
95                     return null;
96                 }
97             });
98         } catch (Exception JavaDoc e) {
99             System.out.println("Error running privileged: "+e);
100         }
101     }
102 }
103
Popular Tags