1 package edu.jiangxin.apktoolbox.utils;
2
3 import org.apache.commons.configuration2.Configuration;
4 import org.apache.commons.configuration2.FileBasedConfiguration;
5 import org.apache.commons.configuration2.PropertiesConfiguration;
6 import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
7 import org.apache.commons.configuration2.builder.fluent.Parameters;
8 import org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters;
9 import org.apache.commons.configuration2.ex.ConfigurationException;
10 import org.apache.commons.exec.*;
11 import org.apache.commons.lang3.StringUtils;
12 import org.apache.logging.log4j.Level;
13 import org.apache.logging.log4j.LogManager;
14 import org.apache.logging.log4j.Logger;
15
16 import javax.swing.*;
17 import java.awt.*;
18 import java.io.*;
19
20
21
22
23
24
25 public class Utils {
26 private static final Logger logger = LogManager.getLogger(Utils.class.getSimpleName());
27
28 private static String userConfigPath;
29
30 private static String userPluginDirPath;
31
32 private static FileBasedConfigurationBuilder<FileBasedConfiguration> builder;
33
34 public static String getPluginDirPath() {
35 return userPluginDirPath;
36 }
37
38 public static boolean checkAndInitEnvironment() {
39 boolean ret = initUserDir();
40 if (!ret) {
41 return false;
42 }
43 SystemInfoUtils.logSystemInfo();
44 return true;
45 }
46
47 private static boolean initUserDir() {
48 String userHomePath = System.getProperty("user.home");
49 String userDirPath;
50 if (StringUtils.isEmpty(userHomePath)) {
51 logger.error("user.home is empty");
52 userDirPath = ".apktoolboxgui";
53 } else {
54 userDirPath = userHomePath + File.separator + ".apktoolboxgui";
55 }
56 File userDirFile = new File(userDirPath);
57 if (!userDirFile.exists()) {
58 logger.info("userDirFile does not exist");
59 boolean ret = userDirFile.mkdir();
60 if (!ret) {
61 logger.error("mkdir failed: {}", userDirPath);
62 return false;
63 }
64 }
65 userConfigPath = userDirPath + File.separator + "apktoolboxgui.properties";
66 File userConfigFile = new File(userConfigPath);
67 if (!userConfigFile.exists()) {
68 try {
69 logger.info("userConfigFile does not exist");
70 boolean ret = userConfigFile.createNewFile();
71 if (!ret) {
72 logger.error("createNewFile fail: {}", userConfigPath);
73 return false;
74 }
75 } catch (IOException e) {
76 logger.error("createNewFile fail", e);
77 }
78 }
79 userPluginDirPath = userDirPath + File.separator + "plugins";
80 File userPluginDirFile = new File(userPluginDirPath);
81 if (!userPluginDirFile.exists()) {
82 logger.info("userPluginDirFile does not exist");
83 boolean ret = userPluginDirFile.mkdir();
84 if (!ret) {
85 logger.error("mkdir failed: {}", userPluginDirPath);
86 return false;
87 }
88 }
89 return true;
90 }
91
92 public static Configuration getConfiguration() {
93 if (builder == null) {
94 logger.info("builder is null, create it");
95 File configFile = new File(userConfigPath);
96 Parameters params = new Parameters();
97 PropertiesBuilderParameters parameters = params.properties().setFile(configFile);
98 builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
99 .configure(parameters);
100 }
101 Configuration conf = null;
102 try {
103 conf = builder.getConfiguration();
104 } catch (ConfigurationException e) {
105 logger.error("getConfiguration error", e);
106 }
107 return conf;
108 }
109
110 public static void saveConfiguration() {
111 try {
112 if (builder == null) {
113 logger.info("builder is null");
114 return;
115 }
116 builder.save();
117 logger.info("saveConfiguration success");
118 } catch (ConfigurationException e) {
119 logger.error("saveConfiguration error", e);
120 }
121 }
122
123 public static String getToolsPath() {
124 String tmp = Utils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
125 if (StringUtils.isEmpty(tmp)) {
126 logger.error("tmp is empty");
127 return null;
128 }
129 File file = new File(tmp);
130 if (!file.exists()) {
131 logger.error("file does not exist");
132 return null;
133 }
134 while (file.getParentFile().exists()) {
135 File parent = file.getParentFile();
136 File tools = new File(parent, "tools");
137 if (tools.exists()) {
138 try {
139 return tools.getCanonicalPath();
140 } catch (IOException e) {
141 logger.error("getCanonicalPath fail");
142 return null;
143 }
144 }
145 file = file.getParentFile();
146 }
147
148 return null;
149
150 }
151
152 public static String getFrameTitle(JComponent component) {
153 Container container = component.getParent();
154 while(container != null) {
155 if (container instanceof JFrame) {
156 return ((JFrame) container).getTitle();
157 }
158 container = container.getParent();
159 }
160 return "";
161 }
162
163 public static void setFrameTitle(JComponent component, String title) {
164 Container container = component.getParent();
165 while(container != null) {
166 if (container instanceof JFrame) {
167 ((JFrame) container).setTitle(title);
168 return;
169 }
170 container = container.getParent();
171 }
172 }
173
174 public static void blockedExecutor(String cmd) {
175 logger.info(cmd);
176 try (ProcessLogOutputStream outStream = new ProcessLogOutputStream(logger, Level.INFO);
177 ProcessLogOutputStream errStream = new ProcessLogOutputStream(logger, Level.ERROR)
178 ) {
179 CommandLine commandLine = CommandLine.parse(cmd);
180 DefaultExecutor exec = new DefaultExecutor();
181 PumpStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
182 exec.setStreamHandler(streamHandler);
183 int exitValue = exec.execute(commandLine);
184 logger.info("exitValue: [" + exitValue + "]");
185 } catch (IOException ioe) {
186 logger.error("exec fail. ", ioe);
187 }
188 }
189
190 public static void unBlockedExecutor(String cmd) {
191 logger.info(cmd);
192 try (ProcessLogOutputStream outStream = new ProcessLogOutputStream(logger, Level.INFO);
193 ProcessLogOutputStream errStream = new ProcessLogOutputStream(logger, Level.ERROR)
194 ) {
195 CommandLine commandLine = CommandLine.parse(cmd);
196 DefaultExecutor exec = new DefaultExecutor();
197 PumpStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
198 exec.setStreamHandler(streamHandler);
199 DefaultExecuteResultHandler erh = new DefaultExecuteResultHandler() {
200 @Override
201 public void onProcessComplete(int exitValue) {
202 logger.info("complete: [" + cmd + "], exitValue: [" + exitValue + "]");
203 }
204
205 @Override
206 public void onProcessFailed(ExecuteException ee) {
207 logger.info("failed: [" + cmd + "], execute exception: [" + ee.getMessage() + "]");
208 }
209 };
210 exec.execute(commandLine, erh);
211 } catch (IOException ioe) {
212 logger.error("exec fail. ", ioe);
213 }
214 }
215
216 public static int getFileLineCount(File file) {
217 try (LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(file))) {
218 while (lineNumberReader.skip(Long.MAX_VALUE) > 0) {
219 logger.info("getFileLineCount skip " + Long.MAX_VALUE + " characters");
220 }
221 return lineNumberReader.getLineNumber();
222 } catch (IOException e) {
223 logger.error("getFileLineCount IOException");
224 return 0;
225 }
226 }
227 }