1 | |
package org.apache.torque.util; |
2 | |
|
3 | |
import org.kuali.core.db.torque.PrettyPrint; |
4 | |
import org.kuali.core.db.torque.Utils; |
5 | |
import org.kuali.db.DatabaseEvent; |
6 | |
import org.kuali.db.DatabaseListener; |
7 | |
import org.apache.commons.lang.StringUtils; |
8 | |
import org.apache.maven.plugin.logging.Log; |
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
public class MojoDatabaseListener implements DatabaseListener { |
14 | 0 | Utils utils = new Utils(); |
15 | |
PrettyPrint prettyPrint; |
16 | |
Log log; |
17 | 0 | int updateStatusInterval = 50; |
18 | |
|
19 | |
public MojoDatabaseListener() { |
20 | 0 | this(null); |
21 | 0 | } |
22 | |
|
23 | |
public MojoDatabaseListener(Log log) { |
24 | 0 | super(); |
25 | 0 | this.log = log; |
26 | 0 | } |
27 | |
|
28 | |
@Override |
29 | |
public void messageLogged(DatabaseEvent event) { |
30 | 0 | switch (event.getPriority()) { |
31 | |
case DEBUG: |
32 | 0 | getLog().debug(event.getMessage()); |
33 | 0 | break; |
34 | |
case INFO: |
35 | 0 | getLog().info(event.getMessage()); |
36 | 0 | break; |
37 | |
case WARN: |
38 | 0 | getLog().warn(event.getMessage()); |
39 | 0 | break; |
40 | |
case ERROR: |
41 | 0 | System.out.println(); |
42 | 0 | getLog().error(event.getMessage(), event.getException()); |
43 | 0 | break; |
44 | |
default: |
45 | 0 | getLog().warn("Unknown message priority " + event.getPriority() + " for message: " + event.getMessage()); |
46 | |
break; |
47 | |
} |
48 | 0 | } |
49 | |
|
50 | |
protected String getMessage(String s) { |
51 | 0 | if (StringUtils.isEmpty(s)) { |
52 | 0 | return s; |
53 | |
} |
54 | 0 | int pos = s.lastIndexOf("/"); |
55 | 0 | if (pos == -1) { |
56 | 0 | pos = s.lastIndexOf("\\"); |
57 | |
} |
58 | 0 | if (pos == -1) { |
59 | 0 | pos = 0; |
60 | |
} else { |
61 | 0 | pos++; |
62 | |
} |
63 | 0 | return s.substring(pos); |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public void beginTransaction(DatabaseEvent event) { |
68 | 0 | String message = "Executing " + getMessage(event.getTransaction().getResourceLocation() + " "); |
69 | 0 | if (!StringUtils.isEmpty(event.getTransaction().getSqlCommand())) { |
70 | 0 | message = "Executing SQL "; |
71 | |
} |
72 | 0 | String description = event.getTransaction().getDescription(); |
73 | 0 | if (!StringUtils.isEmpty(description)) { |
74 | 0 | message += "to " + description; |
75 | |
} |
76 | 0 | prettyPrint = new PrettyPrint("[INFO] " + message); |
77 | 0 | utils.left(prettyPrint); |
78 | 0 | } |
79 | |
|
80 | |
@Override |
81 | |
public void finishTransaction(DatabaseEvent event) { |
82 | 0 | utils.right(prettyPrint); |
83 | 0 | prettyPrint = null; |
84 | 0 | } |
85 | |
|
86 | |
@Override |
87 | |
public void beforeExecuteSQL(DatabaseEvent event) { |
88 | 0 | int totalStatements = event.getTotalStatements(); |
89 | 0 | if ((totalStatements % updateStatusInterval) == 0) { |
90 | 0 | System.out.print("."); |
91 | 0 | prettyPrint.setMsg(prettyPrint.getMsg() + "."); |
92 | |
} |
93 | 0 | } |
94 | |
|
95 | |
@Override |
96 | |
public void afterExecuteSQL(DatabaseEvent event) { |
97 | 0 | } |
98 | |
|
99 | |
@Override |
100 | |
public void afterProcessingSQLResults(DatabaseEvent event) { |
101 | |
|
102 | |
|
103 | 0 | } |
104 | |
|
105 | |
public PrettyPrint getPrettyPrint() { |
106 | 0 | return prettyPrint; |
107 | |
} |
108 | |
|
109 | |
public void setPrettyPrint(PrettyPrint pp) { |
110 | 0 | this.prettyPrint = pp; |
111 | 0 | } |
112 | |
|
113 | |
public Log getLog() { |
114 | 0 | return log; |
115 | |
} |
116 | |
|
117 | |
public void setLog(Log log) { |
118 | 0 | this.log = log; |
119 | 0 | } |
120 | |
|
121 | |
public Utils getUtils() { |
122 | 0 | return utils; |
123 | |
} |
124 | |
|
125 | |
public void setUtils(Utils utils) { |
126 | 0 | this.utils = utils; |
127 | 0 | } |
128 | |
|
129 | |
public int getUpdateStatusInterval() { |
130 | 0 | return updateStatusInterval; |
131 | |
} |
132 | |
|
133 | |
public void setUpdateStatusInterval(int updateStatusInterval) { |
134 | 0 | this.updateStatusInterval = updateStatusInterval; |
135 | 0 | } |
136 | |
|
137 | |
} |