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