KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > commandline > deploymentdaemoncommandlinetool > Main


1 /*
2  * Timer: The timer class
3  * Copyright (C) 2006-2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Main.java
20  */

21
22 package com.rift.coad.commandline.deploymentdaemoncommandlinetool;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.BufferedReader JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import com.rift.coad.daemon.deployment.DeploymentDaemon;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Hashtable JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Set JavaDoc;
40 import javax.naming.Context JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.rmi.PortableRemoteObject JavaDoc;
43 import java.util.PropertyResourceBundle JavaDoc;
44 import org.apache.log4j.BasicConfigurator;
45
46 /**
47  *
48  * @author Glynn Chaldecott
49  *
50  * This class is a command line tool for the Deployment Daemon giving a user
51  * the ability to do basic operations via the command line.
52  */

53 public class Main {
54     public BufferedReader JavaDoc br = new BufferedReader JavaDoc(
55             new InputStreamReader JavaDoc(System.in));
56     public String JavaDoc[] args = null;
57     public String JavaDoc url = "";
58     public String JavaDoc host = "";
59     public String JavaDoc username = "";
60     public String JavaDoc password = "";
61     
62     /** Creates a new instance of Main */
63     public Main() {
64         BasicConfigurator.configure();
65     }
66     
67     /**
68      * @param args the command line arguments
69      */

70     public static void main(String JavaDoc[] args) {
71         Main mainProgram = new Main();
72         mainProgram.setArgs(args);
73         mainProgram.commandLine();
74     }
75     
76     /**
77      * This method simple sets the public args variable.
78      *
79      * @param args This is the args passed into the main method.
80      */

81     public void setArgs(String JavaDoc[] args) {
82         this.args = args;
83     }
84     
85     /**
86      * This method breaks the arguments down and uses them to perform the
87      * necessary task.
88      */

89     public void commandLine() {
90         try {
91             DeploymentDaemon sb = null;
92             int choice = -1;
93             String JavaDoc location = "";
94             String JavaDoc name = "";
95             String JavaDoc path = "";
96             byte[] fileBytes = null;
97             HashMap JavaDoc arguments = new HashMap JavaDoc();
98             for (int i = 0; i < args.length; i ++) {
99                 if (args[i].equals("-d")) {
100                     choice = 0;
101                 } else if (args[i].equals("-f")) {
102                     choice = 1;
103                 } else if (args[i].equals("-h")) {
104                     choice = 2;
105                 } else if (args[i].equals("-l")) {
106                     i ++;
107                     String JavaDoc spacer = "";
108                     location = "";
109                     boolean flag = true;
110                     while (flag) {
111                         location += spacer + args[i];
112                         spacer = " ";
113                         i ++;
114                         if (i == args.length) {
115                             flag = false;
116                         } else if (args[i].charAt(0) == '-') {
117                             flag = false;
118                         }
119                     }
120                     i --;
121                     File JavaDoc file = new File JavaDoc(location);
122                     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
123                     BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(fis);
124                     int count = 0;
125                     byte[] bytes = new byte[1024];
126                     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
127                     while ((count = bis.read(bytes)) != -1) {
128                         baos.write(bytes,0,count);
129                     }
130                     fileBytes = baos.toByteArray();
131                 } else if (args[i].equals("-n")) {
132                     i ++;
133                     name = args[i];
134                 } else if (args[i].equals("-p")) {
135                     i ++;
136                     String JavaDoc spacer = "";
137                     path = "";
138                     boolean flag = true;
139                     while (flag) {
140                         path += spacer + args[i];
141                         spacer = " ";
142                         i ++;
143                         if (i == args.length) {
144                             flag = false;
145                         } else if (args[i].charAt(0) == '-') {
146                             flag = false;
147                         }
148                     }
149                     i --;
150                 } else if (args[i].equals("-s")) {
151                     i ++;
152                     host = args[i];
153                     if (host.indexOf(":") == -1) {
154                         host += ":2000";
155                     }
156                 } else if (args[i].equals("-u")) {
157                     i ++;
158                     url = args[i];
159                 } else if (args[i].equals("-U")) {
160                     i ++;
161                     username = args[i];
162                 } else if (args[i].equals("-P")) {
163                     i ++;
164                     password = args[i];
165                 }
166             }
167             switch (choice) {
168                 case 0: if (!url.equals("") && !host.equals("")) {
169                             sb = createConnection();
170                             sb.daemonDeployer(fileBytes,name);
171                             System.out.println("The daemon has been deployed.");
172                         } else {
173                             System.out.println("Statment missing elements.");
174                             help();
175                         }
176                         break;
177                 case 1: if (!url.equals("") && !host.equals("")) {
178                             sb = createConnection();
179                             sb.copyFile(fileBytes,name,path);
180                             System.out.println("The file has been uploaded.");
181                         } else {
182                             System.out.println("Statment missing elements.");
183                             help();
184                         }
185                         break;
186                 case 2: help();
187                         break;
188                 default: System.out.println("There was a problem with the " +
189                         "statement"); break;
190             }
191         } catch (Exception JavaDoc ex) {
192             ex.printStackTrace();
193         }
194     }
195     
196     public void help() {
197         System.out.println("Help");
198         System.out.println("\t-d \tIs used in order to deploy a Daemon.");
199         System.out.println("\t-f \tIs used to upload a file.");
200         System.out.println("\t-l \tIs followed by the location of the daemon " +
201                 "or file to be uploaded.");
202         System.out.println("\t-n \tThis is followed by the name of the daemon" +
203                 " or file.");
204         System.out.println("\t-p \tThis is followed by the path to which the " +
205                 "file is to be stored.");
206         System.out.println("\t-s \tThis is to be followed by the host and " +
207                 "port on which the Deployment Daemon is running and is " +
208                 "required.");
209         System.out.println("\t-u \tThis is to be followed by the JNDI for " +
210                 "the Deployment Daemon and is required.");
211         System.out.println("\t-U \tThis must be followed by the username for " +
212                 "the connection and is required.");
213         System.out.println("\t-P \tThis must be followed by the password for " +
214                 "the connection and is required.");
215     }
216     
217     /**
218      * This method connects to the Timer Daemon and then returns the Object.
219      *
220      * @return This method returns a connection to the Timer Daemon.
221      */

222     public DeploymentDaemon createConnection()
223             throws Exception JavaDoc {
224         try {
225             Hashtable JavaDoc env = new Hashtable JavaDoc();
226             env.put(Context.INITIAL_CONTEXT_FACTORY,
227                     "com.rift.coad.client.naming." +
228                     "CoadunationInitialContextFactory");
229             env.put(Context.PROVIDER_URL,host);
230             env.put("com.rift.coad.username",username);
231             env.put("com.rift.coad.password",password);
232             Context JavaDoc ctx = new InitialContext JavaDoc(env);
233             
234             Object JavaDoc obj = ctx.lookup(url);
235             DeploymentDaemon beanInterface =
236                     (DeploymentDaemon)
237                     PortableRemoteObject.narrow(obj,
238                     com.rift.coad.daemon.deployment.DeploymentDaemon.class);
239             
240             if (beanInterface == null) {
241                 throw new Exception JavaDoc("narrow failed.");
242             } else {
243                 return beanInterface;
244             }
245         } catch (Exception JavaDoc ex) {
246             System.out.println(ex.getMessage());
247             ex.printStackTrace(System.out);
248             throw new Exception JavaDoc(ex);
249         }
250     }
251     
252 }
253
Popular Tags