/*-------------------------------------------------------------------------- * Copyright 2008 utgenome.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *--------------------------------------------------------------------------*/ //-------------------------------------- // utgb-shell Project // // Add.java // Since: Jan 9, 2008 // // $URL$ // $Author$ //-------------------------------------- package org.utgenome.shell; import java.text.DateFormat; import java.util.Date; import java.util.Properties; import org.utgenome.config.TrackConfiguration; import org.xerial.util.cui.OptionHandler; import org.xerial.util.cui.OptionParser; import org.xerial.util.cui.OptionParserException; import org.xerial.util.log.Logger; /** * A UTGBShell subcommand for adding new action class * * @author leo * */ public class AddAction extends UTGBShellSubCommandBase { private static Logger _logger = Logger.getLogger(AddAction.class); enum Opt { PACKAGE, } private OptionParser optionParser = new OptionParser(); private String packageName = null; public AddAction() { optionParser.addOptionWithArgument(Opt.PACKAGE, "p", "package", "PACKAGE", "base package name to add a new action class", new OptionHandler() { public void handle(OptionParser parser) throws OptionParserException { packageName = parser.getValue(Opt.PACKAGE); } }); } @Override public void execute(String[] args) throws UTGBShellException { try { optionParser.parse(args); if (optionParser.getArgumentLength() <= 0) { throw new UTGBShellException("no target is given."); } String target = optionParser.getArgument(0); if (packageName == null) { // load the package name from the track-config.xml TrackConfiguration config = loadTrackConfiguration(); packageName = config.getPackage(); } String appPackageName = packageName + "." + APP_FOLDER; String newActionClassFullPath = appPackageName + "." + target.replaceAll("/", "."); _logger.info("add a new action class : " + newActionClassFullPath); int extPos = newActionClassFullPath.lastIndexOf("."); String actionClassName = (extPos > 0) ? newActionClassFullPath.substring(extPos + 1) : newActionClassFullPath; String actionPackageName = (extPos > 0) ? newActionClassFullPath.substring(0, extPos) : appPackageName; // create an action class Properties prop = new Properties(); prop.put("actionClass", actionClassName); prop.put("actionPackage", actionPackageName); Date now = new Date(); prop.put("date", DateFormat.getDateInstance().format(now)); String javaFileName = SRC_FOLDER + "/" + newActionClassFullPath.replaceAll("\\.", "/") + ".java"; createFileFromTemplate(AddAction.class, "template/java/Action.java.template", javaFileName, prop); } catch (Exception e) { throw new UTGBShellException(e); } } @Override public String name() { return "action"; } public String getOneLinerDescription() { return "add a new web action handler"; } public String getDetailedDescription() { return loadUsage("help-add.txt"); } public String getOptionList() { return optionParser.helpMessage(); } }