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.File;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.net.URL;
31 import java.util.Properties;
32
33 import org.utgenome.config.UTGBConfig;
34 import org.utgenome.shell.UTGBShell.UTGBShellOption;
35 import org.xerial.core.XerialException;
36 import org.xerial.lens.SilkLens;
37 import org.xerial.util.FileResource;
38 import org.xerial.util.log.Logger;
39 import org.xerial.util.opt.Usage;
40 import org.xerial.util.text.Template;
41
42
43
44
45
46
47
48 @Usage(templatePath = "org/utgenome/shell/help.template")
49 public abstract class UTGBShellCommand implements Comparable<UTGBShellCommand> {
50 private static Logger _logger = Logger.getLogger(UTGBShell.class);
51 public static final String APP_FOLDER = "app";
52 public static final String SRC_FOLDER = "src/main/java";
53 public static final String WEBAPP_FOLDER = "src/main/webapp";
54 public static final String EXPLODED_WEBAPP_DIR = "target/utgb";
55
56 protected UTGBShellOption globalOption = new UTGBShellOption();
57
58 public abstract String name();
59
60 public void execute(UTGBShellOption globalOption, String[] args) throws Exception {
61 this.globalOption = globalOption;
62 execute(args);
63 }
64
65 public abstract void execute(String[] args) throws Exception;
66
67 public abstract String getOneLinerDescription();
68
69 public Object getOptionHolder() {
70 return null;
71 };
72
73 public File getProjectRoot() {
74 return new File(globalOption.projectDir);
75 }
76
77 public boolean isInProjectRoot() {
78 return isInProjectRoot(globalOption.projectDir);
79 }
80
81 public File getProjectResourcePath(String relativePathFromTheProjectRoot) {
82 return new File(globalOption.projectDir, relativePathFromTheProjectRoot);
83 }
84
85 public File getConfigFile() {
86 String configFileRelativePath = String.format("config/%s.silk", globalOption.environment);
87 if (globalOption.projectDir == null)
88 return new File(configFileRelativePath);
89 else
90 return new File(globalOption.projectDir, configFileRelativePath);
91 }
92
93 public File getObsolteConfigurationFile() {
94 return new File(globalOption.projectDir, "config/track-config.xml");
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 UTGBConfig loadUTGBConfig() throws UTGBShellException {
109 return loadUTGBConfig(globalOption.projectDir);
110 }
111
112 private UTGBConfig loadUTGBConfig(String projectDir) throws UTGBShellException {
113 if (!isInProjectRoot(projectDir)) {
114 throw new UTGBShellException(String.format("Not in the project root folder: configuration file %s not found", getConfigFile()));
115 }
116 try {
117 UTGBConfig config = SilkLens.loadSilk(UTGBConfig.class, getConfigFile().toURI().toURL());
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(UTGBShellCommand 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 prop.put("environment", globalOption.environment);
142
143 String target = EXPLODED_WEBAPP_DIR + "/META-INF/context.xml";
144 createFileFromTemplate(UTGBShellCommand.class, "template/java/context.xml.template", globalOption.projectDir, target, prop, true);
145 }
146
147 public void createFileFromTemplate(Class<?> baseClass, String templateFilePath, String relativePathOfTarget, Properties prop) throws UTGBShellException {
148 createFileFromTemplate(baseClass, templateFilePath, globalOption.projectDir, relativePathOfTarget, prop, false);
149 }
150
151 public static void createFileFromTemplate(Class<?> baseClass, String templateFilePath, String projectFolder, String relativePathOfTarget, Properties prop,
152 boolean overWrite) throws UTGBShellException {
153
154
155
156 File outFile = new File(projectFolder, relativePathOfTarget);
157
158 if (outFile.exists() && !overWrite) {
159 _logger.info(getPath(outFile) + " already exists.");
160 }
161 else {
162 try {
163 URL url = FileResource.find(baseClass, templateFilePath);
164 if (url == null) {
165 throw new UTGBShellException(String.format("resource not found %s", templateFilePath));
166 }
167 Template template;
168 template = new Template(url.openStream());
169 String fileContent = template.apply(prop);
170 outFile.getParentFile().mkdirs();
171
172 FileWriter writer = new FileWriter(outFile);
173 writer.append(fileContent);
174 writer.close();
175 _logger.info("create a file: " + getPath(new File(relativePathOfTarget)));
176 }
177 catch (IOException e) {
178 throw new UTGBShellException(e);
179 }
180 }
181 }
182
183 }