KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > commands > controller > AddDriver


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.text.commands.controller;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileNotFoundException JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
33 import org.objectweb.cjdbc.console.text.ConsoleException;
34 import org.objectweb.cjdbc.console.text.commands.ConsoleCommand;
35 import org.objectweb.cjdbc.console.text.module.AbstractConsoleModule;
36
37 /**
38  * This class defines a AddDriver
39  *
40  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
41  * @version 1.0
42  */

43 public class AddDriver extends ConsoleCommand
44 {
45
46   /**
47    * Creates a new <code>AddDriver.java</code> object
48    *
49    * @param module the command is attached to
50    */

51   public AddDriver(AbstractConsoleModule module)
52   {
53     super(module);
54   }
55
56   /**
57    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#parse(java.lang.String)
58    */

59   public void parse(String JavaDoc commandText) throws IOException JavaDoc, ConsoleException
60   {
61     String JavaDoc filename = null;
62     // Get the file name if needed
63
if (commandText == null || commandText.trim().equals(""))
64     {
65       try
66       {
67         filename = console.readLine(ConsoleTranslate
68             .get("controller.command.add.driver.input.filename"));
69       }
70       catch (Exception JavaDoc che)
71       {
72       }
73     }
74     else
75       filename = commandText.trim();
76
77     if (filename == null || filename.equals(""))
78       throw new ConsoleException(ConsoleTranslate
79           .get("controller.command.add.driver.null.filename"));
80
81     try
82     {
83       // Send the file contents to the controller
84
jmxClient.getControllerProxy().addDriver(readDriver(filename));
85       console.println(ConsoleTranslate.get(
86           "controller.command.add.driver.file.sent", filename));
87     }
88     catch (FileNotFoundException JavaDoc fnf)
89     {
90       throw new ConsoleException(ConsoleTranslate.get(
91           "controller.command.add.driver.file.not.found", filename));
92     }
93     catch (Exception JavaDoc ioe)
94     {
95       throw new ConsoleException(ConsoleTranslate.get(
96           "controller.command.add.driver.sent.failed", ioe));
97     }
98   }
99
100   private byte[] readDriver(String JavaDoc filename) throws FileNotFoundException JavaDoc,
101       IOException JavaDoc
102   {
103     File JavaDoc file;
104     FileInputStream JavaDoc fileInput = null;
105     file = new File JavaDoc(filename);
106     fileInput = new FileInputStream JavaDoc(file);
107
108     // Read the file into an array of bytes
109
long size = file.length();
110     if (size > Integer.MAX_VALUE)
111       throw new IOException JavaDoc(ConsoleTranslate
112           .get("controller.command.add.driver.file.too.big"));
113     byte[] bytes = new byte[(int) size];
114     int nb = fileInput.read(bytes);
115     fileInput.close();
116     if (nb != size)
117       throw new IOException JavaDoc(ConsoleTranslate
118           .get("controller.command.add.driver.file.not.read"));
119     return bytes;
120   }
121
122   /**
123    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandName()
124    */

125   public String JavaDoc getCommandName()
126   {
127     return "upload driver";
128   }
129
130   /**
131    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandDescription()
132    */

133   public String JavaDoc getCommandDescription()
134   {
135     return ConsoleTranslate.get("controller.command.add.driver.description");
136   }
137
138   /**
139    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandParameters()
140    */

141   public String JavaDoc getCommandParameters()
142   {
143     return ConsoleTranslate.get("controller.command.add.driver.params");
144   }
145
146 }
147
Popular Tags