01: package com.bostechcorp.cbesb.custom;
02:
03: import java.util.Map;
04: import java.util.Random;
05:
06: import org.apache.commons.logging.Log;
07: import org.apache.commons.logging.LogFactory;
08:
09: import com.bostechcorp.cbesb.runtime.ccsl.lib.ITransformationOperation;
10:
11: public class RandomNumber implements ITransformationOperation {
12:
13: protected static transient Log logger = LogFactory
14: .getLog(RandomNumber.class);
15:
16: public void addProperty(String name, String value) {
17:
18: }
19:
20: public void cleanup(Map<String, Object> transformerContext)
21: throws Exception {
22:
23: }
24:
25: public void initialize(Map<String, Object> transformerContext)
26: throws Exception {
27:
28: }
29:
30: public boolean process(String[] source, String[] target)
31: throws Exception {
32: if (source.length == 2) {
33: int minValue = Integer.parseInt(source[0]);
34: int maxValue = Integer.parseInt(source[1]);
35:
36: Random random = new Random();
37: int randomInt = random.nextInt(maxValue);
38: while (randomInt < minValue) {
39: randomInt = random.nextInt(maxValue);
40: }
41: target[0] = Integer.toString(randomInt);
42: return true;
43: }
44:
45: return false;
46: }
47:
48: // Main method for testing
49: public static void main(String[] args) {
50: if (args.length != 2) {
51: System.out
52: .println("Usage: RandomNumber minValue maxValue\n");
53: System.exit(0);
54: }
55:
56: String[] target = new String[1];
57: RandomNumber instance = new RandomNumber();
58: try {
59: boolean status = instance.process(args, target);
60: if (status) {
61: System.out.println("Random number = " + target[0]);
62: } else {
63: System.out.println("process returned false");
64: }
65: } catch (Exception e) {
66: logger.error("Exception in main(): " + e.getMessage());
67: if (logger.isDebugEnabled()) {
68: logger.debug("Exception in main():", e);
69: }
70: }
71:
72: }
73: }
|