KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > print > ManagePrintersTask


1 /**
2  * $Id: ManagePrintersTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2003,2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.print;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.types.Reference;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.AssertableLibDefinition;
37 import com.idaremedia.antx.FixtureInitializer;
38 import com.idaremedia.antx.helpers.Strings;
39 import com.idaremedia.antx.helpers.Tk;
40
41 /**
42  * Helper task to (un)install a default printer registry. If a specific action is not
43  * specified, will default to installing the default printer registry. Usually defined
44  * &lt;manageprinters&gt; and used in an early build initialization target; for
45  * example:<pre>
46  * &lt;printer-registry id="jware.antx.printers"&gt;
47  * &lt;printer .../&gt;
48  * ...
49  * &lt;/printer-registry&gt;
50  *
51  * &lt;target name="--init-all"&gt;
52  * ...
53  * &lt;<b>manageprinters</b> registryid="jware.antx.printers"/&gt;
54  * ...
55  * &lt;/target&gt;
56  *
57  * -To remove a previously installed PrinterRegistry use either:-
58  *
59  * &lt;manageprinters action="uninstall" registryid="..."/&gt; -OR-
60  * &lt;manageprinters action="uninstall"/&gt;
61  * </pre>
62  * Only <em>one</em> default printer registry can be active at any one time within
63  * the build-iteration. If &lt;manageprinters&gt; is used to install more than one
64  * registry, the last registry installed wins; the previously installed registries
65  * are overwritten.
66  *
67  * @since JWare/AntX 0.2
68  * @author ssmc, &copy;2002-2003,2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
69  * @version 0.5
70  * @.safety single
71  * @.group api,helper
72  * @see PrinterRegistry
73  * @see PrintTask
74  **/

75
76 public final class ManagePrintersTask extends AssertableLibDefinition
77     implements FixtureInitializer
78 {
79     /**
80      * Initializes an ManagePrintersTask instance.
81      **/

82     public ManagePrintersTask()
83     {
84         super(AntX.print+"ManagePrintersTask:");
85     }
86
87
88
89     /**
90      * Sets the registry which will be made the default by this task.
91      **/

92     public void setRegistryId(Reference ref)
93     {
94         require_(ref!=null,"setRegId- nonzro ref");
95         m_registryReference = ref;
96     }
97
98
99
100     /**
101      * Instructs this task how to manage the target registry.
102      * @param op the operation (one of "install" or "uninstall")
103      **/

104     public void setAction(String JavaDoc op)
105     {
106         m_Op = Tk.lowercaseFrom(op);
107     }
108
109
110
111     /**
112      * Tells this tasks whether the default printer registry should be
113      * stopped on if it already exists.
114      * @param overwrite <i>true</i> to unconditionally replace default registry.
115      * @since JWare/AntX 0.5
116      **/

117     public void setOverwrite(boolean overwrite)
118     {
119         m_willSkip = overwrite ? Boolean.FALSE : Boolean.TRUE;
120     }
121
122
123
124     /**
125      * Returns this task's registry reference. Will return <i>null</i>
126      * if never set. Required for task to execute.
127      **/

128     public final Reference getRegistryReference()
129     {
130         return m_registryReference;
131     }
132
133
134
135     /**
136      * Returns the printer registry this task will try to install
137      * as the build-iteration default. To be called <em>after</em>
138      * this task's configuration has been verified.
139      **/

140     protected final PrinterRegistry getPrinterRegistry()
141     {
142         return (PrinterRegistry)getProject().getReference(getRegistryReference().getRefId());
143     }
144
145
146
147     /**
148      * Returns <i>true</i> if is an installation action (default).
149      **/

150     private boolean isInstall()
151     {
152         return m_Op==null || Strings.INSTALL.equals(m_Op);
153     }
154
155
156
157     /**
158      * Verifies that this task has been completely defined. Ensures the
159      * reference has been specified and exists for an installation operation.
160      * @throws BuildException if verification fails
161      **/

162     public void verifyCanExecute_(String JavaDoc calr) throws BuildException
163     {
164         super.verifyCanExecute_(calr);
165         Reference r = getRegistryReference();
166
167         String JavaDoc ermsg = null;
168         if (r==null || r.getRefId()==null) {
169             if (isInstall()) {
170                 ermsg = uistrs().get("task.needs.this.attr",getTaskName(),"registryid");
171             }
172         } else {
173             Object JavaDoc object = getProject().getReference(r.getRefId());
174             if (!(object instanceof PrinterRegistry)) {
175                 ermsg = uistrs().get("initprint.bad.registryid",r.getRefId());
176             }
177         }
178         if (ermsg!=null) {
179             log(ermsg,Project.MSG_ERR);
180             throw new BuildException(ermsg,getLocation());
181         }
182     }
183
184
185
186     /**
187      * Tries to install or uninstall a {@linkplain PrinterRegistry} as the
188      * default registry for all threads within a single build-iteration.
189      * @throws BuildException if incompletely defined (no reference)
190      **/

191     public void execute() throws BuildException
192     {
193         verifyCanExecute_("execute");
194
195         //Install
196
if (isInstall()) {
197             PrinterRegistry newpr = getPrinterRegistry();
198             PrinterRegistry oldpr = PrinterRegistryContext.setDefaultPrinterRegistry(newpr);
199
200             if (oldpr!=DFLT_PR && oldpr!=null && oldpr!=newpr) {
201                 String JavaDoc warning = uistrs().get("initprint.err.too.many");
202                 log(warning,Project.MSG_WARN);
203             } else {
204                 log("Installed default PrinterRegistry with id=\""+
205                     getRegistryReference().getRefId()+"\"", Project.MSG_DEBUG);
206             }
207         }
208         //Uninstall
209
else if (Strings.UNINSTALL.equals(m_Op)) {
210             Reference r = getRegistryReference();
211
212             if (r!=null && r.getRefId()!=null) {
213                 PrinterRegistry expectedpr = getPrinterRegistry();
214                 if (PrinterRegistryContext.getDefaultPrinterRegistry()!=expectedpr) {
215                     String JavaDoc warning = uistrs().get("initprint.mismatch.installed",r.getRefId());
216                     log(warning,Project.MSG_WARN);
217                 } else {
218                     PrinterRegistryContext.unsetDefaultPrinterRegistry();
219                     log("Uninstalled previously installed default PrinterRegistry with id=\""+
220                         expectedpr.getId()+"\"", Project.MSG_DEBUG);
221                 }
222             } else {
223                 PrinterRegistry oldpr = PrinterRegistryContext.unsetDefaultPrinterRegistry();
224                 if (oldpr!=DFLT_PR && oldpr!=null) {
225                     log("Uninstalled default PrinterRegistry with id=\""+oldpr.getId()+"\"",
226                         Project.MSG_DEBUG);
227                 }
228             }
229         }
230         //?Unknown action
231
else {
232             String JavaDoc warning = uistrs().get("task.manager.err.unknown.operation",m_Op);
233             log(warning,Project.MSG_WARN);
234         }
235     }
236
237
238
239     /** Default printer registry isn't null it is a null placehoder. **/
240     private static final PrinterRegistry DFLT_PR=
241         PrinterRegistryContext.getEmptyPrinterRegistry();
242
243     private Reference m_registryReference;
244     private String JavaDoc m_Op;//NB: install,uninstall
245
private Boolean JavaDoc m_willSkip;
246 }
247
248 /* end-of-ManagePrintersTask.java */
249
Popular Tags