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  /***
18   * 
19   */
20  package org.apache.jetspeed.anttasks;
21  
22  import java.io.File;
23  
24  import org.apache.tools.ant.Project;
25  import org.apache.tools.ant.Target;
26  import org.apache.tools.ant.taskdefs.SQLExec;
27  
28  /***
29   * @author hajo
30   * 
31   */
32  public class ExecuteJavaSQL
33  {
34  
35  	/***
36  	 * @param args
37  	 */
38  	public static void main(String[] args)
39  	{
40  	    final class TEMPSQL extends SQLExec {
41  	        public TEMPSQL() 
42  	        {
43  		 	    setProject(new Project());
44  			    getProject().init();
45  			    super.setTaskType("sql");
46  			    super.setTaskName("sql");
47  			    super.target = new Target();
48  	        }	
49  	    }
50  
51  	    boolean autocommit = true;
52  	    String driver = null;
53  	    String url = null;
54  	    String userid = null;
55  	    String password = null;
56  	    String source = null;
57  	    String onError = null;
58  	    
59  	    
60  	    
61         if (args == null)
62              throw new IllegalArgumentException("ExecuteSQL needs to know what to do - no arguments provided!!! ");
63  
64          
65          // Parse all the command-line arguments
66          for (int n = 0; n < args.length; n++)
67          {
68          	String argument = args[n].toLowerCase().trim();
69          	
70  			if (argument.startsWith("driver="))	           
71  			{
72  				driver = args[n].substring("driver=".length());
73  			}
74  			else
75  				if (argument.startsWith("url="))
76  				{
77  					url = args[n].substring("url=".length());
78  				}
79  				else
80  					if (argument.startsWith("userid="))
81  					{
82  						userid = args[n].substring("userid=".length());
83  					}
84  					else
85  						if (argument.startsWith("password="))
86  						{
87  							password = args[n].substring("password=".length());
88  						}
89  						else
90  							if (argument.startsWith("src=")) 	       
91  							{
92  								source = args[n].substring("src=".length());
93  							}
94  							else
95  								if (argument.startsWith("autocommit="))
96  								{
97  									String s = args[n].substring("src=".length());
98  									try
99  									{
100 										autocommit = Boolean.valueOf(s).booleanValue();
101 									}
102 									catch (Exception e)
103 									{
104 										e.printStackTrace();
105 									}
106 								}
107 								else
108 									if (argument.startsWith("onerror="))
109 									{
110 										onError = args[n].substring("onerror=".length());
111 									}
112 								    else
113 							            {
114 							                throw new IllegalArgumentException("Unknown argument: "
115 							                        + args[n]);
116 							            }
117         }
118 		TEMPSQL sql = new TEMPSQL();
119 		
120 		sql.setAutocommit(autocommit);
121 		sql.setDriver(driver);
122 		sql.setUrl(url);
123 		sql.setUserid(userid);
124 		sql.setPassword(password);
125 		File sqlFile = null;
126 		try
127 		{
128 			sqlFile = new File(source); 
129 		}
130 		catch (Exception e)
131 		{
132 			 throw new IllegalArgumentException("File parameter " + source + " invalid : " + e.getLocalizedMessage());
133 		}
134 		sql.setSrc(sqlFile);
135 		try
136 		{
137 			SQLExec.OnError errorHandling = new SQLExec.OnError();
138 			errorHandling.setValue(onError);
139 			sql.setOnerror(errorHandling);
140 		}
141 		catch (Exception e)
142 		{
143 			 throw new IllegalArgumentException("Error handling parameter " + onError + " invalid : " + e.getLocalizedMessage());
144 		}
145 			
146 
147 		
148 		sql.execute();
149 
150 	}
151 }