1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.shell;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.net.URL;
33 import java.util.Properties;
34
35 import org.utgenome.config.TrackConfiguration;
36 import org.utgenome.shell.UTGBShell.UTGBShellOption;
37 import org.xerial.core.XerialException;
38 import org.xerial.util.FileResource;
39 import org.xerial.util.StringUtil;
40 import org.xerial.util.bean.BeanUtil;
41 import org.xerial.util.log.Logger;
42 import org.xerial.util.template.Template;
43
44
45
46
47
48
49
50 public abstract class UTGBShellSubCommandBase implements UTGBShellSubCommand {
51 private static Logger _logger = Logger.getLogger(UTGBShell.class);
52 public static final String APP_FOLDER = "app";
53 public static final String SRC_FOLDER = "src/main/java";
54 public static final String WEBAPP_FOLDER = "src/main/webapp";
55 public static final String EXPLODED_WEBAPP_DIR = "target/utgb";
56
57 protected UTGBShellOption globalOption = new UTGBShellOption();
58
59 public abstract String name();
60
61 public void execute(UTGBShellOption globalOption, String[] args) throws UTGBShellException {
62 this.globalOption = globalOption;
63 execute(args);
64 }
65
66 public abstract void execute(String[] args) throws UTGBShellException;
67
68 public static String loadUsage(String helpFileName) {
69
70 StringBuilder out = new StringBuilder();
71 try {
72 BufferedReader reader = FileResource.open(Create.class, helpFileName);
73 String line;
74 while ((line = reader.readLine()) != null) {
75 out.append(line);
76 out.append(StringUtil.NEW_LINE);
77 }
78 return out.toString();
79 }
80 catch (IOException e) {
81 return "";
82 }
83 }
84
85 public boolean isInProjectRoot() {
86 return isInProjectRoot(globalOption.projectDir);
87 }
88
89 public File getProjectResourcePath(String relativePathFromTheProjectRoot) {
90 return new File(globalOption.projectDir, relativePathFromTheProjectRoot);
91 }
92
93 public File getConfigFile() {
94 return new File(globalOption.projectDir, String.format("config/%s.silk", globalOption.runningMode));
95 }
96
97 private boolean isInProjectRoot(String projectDir) {
98 return getConfigFile().exists();
99 }
100
101 public void maven(String commandLine) throws UTGBShellException {
102 if (globalOption.projectDir != null)
103 Maven.runMaven(commandLine, new File(globalOption.projectDir));
104 else
105 Maven.runMaven(commandLine);
106 }
107
108 public TrackConfiguration loadTrackConfiguration() throws UTGBShellException {
109 return loadTrackConfiguration(globalOption.projectDir);
110 }
111
112 private TrackConfiguration loadTrackConfiguration(String projectDir) throws UTGBShellException {
113 if (!isInProjectRoot(projectDir))
114 throw new UTGBShellException("not in the project root folder");
115 try {
116 BufferedReader reader = new BufferedReader(new FileReader(getConfigFile()));
117 TrackConfiguration config = BeanUtil.createXMLBean(TrackConfiguration.class, reader);
118 return config;
119 }
120 catch (XerialException e) {
121 throw new UTGBShellException(String.format("syntax error in config file %s : %s", getConfigFile(), e.getMessage()));
122 }
123 catch (IOException e) {
124 throw new UTGBShellException(String.format("failed to load %s: %s", getConfigFile(), e.getMessage()));
125 }
126 }
127
128 public int compareTo(UTGBShellSubCommand o) {
129 return name().compareTo(o.name());
130 }
131
132 public static String getPath(File f) {
133 return f.getPath().replaceAll("\\\\", "/");
134 }
135
136 public void createContextXML(String contextPath, String projectRoot, boolean reloadable) throws UTGBShellException {
137 Properties prop = new Properties();
138 prop.put("contextPath", contextPath);
139 prop.put("projectRoot", projectRoot);
140 prop.put("reloadable", reloadable ? "true" : "false");
141
142 String target = EXPLODED_WEBAPP_DIR + "/META-INF/context.xml";
143 createFileFromTemplate(UTGBShellSubCommandBase.class, "template/java/context.xml.template", globalOption.projectDir, target, prop, true);
144 }
145
146 public void createFileFromTemplate(Class<?> baseClass, String templateFilePath, String relativePathOfTarget, Properties prop) throws UTGBShellException {
147 createFileFromTemplate(baseClass, templateFilePath, globalOption.projectDir, relativePathOfTarget, prop, false);
148 }
149
150 public static void createFileFromTemplate(Class<?> baseClass, String templateFilePath, String projectFolder, String relativePathOfTarget, Properties prop,
151 boolean overWrite) throws UTGBShellException {
152
153
154
155 File outFile = new File(projectFolder, relativePathOfTarget);
156
157 if (outFile.exists() && !overWrite) {
158 _logger.info(getPath(outFile) + " already exists.");
159 }
160 else {
161 try {
162 URL url = FileResource.find(baseClass, templateFilePath);
163 if (url == null) {
164 throw new UTGBShellException(String.format("not found %s", templateFilePath));
165 }
166 Template template;
167 template = new Template(url.openStream());
168 String fileContent = template.apply(prop);
169 outFile.getParentFile().mkdirs();
170
171 FileWriter writer = new FileWriter(outFile);
172 writer.append(fileContent);
173 writer.close();
174 _logger.info("create a file: " + getPath(new File(relativePathOfTarget)));
175 }
176 catch (IOException e) {
177 throw new UTGBShellException(e);
178 }
179 }
180 }
181 }