2 * Copyright 2004-2008 the Seasar Foundation and the Others.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
\r
13 * either express or implied. See the License for the specific language
\r
14 * governing permissions and limitations under the License.
\r
16 package org.seasar.extension.jdbc.gen.task;
\r
18 import java.util.List;
\r
20 import org.apache.tools.ant.BuildException;
\r
21 import org.apache.tools.ant.Task;
\r
22 import org.apache.tools.ant.taskdefs.Java;
\r
23 import org.apache.tools.ant.types.Commandline;
\r
24 import org.apache.tools.ant.types.Environment;
\r
25 import org.apache.tools.ant.types.Path;
\r
26 import org.apache.tools.ant.types.Reference;
\r
27 import org.seasar.extension.jdbc.gen.command.Command;
\r
28 import org.seasar.extension.jdbc.gen.command.CommandAdapter;
\r
29 import org.seasar.extension.jdbc.gen.internal.arg.ArgumentsBuilder;
\r
30 import org.seasar.extension.jdbc.gen.internal.command.CommandInvokerImpl;
\r
33 * {@link Task}の抽象クラスです。
\r
35 * S2JDBC-Gen依存部分を切り離し、AntからS2を使うための汎用タスクとしました。
\r
40 public abstract class AbstractTask extends Task {
\r
42 /** JavaタスクのresultPropertyのサフィックス */
\r
43 protected static String RESULT_PROPERTY_NAME_SUFFIX = "_result";
\r
45 /** JavaタスクのerrorPropertyのサフィックス */
\r
46 protected static String ERROR_PROPERTY_NAME_SUFFIX = "_error";
\r
49 protected Java java = new Java();
\r
52 protected Commandline jvmCommandline = new Commandline();
\r
54 /** コマンドを呼び出すクラスの名前 */
\r
55 protected String commandInvokerClassName = CommandInvokerImpl.class
\r
59 protected Path classpath;
\r
66 public Commandline.Argument createJvmarg() {
\r
67 return jvmCommandline.createArgument();
\r
71 * コマンドを呼び出すクラスの名前を返します。
\r
73 * @return コマンドを呼び出すクラスの名前
\r
75 public String getCommandInvokerClassName() {
\r
76 return commandInvokerClassName;
\r
80 * コマンドを呼び出すクラスの名前を設定します。
\r
82 * @param commandInvokerClassName
\r
85 public void setCommandInvokerClassName(String commandInvokerClassName) {
\r
86 this.commandInvokerClassName = commandInvokerClassName;
\r
95 public void setClasspath(Path classpath) {
\r
96 createClasspath().append(classpath);
\r
105 public void setClasspathRef(Reference reference) {
\r
106 createClasspath().setRefid(reference);
\r
114 public Path createClasspath() {
\r
115 if (classpath == null) {
\r
116 classpath = new Path(getProject());
\r
118 return classpath.createPath();
\r
122 public void execute() throws BuildException {
\r
123 if (classpath == null) {
\r
124 throw new BuildException("classpath is not specified for '"
\r
125 + getTaskName() + "' task");
\r
127 if (classpath.list().length == 0) {
\r
128 throw new BuildException("classpath is empty for '" + getTaskName()
\r
137 protected void executeCommand() {
\r
138 Command command = getCommand();
\r
139 String commandName = command.getClass().getName();
\r
141 ArgumentsBuilder builder = new ArgumentsBuilder(command);
\r
142 List<String> args = builder.build();
\r
144 Environment.Variable commandProperty = new Environment.Variable();
\r
145 commandProperty.setKey(CommandAdapter.COMMAND_KEY);
\r
146 commandProperty.setValue(commandName);
\r
148 Environment.Variable commandInvokerProperty = new Environment.Variable();
\r
149 commandInvokerProperty.setKey(CommandAdapter.COMMAND_INVOKER_KEY);
\r
150 commandInvokerProperty.setValue(commandInvokerClassName);
\r
152 long time = System.currentTimeMillis();
\r
153 String resultPropertyName = commandName + time
\r
154 + RESULT_PROPERTY_NAME_SUFFIX;
\r
155 String errorPropertyName = commandName + time
\r
156 + ERROR_PROPERTY_NAME_SUFFIX;
\r
158 java.bindToOwner(this);
\r
159 java.addSysproperty(commandProperty);
\r
160 java.addSysproperty(commandInvokerProperty);
\r
161 for (String arg : args) {
\r
162 java.createArg().setValue(arg);
\r
164 for (String arg : jvmCommandline.getArguments()) {
\r
165 java.createJvmarg().setValue(arg);
\r
167 java.setClasspath(classpath);
\r
168 java.setClassname(CommandAdapter.class.getName());
\r
169 java.setResultProperty(resultPropertyName);
\r
170 java.setErrorProperty(errorPropertyName);
\r
171 java.setFork(true);
\r
174 String result = getProject().getProperty(resultPropertyName);
\r
175 if (!"0".equals(result)) {
\r
176 throw new BuildException(getProject()
\r
177 .getProperty(errorPropertyName));
\r
186 protected abstract Command getCommand();
\r