xref: /titanic_50/usr/src/cmd/print/printmgr/com/sun/admin/pm/server/DoPrinterDelete.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 (c) 1999 by Sun Microsystems, Inc.
26  * All rights reserved.
27  *
28  * DoPrinterDelete class
29  * Worker class for deleting a printer.
30  */
31 
32 package com.sun.admin.pm.server;
33 
34 import java.io.*;
35 
36 public class  DoPrinterDelete {
37 
38     //
39     // main for testing
40     //
main(String[] args)41     public static void main(String[] args) {
42 
43 	Printer p = null;
44 	try {
45 		NameService ns = new NameService();
46 
47 		p = new Printer(ns);
48 		p.setPrinterName("javatest");
49 
50 		delete(p, ns);
51 	}
52 	catch (Exception e)
53 	{
54 		System.out.println(e);
55 		System.exit(1);
56 	}
57 	System.out.println("Commands:\n" + p.getCmdLog());
58 	System.out.println("Errors:\n" + p.getErrorLog());
59 	System.out.println("Warnings:\n" + p.getWarnLog());
60 	System.exit(0);
61     }
62 
63     //
64     // Interface to Printer object.
65     //
delete( Printer p, NameService ns)66     public static void delete(
67 	Printer p,
68 	NameService ns) throws Exception
69     {
70 	Debug.message("SVR: DoPrinterDelete.delete()");
71 
72 	boolean success = true;
73 	String err = null;
74 	String cmd = null;
75 	SysCommand syscmd = null;
76 
77 	String printername = p.getPrinterName();
78 	String nameservice = ns.getNameService();
79 
80 	boolean islocal = DoPrinterUtil.isLocal(printername);
81 
82 	// Take care of locally installed case first.
83 	if (islocal) {
84 		// See if printer is already rejected.
85 		// Don't disable so queue can drain.
86 		Printer curr = new Printer();
87 		curr.setPrinterName(printername);
88 		try {
89 			DoPrinterView.view(curr, ns);
90 		}
91 		catch (Exception e) {
92 			Debug.message("SVR:" + e.getMessage());
93 			curr.setAccept(false);
94 		}
95 
96 		if (curr.getAccept()) {
97 			cmd = "/usr/sbin/reject " + printername;
98 			p.setCmdLog(cmd);
99 			syscmd = new SysCommand();
100 			syscmd.exec(cmd);
101 			err = syscmd.getError();
102 			if (err != null) {
103 				p.setWarnLog(err);
104 			}
105 			syscmd = null;
106 		}
107 		curr = null;
108 		try {
109 			deleteLocal(p);
110 		}
111 		catch (Exception e) {
112 			Debug.message("SVR:" + e.getMessage());
113 			success = false;
114 		}
115 	}
116 
117 	//
118 	// Check if we already removed it from /etc/printers.conf
119 	//
120 	boolean exists;
121 	exists = DoPrinterUtil.exists(printername, "system");
122 	if (nameservice.equals("system")) {
123 		if (exists) {
124 			try {
125 				deleteLocal(p);
126 			}
127 			catch (Exception e) {
128 				Debug.message("SVR:" + e.getMessage());
129 				success = false;
130 			}
131 		}
132 	} else {
133 		if ((nameservice.equals("nis")) && exists) {
134 			//
135 			// Special case if we are nis master
136 			//
137         		Host h = new Host();
138 			String nshost = ns.getNameServiceHost();
139         		String lh = h.getLocalHostName();
140         		if (lh.equals(nshost)) {
141 				try {
142 					deleteLocal(p);
143 				}
144 				catch (Exception e) {
145 					Debug.message("SVR:" + e.getMessage());
146 					success = false;
147 				}
148 			}
149 			h = null;
150 		}
151 		DoPrinterNS.set("delete", p, ns);
152         }
153 	if (!success) {
154 		throw new pmException();
155 	}
156 	return;
157     }
158 
deleteLocal(Printer p)159     private static void deleteLocal(Printer p) throws Exception
160     {
161 	Debug.message("SVR: DoPrinterDelete.deleteLocal()");
162 
163 	String cmd = null;
164 	String err = null;
165 	SysCommand syscmd = null;
166 	String printername = null;
167 
168 	printername = p.getPrinterName();
169 
170 	// Workaround for lpadmin bug not removing default
171 	String def = DoPrinterUtil.getDefault("system");
172 	if ((def != null) && (def.equals(printername))) {
173 		cmd = "/usr/sbin/lpadmin -x _default";
174 		p.setCmdLog(cmd);
175 		syscmd = new SysCommand();
176 		syscmd.exec(cmd);
177 		err = syscmd.getError();
178 		if (err != null) {
179 			p.setWarnLog(err);
180 		}
181 		syscmd = null;
182 	}
183 
184 	cmd = "/usr/sbin/lpadmin -x " + printername;
185 	p.setCmdLog(cmd);
186 	syscmd = new SysCommand();
187 	syscmd.exec(cmd);
188 	err = syscmd.getError();
189 	if (err != null) {
190 		p.setWarnLog(err);
191 	}
192 	if (syscmd.getExitValue() != 0) {
193 		syscmd = null;
194 		throw new pmException(err);
195 	}
196     }
197 }
198