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 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.Task;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import 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> <nice currentPriority="current.value" ></pre><p>
|
---|
29 | * Set <code>currentPriority</code> to the current priority
|
---|
30 | * <pre> <nice newPriority="10" ></pre><p>
|
---|
31 | * Raise the priority of the build process (But not forked programs)
|
---|
32 | * <pre> <nice currentPriority="old" newPriority="3" ></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 | */
|
---|
38 | public 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 | }
|
---|