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(String location, ResourceLoader loader) throws FileNotFoundException { |
103 | 0 | Resource resource = loader.getResource(location); |
104 | 0 | if (!resource.exists()) { |
105 | 0 | throw new FileNotFoundException("Unable to locate " + location); |
106 | |
} |
107 | 0 | } |
108 | |
|
109 | |
public void verifyExists(List<String> locations) throws FileNotFoundException { |
110 | 0 | ResourceLoader loader = new DefaultResourceLoader(); |
111 | 0 | for (String location : locations) { |
112 | 0 | verifyExists(location, loader); |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
public Database getDatabase(String schemaXMLResource, String targetDatabase) throws IOException { |
117 | 0 | if (!isFileOrResource(schemaXMLResource)) { |
118 | 0 | throw new IOException("Unable to locate " + schemaXMLResource); |
119 | |
} |
120 | |
|
121 | |
|
122 | 0 | KualiXmlToAppData xmlParser = new KualiXmlToAppData(targetDatabase, ""); |
123 | |
|
124 | |
|
125 | |
try { |
126 | 0 | return xmlParser.parseResource(schemaXMLResource); |
127 | 0 | } catch (Exception e) { |
128 | 0 | throw new IOException("Error parsing: " + schemaXMLResource, e); |
129 | |
} |
130 | |
} |
131 | |
|
132 | |
public List<Database> getDatabases(List<String> schemaXMLResources, String targetDatabase) throws IOException { |
133 | 0 | List<Database> databases = new ArrayList<Database>(); |
134 | 0 | if (schemaXMLResources == null) { |
135 | 0 | return databases; |
136 | |
} |
137 | |
|
138 | 0 | verifyExists(schemaXMLResources); |
139 | |
|
140 | 0 | for (String location : schemaXMLResources) { |
141 | |
|
142 | 0 | KualiXmlToAppData xmlParser = new KualiXmlToAppData(targetDatabase, ""); |
143 | |
|
144 | |
|
145 | |
try { |
146 | 0 | Database database = xmlParser.parseResource(location); |
147 | 0 | databases.add(database); |
148 | 0 | } catch (Exception e) { |
149 | 0 | throw new IOException("Error parsing: " + location, e); |
150 | 0 | } |
151 | 0 | } |
152 | 0 | return databases; |
153 | |
} |
154 | |
|
155 | |
public int getDefaultPrintableConsoleWidth() { |
156 | 0 | return defaultPrintableConsoleWidth; |
157 | |
} |
158 | |
|
159 | |
public void setDefaultPrintableConsoleWidth(int defaultPrintableConsoleWidth) { |
160 | 0 | this.defaultPrintableConsoleWidth = defaultPrintableConsoleWidth; |
161 | 0 | } |
162 | |
|
163 | |
} |