KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > webui > Shutdown


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.webui;
17
18 import org.jmanage.core.auth.UserManager;
19 import org.jmanage.core.auth.User;
20 import org.jmanage.core.auth.AuthConstants;
21 import org.jmanage.core.util.PasswordField;
22 import org.jmanage.core.util.JManageProperties;
23 import org.jmanage.core.crypto.Crypto;
24
25 import java.net.Socket JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 /**
30  * Shutdown is used to remotely shut down the jManage Server. It uses
31  * admin password to generate a key which is recognized by the remote
32  * process to authorize a shutdown.
33  *
34  * @author Shashank Bellary
35  * Date: Nov 29, 2005
36  */

37 public class Shutdown {
38
39     private static int _port = Integer.parseInt(JManageProperties.getStopPort());
40     private static String JavaDoc _key;
41
42     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
43         UserManager userManager = UserManager.getInstance();
44         User user = null;
45         char[] password = null;
46         int invalidAttempts = 0;
47
48         if(args.length == 1){
49             password = args[0].toCharArray();
50             user = userManager.verifyUsernamePassword(
51                     AuthConstants.USER_ADMIN, password);
52             /* invalid password was tried */
53             if(user == null){
54                 invalidAttempts ++;
55             }
56         }
57
58         while(user == null){
59             if(invalidAttempts > 0){
60                 System.out.println("Invalid Admin Password.");
61             }
62             /* get the password */
63             password = PasswordField.getPassword("Enter password:");
64             /* the password should match for the admin user */
65             user = userManager.verifyUsernamePassword(
66                     AuthConstants.USER_ADMIN, password);
67             invalidAttempts ++;
68             if(invalidAttempts >= 3){
69                 break;
70             }
71         }
72
73         /* exit if the admin password is still invalid */
74         if(user == null){
75             System.out.println("Number of invalid attempts exceeded. Exiting !");
76             return;
77         }
78
79         final JettyStopKey stopKey = new JettyStopKey(new String JavaDoc(password));
80         _key = stopKey.toString();
81
82         System.out.println("Shutting down...");
83
84         stop();
85     }
86
87     /**
88      * Main shutdown method
89      */

90     static void stop(){
91         try{
92             Socket JavaDoc s = new Socket JavaDoc(InetAddress.getLocalHost(), _port);
93             OutputStream JavaDoc out = s.getOutputStream();
94             out.write((_key+"\r\nstop\r\n").getBytes());
95             out.flush();
96             s.shutdownOutput();
97             s.close();
98         }catch (Exception JavaDoc e){
99             e.printStackTrace();
100         }
101     }
102 }
103
Popular Tags