001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Nicolas Modrzyk
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.console.text.commands.controller;
022:
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileNotFoundException;
026: import java.io.IOException;
027:
028: import org.continuent.sequoia.common.i18n.ConsoleTranslate;
029: import org.continuent.sequoia.console.text.ConsoleException;
030: import org.continuent.sequoia.console.text.commands.ConsoleCommand;
031: import org.continuent.sequoia.console.text.module.AbstractConsoleModule;
032:
033: /**
034: * This class defines a AddDriver
035: *
036: * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
037: * @version 1.0
038: */
039: public class AddDriver extends ConsoleCommand {
040:
041: /**
042: * Creates a new <code>AddDriver.java</code> object
043: *
044: * @param module the command is attached to
045: */
046: public AddDriver(AbstractConsoleModule module) {
047: super (module);
048: }
049:
050: /**
051: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
052: */
053: public void parse(String commandText) throws IOException,
054: ConsoleException {
055: checkJmxConnectionToController();
056: String filename = null;
057: // Get the file name if needed
058: if (commandText == null || commandText.trim().equals("")) //$NON-NLS-1$
059: {
060: try {
061: filename = console
062: .readLine(ConsoleTranslate
063: .get("controller.command.add.driver.input.filename")); //$NON-NLS-1$
064: } catch (Exception che) {
065: }
066: } else
067: filename = commandText.trim();
068:
069: if (filename == null || filename.equals("")) //$NON-NLS-1$
070: throw new ConsoleException(ConsoleTranslate
071: .get("controller.command.add.driver.null.filename")); //$NON-NLS-1$
072:
073: try {
074: // Send the file contents to the controller
075: jmxClient.getControllerProxy().addDriver(
076: readDriver(filename));
077: console
078: .printInfo(ConsoleTranslate
079: .get(
080: "controller.command.add.driver.file.sent", filename)); //$NON-NLS-1$
081: } catch (FileNotFoundException fnf) {
082: throw new ConsoleException(
083: ConsoleTranslate
084: .get(
085: "controller.command.add.driver.file.not.found", filename)); //$NON-NLS-1$
086: } catch (Exception ioe) {
087: throw new ConsoleException(ConsoleTranslate.get(
088: "controller.command.add.driver.sent.failed", ioe)); //$NON-NLS-1$
089: }
090: }
091:
092: private byte[] readDriver(String filename)
093: throws FileNotFoundException, IOException {
094: File file;
095: FileInputStream fileInput = null;
096: file = new File(filename);
097: fileInput = new FileInputStream(file);
098:
099: // Read the file into an array of bytes
100: long size = file.length();
101: if (size > Integer.MAX_VALUE)
102: throw new IOException(ConsoleTranslate
103: .get("controller.command.add.driver.file.too.big")); //$NON-NLS-1$
104: byte[] bytes = new byte[(int) size];
105: int nb = fileInput.read(bytes);
106: fileInput.close();
107: if (nb != size)
108: throw new IOException(ConsoleTranslate
109: .get("controller.command.add.driver.file.not.read")); //$NON-NLS-1$
110: return bytes;
111: }
112:
113: /**
114: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
115: */
116: public String getCommandName() {
117: return "upload driver"; //$NON-NLS-1$
118: }
119:
120: /**
121: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
122: */
123: public String getCommandDescription() {
124: return ConsoleTranslate
125: .get("controller.command.add.driver.description"); //$NON-NLS-1$
126: }
127:
128: /**
129: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
130: */
131: public String getCommandParameters() {
132: return ConsoleTranslate
133: .get("controller.command.add.driver.params"); //$NON-NLS-1$
134: }
135:
136: }
|