KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > SudoPanel


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2003 Jan Blok
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.panels;
23
24 import java.awt.BorderLayout JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import javax.swing.BorderFactory JavaDoc;
35 import javax.swing.Box JavaDoc;
36 import javax.swing.BoxLayout JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JOptionPane JavaDoc;
39 import javax.swing.JPanel JavaDoc;
40 import javax.swing.JPasswordField JavaDoc;
41 import javax.swing.JTextField JavaDoc;
42
43 import com.izforge.izpack.ExecutableFile;
44 import com.izforge.izpack.ParsableFile;
45 import com.izforge.izpack.gui.LabelFactory;
46 import com.izforge.izpack.installer.InstallData;
47 import com.izforge.izpack.installer.InstallerFrame;
48 import com.izforge.izpack.installer.IzPanel;
49 import com.izforge.izpack.installer.ScriptParser;
50 import com.izforge.izpack.util.FileExecutor;
51 import com.izforge.izpack.util.OsConstraint;
52 import com.izforge.izpack.util.VariableSubstitutor;
53
54 /**
55  * The packs selection panel class.
56  *
57  * @author Jan Blok
58  * @since November 27, 2003
59  */

60 public class SudoPanel extends IzPanel implements ActionListener JavaDoc
61 {
62
63     /**
64      *
65      */

66     private static final long serialVersionUID = 3689628116465561651L;
67
68     private JTextField JavaDoc passwordField;
69
70     private boolean isValid = false;
71
72     /**
73      * The constructor.
74      *
75      * @param parent The parent window.
76      * @param idata The installation data.
77      */

78     public SudoPanel(InstallerFrame parent, InstallData idata)
79     {
80         super(parent, idata);
81
82         setLayout(new BoxLayout JavaDoc(this, BoxLayout.Y_AXIS));
83
84         add(LabelFactory
85                 .create(
86                         /* parent.langpack.getString("SudoPanel.info") */"For installing administrator privileges are necessary",
87                         JLabel.TRAILING));
88
89         add(Box.createRigidArea(new Dimension JavaDoc(0, 5)));
90
91         add(LabelFactory
92                 .create(
93                         /* parent.langpack.getString("SudoPanel.tip") */"Please note that passwords are case-sensitive",
94                         parent.icons.getImageIcon("tip"), JLabel.TRAILING));
95
96         add(Box.createRigidArea(new Dimension JavaDoc(0, 5)));
97
98         JPanel JavaDoc spacePanel = new JPanel JavaDoc();
99         spacePanel.setAlignmentX(LEFT_ALIGNMENT);
100         spacePanel.setAlignmentY(CENTER_ALIGNMENT);
101         spacePanel.setBorder(BorderFactory.createEmptyBorder(80, 30, 0, 50));
102         spacePanel.setLayout(new BorderLayout JavaDoc(5, 5));
103         spacePanel
104                 .add(
105                         LabelFactory
106                                 .create(
107                                 /* parent.langpack.getString("SudoPanel.specifyAdminPassword") */"Please specify your password:"),
108                         BorderLayout.NORTH);
109         passwordField = new JPasswordField JavaDoc();
110         passwordField.addActionListener(this);
111         JPanel JavaDoc space2Panel = new JPanel JavaDoc();
112         space2Panel.setLayout(new BorderLayout JavaDoc());
113         space2Panel.add(passwordField, BorderLayout.NORTH);
114         space2Panel.add(Box.createRigidArea(new Dimension JavaDoc(0, 5)), BorderLayout.CENTER);
115         spacePanel.add(space2Panel, BorderLayout.CENTER);
116         add(spacePanel);
117     }
118
119     /** Called when the panel becomes active. */
120     public void panelActivate()
121     {
122         passwordField.requestFocus();
123     }
124
125     /**
126      * Actions-handling method.
127      *
128      * @param e The event.
129      */

130     public void actionPerformed(ActionEvent JavaDoc e)
131     {
132         doSudoCmd();
133     }
134
135     // check if sudo password is correct (so sudo can be used in all other
136
// scripts, even without password, lasts for 5 minutes)
137
private void doSudoCmd()
138     {
139         String JavaDoc pass = passwordField.getText();
140
141         File JavaDoc file = null;
142         try
143         {
144             // write file in /tmp
145
file = new File JavaDoc("/tmp/cmd_sudo.sh");// ""c:/temp/run.bat""
146
FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
147             fos.write("echo $password | sudo -S ls\nexit $?".getBytes()); // "echo
148
// $password
149
// >
150
// pipo.txt"
151
fos.close();
152
153             // execute
154
Properties JavaDoc vars = new Properties JavaDoc();
155             vars.put("password", pass);
156
157             List JavaDoc oses = new ArrayList JavaDoc();
158             oses.add(new OsConstraint("unix", null, null, null));
159
160             ArrayList JavaDoc plist = new ArrayList JavaDoc();
161             ParsableFile pf = new ParsableFile(file.getAbsolutePath(), null, null, oses);
162             plist.add(pf);
163             ScriptParser sp = new ScriptParser(plist, new VariableSubstitutor(vars));
164             sp.parseFiles();
165
166             ArrayList JavaDoc elist = new ArrayList JavaDoc();
167             ExecutableFile ef = new ExecutableFile(file.getAbsolutePath(),
168                     ExecutableFile.POSTINSTALL, ExecutableFile.ABORT, oses, false);
169             elist.add(ef);
170             FileExecutor fe = new FileExecutor(elist);
171             int retval = fe.executeFiles(ExecutableFile.POSTINSTALL, this);
172             if (retval == 0)
173             {
174                 idata.setVariable("password", pass);
175                 isValid = true;
176             }
177             // else is already showing dialog
178
// {
179
// JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd,
180
// check your password", "Error", JOptionPane.ERROR_MESSAGE);
181
// }
182
}
183         catch (Exception JavaDoc e)
184         {
185             // JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd,
186
// check your password", "Error", JOptionPane.ERROR_MESSAGE);
187
e.printStackTrace();
188             isValid = false;
189         }
190         try
191         {
192             if (file != null && file.exists()) file.delete();// you don't
193
// want the file
194
// with password
195
// tobe arround,
196
// in case of
197
// error
198
}
199         catch (Exception JavaDoc e)
200         {
201             // ignore
202
}
203     }
204
205     /**
206      * Indicates wether the panel has been validated or not.
207      *
208      * @return Always true.
209      */

210     public boolean isValidated()
211     {
212         if (!isValid)
213         {
214             doSudoCmd();
215         }
216         if (!isValid)
217         {
218             JOptionPane.showInternalMessageDialog(this, "Password", "Password is not valid",
219                     JOptionPane.ERROR_MESSAGE);
220         }
221         return isValid;
222     }
223 }
224
Popular Tags