KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > example > swing > dialog > CheckPermissionDialog


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.example.swing.dialog;
29
30 import java.awt.BorderLayout JavaDoc;
31 import java.awt.Dimension JavaDoc;
32 import java.awt.Frame JavaDoc;
33 import java.awt.GridLayout JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.io.BufferedReader JavaDoc;
37 import java.io.FileReader JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.security.AccessController JavaDoc;
40 import java.security.PrivilegedActionException JavaDoc;
41 import java.security.PrivilegedExceptionAction JavaDoc;
42
43 import javax.security.auth.Subject JavaDoc;
44 import javax.swing.JButton JavaDoc;
45 import javax.swing.JDialog JavaDoc;
46 import javax.swing.JFileChooser JavaDoc;
47 import javax.swing.JLabel JavaDoc;
48 import javax.swing.JOptionPane JavaDoc;
49 import javax.swing.JPanel JavaDoc;
50 import javax.swing.JTextField JavaDoc;
51
52 import net.sf.jguard.core.authorization.policy.AccessControlContextUtils;
53
54 import org.apache.log4j.Logger;
55
56 /**
57  *
58  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
59  * @author <a HREF="mailto:vberetti@users.sourceforge.net">Vincent Beretti</a>
60  */

61 public class CheckPermissionDialog extends JDialog JavaDoc {
62
63     private static final Logger logger = Logger
64             .getLogger(CheckPermissionDialog.class);
65
66     private static final long serialVersionUID = -7641435710653804135L;
67
68     private Subject JavaDoc subject;
69
70     private JTextField JavaDoc fileTxtField;
71
72     public CheckPermissionDialog(Frame JavaDoc parentFrame,Subject JavaDoc subject){
73         super(parentFrame,"Check Permission Dialog",true);
74         this.subject = subject;
75
76         this.setLayout(new BorderLayout JavaDoc());
77
78         JPanel JavaDoc informationPanel = new JPanel JavaDoc();
79         GridLayout JavaDoc informationLayout = new GridLayout JavaDoc(1,3);
80         informationPanel.setLayout(informationLayout);
81
82         JLabel JavaDoc fileLabel = new JLabel JavaDoc(" filepath :");
83         fileTxtField = new JTextField JavaDoc(100);
84         fileTxtField.setColumns(100);
85
86         JButton JavaDoc browseButton = new JButton JavaDoc("browse ...");
87         browseButton.addActionListener(new ActionListener JavaDoc(){
88             public void actionPerformed(ActionEvent JavaDoc evt){
89                 handleBrowse();
90             }
91         });
92         informationPanel.add(fileLabel);
93         informationPanel.add(fileTxtField);
94         informationPanel.add(browseButton);
95
96         informationPanel.setPreferredSize(new Dimension JavaDoc(450,20));
97
98         JPanel JavaDoc validationPanel=new JPanel JavaDoc();
99
100         JButton JavaDoc tryButton = new JButton JavaDoc("try");
101         tryButton.addActionListener(new ActionListener JavaDoc(){
102             public void actionPerformed(ActionEvent JavaDoc e){
103                 handleTry();
104             }
105         });
106
107         validationPanel.add(tryButton);
108
109
110         this.add(informationPanel,BorderLayout.CENTER);
111         this.add(validationPanel,BorderLayout.SOUTH);
112
113         this.pack();
114         this.setResizable(false);
115         this.setLocation(parentFrame.getLocation().x,parentFrame.getLocation().y);
116         this.setVisible(true);
117     }
118
119
120     private void handleTry(){
121
122         final String JavaDoc filePath = fileTxtField.getText();
123         try{
124             // execute code with subject permissions and protectionDomain from currentThread
125
// @see AccessControllerUtils.getStackSubjectAccessControlContext()
126
AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
127                 public Object JavaDoc run() throws SecurityException JavaDoc, IOException JavaDoc{
128
129                         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(filePath));
130                         br.readLine();
131                         br.close();
132
133                     return null;
134                 }
135             },AccessControlContextUtils.getStackSubjectAccessControlContext(subject));
136
137             JOptionPane.showMessageDialog(this,"You have the permissions to do that","permission allowed",JOptionPane.INFORMATION_MESSAGE);
138
139         }catch(SecurityException JavaDoc e){
140             logger.error("Logged subject has no permission to read this file",e);
141             JOptionPane.showMessageDialog(this,"You don't have the permissions to do that ","permission denied",JOptionPane.ERROR_MESSAGE);
142         } catch (PrivilegedActionException JavaDoc e) {
143             logger.error("PrivilegedActionException ioException",e);
144             JOptionPane.showMessageDialog(this,"You don't have the permissions to do that ","permission denied",JOptionPane.ERROR_MESSAGE);
145         }
146     }
147
148
149     private void handleBrowse(){
150
151         JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc();
152         int state = fileChooser.showOpenDialog(this);
153         if(state == JFileChooser.APPROVE_OPTION){
154             String JavaDoc selectedPath = fileChooser.getSelectedFile().getAbsolutePath();
155             fileTxtField.setText(selectedPath);
156         }
157
158     }
159 }
160
Popular Tags