01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.jetspeed.anttasks;
18:
19: import org.apache.tools.ant.BuildException;
20: import org.apache.tools.ant.taskdefs.JDBCTask;
21: import java.sql.Connection;
22: import java.sql.SQLException;
23: import java.util.Properties;
24: import org.apache.tools.ant.Project;
25:
26: public class StartDatabase extends JDBCTask {
27:
28: public void execute() throws BuildException {
29: if (StartDerby.getDriver(getProject()) == null)
30: throw new BuildException("Derby driver not established!",
31: getLocation());
32: // reuse excisting connection:
33: if (StartDerby.getConnection(getProject()) != null) {
34: System.out.println("Connection already established");
35: return;
36: }
37: // do almost the same as in the orignial JDBC tasks:
38:
39: if (getUrl() == null) {
40: throw new BuildException("Url attribute must be set!",
41: getLocation());
42: }
43: try {
44:
45: log("connecting to " + getUrl(), Project.MSG_VERBOSE);
46: Properties info = new Properties();
47: if (getUserId() != null)
48: info.put("user", getUserId());
49: if (getPassword() != null)
50: info.put("password", getPassword());
51:
52: Connection conn = StartDerby.getDriver(getProject())
53: .connect(getUrl(), info);
54: if (conn == null) {
55: // Driver doesn't understand the URL
56: throw new SQLException("No suitable Driver for "
57: + getUrl());
58: }
59:
60: conn.setAutoCommit(isAutocommit());
61: StartDerby.setConnection(getProject(), conn);
62: System.out.println("Derby connected to " + getUrl());
63: return;
64: } catch (SQLException e) {
65: throw new BuildException(e, getLocation());
66: }
67: }
68:
69: }
|