001: /**
002: * CertAdminUtil.java
003: * @author ss133690
004: * @version 0.1
005: */package com.sun.portal.cli.cert;
006:
007: import java.io.*;
008: import com.sun.portal.log.common.PortalLogger;
009: import java.util.Date;
010: import java.text.SimpleDateFormat;
011:
012: public class CertAdminUtil {
013: /**
014: * Displays the CertAdmin Menu.
015: */
016: static void showMenu() {
017: //println("************************************************************************");
018: println(CertAdminLocale
019: .getPFString("m7", CertAdminConstants.m7));
020: //println("********* Certificate Administration *********");
021: println(CertAdminLocale.getPFString("m71",
022: CertAdminConstants.m71));
023: //println("************************************************************************");
024: println(CertAdminLocale
025: .getPFString("m7", CertAdminConstants.m7));
026: println();
027: //println("1) Generate Self-Signed Certificate");
028: println(CertAdminLocale.getPFString("m72",
029: CertAdminConstants.m72));
030: //println("2) Generate Certificate Signing Request (CSR)");
031: println(CertAdminLocale.getPFString("m73",
032: CertAdminConstants.m73));
033: //println("3) Add Root CA Certificate");
034: println(CertAdminLocale.getPFString("m74",
035: CertAdminConstants.m74));
036: //println("4) Install Certificate From Certificate Authority (CA)");
037: println(CertAdminLocale.getPFString("m75",
038: CertAdminConstants.m75));
039: //println("5) Delete Certificate");
040: println(CertAdminLocale.getPFString("m76",
041: CertAdminConstants.m76));
042: //println("6) Modify Trust Attributes of Certificate (e.g., for PDC)");
043: println(CertAdminLocale.getPFString("m77",
044: CertAdminConstants.m77));
045: //println("7) List Root CA Certificates");
046: println(CertAdminLocale.getPFString("m78",
047: CertAdminConstants.m78));
048: //println("8) List All Certificates");
049: println(CertAdminLocale.getPFString("m79",
050: CertAdminConstants.m79));
051: //println("9) println Certificate content");
052: println(CertAdminLocale.getPFString("m711",
053: CertAdminConstants.m711));
054: //println("10) Quit");
055: println(CertAdminLocale.getPFString("m712",
056: CertAdminConstants.m712));
057: //println("----------------------------");
058: println(CertAdminLocale.getPFString("m713",
059: CertAdminConstants.m713));
060: //print("choice: [10] ");
061: print(CertAdminLocale.getPFString("m714",
062: CertAdminConstants.m714));
063:
064: }
065:
066: static int processMenuOption() {
067: CertAdminUtil.println(CertAdminConstants.newline);
068: showMenu();
069: String input = readConsoleInput();
070: int inputval = 99;
071: try {
072: inputval = Integer.parseInt(input);
073: } catch (Exception ex) {
074: }
075:
076: while ((!input.equals(""))
077: && ((inputval < 1) || (inputval > 10))) {
078: //println("Invalid Option!");
079: println(CertAdminLocale.getPFString("m8",
080: CertAdminConstants.m8));
081: print(CertAdminLocale.getPFString("m714",
082: CertAdminConstants.m714));
083: input = readConsoleInput();
084: try {
085: inputval = Integer.parseInt(input);
086: } catch (Exception ex) {
087: }
088: }
089: CertAdminUtil.println(CertAdminConstants.newline);
090: return inputval;
091:
092: }
093:
094: static boolean yesno(String msg) {
095: String input = question(msg);
096: if (input.equalsIgnoreCase("Y") || input.equals("")) {
097: return true;
098: } else {
099: return false;
100: }
101: }
102:
103: static String question(String msg) {
104: print(msg);
105: return readConsoleInput();
106: }
107:
108: /**
109: * Masks the input entered on the console.
110: */
111: static String questionMasked(String msg) {
112: MaskingThread maskingthread = new MaskingThread(msg);
113: Thread thread = new Thread(maskingthread);
114: thread.start();
115: String maskedInput = readConsoleInput();
116: maskingthread.stopMasking();
117: return maskedInput;
118: }
119:
120: //Checks if the specified file exist.
121: static boolean fileExist(String file) {
122: return new File(file).exists();
123: }
124:
125: //Reads a line from the specified file
126: static String readLine(String infile) {
127: try {
128: File file = new File(infile);
129: BufferedReader br = new BufferedReader(
130: new InputStreamReader(new FileInputStream(file)));
131: return br.readLine();
132: } catch (Exception ex) {
133: return null;
134: }
135: }
136:
137: //Writes a line to the specified file
138: static boolean writeLine(String data, String outfile, boolean append) {
139: try {
140: FileOutputStream fos = new FileOutputStream(outfile, append);
141: PrintWriter pw = new PrintWriter(fos, true);
142: pw.print(data);
143: pw.close();
144: return true;
145: } catch (Exception ex) {
146: return false;
147: }
148: }
149:
150: static boolean writeLine(String data, String outfile) {
151: return writeLine(data, outfile, false);
152: }
153:
154: static boolean delete(String file) {
155: try {
156: new File(file).delete();
157: return true;
158: } catch (Exception ex) {
159: return false;
160: }
161: }
162:
163: static boolean rename(String src, String dest) {
164:
165: try {
166: File srcf = new File(src);
167: File destf = new File(dest);
168: srcf.renameTo(destf);
169: return true;
170: } catch (Exception ex) {
171: return false;
172: }
173:
174: }
175:
176: static boolean mkdir(String dir) {
177: try {
178: File directory = new File(dir);
179: return directory.mkdir();
180: } catch (Exception ex) {
181: return false;
182: }
183: }
184:
185: static String[] list(String dir) {
186: try {
187: File file = new File(dir);
188: if (!file.isDirectory()) {
189: String[] list = new String[1];
190: list[0] = dir;
191: return list;
192: } else {
193: return file.list();
194: }
195: } catch (Exception ex) {
196: return new String[0];
197: }
198: }
199:
200: static void exitOnError(String message) {
201: println(message);
202: System.exit(0);
203: }
204:
205: static void println(String msg) {
206: System.out.println(msg);
207: }
208:
209: static void println() {
210: println("");
211: }
212:
213: static void print(String msg) {
214: System.out.print(msg);
215: }
216:
217: static void print() {
218: print("");
219: }
220:
221: static String readConsoleInput() {
222: String input;
223: try {
224: input = new BufferedReader(new InputStreamReader(System.in))
225: .readLine();
226: } catch (Exception ex) {
227: input = "";
228: }
229: return input.trim();
230: }
231:
232: static String getCurrentDateTime() {
233: Date date = new Date();
234: SimpleDateFormat sdf = new SimpleDateFormat("MMddyykkmmss");
235: String sDate = sdf.format(date);
236: return sDate.trim();
237: }
238: }
|