KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > commands > controller > AddDriver


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk
20  * Contributor(s): ______________________.
21  */

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

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

49   public AddDriver(AbstractConsoleModule module)
50   {
51     super(module);
52   }
53
54   /**
55    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
56    */

57   public void parse(String JavaDoc commandText) throws IOException JavaDoc, ConsoleException
58   {
59     checkJmxConnectionToController();
60     String JavaDoc filename = null;
61     // Get the file name if needed
62
if (commandText == null || commandText.trim().equals("")) //$NON-NLS-1$
63
{
64       try
65       {
66         filename = console.readLine(ConsoleTranslate
67             .get("controller.command.add.driver.input.filename")); //$NON-NLS-1$
68
}
69       catch (Exception JavaDoc che)
70       {
71       }
72     }
73     else
74       filename = commandText.trim();
75
76     if (filename == null || filename.equals("")) //$NON-NLS-1$
77
throw new ConsoleException(ConsoleTranslate
78           .get("controller.command.add.driver.null.filename")); //$NON-NLS-1$
79

80     try
81     {
82       // Send the file contents to the controller
83
jmxClient.getControllerProxy().addDriver(readDriver(filename));
84       console.printInfo(ConsoleTranslate.get(
85           "controller.command.add.driver.file.sent", filename)); //$NON-NLS-1$
86
}
87     catch (FileNotFoundException JavaDoc fnf)
88     {
89       throw new ConsoleException(ConsoleTranslate.get(
90           "controller.command.add.driver.file.not.found", filename)); //$NON-NLS-1$
91
}
92     catch (Exception JavaDoc ioe)
93     {
94       throw new ConsoleException(ConsoleTranslate.get(
95           "controller.command.add.driver.sent.failed", ioe)); //$NON-NLS-1$
96
}
97   }
98
99   private byte[] readDriver(String JavaDoc filename) throws FileNotFoundException JavaDoc,
100       IOException JavaDoc
101   {
102     File JavaDoc file;
103     FileInputStream JavaDoc fileInput = null;
104     file = new File JavaDoc(filename);
105     fileInput = new FileInputStream JavaDoc(file);
106
107     // Read the file into an array of bytes
108
long size = file.length();
109     if (size > Integer.MAX_VALUE)
110       throw new IOException JavaDoc(ConsoleTranslate
111           .get("controller.command.add.driver.file.too.big")); //$NON-NLS-1$
112
byte[] bytes = new byte[(int) size];
113     int nb = fileInput.read(bytes);
114     fileInput.close();
115     if (nb != size)
116       throw new IOException JavaDoc(ConsoleTranslate
117           .get("controller.command.add.driver.file.not.read")); //$NON-NLS-1$
118
return bytes;
119   }
120
121   /**
122    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
123    */

124   public String JavaDoc getCommandName()
125   {
126     return "upload driver"; //$NON-NLS-1$
127
}
128
129   /**
130    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
131    */

132   public String JavaDoc getCommandDescription()
133   {
134     return ConsoleTranslate.get("controller.command.add.driver.description"); //$NON-NLS-1$
135
}
136
137   /**
138    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
139    */

140   public String JavaDoc getCommandParameters()
141   {
142     return ConsoleTranslate.get("controller.command.add.driver.params"); //$NON-NLS-1$
143
}
144
145 }
146
Popular Tags