KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > EjbSecurityPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.multiview;
21
22 import java.util.StringTokenizer JavaDoc;
23 import javax.swing.JRadioButton JavaDoc;
24 import org.netbeans.modules.j2ee.dd.api.common.RunAs;
25 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
26 import org.netbeans.modules.j2ee.dd.api.ejb.AssemblyDescriptor;
27 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
28 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
29 import org.netbeans.modules.j2ee.dd.api.ejb.Method;
30 import org.netbeans.modules.j2ee.dd.api.ejb.MethodPermission;
31 import org.netbeans.modules.j2ee.dd.api.ejb.SecurityIdentity;
32 import org.netbeans.modules.j2ee.ddloaders.multiview.ui.SecurityForm;
33 import org.netbeans.modules.xml.multiview.ItemCheckBoxHelper;
34 import org.netbeans.modules.xml.multiview.ItemEditorHelper;
35 import org.netbeans.modules.xml.multiview.ItemOptionHelper;
36 import org.netbeans.modules.xml.multiview.XmlMultiViewDataSynchronizer;
37 import org.netbeans.modules.xml.multiview.ui.SectionNodeView;
38
39 /**
40  * EjbSecurityPanel.java
41  *
42  * Panel for adding and creating security related deployment descriptor elements.
43  * @author ptliu
44  */

