View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  
20  import java.sql.Connection;
21  import java.sql.SQLException;
22  import org.apache.tools.ant.BuildException;
23  import org.apache.tools.ant.taskdefs.SQLExec;
24  import java.util.Properties;
25  import org.apache.tools.ant.Project;;
26  
27  public class ExecuteSQL extends SQLExec
28  {  
29  
30    
31  	public  void execute() throws BuildException 
32  	{
33  		
34  	
35  		try
36  		{
37  			System.out.println("Executing SQL statement");
38  			super.execute();
39  		}
40  		catch (Exception e)
41  		{
42  			throw new BuildException(e, getLocation());
43  		}
44  		finally
45  		{
46  			if (StartDerby.getConnection(getProject()) != null)
47  			{
48  				try
49  				{
50  					if (!(StartDerby.getConnection(getProject()).isClosed()))
51  						StartDerby.getConnection(getProject()).close();
52  				}
53  				catch (Exception e1)
54  				{
55  					// just a safetyguard
56  				}
57  				StartDerby.setConnection(getProject(),null);
58  			}
59  		}
60  	}
61  		
62  
63    /***
64       * Creates a new Connection as using the driver, url, userid and password
65       * specified.
66       *
67       * The calling method is responsible for closing the connection.
68       *
69       * @return Connection the newly created connection.
70       * @throws BuildException if the UserId/Password/Url is not set or there
71       * is no suitable driver or the driver fails to load.
72       */
73    protected Connection getConnection() throws BuildException 
74    {
75  	 	if (StartDerby.getDriver(getProject()) == null)
76  	 		throw new BuildException("Derby driver not established!", getLocation());		
77    	// reuse excisting connection:
78    	if (StartDerby.getConnection(getProject())!= null)
79    	{
80    		System.out.println("Connection already established");
81    		return StartDerby.getConnection(getProject());
82    	}
83    	// do almost the same as in the orignial JDBC tasks:
84    	
85          if (getUrl() == null) {
86              throw new BuildException("Url attribute must be set!", getLocation());
87          }
88          try {
89  
90              log("connecting to " + getUrl(), Project.MSG_VERBOSE);
91              Properties info = new Properties();
92              if (getUserId() != null)
93              	info.put("user", getUserId());
94              if (getPassword() != null)
95              	info.put("password", getPassword());
96              
97              Connection conn = StartDerby.getDriver(getProject()).connect(getUrl(), info);
98              if (conn == null) {
99                  // Driver doesn't understand the URL
100                 throw new SQLException("No suitable Driver for " + getUrl());
101             }
102 
103             conn.setAutoCommit(isAutocommit());
104       		System.out.println("Connection to " + getUrl() + " established");
105       		StartDerby.setConnection(getProject(),conn);
106             return conn;
107         } catch (SQLException e) {
108             throw new BuildException(e, getLocation());
109         }
110 
111     }
112   
113   
114 }