View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-shell Project
18  //
19  // Add.java
20  // Since: Jan 9, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.text.DateFormat;
28  import java.util.Date;
29  import java.util.Properties;
30  
31  import org.utgenome.config.UTGBConfig;
32  import org.xerial.util.log.Logger;
33  import org.xerial.util.opt.Argument;
34  import org.xerial.util.opt.Option;
35  
36  /**
37   * A UTGBShell sub-command for adding new action class
38   * 
39   * @author leo
40   * 
41   */
42  public class AddAction extends UTGBShellCommand {
43  
44  	private static Logger _logger = Logger.getLogger(AddAction.class);
45  
46  	@Option(symbol = "p", longName = "package", varName = "PACKAGE", description = "base package name to add a new action class")
47  	private String packageName;
48  
49  	@Argument
50  	private String target;
51  
52  	public AddAction() {
53  
54  	}
55  
56  	@Override
57  	public void execute(String[] args) throws Exception {
58  
59  		if (packageName == null) {
60  			// load the package name from the track-config.xml
61  			UTGBConfig config = loadUTGBConfig();
62  			packageName = config.javaPackage;
63  		}
64  		String appPackageName = packageName + "." + APP_FOLDER;
65  
66  		if (target == null) {
67  			throw new UTGBShellException("No web action name is given. Type utgb action --help for the usage");
68  		}
69  
70  		String newActionClassFullPath = appPackageName + "." + target.replaceAll("/", ".");
71  		_logger.info("add a new action class : " + newActionClassFullPath);
72  
73  		int extPos = newActionClassFullPath.lastIndexOf(".");
74  		String actionClassName = (extPos > 0) ? newActionClassFullPath.substring(extPos + 1) : newActionClassFullPath;
75  		String actionPackageName = (extPos > 0) ? newActionClassFullPath.substring(0, extPos) : appPackageName;
76  
77  		// create an action class
78  		Properties prop = new Properties();
79  		prop.put("actionClass", actionClassName);
80  		prop.put("actionPackage", actionPackageName);
81  		Date now = new Date();
82  		prop.put("date", DateFormat.getDateInstance().format(now));
83  
84  		String javaFileName = SRC_FOLDER + "/" + newActionClassFullPath.replaceAll("\\.", "/") + ".java";
85  		createFileFromTemplate(AddAction.class, "template/java/Action.java.template", javaFileName, prop);
86  
87  	}
88  
89  	@Override
90  	public String name() {
91  		return "action";
92  	}
93  
94  	public String getOneLinerDescription() {
95  		return "add a new web action handler";
96  	}
97  
98  }