Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jeteam/mbean/InitialLoad.java


1   /*
2    * JETeam, Java Enterprise TeamWork
3    *
4    * Distributable under the GPL license.
5    * See terms of license at http://www.gnu.org
6    *
7    * $Source: /cvsroot/jeteam/jeteam/jeteam-bcx/src/java/org/jeteam/mbean/InitialLoad.java,v $
8    * $Date: 2003/05/29 08:25:56 $
9    * $Author: draftdog $
10   * $Revision: 1.4 $
11   */
12  package org.jeteam.mbean;
13  
14  import javax.naming.Context;
15  import javax.naming.InitialContext;
16  import javax.sql.DataSource;
17  import java.io.BufferedReader;
18  import java.io.InputStreamReader;
19  import java.sql.Connection;
20  import java.sql.Statement;
21  import java.sql.DatabaseMetaData;
22  
23  /**
24   * This class loads the <i>initial-load.sql</i> queries and updates the data-source bound to <i>jeteamDS</i>.
25   * <p>
26   * All lines in this script, that start with the '#' character are being ignored.
27   * <p>
28   * The default data-source is "java:/jeteamDS", the script file name is "initial-load.sql", the comment delimiter
29   * for that script is the '#' character.
30   */
31  public class InitialLoad implements InitialLoadMBean
32  {
33     private String dataSourceName = "java:/jeteamDS";
34     private final String scriptFileName = "initial-load.sql";
35     private final String scriptCommentDelimiter = "#";
36  
37     public String getDataSourceName()
38     {
39        return dataSourceName;
40     }
41  
42     public void setDataSourceName(String dataSourceName)
43     {
44        this.dataSourceName = dataSourceName;
45     }
46  
47     /**
48      * Updates the data-source with all queries in one batch operation.
49      *
50      * @param connection The connection on which to send the batch update.
51      * @param reader The reader reading the script
52      * @throws Exception In case something went wrong
53      */
54     private final void batchUpdate(Connection connection, BufferedReader reader) throws Exception
55     {
56        Statement statement = null;
57  
58        try
59        {
60           statement = connection.createStatement();
61           String query = reader.readLine();
62  
63           while (query != null)
64           {
65              if (!query.startsWith(scriptCommentDelimiter))
66              {
67                 statement.addBatch(query);
68              }
69              query = reader.readLine();
70           }
71  
72           statement.executeBatch();
73        }
74        finally
75        {
76           try { statement.close(); } catch(Exception e) {}
77        }
78     }
79  
80     /**
81      * Updates the data-source with a single query at a time, doing a call for each line in the
82      * script that is not comment.
83      *
84      * @param connection The connection on which to send the set of updates.
85      * @param reader The reader reading the script
86      * @throws Exception In case something went wrong
87      */
88     private final void singleUpdates(Connection connection, BufferedReader reader) throws Exception
89     {
90        Statement statement = null;
91        String query = reader.readLine();
92  
93        while (query != null)
94        {
95           if (!query.startsWith(scriptCommentDelimiter))
96           {
97              try
98              {
99                 statement = connection.createStatement();
100                statement.execute(query);
101             }
102             finally
103             {
104                try { statement.close(); } catch(Exception e) {}
105             }
106          }
107          query = reader.readLine();
108       }
109    }
110 
111    /**
112     * Uploads the initial-load.
113     */
114    public void uploadInitialLoad()
115    {
116       BufferedReader reader = null;
117       Context context = null;
118       Connection connection = null;
119 
120       try
121       {
122          /*
123           * Create a line-based reader for the sql script, we will
124           * read sql queries from this file and add them to the statement
125           */
126          reader =
127             new BufferedReader(
128                new InputStreamReader(
129                   Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFileName)) );
130 
131          /*
132           * Look up the data source on which to perform the operation
133           * and create the statement
134           */
135          context = new InitialContext();
136          DataSource dataSource = (DataSource) context.lookup(dataSourceName);
137          connection = dataSource.getConnection();
138 
139          /**
140           * We need to make sure the queries will be performed correctly, therefore
141           * check whether or not the database driver can handle batches.
142           */
143          DatabaseMetaData dbInfo = connection.getMetaData();
144          if (dbInfo.supportsBatchUpdates())
145          {
146             batchUpdate(connection, reader);
147          }
148          else
149          {
150             singleUpdates(connection, reader);
151          }
152       }
153       catch (Exception e)
154       {
155          e.printStackTrace();
156       }
157       finally
158       {
159          try { connection.close(); } catch(Exception e) {}
160          try { reader.close(); } catch(Exception e) {}
161          try { context.close(); } catch(Exception e) {}
162       }
163 
164    }
165 }