001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.ddl.util;
043:
044: import java.util.*;
045:
046: import org.openide.*;
047:
048: import org.netbeans.lib.ddl.*;
049:
050: /**
051: * Command buffer used to execute a bunch of commands. Main advantages of using
052: * buffer is:
053: * - Optimized connection handling. Buffer opens JDBC connection before executing
054: * of first command and closes it after a last one. It's safely then manually
055: * handling connection and better then leaving commands open and close connection
056: * for each comand separately.
057: * - Exception handler. You can assign an exception handler to buffer. When any
058: * error occures during the execution, this handler catches it and lets user to
059: * decide if continue or not (when you're dropping nonexisting table, you probably
060: * would like to continue).
061: * - Debgging. You can set up debug mode and buffer will print each command to
062: * System.out before execution.
063: *
064: * @author Slavek Psenicka
065: */
066: public class CommandBuffer {
067: /** Buffered items */
068: Vector commands;
069:
070: /** Exception handler */
071: CommandBufferExceptionHandler handler;
072:
073: /** Debug mode */
074: boolean debugmode;
075:
076: /** Execution command with some exception */
077: boolean executionWithException;
078:
079: /** Adds command to buffer
080: * @param cmd Command to add.
081: */
082: public void add(DDLCommand cmd) {
083: if (commands == null)
084: commands = new Vector();
085: commands.add(cmd);
086: }
087:
088: /** Sets exception handler.
089: * This handler will catch and alows user to solve all exception throwed during
090: * the executing of buffered commands.
091: */
092: public void setExceptionHandler(CommandBufferExceptionHandler hand) {
093: handler = hand;
094: }
095:
096: /** Returns true if debugging mode is on.
097: * You can set up debug mode and buffer will print each command to
098: * System.out before execution.
099: */
100: public boolean isDebugMode() {
101: return debugmode;
102: }
103:
104: /** Sets debug mode on/off.
105: * You can set up debug mode and buffer will print each command to
106: * System.out before execution.
107: * @param flag true = debugging enabled
108: */
109: public void setDebugMode(boolean flag) {
110: debugmode = flag;
111: }
112:
113: /** Returns a string with string representation of all commands in buffer
114: */
115: public String getCommands() throws DDLException {
116: String cmds = "";
117: Enumeration cmd_e = commands.elements();
118: while (cmd_e.hasMoreElements()) {
119: DDLCommand e_cmd = (DDLCommand) cmd_e.nextElement();
120: cmds = cmds + e_cmd.getCommand() + "\n";
121: }
122:
123: return cmds;
124: }
125:
126: /** Executes commnds in buffer.
127: * Buffer opens JDBC connection before executing (if isn't already open)
128: * of first command and closes it after a last one. It's safely then manually
129: * handling connection and better then leaving commands open and close connection
130: * for each comand separately. You can also assign an exception handler to buffer.
131: * When any error occures during the execution, this handler catches it and lets user to
132: * decide if continue or not (when you're dropping nonexisting table, you probably
133: * would like to continue).
134: */
135: public void execute() throws DDLException {
136: boolean opencon = false;
137: executionWithException = false;
138: DatabaseSpecification spec = null;
139: Enumeration cmd_e = commands.elements();
140: while (cmd_e.hasMoreElements()) {
141: DDLCommand e_cmd = (DDLCommand) cmd_e.nextElement();
142: try {
143: if (spec == null) {
144: spec = e_cmd.getSpecification();
145: if (spec.getJDBCConnection() == null) {
146: opencon = true;
147: spec.openJDBCConnection();
148: }
149: }
150: if (debugmode)
151: System.out.println(e_cmd);
152: e_cmd.execute();
153: executionWithException = e_cmd.wasException();
154: } catch (Exception e) {
155: // e.printStackTrace();
156: executionWithException = true;
157: boolean exres = false;
158: if (handler != null)
159: exres = handler.shouldContinueAfterException(e);
160: if (!exres)
161: DialogDisplayer.getDefault().notify(
162: new NotifyDescriptor.Message(
163: e.getMessage(),
164: NotifyDescriptor.ERROR_MESSAGE));
165: }
166: }
167:
168: if (opencon)
169: spec.closeJDBCConnection();
170: }
171:
172: /** information about appearance some exception in the last execute a bunch of commands */
173: public boolean wasException() {
174: return executionWithException;
175: }
176: }
|