xref: /titanic_41/usr/src/cmd/print/printmgr/com/sun/admin/pm/client/pmUtility.java (revision 62d717f5277d7b19b63db2d800310f877b57c197)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  *
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  *
28  * pmUtility.java
29  * Resource loading and utility classes
30  */
31 
32 package com.sun.admin.pm.client;
33 
34 import java.awt.*;
35 import java.applet.*;
36 import java.io.*;
37 import java.util.*;
38 import javax.swing.*;
39 
40 import com.sun.admin.pm.server.*;
41 
42 
43 /*
44  * Utility class to provide common functions to the printing
45  * manager classes
46  */
47 
48 public class pmUtility {
49 
50 /*
51  * Gets the localized string from the named bundle
52  */
53 
getCopyrightResource(String key)54     public static String getCopyrightResource(String key) {
55 	String keyvalue = null;
56 	ResourceBundle bundle = null;
57 
58 
59 	try {
60 		bundle = ResourceBundle.getBundle(
61 			"com.sun.admin.pm.client.pmCopyright");
62 	} catch (MissingResourceException e) {
63 		Debug.fatal("Could not load pmCopyright file");
64 	}
65 
66 	try {
67 		keyvalue = bundle.getString(key);
68 	} catch (MissingResourceException e) {
69 		Debug.error("CLNT: getCopyrightResource: Missing: " + key);
70            keyvalue = new String("<<" + key + ">>");
71 	}
72 
73 	return keyvalue;
74     }
75 
getResource(String key)76     public static String getResource(String key) {
77 	String keyvalue = null;
78 	ResourceBundle bundle = null;
79 
80 
81 	try {
82 		bundle = ResourceBundle.getBundle(
83 			"com.sun.admin.pm.client.pmResources");
84 	} catch (MissingResourceException e) {
85 		Debug.fatal("Could not load pmResources file");
86 	}
87 
88 	try {
89 		keyvalue = bundle.getString(key);
90 	} catch (MissingResourceException e) {
91 		Debug.error("CLNT: getResource: Missing: " + key);
92            keyvalue = new String("<<" + key + ">>");
93 	}
94 
95 	return keyvalue;
96     }
97 
getIntResource(String key)98     public static int getIntResource(String key) {
99 	int keyvalue = 0;
100 	String s = null;
101 	ResourceBundle bundle = null;
102 
103 	try {
104 		bundle = ResourceBundle.getBundle(
105 			"com.sun.admin.pm.client.pmResources");
106 	} catch (MissingResourceException e) {
107 		Debug.fatal("Could not load pmResources file");
108 	}
109 
110 	try {
111     		s = bundle.getString(key);
112 	} catch (MissingResourceException e) {
113 		Debug.error("Missing: " + key);
114 	}
115 
116 	Debug.message("Resource: " + key + " Value: " + s);
117 
118 	if (s != null) {
119 		try {
120 		    keyvalue = s.charAt(0);
121             	} catch (Exception x) {
122 		    Debug.error("Resource: " + key + " threw: " + x);
123 		}
124         }
125 
126 	return keyvalue;
127     }
128 
doLogin( pmTop mytop, JFrame frame)129     public static void doLogin(
130 	pmTop mytop, JFrame frame) throws pmGuiException {
131 
132 	pmLogin l;
133 
134 	if (mytop.ns.getNameService().equals("nis") ||
135 		mytop.ns.getNameService().equals("ldap")) {
136 
137 	    if (mytop.ns.getNameService().equals("nis")) {
138 
139 		l = new pmLogin(
140 		    frame,
141 		    pmUtility.getResource("NIS.Authentication"),
142 		    pmUtility.getResource("Enter.NIS.authentication.data."),
143 		    mytop,
144 		    "NISAuthentication");
145 
146 	    } else { // LDAP
147 
148 		l = new pmLogin(
149 		    frame,
150 		    pmUtility.getResource("LDAP.Authentication"),
151 		    pmUtility.getResource("Enter.LDAP.authentication.data."),
152 		    mytop,
153 		    "LDAPAuthentication");
154 	    }
155 
156 	    l.setVisible(true);
157 
158 	    if ((l.getValue() != JOptionPane.OK_OPTION) &&
159 		 (l.getValue() != JOptionPane.CANCEL_OPTION)) {
160 
161 			pmMessageDialog m = new pmMessageDialog(
162 				frame,
163 				pmUtility.getResource("Login.Failure"),
164 				pmUtility.getResource(
165 					"Request.cannot.be.completed."));
166 			m.setVisible(true);
167 			throw new pmGuiException
168 				("pmAccess: Cannot create Login screen");
169 	    }
170 
171 
172 	    if (l.getValue() == JOptionPane.CANCEL_OPTION) {
173 		    throw new pmUserCancelledException("User.Cancelled.Login");
174 	    } else {
175 
176 		// Pass data to backend
177 
178 		    // getPassword sends back untrimmed string that is invalid
179 		    // as a password as it's too long
180 		    String tmpp = new String(l.passwordField.getPassword());
181 		    mytop.ns.setPasswd(tmpp.trim());
182 
183 		    if (mytop.ns.getNameService().equals("ldap")) {
184 			// setUser for binddn
185 			mytop.ns.setUser(l.dnField.getText());
186 			// setNameServiceHost overloaded for LDAP server name
187 			mytop.ns.setNameServiceHost(l.serverField.getText());
188 		    }
189 
190 		    try {
191 			mytop.ns.checkAuth();
192 				Debug.message("doLogin():checkauth() OK");
193 		    } catch (Exception e) {
194 			Debug.warning("doLogin:checkAuth()exception " + e);
195 			throw new pmGuiException("Login.Authorization.Failed");
196 		    }
197 	    }
198 
199 
200 	// User has not put in printer or server
201 	} else {
202 	    pmMessageDialog m =
203 		new pmMessageDialog(
204 			frame,
205 			pmUtility.getResource("Login.Failure"),
206 			pmUtility.getResource("Request.cannot.be.completed."),
207 			mytop, "LoginFailed");
208 
209 	    m.setVisible(true);
210 	    throw new pmGuiException("pmAccess: Cannot create Login screen");
211 	}
212 
213     }
214 
215 
216 }
217