KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > util > CommandBuffer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.ddl.util;
21
22 import java.util.*;
23
24 import org.openide.*;
25
26 import org.netbeans.lib.ddl.*;
27
28 /**
29 * Command buffer used to execute a bunch of commands. Main advantages of using
30 * buffer is:
31 * - Optimized connection handling. Buffer opens JDBC connection before executing
32 * of first command and closes it after a last one. It's safely then manually
33 * handling connection and better then leaving commands open and close connection
34 * for each comand separately.
35 * - Exception handler. You can assign an exception handler to buffer. When any
36 * error occures during the execution, this handler catches it and lets user to
37 * decide if continue or not (when you're dropping nonexisting table, you probably
38 * would like to continue).
39 * - Debgging. You can set up debug mode and buffer will print each command to
40 * System.out before execution.
41 *
42 * @author Slavek Psenicka
43 */

44 public class CommandBuffer
45 {
46     /** Buffered items */
47     Vector commands;
48
49     /** Exception handler */
50     CommandBufferExceptionHandler handler;
51
52     /** Debug mode */
53     boolean debugmode;
54     
55     /** Execution command with some exception */
56     boolean executionWithException;
57
58     /** Adds command to buffer
59     * @param cmd Command to add.
60     */

61     public void add(DDLCommand cmd)
62     {
63         if (commands == null) commands = new Vector();
64         commands.add(cmd);
65     }
66
67     /** Sets exception handler.
68     * This handler will catch and alows user to solve all exception throwed during
69     * the executing of buffered commands.
70     */

71     public void setExceptionHandler(CommandBufferExceptionHandler hand)
72     {
73         handler = hand;
74     }
75
76     /** Returns true if debugging mode is on.
77     * You can set up debug mode and buffer will print each command to
78     * System.out before execution.
79     */

80     public boolean isDebugMode()
81     {
82         return debugmode;
83     }
84
85     /** Sets debug mode on/off.
86     * You can set up debug mode and buffer will print each command to
87     * System.out before execution.
88     * @param flag true = debugging enabled
89     */

90     public void setDebugMode(boolean flag)
91     {
92         debugmode = flag;
93     }
94
95     /** Returns a string with string representation of all commands in buffer
96     */

97     public String JavaDoc getCommands()
98     throws DDLException
99     {
100         String JavaDoc cmds = "";
101         Enumeration cmd_e = commands.elements();
102         while (cmd_e.hasMoreElements()) {
103             DDLCommand e_cmd = (DDLCommand)cmd_e.nextElement();
104             cmds = cmds + e_cmd.getCommand() + "\n";
105         }
106
107         return cmds;
108     }
109
110     /** Executes commnds in buffer.
111     * Buffer opens JDBC connection before executing (if isn't already open)
112     * of first command and closes it after a last one. It's safely then manually
113     * handling connection and better then leaving commands open and close connection
114     * for each comand separately. You can also assign an exception handler to buffer.
115     * When any error occures during the execution, this handler catches it and lets user to
116     * decide if continue or not (when you're dropping nonexisting table, you probably
117     * would like to continue).
118     */

119     public void execute()
120     throws DDLException
121     {
122         boolean opencon = false;
123         executionWithException = false;
124         DatabaseSpecification spec = null;
125         Enumeration cmd_e = commands.elements();
126         while (cmd_e.hasMoreElements()) {
127             DDLCommand e_cmd = (DDLCommand)cmd_e.nextElement();
128             try {
129                 if (spec == null) {
130                     spec = e_cmd.getSpecification();
131                     if (spec.getJDBCConnection() == null) {
132                         opencon = true;
133                         spec.openJDBCConnection();
134                     }
135                 }
136                 if (debugmode) System.out.println(e_cmd);
137                 e_cmd.execute();
138                 executionWithException = e_cmd.wasException();
139             } catch (Exception JavaDoc e) {
140                 // e.printStackTrace();
141
executionWithException = true;
142                 boolean exres = false;
143                 if (handler != null)
144                     exres = handler.shouldContinueAfterException(e);
145                 if (!exres)
146                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
147             }
148         }
149
150         if (opencon) spec.closeJDBCConnection();
151     }
152     
153     /** information about appearance some exception in the last execute a bunch of commands */
154     public boolean wasException() {
155         return executionWithException;
156     }
157 }
158
Popular Tags