PatternEntry.java

00001 
00027 package org.objectweb.jonas_web.deployment.api;
00028 
00029 import java.security.Permission;
00030 import java.security.PermissionCollection;
00031 import java.security.Permissions;
00032 import java.util.ArrayList;
00033 import java.util.Enumeration;
00034 import java.util.HashMap;
00035 import java.util.Iterator;
00036 import java.util.List;
00037 import java.util.Map;
00038 
00039 import javax.security.jacc.WebResourcePermission;
00040 import javax.security.jacc.WebUserDataPermission;
00041 
00042 
00047 public class PatternEntry {
00048 
00052     private Pattern pattern = null;
00053 
00054 
00058     private boolean uncheckedLastEntry = false;
00059 
00063     private MethodsDesc methods = null;
00064 
00068     private boolean irrelevant = false;
00069 
00070 
00074     private StringBuffer qualified = null;
00075 
00080     public PatternEntry(String pattern) {
00081         this.pattern = new Pattern(pattern);
00082         methods = new MethodsDesc();
00083         qualified = new StringBuffer(pattern);
00084     }
00085 
00086 
00093     public void addMethods(String[] methods, String transportGuarantee, boolean isExcluded) {
00094         this.methods.addMethods(methods, transportGuarantee, isExcluded);
00095     }
00096 
00102     public void addExcludedMethods(String[] methods, String transportGuarantee) {
00103         addMethods(methods, transportGuarantee, true);
00104     }
00105 
00111     public void addUncheckedMethods(String[] methods, String transportGuarantee) {
00112         addMethods(methods, transportGuarantee, false);
00113     }
00114 
00115 
00122     public void addMethodsOnRoles(String[] methods, String[] roles, String transportGuarantee) {
00123         for (int r = 0; r < roles.length; r++) {
00124             addMethodsOnRole(methods, roles[r], transportGuarantee);
00125         }
00126     }
00127 
00128 
00135     public void addMethodsOnRole(String[] methods, String role, String transportGuarantee) {
00136         this.methods.addMethodsOnRole(methods, role, transportGuarantee);
00137     }
00138 
00143     public void setUncheckedLastEntry() {
00144         uncheckedLastEntry = true;
00145     }
00146 
00152     public boolean isUncheckedLastEntry() {
00153         return uncheckedLastEntry;
00154     }
00155 
00156 
00157 
00158 
00164     public void addQualifiedPattern(Pattern otherPattern) {
00165         /*
00166          * Any pattern, qualified by a pattern that matches it, is overriden
00167          * and made irrelevant (in the translation) by the qualifying pattern
00168          */
00169         if (otherPattern.isMatching(pattern)) {
00170             irrelevant = true;
00171         } else {
00172             qualified.append(":");
00173             qualified.append(otherPattern);
00174         }
00175 
00176     }
00177 
00183     public Map getRolesPermissionsMap() {
00184         Map roleMapActions = methods.getRoleMapActions();
00185         String roleName = null;
00186         String actions = null;
00187         Map rolesPermissionsMap = new HashMap();
00188 
00189         // Need to build WebResource for each role Actions
00190         for (Iterator it = roleMapActions.keySet().iterator(); it.hasNext();) {
00191             roleName = (String) it.next();
00192             actions = (String) roleMapActions.get(roleName);
00193             if (actions != null) {
00194                 PermissionCollection pc = new Permissions();
00195                 pc.add(new WebResourcePermission(getQualifiedPattern(), actions));
00196                 rolesPermissionsMap.put(roleName, pc);
00197             }
00198         }
00199         return rolesPermissionsMap;
00200     }
00201 
00202 
00207     public PermissionCollection getExcludedPermissions() {
00208         // Need to build WebResource and WebuserData on these actions
00209         PermissionCollection pc = new Permissions();
00210         String actions = methods.getExcludedActions();
00211         if (!actions.equals("")) {
00212             pc.add(new WebResourcePermission(getQualifiedPattern(), actions));
00213             pc.add(new WebUserDataPermission(getQualifiedPattern(), actions));
00214         }
00215         return pc;
00216     }
00217 
00218 
00223     public PermissionCollection getUncheckedPermissions() {
00224         // First add unchecked permissions and then the WebUserData
00225         // permissions of no excluding auth-constraint
00226         String actions = null;
00227         List permissions = new ArrayList();
00228 
00229         actions = methods.getUncheckedActions();
00230         if (actions == null || (!actions.equals(""))) {
00231             permissions.add(new WebResourcePermission(getQualifiedPattern(), actions));
00232             permissions.add(new WebUserDataPermission(getQualifiedPattern(), actions));
00233         }
00234 
00235         // WebUserData (no excluding auth-constraint)
00236         List actionsList = methods.getUncheckedWebUserDataActionsRoleList();
00237         for (Iterator it = actionsList.iterator(); it.hasNext();) {
00238             actions = (String) it.next();
00239             permissions.add(new WebUserDataPermission(getQualifiedPattern(), actions));
00240         }
00241 
00242         // Try to merge UserDataPermissions with same transport guarantee
00243         PermissionCollection pc = new Permissions();
00244         for (Iterator it = permissions.iterator(); it.hasNext();) {
00245             Permission p = (Permission) it.next();
00246             if (p instanceof WebUserDataPermission) {
00247                 WebUserDataPermission wdp = (WebUserDataPermission) p;
00248                 // get actions of this permission
00249                 String wdpName = wdp.getName();
00250                 String wdpActions = wdp.getActions();
00251                 if (wdpActions == null) {
00252                     // If the permissions got all actions (null = all actions)
00253                     pc.add(p);
00254                     continue;
00255                 }
00256                 boolean wasMerged = false;
00257                 // Now, search all permissions with same transport guarantee
00258                 for (Iterator itLoop = permissions.iterator(); itLoop.hasNext();) {
00259                     Permission loopPerm = (Permission) itLoop.next();
00260                     if (loopPerm instanceof WebUserDataPermission) {
00261                         WebUserDataPermission loopWdp = (WebUserDataPermission) loopPerm;
00262                         // if same permission than our, go on
00263                         if (loopWdp.equals(wdp)) {
00264                             continue;
00265                         }
00266                         String loopWdpName = loopWdp.getName();
00267                         String loopWdpActions = loopWdp.getActions();
00268                         if (loopWdpActions == null) {
00269                             continue;
00270                         }
00271                         boolean wNoTransport = (wdpActions.indexOf(":") == -1);
00272                         boolean loopNoWTransport = (loopWdpActions.indexOf(":") == -1);
00273 
00274                         // Same name and no transport guarantee
00275                         if (wdpName.equals(loopWdpName) && wNoTransport && loopNoWTransport) {
00276                             // merge actions
00277                             String newActions = wdpActions + "," + loopWdpActions;
00278 
00279                             //Add new actions if it doesn't exists
00280                             Enumeration existingPermissions = pc.elements();
00281                             boolean exist = false;
00282                             Permission permissionToAdd = new WebUserDataPermission(wdpName, newActions);
00283                             while (existingPermissions.hasMoreElements()) {
00284                                 Permission perm = (Permission) existingPermissions.nextElement();
00285                                 if (perm.equals(permissionToAdd)) {
00286                                     exist = true;
00287                                 }
00288                             }
00289                             if (!exist) {
00290                                 wasMerged = true;
00291                                 pc.add(permissionToAdd);
00292                             }
00293                         }
00294                     }
00295                 }
00296                 // There was no merge for this permission, just add it.
00297                 if (!wasMerged) {
00298                     pc.add(p);
00299                 }
00300 
00301             } else {
00302                 // Do not merge as it is a WebResourcePermission
00303                 pc.add(p);
00304             }
00305         }
00306 
00307 
00308         return pc;
00309     }
00310 
00311 
00316     public boolean isIrrelevant() {
00317         return irrelevant;
00318     }
00319 
00324     public String getQualifiedPattern() {
00325         return qualified.toString();
00326     }
00327 
00332     public String toString() {
00333         StringBuffer sb = new StringBuffer();
00334         sb.append("PatternEntry[pattern=");
00335         sb.append(pattern);
00336         sb.append(";qualified=");
00337         sb.append(getQualifiedPattern());
00338         sb.append(";irrelevant=");
00339         sb.append(irrelevant);
00340         sb.append("]");
00341         return sb.toString();
00342     }
00343 }

Generated on Tue Feb 15 15:05:58 2005 for JOnAS by  doxygen 1.3.9.1