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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 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 * Test 29 * Command line front end to Printer class 30 * 31 * Takes one argument - the name of a test file 32 * The format of the file is: 33 * Required: 34 * action=[add,delete,modify,view,list] 35 * 36 * Optional: 37 * printername= 38 * printertype= 39 * printserver= 40 * comment= 41 * device= 42 * notify= 43 * protocol= 44 * destination= 45 * extensions= 46 * default_printer=[true,false] 47 * banner= 48 * enable=[true,false] 49 * accept=[true,false] 50 * file_contents=[space seperated list] 51 * user_allow_list=[space seperated list] 52 * user_deny_list=[space seperated list] 53 * nameservice=[system,nis] 54 * nshost= 55 * user= 56 * passwd= 57 * locale= 58 * 59 */ 60 61 package com.sun.admin.pm.server; 62 63 import java.io.*; 64 import java.util.*; 65 66 public class Test { 67 public static void main(String[] args) { 68 String tmpstr; 69 int i; 70 String testfile = ""; 71 if (args.length == 1) { 72 testfile = args[0]; 73 } else { 74 System.out.println("Usage: Test testfile"); 75 System.exit(1); 76 } 77 78 String cmd = "/usr/bin/cat " + testfile; 79 String o = null; 80 try { 81 SysCommand syscmd = new SysCommand(); 82 syscmd.exec(cmd); 83 if (syscmd.getExitValue() != 0) { 84 System.out.println("Problem opening test file"); 85 System.exit(1); 86 } 87 o = syscmd.getOutput(); 88 syscmd = null; 89 } 90 catch (Exception e) 91 { 92 System.out.println(e); 93 System.exit(1); 94 } 95 o = o.concat("\n"); 96 97 String action = getToken(o, "action="); 98 String printername = getToken(o, "printername="); 99 String printertype = getToken(o, "printertype="); 100 String printserver = getToken(o, "printserver="); 101 String comment = getToken(o, "comment="); 102 String device = getToken(o, "device="); 103 String notify = getToken(o, "notify="); 104 String banner = getToken(o, "banner="); 105 String protocol = getToken(o, "protocol="); 106 String destination = getToken(o, "destination="); 107 String extensions = getToken(o, "extensions="); 108 109 String[] file_contents = null; 110 String[] user_allow_list = null; 111 String[] user_deny_list = null; 112 113 StringTokenizer st; 114 tmpstr = getToken(o, "file_contents="); 115 if (tmpstr != null) { 116 st = new StringTokenizer(tmpstr); 117 if (st.countTokens() != 0) { 118 file_contents = new String[st.countTokens()]; 119 for (i = 0; st.hasMoreTokens(); i++) { 120 file_contents[i] = st.nextToken(); 121 } 122 } 123 } 124 tmpstr = getToken(o, "user_allow_list="); 125 if (tmpstr != null) { 126 st = new StringTokenizer(tmpstr); 127 if (st.countTokens() != 0) { 128 user_allow_list = new String[st.countTokens()]; 129 for (i = 0; st.hasMoreTokens(); i++) { 130 user_allow_list[i] = st.nextToken(); 131 } 132 } 133 } 134 tmpstr = getToken(o, "user_deny_list="); 135 if (tmpstr != null) { 136 st = new StringTokenizer(tmpstr); 137 if (st.countTokens() != 0) { 138 user_deny_list = new String[st.countTokens()]; 139 for (i = 0; st.hasMoreTokens(); i++) { 140 user_deny_list[i] = st.nextToken(); 141 } 142 } 143 } 144 145 boolean default_printer = false; 146 boolean enable = false; 147 boolean accept = false; 148 149 tmpstr = getToken(o, "default_printer="); 150 if (tmpstr != null) { 151 if (tmpstr.equals("true")) { 152 default_printer = true; 153 } 154 } 155 tmpstr = getToken(o, "enable="); 156 if (tmpstr != null) { 157 if (tmpstr.equals("true")) { 158 enable = true; 159 } 160 } 161 tmpstr = getToken(o, "accept="); 162 if (tmpstr != null) { 163 if (tmpstr.equals("true")) { 164 accept = true; 165 } 166 } 167 168 String nameservice = getToken(o, "nameservice="); 169 String nshost = getToken(o, "nshost="); 170 String user = getToken(o, "user="); 171 String passwd = getToken(o, "passwd="); 172 String locale = getToken(o, "locale="); 173 174 // 175 // Done parsing. Let's do the work. 176 // 177 Debug.setDebugLevel(Debug.ALL); 178 179 NameService ns = null; 180 try { 181 ns = new NameService(nameservice); 182 } 183 catch (Exception e) 184 { 185 System.out.println(e); 186 System.exit(1); 187 } 188 if (nameservice.equals("nis") || nameservice.equals("ldap")) { 189 if (nshost != null) 190 ns.setNameServiceHost(nshost); 191 if (user != null) 192 ns.setUser(user); 193 if (passwd != null) 194 ns.setPasswd(passwd); 195 } 196 try { 197 ns.checkAuth(); 198 } 199 catch (Exception e) { 200 System.out.println(e); 201 } 202 203 Printer p = new Printer(ns); 204 205 p.setPrinterName(printername); 206 p.setPrinterType(printertype); 207 p.setPrintServer(printserver); 208 p.setFileContents(file_contents); 209 p.setComment(comment); 210 p.setDevice(device); 211 p.setNotify(notify); 212 p.setProtocol(protocol); 213 p.setDestination(destination); 214 p.setIsDefaultPrinter(default_printer); 215 p.setBanner(banner); 216 p.setEnable(enable); 217 p.setAccept(accept); 218 p.setUserAllowList(user_allow_list); 219 p.setUserDenyList(user_deny_list); 220 p.setLocale(locale); 221 222 if (action.equals("list")) { 223 String[] plist = null; 224 try { 225 plist = PrinterUtil.getPrinterList(ns); 226 } 227 catch (Exception e) 228 { 229 System.out.println(e); 230 System.exit(1); 231 } 232 if (plist == null) { 233 System.out.println("No printers"); 234 } else { 235 printPList(plist); 236 } 237 } else if (action.equals("view")) { 238 try { 239 p.getPrinterDetails(); 240 } 241 catch (Exception e) 242 { 243 System.out.println(e); 244 } 245 printView(p); 246 } else if (action.equals("add")) { 247 try { 248 if (device == null) { 249 p.addRemotePrinter(); 250 } else { 251 p.addLocalPrinter(); 252 } 253 } 254 catch (Exception e) 255 { 256 System.out.println(e); 257 } 258 } else if (action.equals("modify")) { 259 try { 260 p.modifyPrinter(); 261 } 262 catch (Exception e) 263 { 264 System.out.println(e); 265 } 266 } else if (action.equals("delete")) { 267 try { 268 p.deletePrinter(); 269 } 270 catch (Exception e) 271 { 272 System.out.println(e); 273 } 274 } else { 275 System.out.println("unknown action"); 276 System.exit(1); 277 } 278 System.out.println("============================"); 279 System.out.println("Commands:\n" + p.getCmdLog()); 280 System.out.println("Errors:\n" + p.getErrorLog()); 281 System.out.println("Warnings:\n" + p.getWarnLog()); 282 System.exit(0); 283 } 284 285 private static String getToken(String str, String sub) 286 { 287 int i = -1; 288 int j = -1; 289 String result = null; 290 291 i = str.indexOf(sub); 292 if (i != -1) { 293 i = i + sub.length(); 294 j = str.indexOf("\n", i); 295 if (j != -1) { 296 result = str.substring(i, j); 297 result = result.trim(); 298 if (result.equals("")) 299 result = null; 300 } 301 } 302 return (result); 303 } 304 305 private static void printPList(String[] list) 306 { 307 if (list == null) 308 return; 309 if (list[0].equals("")) { 310 System.out.println("No printers found"); 311 return; 312 } 313 314 for (int i = 0; i < list.length; ) { 315 System.out.println("name: " + list[i++]); 316 System.out.println("server: " + list[i++]); 317 System.out.println("comment: " + list[i++]); 318 } 319 } 320 321 private static void printView(Printer p) 322 { 323 String arr[]; 324 int i; 325 326 System.out.println("Name: " + p.getPrinterName()); 327 System.out.println("Type: " + p.getPrinterType()); 328 System.out.println("Server: " + p.getPrintServer()); 329 System.out.println("Comment: " + p.getComment()); 330 System.out.println("Device: " + p.getDevice()); 331 System.out.println("Notify: " + p.getNotify()); 332 System.out.println("Protocol: " + p.getProtocol()); 333 System.out.println("Dest: " + p.getDestination()); 334 System.out.println("Extensions: " + p.getExtensions()); 335 System.out.println("Default: " + p.getIsDefaultPrinter()); 336 System.out.println("Banner: " + p.getBanner()); 337 System.out.println("Enable: " + p.getEnable()); 338 System.out.println("Accept: " + p.getAccept()); 339 340 arr = p.getFileContents(); 341 if (arr == null) { 342 System.out.println("Contents: NULL"); 343 } else { 344 System.out.println("Contents:"); 345 for (i = 0; i < arr.length; i++) { 346 System.out.println("\t\t" + arr[i]); 347 } 348 } 349 arr = p.getUserAllowList(); 350 if (arr == null) { 351 System.out.println("Users allow: NULL"); 352 } else { 353 System.out.println("Users allow:"); 354 for (i = 0; i < arr.length; i++) { 355 System.out.println("\t\t" + arr[i]); 356 } 357 } 358 arr = p.getUserDenyList(); 359 if (arr == null) { 360 System.out.println("Users deny: NULL"); 361 } else { 362 System.out.println("Users deny:"); 363 for (i = 0; i < arr.length; i++) { 364 System.out.println("\t\t" + arr[i]); 365 } 366 } 367 368 } 369 } 370