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. package org.apache.tools.ant.taskdefs;
  18. import org.apache.tools.ant.Task;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. /**
  22. * A task to provide "nice-ness" to the current thread, and/or to
  23. * query the current value.
  24. * Examples:
  25. * <pre> <nice currentPriority="current.value" ></pre><p>
  26. * Set <code>currentPriority</code> to the current priority
  27. * <pre> <nice newPriority="10" ></pre><p>
  28. * Raise the priority of the build process (But not forked programs)
  29. * <pre> <nice currentPriority="old" newPriority="3" ></pre><p>
  30. * Lower the priority of the build process (But not forked programs), and save
  31. * the old value to the property <code>old</code>.
  32. *
  33. * @ant.task name="nice" category="control"
  34. */
  35. public class Nice extends Task {
  36. /**
  37. * the new priority
  38. */
  39. private Integer newPriority;
  40. /**
  41. * the current priority
  42. */
  43. private String currentPriority;
  44. /**
  45. * Execute the task
  46. * @exception BuildException if something goes wrong with the build
  47. */
  48. public void execute() throws BuildException {
  49. Thread self = Thread.currentThread();
  50. int priority = self.getPriority();
  51. if (currentPriority != null) {
  52. String current = Integer.toString(priority);
  53. getProject().setNewProperty(currentPriority, current);
  54. }
  55. //if there is a new priority, and it is different, change it
  56. if (newPriority != null && priority != newPriority.intValue()) {
  57. try {
  58. self.setPriority(newPriority.intValue());
  59. } catch (SecurityException e) {
  60. //catch permissions denial and keep going
  61. log("Unable to set new priority -a security manager is in the way",
  62. Project.MSG_WARN);
  63. } catch (IllegalArgumentException iae) {
  64. throw new BuildException("Priority out of range", iae);
  65. }
  66. }
  67. }
  68. /**
  69. * The name of a property to set to the value of the current
  70. * thread priority. Optional
  71. * @param currentPriority the property name.
  72. */
  73. public void setCurrentPriority(String currentPriority) {
  74. this.currentPriority = currentPriority;
  75. }
  76. /**
  77. * the new priority, in the range 1-10.
  78. * @param newPriority the new priority value.
  79. */
  80. public void setNewPriority(int newPriority) {
  81. if (newPriority < Thread.MIN_PRIORITY || newPriority > Thread.MAX_PRIORITY) {
  82. throw new BuildException("The thread priority is out of the range 1-10");
  83. }
  84. this.newPriority = new Integer(newPriority);
  85. }
  86. }