001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import org.apache.tools.ant.Task;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Project;
024:
025: /**
026: * A task to provide "nice-ness" to the current thread, and/or to
027: * query the current value.
028: * Examples:
029: * <pre> <nice currentPriority="current.value" ></pre><p>
030: * Set <code>currentPriority</code> to the current priority
031: * <pre> <nice newPriority="10" ></pre><p>
032: * Raise the priority of the build process (But not forked programs)
033: * <pre> <nice currentPriority="old" newPriority="3" ></pre><p>
034: * Lower the priority of the build process (But not forked programs), and save
035: * the old value to the property <code>old</code>.
036: *
037: * @ant.task name="nice" category="control"
038: */
039: public class Nice extends Task {
040:
041: /**
042: * the new priority
043: */
044: private Integer newPriority;
045:
046: /**
047: * the current priority
048: */
049: private String currentPriority;
050:
051: /**
052: * Execute the task
053: * @exception BuildException if something goes wrong with the build
054: */
055: public void execute() throws BuildException {
056:
057: Thread self = Thread.currentThread();
058: int priority = self.getPriority();
059: if (currentPriority != null) {
060: String current = Integer.toString(priority);
061: getProject().setNewProperty(currentPriority, current);
062: }
063: //if there is a new priority, and it is different, change it
064: if (newPriority != null && priority != newPriority.intValue()) {
065: try {
066: self.setPriority(newPriority.intValue());
067: } catch (SecurityException e) {
068: //catch permissions denial and keep going
069: log(
070: "Unable to set new priority -a security manager is in the way",
071: Project.MSG_WARN);
072: } catch (IllegalArgumentException iae) {
073: throw new BuildException("Priority out of range", iae);
074: }
075: }
076: }
077:
078: /**
079: * The name of a property to set to the value of the current
080: * thread priority. Optional
081: * @param currentPriority the property name.
082: */
083: public void setCurrentPriority(String currentPriority) {
084: this .currentPriority = currentPriority;
085: }
086:
087: /**
088: * the new priority, in the range 1-10.
089: * @param newPriority the new priority value.
090: */
091: public void setNewPriority(int newPriority) {
092: if (newPriority < Thread.MIN_PRIORITY
093: || newPriority > Thread.MAX_PRIORITY) {
094: throw new BuildException(
095: "The thread priority is out of the range 1-10");
096: }
097: this .newPriority = new Integer(newPriority);
098: }
099:
100: }
|