PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/apache-log4j-1.2.17/examples/Sort.java

#
Java | 95 lines | 43 code | 12 blank | 40 comment | 4 complexity | 4c4b9c6975bad8b9386eb1a4821b3486 MD5 | raw file
Possible License(s): Apache-2.0
  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. package examples;
  18. import org.apache.log4j.PropertyConfigurator;
  19. import org.apache.log4j.Logger;
  20. /**
  21. Example code for log4j to viewed in conjunction with the {@link
  22. examples.SortAlgo SortAlgo} class.
  23. <p>This program expects a configuration file name as its first
  24. argument, and the size of the array to sort as the second and last
  25. argument. See its <b><a href="doc-files/Sort.java">source
  26. code</a></b> for more details.
  27. <p>Play around with different values in the configuration file and
  28. watch the changing behavior.
  29. <p>Example configuration files can be found in <a
  30. href="doc-files/sort1.properties">sort1.properties</a>, <a
  31. href="doc-files/sort2.properties">sort2.properties</a>, <a
  32. href="doc-files/sort3.properties">sort3.properties</a> and <a
  33. href="doc-files/sort4.properties">sort4.properties</a> are supplied with the
  34. package.
  35. <p>If you are also interested in logging performance, then have
  36. look at the {@link org.apache.log4j.performance.Logging} class.
  37. @author Ceki G&uuml;lc&uuml; */
  38. public class Sort {
  39. static Logger logger = Logger.getLogger(Sort.class.getName());
  40. public static void main(String[] args) {
  41. if(args.length != 2) {
  42. usage("Incorrect number of parameters.");
  43. }
  44. int arraySize = -1;
  45. try {
  46. arraySize = Integer.valueOf(args[1]).intValue();
  47. if(arraySize <= 0)
  48. usage("Negative array size.");
  49. }
  50. catch(java.lang.NumberFormatException e) {
  51. usage("Could not number format ["+args[1]+"].");
  52. }
  53. PropertyConfigurator.configure(args[0]);
  54. int[] intArray = new int[arraySize];
  55. logger.info("Populating an array of " + arraySize + " elements in" +
  56. " reverse order.");
  57. for(int i = arraySize -1 ; i >= 0; i--) {
  58. intArray[i] = arraySize - i - 1;
  59. }
  60. SortAlgo sa1 = new SortAlgo(intArray);
  61. sa1.bubbleSort();
  62. sa1.dump();
  63. // We intentionally initilize sa2 with null.
  64. SortAlgo sa2 = new SortAlgo(null);
  65. logger.info("The next log statement should be an error message.");
  66. sa2.dump();
  67. logger.info("Exiting main method.");
  68. }
  69. static
  70. void usage(String errMsg) {
  71. System.err.println(errMsg);
  72. System.err.println("\nUsage: java org.apache.examples.Sort " +
  73. "configFile ARRAY_SIZE\n"+
  74. "where configFile is a configuration file\n"+
  75. " ARRAY_SIZE is a positive integer.\n");
  76. System.exit(1);
  77. }
  78. }