source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/Nice.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 3.3 KB
Line 
1/*
2 * Copyright 2003-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs;
19
20import org.apache.tools.ant.Task;
21import org.apache.tools.ant.BuildException;
22import org.apache.tools.ant.Project;
23
24/**
25 * A task to provide "nice-ness" to the current thread, and/or to
26 * query the current value.
27 * Examples:
28 * <pre> &lt;nice currentPriority="current.value" &gt;</pre><p>
29 * Set <code>currentPriority</code> to the current priority
30 * <pre> &lt;nice newPriority="10" &gt;</pre><p>
31 * Raise the priority of the build process (But not forked programs)
32 * <pre> &lt;nice currentPriority="old" newPriority="3" &gt;</pre><p>
33 * Lower the priority of the build process (But not forked programs), and save
34 * the old value to the property <code>old</code>.
35 *
36 * @ant.task name="nice" category="control"
37 */
38public class Nice extends Task {
39
40 /**
41 * the new priority
42 */
43 private Integer newPriority;
44
45 /**
46 * the current priority
47 */
48 private String currentPriority;
49
50
51
52 /**
53 * Execute the task
54 * @exception BuildException if something goes wrong with the build
55 */
56 public void execute() throws BuildException {
57
58 Thread self = Thread.currentThread();
59 int priority = self.getPriority();
60 if (currentPriority != null) {
61 String current = Integer.toString(priority);
62 getProject().setNewProperty(currentPriority, current);
63 }
64 //if there is a new priority, and it is different, change it
65 if (newPriority != null && priority != newPriority.intValue()) {
66 try {
67 self.setPriority(newPriority.intValue());
68 } catch (SecurityException e) {
69 //catch permissions denial and keep going
70 log("Unable to set new priority -a security manager is in the way",
71 Project.MSG_WARN);
72 } catch (IllegalArgumentException iae) {
73 throw new BuildException("Priority out of range", iae);
74 }
75 }
76 }
77
78 /**
79 * The name of a property to set to the value of the current
80 * thread priority. Optional
81 * @param currentPriority the property name.
82 */
83 public void setCurrentPriority(String currentPriority) {
84 this.currentPriority = currentPriority;
85 }
86
87 /**
88 * the new priority, in the range 1-10.
89 * @param newPriority the new priority value.
90 */
91 public void setNewPriority(int newPriority) {
92 if (newPriority < Thread.MIN_PRIORITY || newPriority > Thread.MAX_PRIORITY) {
93 throw new BuildException("The thread priority is out of the range 1-10");
94 }
95 this.newPriority = new Integer(newPriority);
96 }
97
98}
Note: See TracBrowser for help on using the repository browser.