45 public class EjbSecurityPanel extends SecurityForm {
46     private static String JavaDoc ALL_METHODS = "*"; //NOI18N
47

48     private MethodPermission methodPermission;
49     
50     private EjbJar ejbJar;
51     
52     private AssemblyDescriptor assemblyDesc;
53     
54     /** Creates a new instance of EjbSecurityPanel */
55     public EjbSecurityPanel(SectionNodeView sectionNodeView, final Ejb ejb) {
56         super(sectionNodeView);
57  
58         EjbJarMultiViewDataObject dataObject = (EjbJarMultiViewDataObject) sectionNodeView.getDataObject();
59         this.ejbJar = dataObject.getEjbJar();
60         
61         this.assemblyDesc = ejbJar.getSingleAssemblyDescriptor();
62         
63         final XmlMultiViewDataSynchronizer synchronizer = dataObject.getModelSynchronizer();
64         
65         addRefreshable(new ItemOptionHelper(synchronizer, getSecurityIDButtonGroup()) {
66             public String JavaDoc getItemValue() {
67                 SecurityIdentity securityIdentity = ejb.getSecurityIdentity();
68                 
69                 if (securityIdentity != null) {
70                     if (securityIdentity.isUseCallerIdentity()) {
71                         return USE_CALLER_ID;
72                     } else if (securityIdentity.getRunAs() != null) {
73                         return RUN_AS;
74                     }
75                 }
76                 
77                 return NO_SECURITY_ID;
78             }
79             
80             public void setItemValue(String JavaDoc value) {
81                 updateSecurityIdentity(ejb);
82                 
83                 updateVisualState();
84             }
85         });
86         
87         addRefreshable(new ItemEditorHelper(getRunAsRoleNameTF(),
88                 new TextItemEditorModel(synchronizer, true, true) {
89             protected String JavaDoc getValue() {
90                 RunAs runAs = getRunAs(ejb);
91                 
92                 if (runAs != null) {
93                     return runAs.getRoleName();
94                 } else {
95                     return getRunAsRoleNameTF().getText();
96                 }
97             }
98             
99             protected void setValue(String JavaDoc value) {
100                 RunAs runAs = getRunAs(ejb);
101                 
102                 if (runAs != null) {
103                     updateRunAs(runAs);
104                 }
105             }
106         }));
107         
108         addRefreshable(new ItemEditorHelper(getRunAsDescriptionTF(),
109                 new TextItemEditorModel(synchronizer, true, true) {
110             protected String JavaDoc getValue() {
111                 RunAs runAs = getRunAs(ejb);
112                 
113                 if (runAs != null) {
114                     return runAs.getDefaultDescription();
115                 } else {
116                     return getRunAsDescriptionTF().getText();
117                 }
118             }
119             
120             protected void setValue(String JavaDoc value) {
121                 RunAs runAs = getRunAs(ejb);
122                 
123                 if (runAs != null) {
124                     updateRunAs(runAs);
125                 }
126             }
127         }));
128         
129         addRefreshable(new ItemOptionHelper(synchronizer,
130                 getGlobalMethodPermissionButtonGroup()) {
131             public String JavaDoc getItemValue() {
132                 MethodPermission permission = getGlobalMethodPermission(ejb);
133   
134                 if (permission != null) {
135                     try {
136                         if (permission.isUnchecked()) {
137                             return ALL_METHOD_PERMISSION;
138                         } else {
139                             return SET_ROLE_METHOD_PERMISSION;
140                         }
141                     } catch (Exception JavaDoc ex) {
142                         return SET_ROLE_METHOD_PERMISSION;
143                     }
144                 }
145                 
146                 return NO_METHOD_PERMISSION;
147             }
148             
149             public void setItemValue(String JavaDoc value) {
150                 updateMethodPermission(assemblyDesc, ejb);
151                 
152                 updateVisualState();
153             }
154         });
155         
156         addRefreshable(new ItemEditorHelper(getSetRoleRoleNamesTF(),
157                 new TextItemEditorModel(synchronizer, true, true) {
158             boolean endsWithComma = false;
159             
160             protected String JavaDoc getValue() {
161                 MethodPermission permission = getGlobalMethodPermission(ejb);
162                 
163                 try {
164                     if (permission != null && !permission.isUnchecked()) {
165                         String JavaDoc roleNames = getCommaSeparatedString(permission.getRoleName());
166                         if (endsWithComma)
167                             roleNames += ","; //NOI18N
168

169                         return roleNames;
170                     } else {
171                         return getSetRoleRoleNamesTF().getText();
172                     }
173                 } catch (VersionNotSupportedException ex) {
174                     return ""; //NOI18N
175
}
176             }
177             
178             protected void setValue(String JavaDoc value) {
179                 if (value != null && value.trim().endsWith(",")) { //NOI18N
180
endsWithComma = true;
181                 } else {
182                     endsWithComma = false;
183                 }
184                 
185                 updateMethodPermission(assemblyDesc, ejb);
186             }
187         }));
188         
189         updateVisualState();
190     }
191     
192     public void dataModelPropertyChange(Object JavaDoc source, String JavaDoc propertyName,
193             Object JavaDoc oldValue, Object JavaDoc newValue) {
194         scheduleRefreshView();
195     }
196     
197     private void updateSecurityIdentity(Ejb ejb) {
198         JRadioButton JavaDoc noSecurityIDRB = getNoSecurityIDRB();
199         JRadioButton JavaDoc useCallerIDRB = getUseCallerIDRB();
200         JRadioButton JavaDoc runAsRB = getRunAsRB();
201         
202         if (noSecurityIDRB.isSelected()) {
203             removeSecurityIdentity(ejb);
204         } else {
205             SecurityIdentity securityID = ejb.getSecurityIdentity();
206             if (securityID == null) {
207                 securityID = ejb.newSecurityIdentity();
208                 ejb.setSecurityIdentity(securityID);
209             }
210             
211             if (runAsRB.isSelected()) {
212                 RunAs runAs = securityID.getRunAs();
213                 
214                 if (runAs == null) {
215                     runAs = securityID.newRunAs();
216                     securityID.setRunAs(runAs);
217                     
218                     updateRunAs(runAs);
219                 }
220             } else {
221                 removeRunAs(securityID);
222             }
223             
224             if (useCallerIDRB.isSelected()) {
225                 securityID.setUseCallerIdentity(true);
226             } else {
227                 securityID.setUseCallerIdentity(false);
228             }
229         }
230     }
231     
232     private void removeSecurityIdentity(Ejb ejb) {
233         ejb.setSecurityIdentity(null);
234     }
235     
236     private RunAs getRunAs(Ejb ejb) {
237         SecurityIdentity securityIdentity = ejb.getSecurityIdentity();
238         if (securityIdentity != null) {
239             return securityIdentity.getRunAs();
240         }
241         
242         return null;
243     }
244     
245     private void updateRunAs(RunAs runAs) {
246         String JavaDoc prevRoleName = runAs.getRoleName();
247         String JavaDoc newRoleName = getRunAsRoleNameTF().getText();
248         runAs.setRoleName(newRoleName);
249         runAs.setDescription(this.getRunAsDescriptionTF().getText());
250     }
251     
252     private void removeRunAs(SecurityIdentity securityIdentity) {
253         RunAs runAs = securityIdentity.getRunAs();
254         
255         if (runAs != null) {
256             securityIdentity.setRunAs(null);
257         }
258     }
259     
260     private MethodPermission getGlobalMethodPermission(Ejb ejb) {
261         
262         if (assemblyDesc == null) return null;
263         
264         MethodPermission methodPermission = null;
265         
266         MethodPermission[] permissions = assemblyDesc.getMethodPermission();
267         String JavaDoc ejbName = ejb.getEjbName();
268         
269         for (int i = 0; i < permissions.length; i++) {
270             MethodPermission permission = permissions[i];
271             Method method = permission.getMethod(0);
272             
273             if (method != null) {
274                 String JavaDoc methodEjbName = method.getEjbName();
275                 String JavaDoc methodName = method.getMethodName();
276                 
277                 if (methodEjbName != null && methodEjbName.equals(ejbName) &&
278                         methodName != null && methodName.equals(ALL_METHODS)) {
279                     methodPermission = permission;
280                     break;
281                 }
282             }
283         }
284      
285         return methodPermission;
286     }
287     
288     private MethodPermission createGlobalMethodPermission(Ejb ejb) {
289         if (assemblyDesc == null) {
290             assemblyDesc = getAssemblyDesc();
291         }
292         
293         methodPermission = assemblyDesc.newMethodPermission();
294         Method method = methodPermission.newMethod();
295         method.setEjbName(ejb.getEjbName());
296         method.setMethodName(ALL_METHODS); //NOI18N
297
methodPermission.addMethod(method);
298         assemblyDesc.addMethodPermission(methodPermission);
299         
300         return methodPermission;
301     }
302     
303     private void removeGlobalMethodPermission() {
304         if (methodPermission != null) {
305             assemblyDesc.removeMethodPermission(methodPermission);
306             methodPermission = null;
307         }
308     }
309     
310     private void updateMethodPermission(AssemblyDescriptor assemblyDesc, Ejb ejb) {
311         if (this.getNoPermissionRB().isSelected()) {
312             removeGlobalMethodPermission();
313         } else {
314             MethodPermission permission = getGlobalMethodPermission(ejb);
315             if (permission == null) {
316                 permission = createGlobalMethodPermission(ejb);
317             }
318             
319             if (this.getAllMethodPermissionRB().isSelected()) {
320                 permission.setRoleName(null);
321                 
322                 try {
323                     permission.setUnchecked(true);
324                 } catch (VersionNotSupportedException ex) {
325                     ex.printStackTrace();
326                 }
327             } else if (this.getSetRolePermissionRB().isSelected()) {
328                 try {
329                     permission.setUnchecked(false);
330                 } catch (VersionNotSupportedException ex) {
331                     ex.printStackTrace();
332                 }
333                 
334                 String JavaDoc roleNames = getSetRoleRoleNamesTF().getText();
335                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(roleNames, ","); //NOI18N
336
permission.setRoleName(null);
337                 
338                 while (tokenizer.hasMoreTokens()) {
339                     String JavaDoc roleName = tokenizer.nextToken().trim();
340                     
341                     if (roleName.length() > 0)
342                         permission.addRoleName(roleName);
343                 }
344             }
345         }
346     }
347     
348     private String JavaDoc getCommaSeparatedString(String JavaDoc[] values) {
349         String JavaDoc result = ""; //NOI18N
350

351         for (int i = 0; i < values.length; i++) {
352             if (i > 0) {
353                 result += ", "; //NOI18N
354
}
355             
356             result += values[i];
357         }
358         
359         return result;
360     }
361     
362     private AssemblyDescriptor getAssemblyDesc() {
363         AssemblyDescriptor assemblyDesc = ejbJar.getSingleAssemblyDescriptor();
364         
365         if (assemblyDesc == null) {
366             assemblyDesc = ejbJar.newAssemblyDescriptor();
367             ejbJar.setAssemblyDescriptor(assemblyDesc);
368         }
369         
370         return assemblyDesc;
371     }
372 }
373
Popular Tags