KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > SecurityRoleMapping


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 package org.netbeans.modules.j2ee.sun.share.configbean;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.enterprise.deploy.model.DDBean JavaDoc;
27 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
28 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
29
30 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
31 import org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException;
32 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
33
34 import org.netbeans.modules.j2ee.sun.share.PrincipalNameMapping;
35 import org.netbeans.modules.j2ee.sun.share.configbean.Base.DefaultSnippet;
36
37
38 /** Property structure of SecurityRoleMapping from DTD:
39  *
40  * securityRoleMapping <security-role-mapping> : SecurityRoleMapping[0,n]
41  * roleName <role-name> : String
42  * (
43  * | principalName <principal-name> : String
44  * | [attr: class-name CDATA #IMPLIED ]
45  * | groupName <group-name> : String
46  * )[1,n]
47  *
48  * Master list of principal and group names are stored in ---
49  *
50  * @author Peter Williams
51  * @version %I%, %G%
52  */

53 public class SecurityRoleMapping extends Base {
54     
55     /** property event names
56      */

57     public static final String JavaDoc ROLE_NAME = "roleName"; // NOI18N
58

59     private DDBean JavaDoc securityRoleNameDD;
60     
61     /** Holds value of property principalName. */
62     private ArrayList JavaDoc/*PrincipalNameMapping*/ principalNames;
63     
64     /** Holds value of property groupName. */
65     private ArrayList JavaDoc/*String*/ groupNames;
66     
67     /** Our parent if this security-role-mapping is inside a root module (EAR, WAR, EJB) */
68     private BaseRoot rootParent;
69     
70     /** Creates a new instance of SunONESRMDConfigBean */
71     public SecurityRoleMapping() {
72         setDescriptorElement(bundle.getString("BDN_SecurityRoleMapping")); // NOI18N
73
}
74
75     /** Override init to enable grouping support for this bean
76      * @param dDBean DDBean matching this bean
77      * @param parent Parent DConfigBean in the tree
78      */

79     protected void init(DDBean JavaDoc dDBean, Base parent) throws ConfigurationException JavaDoc {
80         super.init(dDBean, parent);
81 // !PW Disable grouping code for now, spec non-compliance.
82
// initGroup(dDBean, parent);
83

84         securityRoleNameDD = getNameDD("role-name"); // NOI18N
85

86         updateNamedBeanCache(SunWebApp.SECURITY_ROLE_MAPPING);
87         
88         loadFromPlanFile(getConfig());
89     }
90     
91     protected String JavaDoc getComponentName() {
92         return getRoleName();
93     }
94     
95     /** Getter for helpId property
96      * @return Help context ID for this DConfigBean
97      */

98     public String JavaDoc getHelpId() {
99         return "AS_CFG_SecurityRoleAssignment";
100     }
101     
102     /** The DDBean (or one of it's children) that this DConfigBean is bound to
103      * has changed.
104      *
105      * @param xpathEvent
106      */

107     public void notifyDDChange(XpathEvent JavaDoc xpathEvent) {
108         super.notifyDDChange(xpathEvent);
109
110         if(securityRoleNameDD == xpathEvent.getBean()) {
111             // name changed...
112
getPCS().firePropertyChange(ROLE_NAME, "", getRoleName());
113             getPCS().firePropertyChange(DISPLAY_NAME, "", getDisplayName());
114
115             updateNamedBeanCache(SunWebApp.SECURITY_ROLE_MAPPING);
116         }
117     }
118
119     /** Getter for property roleName.
120      * @return Value of property roleName.
121      *
122      */

123     public String JavaDoc getRoleName() {
124         return cleanDDBeanText(securityRoleNameDD); // .getText();
125
}
126     
127     /** Getter for property principalNames.
128      * @return Value of property principalNames.
129      *
130      */

131     public List JavaDoc/*PrincipalNameMapping*/ getPrincipalNames() {
132         return principalNames;
133     }
134     
135     public PrincipalNameMapping getPrincipalName(int index) {
136         return (PrincipalNameMapping) principalNames.get(index);
137     }
138     
139     /** Setter for property principalNames.
140      * @param newPrincipalNames New value of property principalNames.
141      *
142      * @throws PropertyVetoException
143      *
144      */

145     public void setPrincipalNames(ArrayList JavaDoc/*PrincipalNameMapping*/ newPrincipalNames) throws java.beans.PropertyVetoException JavaDoc {
146         List JavaDoc oldPrincipalNames = principalNames;
147         getVCS().fireVetoableChange("principalNames", oldPrincipalNames, newPrincipalNames); // NOI18N
148
principalNames = newPrincipalNames;
149         getPCS().firePropertyChange("principalNames", oldPrincipalNames, principalNames); // NOI18N
150
}
151     
152     public void addPrincipalName(PrincipalNameMapping newPrincipalName) throws java.beans.PropertyVetoException JavaDoc {
153         getVCS().fireVetoableChange("principalName", null, newPrincipalName); // NOI18N
154
principalNames.add(newPrincipalName);
155         getPCS().firePropertyChange("principalName", null, newPrincipalName ); // NOI18N
156
}
157     
158     public void removePrincipalName(PrincipalNameMapping oldPrincipalName) throws java.beans.PropertyVetoException JavaDoc {
159         getVCS().fireVetoableChange("principalName", oldPrincipalName, null); // NOI18N
160
principalNames.remove(oldPrincipalName);
161         getPCS().firePropertyChange("principalName", oldPrincipalName, null ); // NOI18N
162
}
163     
164     /** Getter for property groupNames.
165      * @return Value of property groupNames.
166      *
167      */

168     public List JavaDoc getGroupNames() {
169         return this.groupNames;
170     }
171     
172     public String JavaDoc getGroupName(int index) {
173         return (String JavaDoc) groupNames.get(index);
174     }
175
176     /** Setter for property groupName.
177      * @param newGroupNames New value of property groupNames.
178      *
179      * @throws PropertyVetoException
180      *
181      */

182     public void setGroupNames(ArrayList JavaDoc newGroupNames) throws java.beans.PropertyVetoException JavaDoc {
183         List JavaDoc oldGroupNames = groupNames;
184         getVCS().fireVetoableChange("groupNames", oldGroupNames, newGroupNames); // NOI18N
185
groupNames = newGroupNames;
186         getPCS().firePropertyChange("groupNames", oldGroupNames, groupNames); // NOI18N
187
}
188     
189     public void addGroupName(String JavaDoc newGroupName) throws java.beans.PropertyVetoException JavaDoc {
190         getVCS().fireVetoableChange("groupName", null, newGroupName); // NOI18N
191
groupNames.add(newGroupName);
192         getPCS().firePropertyChange("groupName", null, newGroupName ); // NOI18N
193
}
194     
195     public void removeGroupName(String JavaDoc oldGroupName) throws java.beans.PropertyVetoException JavaDoc {
196         getVCS().fireVetoableChange("groupName", oldGroupName, null); // NOI18N
197
groupNames.remove(oldGroupName);
198         getPCS().firePropertyChange("groupName", oldGroupName, null ); // NOI18N
199
}
200     
201     /* ------------------------------------------------------------------------
202      * Persistence support. Loads DConfigBeans from previously saved deployment
203      * plan file.
204      */

205     Collection JavaDoc getSnippets() {
206         Collection JavaDoc snippets = new ArrayList JavaDoc();
207         Snippet snipOne = new DefaultSnippet() {
208             public CommonDDBean getDDSnippet() {
209                 org.netbeans.modules.j2ee.sun.dd.api.common.SecurityRoleMapping srm =
210                     getConfig().getStorageFactory().createSecurityRoleMapping();
211
212                 // write properties into SecurityRoleMapping bean
213
srm.setRoleName(getRoleName());
214                 
215                 if(principalNames.size() > 0) {
216                     Iterator JavaDoc principalIter = principalNames.iterator();
217                     while(principalIter.hasNext()) {
218                         PrincipalNameMapping nameMap = (PrincipalNameMapping) principalIter.next();
219                         int index = srm.addPrincipalName(nameMap.getPrincipalName());
220                         if(Utils.notEmpty(nameMap.getClassName())) {
221                             try {
222                                 srm.setPrincipalNameClassName(index, nameMap.getClassName());
223                             } catch(VersionNotSupportedException ex) {
224                                 // Should not happen at runtime.
225
}
226                         }
227                     }
228                 }
229                 
230                 if(groupNames.size() > 0) {
231                     String JavaDoc [] names = (String JavaDoc []) groupNames.toArray(new String JavaDoc[groupNames.size()]);
232                     srm.setGroupName(names);
233                 }
234
235                 return srm;
236             }
237             
238             public boolean hasDDSnippet() {
239                 if(principalNames != null && principalNames.size() > 0) {
240                     return true;
241                 }
242                 
243                 if(groupNames != null && groupNames.size() > 0) {
244                     return true;
245                 }
246                 
247                 return false;
248             }
249             
250             public String JavaDoc getPropertyName() {
251                 return SunWebApp.SECURITY_ROLE_MAPPING;
252             }
253         };
254         
255         snippets.add(snipOne);
256         return snippets;
257     }
258     
259     private class SecurityRoleMappingFinder extends NameBasedFinder {
260         public SecurityRoleMappingFinder(String JavaDoc beanName) {
261             super(org.netbeans.modules.j2ee.sun.dd.api.common.SecurityRoleMapping.ROLE_NAME,
262                 beanName, org.netbeans.modules.j2ee.sun.dd.api.common.SecurityRoleMapping.class);
263         }
264     }
265     
266     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
267         String JavaDoc uriText = getUriText();
268
269         org.netbeans.modules.j2ee.sun.dd.api.common.SecurityRoleMapping beanGraph =
270             (org.netbeans.modules.j2ee.sun.dd.api.common.SecurityRoleMapping) config.getBeans(
271             uriText, constructFileName(), getParser(), new SecurityRoleMappingFinder(getRoleName()));
272         
273         clearProperties();
274         
275         if(beanGraph != null) {
276             String JavaDoc [] names = beanGraph.getPrincipalName();
277             if(names != null && names.length > 0) {
278                 principalNames = new ArrayList JavaDoc(names.length+3);
279                 for(int i = 0; i < names.length; i++) {
280                     String JavaDoc className = null;
281                     try {
282                         className = beanGraph.getPrincipalNameClassName(i);
283                     } catch(VersionNotSupportedException ex) {
284                         // Should not happen at runtime.
285
}
286                     principalNames.add(new PrincipalNameMapping(names[i], className));
287                 }
288             }
289             
290             names = beanGraph.getGroupName();
291             if(names != null && names.length > 0) {
292                 groupNames = new ArrayList JavaDoc(names.length+3);
293                 for(int i = 0; i < names.length; i++) {
294                     groupNames.add(names[i]);
295                 }
296             }
297         } else {
298             setDefaultProperties();
299         }
300         
301         return (beanGraph != null);
302     }
303     
304     protected void clearProperties() {
305         principalNames = new ArrayList JavaDoc(3);
306         groupNames = new ArrayList JavaDoc(3);
307     }
308     
309     protected void setDefaultProperties() {
310         // no defaults
311
}
312 }
313
Popular Tags