summaryrefslogtreecommitdiffstats
path: root/pki/base/silent/src/common/CMSTask.java
blob: 31ba4547f98c7b83794553f9398750615e809752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

import java.net.*;
import java.io.*;


/**
 * CS Test framework .
 * This class starts and stops CS server from command line  
 */

public class CMSTask {

    private static String operation;
    private static String debug;
    private static String serverRoot;
    private Process p = null;

    /**
     * Constructor . Takes CMS server root as parameter  
     * for example (/export/qa/cert-jupiter2)
     **/

    public CMSTask() {// do nothing
    }

    public CMSTask(String sroot) {
        serverRoot = sroot;
    }

    public boolean CMSStart() {

        try {
            System.out.println("Starting Certificate System:");
            Runtime r = Runtime.getRuntime();

            p = r.exec(serverRoot + "/start-cert");

            InputStreamReader isr = new InputStreamReader(p.getInputStream());
            BufferedReader br = new BufferedReader(isr);
            String s = null;

            try {
                while ((s = br.readLine()) != null) {
                    if (s.indexOf("started") > 0) { 
                        return true;
                    }
                    // do something
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

            return false;

        } catch (Throwable e) {
            e.printStackTrace();
        }

        return false;
    }

    public boolean CMSStop() {
        try {
            Runtime r = Runtime.getRuntime();

            System.out.println("Stopping Certificate System:");
            p = r.exec(serverRoot + "/stop-cert");
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line;

            while ((line = br.readLine()) != null) {
                System.out.println("     " + line);
                if (line.indexOf("server shut down") > -1) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean CMSRestart() {
        try {
            System.out.println("Restarting Certificate System:");
            Runtime r = Runtime.getRuntime();

            p = r.exec(serverRoot + "/restart-cert");
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line;

            while ((line = br.readLine()) != null) {
                System.out.println("     " + line);
                if (line.indexOf("started") > -1) {
                    return true;
                } else {
                    return false;
                }
            }

        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean task() {
        if (operation.equalsIgnoreCase("stop")) {
            CMSStop();
            return true;
        }

        if (operation.equalsIgnoreCase("start")) {
            CMSStart();
            return true;
        }

        if (operation.equalsIgnoreCase("restart")) {
            CMSRestart();
            return true;
        }

        return false;
    }

    public static void main(String args[]) {
        CMSTask prof = new CMSTask();
        // parse args
        StringHolder x_instance_root = new StringHolder();
        StringHolder x_operation = new StringHolder();

        // parse the args
        ArgParser parser = new ArgParser("CMSTask");

        parser.addOption("-instance_root %s #CA Server Root", x_instance_root);
        parser.addOption("-operation %s #CA operation [stop,start,restart]",
                x_operation);

        // and then match the arguments
        String[] unmatched = null;

        unmatched = parser.matchAllArgs(args, 0, parser.EXIT_ON_UNMATCHED);

        if (unmatched != null) {
            System.out.println("ERROR: Argument Mismatch");
            System.exit(-1);
        }

        // set variables
        serverRoot = x_instance_root.value;
        operation = x_operation.value;
		
        boolean st = prof.task();

        if (!st) {
            System.out.println("ERROR");
        }

        System.out.println("SUCCESS");

    } // end of function main

} // end of class