001: /*
002: * Timer: The timer class
003: * Copyright (C) 2006-2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * Main.java
020: */
021:
022: package com.rift.coad.commandline.deploymentdaemoncommandlinetool;
023:
024: import java.io.BufferedInputStream;
025: import java.io.BufferedReader;
026: import java.io.ByteArrayOutputStream;
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.FileReader;
030: import java.io.InputStreamReader;
031: import com.rift.coad.daemon.deployment.DeploymentDaemon;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Enumeration;
035: import java.util.HashMap;
036: import java.util.Hashtable;
037: import java.util.List;
038: import java.util.Map;
039: import java.util.Set;
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.rmi.PortableRemoteObject;
043: import java.util.PropertyResourceBundle;
044: import org.apache.log4j.BasicConfigurator;
045:
046: /**
047: *
048: * @author Glynn Chaldecott
049: *
050: * This class is a command line tool for the Deployment Daemon giving a user
051: * the ability to do basic operations via the command line.
052: */
053: public class Main {
054: public BufferedReader br = new BufferedReader(
055: new InputStreamReader(System.in));
056: public String[] args = null;
057: public String url = "";
058: public String host = "";
059: public String username = "";
060: public String password = "";
061:
062: /** Creates a new instance of Main */
063: public Main() {
064: BasicConfigurator.configure();
065: }
066:
067: /**
068: * @param args the command line arguments
069: */
070: public static void main(String[] args) {
071: Main mainProgram = new Main();
072: mainProgram.setArgs(args);
073: mainProgram.commandLine();
074: }
075:
076: /**
077: * This method simple sets the public args variable.
078: *
079: * @param args This is the args passed into the main method.
080: */
081: public void setArgs(String[] args) {
082: this .args = args;
083: }
084:
085: /**
086: * This method breaks the arguments down and uses them to perform the
087: * necessary task.
088: */
089: public void commandLine() {
090: try {
091: DeploymentDaemon sb = null;
092: int choice = -1;
093: String location = "";
094: String name = "";
095: String path = "";
096: byte[] fileBytes = null;
097: HashMap arguments = new HashMap();
098: for (int i = 0; i < args.length; i++) {
099: 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 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 file = new File(location);
122: FileInputStream fis = new FileInputStream(file);
123: BufferedInputStream bis = new BufferedInputStream(
124: fis);
125: int count = 0;
126: byte[] bytes = new byte[1024];
127: ByteArrayOutputStream baos = new ByteArrayOutputStream();
128: while ((count = bis.read(bytes)) != -1) {
129: baos.write(bytes, 0, count);
130: }
131: fileBytes = baos.toByteArray();
132: } else if (args[i].equals("-n")) {
133: i++;
134: name = args[i];
135: } else if (args[i].equals("-p")) {
136: i++;
137: String spacer = "";
138: path = "";
139: boolean flag = true;
140: while (flag) {
141: path += spacer + args[i];
142: spacer = " ";
143: i++;
144: if (i == args.length) {
145: flag = false;
146: } else if (args[i].charAt(0) == '-') {
147: flag = false;
148: }
149: }
150: i--;
151: } else if (args[i].equals("-s")) {
152: i++;
153: host = args[i];
154: if (host.indexOf(":") == -1) {
155: host += ":2000";
156: }
157: } else if (args[i].equals("-u")) {
158: i++;
159: url = args[i];
160: } else if (args[i].equals("-U")) {
161: i++;
162: username = args[i];
163: } else if (args[i].equals("-P")) {
164: i++;
165: password = args[i];
166: }
167: }
168: switch (choice) {
169: case 0:
170: if (!url.equals("") && !host.equals("")) {
171: sb = createConnection();
172: sb.daemonDeployer(fileBytes, name);
173: System.out.println("The daemon has been deployed.");
174: } else {
175: System.out.println("Statment missing elements.");
176: help();
177: }
178: break;
179: case 1:
180: if (!url.equals("") && !host.equals("")) {
181: sb = createConnection();
182: sb.copyFile(fileBytes, name, path);
183: System.out.println("The file has been uploaded.");
184: } else {
185: System.out.println("Statment missing elements.");
186: help();
187: }
188: break;
189: case 2:
190: help();
191: break;
192: default:
193: System.out.println("There was a problem with the "
194: + "statement");
195: help();
196: break;
197: }
198: } catch (Exception ex) {
199: ex.printStackTrace();
200: }
201: }
202:
203: public void help() {
204: System.out.println("Help");
205: System.out
206: .println("\t-d \tIs used in order to deploy a Daemon.");
207: System.out.println("\t-f \tIs used to upload a file.");
208: System.out
209: .println("\t-l \tIs followed by the location of the daemon "
210: + "or file to be uploaded.");
211: System.out
212: .println("\t-n \tThis is followed by the name of the daemon"
213: + " or file.");
214: System.out
215: .println("\t-p \tThis is followed by the path to which the "
216: + "file is to be stored.");
217: System.out
218: .println("\t-s \tThis is to be followed by the host and "
219: + "port on which the Deployment Daemon is running and is "
220: + "required.");
221: System.out
222: .println("\t-u \tThis is to be followed by the JNDI for "
223: + "the Deployment Daemon and is required.");
224: System.out
225: .println("\t-U \tThis must be followed by the username for "
226: + "the connection and is required.");
227: System.out
228: .println("\t-P \tThis must be followed by the password for "
229: + "the connection and is required.");
230: }
231:
232: /**
233: * This method connects to the Timer Daemon and then returns the Object.
234: *
235: * @return This method returns a connection to the Timer Daemon.
236: */
237: public DeploymentDaemon createConnection() throws Exception {
238: try {
239: Hashtable env = new Hashtable();
240: env.put(Context.INITIAL_CONTEXT_FACTORY,
241: "com.rift.coad.client.naming."
242: + "CoadunationInitialContextFactory");
243: env.put(Context.PROVIDER_URL, host);
244: env.put("com.rift.coad.username", username);
245: env.put("com.rift.coad.password", password);
246: Context ctx = new InitialContext(env);
247:
248: Object obj = ctx.lookup(url);
249: DeploymentDaemon beanInterface = (DeploymentDaemon) PortableRemoteObject
250: .narrow(
251: obj,
252: com.rift.coad.daemon.deployment.DeploymentDaemon.class);
253:
254: if (beanInterface == null) {
255: throw new Exception("narrow failed.");
256: } else {
257: return beanInterface;
258: }
259: } catch (Exception ex) {
260: System.out.println(ex.getMessage());
261: ex.printStackTrace(System.out);
262: throw new Exception(ex);
263: }
264: }
265:
266: }
|