1 | |
package org.kuali.core.db.torque; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileNotFoundException; |
5 | |
import java.io.IOException; |
6 | |
import java.text.NumberFormat; |
7 | |
import java.util.ArrayList; |
8 | |
import java.util.List; |
9 | |
|
10 | |
import org.apache.commons.lang.StringUtils; |
11 | |
import org.apache.torque.engine.database.model.Database; |
12 | |
import org.springframework.core.io.DefaultResourceLoader; |
13 | |
import org.springframework.core.io.Resource; |
14 | |
import org.springframework.core.io.ResourceLoader; |
15 | |
|
16 | |
public class Utils { |
17 | 0 | NumberFormat nf = NumberFormat.getInstance(); |
18 | 0 | int length = 68; |
19 | 0 | int defaultPrintableConsoleWidth = 79; |
20 | 0 | String padding = StringUtils.repeat(".", length); |
21 | |
|
22 | |
public Utils() { |
23 | 0 | super(); |
24 | 0 | nf.setMaximumFractionDigits(3); |
25 | 0 | nf.setMinimumFractionDigits(3); |
26 | 0 | nf.setGroupingUsed(false); |
27 | 0 | } |
28 | |
|
29 | |
public String getFilename(String fileOrResource) { |
30 | 0 | if (!isFileOrResource(fileOrResource)) { |
31 | 0 | return null; |
32 | |
} |
33 | 0 | File f = new File(fileOrResource); |
34 | 0 | if (f.exists()) { |
35 | 0 | return f.getName(); |
36 | |
} |
37 | 0 | ResourceLoader loader = new DefaultResourceLoader(); |
38 | 0 | Resource resource = loader.getResource(fileOrResource); |
39 | 0 | return resource.getFilename(); |
40 | |
} |
41 | |
|
42 | |
public void right(PrettyPrint pp) { |
43 | 0 | long millis = System.currentTimeMillis() - pp.getStart(); |
44 | 0 | String elapsed = getElapsed(millis); |
45 | 0 | String padding = StringUtils.repeat(".", defaultPrintableConsoleWidth); |
46 | 0 | String right = padding + " " + elapsed; |
47 | 0 | int rightLength = defaultPrintableConsoleWidth - pp.getMsg().length(); |
48 | 0 | if (rightLength < elapsed.length()) { |
49 | 0 | System.out.println(elapsed); |
50 | |
} else { |
51 | 0 | System.out.println(StringUtils.right(right, rightLength)); |
52 | |
|
53 | |
} |
54 | 0 | } |
55 | |
|
56 | |
public void left(PrettyPrint pp) { |
57 | 0 | System.out.print(pp.getMsg()); |
58 | 0 | pp.setStart(System.currentTimeMillis()); |
59 | 0 | } |
60 | |
|
61 | |
public String getEncoding(String encoding) { |
62 | 0 | if (StringUtils.isEmpty(encoding)) { |
63 | 0 | return System.getProperty("file.encoding"); |
64 | |
} else { |
65 | 0 | return encoding; |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
public String pad(String msg, long millis) { |
70 | 0 | String elapsed = getElapsed(millis); |
71 | 0 | int leftWidth = msg.length() + 1; |
72 | 0 | int rightWidth = elapsed.length() + 1; |
73 | 0 | int chop = leftWidth + rightWidth; |
74 | 0 | if (chop > padding.length()) { |
75 | 0 | return msg + " " + elapsed; |
76 | |
} else { |
77 | 0 | return msg + " " + padding.substring(chop) + " " + elapsed; |
78 | |
} |
79 | |
} |
80 | |
|
81 | |
public String getElapsed(long millis) { |
82 | 0 | return "[" + nf.format(millis / 1000D) + "s]"; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public boolean isFileOrResource(String location) { |
89 | 0 | if (location == null) { |
90 | 0 | return false; |
91 | |
} |
92 | 0 | File file = new File(location); |
93 | 0 | if (file.exists()) { |
94 | 0 | return true; |
95 | |
} |
96 | 0 | ResourceLoader loader = new DefaultResourceLoader(); |
97 | 0 | Resource resource = loader.getResource(location); |
98 | 0 | return resource.exists(); |
99 | |
|
100 | |
} |
101 | |
|
102 | |
public void verifyExists(List<String> locations) throws FileNotFoundException { |
103 | 0 | ResourceLoader loader = new DefaultResourceLoader(); |
104 | 0 | for (String location : locations) { |
105 | 0 | Resource resource = loader.getResource(location); |
106 | 0 | if (!resource.exists()) { |
107 | 0 | throw new FileNotFoundException("Unable to locate " + location); |
108 | |
} |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | |
public List<Database> getDatabases(List<String> schemaXMLResources, String targetDatabase) throws IOException { |
113 | 0 | List<Database> databases = new ArrayList<Database>(); |
114 | 0 | if (schemaXMLResources == null) { |
115 | 0 | return databases; |
116 | |
} |
117 | |
|
118 | 0 | verifyExists(schemaXMLResources); |
119 | |
|
120 | 0 | for (String location : schemaXMLResources) { |
121 | |
|
122 | 0 | KualiXmlToAppData xmlParser = new KualiXmlToAppData(targetDatabase, ""); |
123 | |
|
124 | |
|
125 | |
try { |
126 | 0 | Database database = xmlParser.parseResource(location); |
127 | 0 | databases.add(database); |
128 | 0 | } catch (Exception e) { |
129 | 0 | throw new IOException("Error parsing: " + location, e); |
130 | 0 | } |
131 | 0 | } |
132 | 0 | return databases; |
133 | |
} |
134 | |
|
135 | |
public int getDefaultPrintableConsoleWidth() { |
136 | 0 | return defaultPrintableConsoleWidth; |
137 | |
} |
138 | |
|
139 | |
public void setDefaultPrintableConsoleWidth(int defaultPrintableConsoleWidth) { |
140 | 0 | this.defaultPrintableConsoleWidth = defaultPrintableConsoleWidth; |
141 | 0 | } |
142 | |
|
143 | |
} |