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  import org.apache.tools.ant.BuildException;
20  import org.apache.tools.ant.Task;
21  import java.sql.Driver;
22  import java.sql.Connection;
23  import org.apache.tools.ant.Project;
24  import java.util.Hashtable;
25  
26  
27  public class StartDerby 
28    extends Task {
29   
30  //	public static Driver DERBY_DRIVER = null;
31  //	public static Connection  DERBY_CONNECTION = null;
32  
33    public void execute() 
34      throws BuildException {
35        try {
36    	   	if (StartDerby.getDriver(getProject()) != null)
37         	{
38          		System.out.println("Derby Driver has ALREADY BEEN ESTABLISHED!");
39          		return;
40         	}
41    	  StartDerby.setDriver(getProject(),(java.sql.Driver)(Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance()));
42          System.out.println("Derby Driver has been started!");
43        } catch (Exception e) {
44          System.out.println(
45            "Derby could not start. This is most likely " +
46            "due to missing Derby JAR files. Please check your classpath" +
47            "and try again.");
48          throw new BuildException(e);
49        }
50    }
51    
52    public static void setDriver(Project project, Driver d)
53    {
54  	  try
55  	  {
56  		  if (d != null)
57  		  {
58  			  project.addReference("DRIVER",d);
59  			  System.out.println("Driver reference in Project set ");
60  		  }
61  		  else
62  		  {
63  			  Hashtable h = project.getReferences();
64  			  h.remove("DRIVER");
65  			  System.out.println("Driver reference in Project removed ");
66  		  }
67  	  }
68  	  catch (Exception e)
69  	  {
70  		  System.out.println("Could't SET Driver reference in Project : " + e.getLocalizedMessage());
71  		  
72  	  }
73    }
74    public static Driver getDriver(Project project)
75    {
76  	  try
77  	  {
78  		  Object o = project.getReference("DRIVER");
79  		  if (o != null)
80  			  return (Driver)o;
81  		  else return null;
82  	  }
83  	  catch (Exception e)
84  	  {
85  		  System.out.println("Could't get Driver reference in Project : " + e.getLocalizedMessage());
86  		  return null;
87  	  }
88    }
89    
90    public static void setConnection(Project project, Connection d)
91    {
92  	  try
93  	  {
94  		  if (d != null)
95  		  {
96  			  project.addReference("Connection",d);
97  			  System.out.println("Connection reference in Project set ");
98  		  }
99  		  else
100 		  {
101 			  Hashtable h = project.getReferences();
102 			  h.remove("Connection");
103 			  System.out.println("Connection reference in Project removed ");
104 		  }
105 	  }
106 	  catch (Exception e)
107 	  {
108 		  System.out.println("Could't SET Connection reference in Project : " + e.getLocalizedMessage());
109 		  
110 	  }
111   }
112   public static Connection getConnection(Project project)
113   {
114 	  try
115 	  {
116 		  Object o = project.getReference("Connection");
117 		  if (o != null)
118 			  return (Connection)o;
119 		  else return null;
120 	  }
121 	  catch (Exception e)
122 	  {
123 		  System.out.println("Could't get Connection reference in Project : " + e.getLocalizedMessage());
124 		  return null;
125 	  }
126   }
127 }