KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Nice


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import org.apache.tools.ant.Task;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24
25 /**
26  * A task to provide "nice-ness" to the current thread, and/or to
27  * query the current value.
28  * Examples:
29  * <pre> &lt;nice currentPriority="current.value" &gt;</pre><p>
30  * Set <code>currentPriority</code> to the current priority
31  * <pre> &lt;nice newPriority="10" &gt;</pre><p>
32  * Raise the priority of the build process (But not forked programs)
33  * <pre> &lt;nice currentPriority="old" newPriority="3" &gt;</pre><p>
34  * Lower the priority of the build process (But not forked programs), and save
35  * the old value to the property <code>old</code>.
36  *
37  * @ant.task name="nice" category="control"
38  */

39 public class Nice extends Task {
40
41     /**
42      * the new priority
43      */

44     private Integer JavaDoc newPriority;
45
46     /**
47      * the current priority
48      */

49     private String JavaDoc currentPriority;
50
51
52
53     /**
54      * Execute the task
55      * @exception BuildException if something goes wrong with the build
56      */

57     public void execute() throws BuildException {
58
59         Thread JavaDoc self = Thread.currentThread();
60         int priority = self.getPriority();
61         if (currentPriority != null) {
62             String JavaDoc current = Integer.toString(priority);
63             getProject().setNewProperty(currentPriority, current);
64         }
65         //if there is a new priority, and it is different, change it
66
if (newPriority != null && priority != newPriority.intValue()) {
67             try {
68                 self.setPriority(newPriority.intValue());
69             } catch (SecurityException JavaDoc e) {
70                 //catch permissions denial and keep going
71
log("Unable to set new priority -a security manager is in the way",
72                         Project.MSG_WARN);
73             } catch (IllegalArgumentException JavaDoc iae) {
74                 throw new BuildException("Priority out of range", iae);
75             }
76         }
77     }
78
79     /**
80      * The name of a property to set to the value of the current
81      * thread priority. Optional
82      * @param currentPriority the property name.
83      */

84     public void setCurrentPriority(String JavaDoc currentPriority) {
85         this.currentPriority = currentPriority;
86     }
87
88     /**
89      * the new priority, in the range 1-10.
90      * @param newPriority the new priority value.
91      */

92     public void setNewPriority(int newPriority) {
93         if (newPriority < Thread.MIN_PRIORITY || newPriority > Thread.MAX_PRIORITY) {
94             throw new BuildException("The thread priority is out of the range 1-10");
95         }
96         this.newPriority = new Integer JavaDoc(newPriority);
97     }
98
99 }
100
Popular Tags