1 8 package org.codehaus.loom.xmlpolicy.verifier; 9 10 import org.codehaus.loom.xmlpolicy.metadata.GrantMetaData; 11 import org.codehaus.loom.xmlpolicy.metadata.KeyStoreMetaData; 12 import org.codehaus.loom.xmlpolicy.metadata.PermissionMetaData; 13 import org.codehaus.loom.xmlpolicy.metadata.PolicyMetaData; 14 import org.codehaus.spice.salt.i18n.Resources; 15 import org.codehaus.spice.salt.i18n.ResourceManager; 16 17 33 public class PolicyVerifier 34 { 35 private final static Resources REZ = 36 ResourceManager.getPackageResources( PolicyVerifier.class ); 37 38 public void verifyPolicy( final PolicyMetaData policy ) 39 throws Exception 40 { 41 String message = null; 42 43 message = REZ.getString( "valid-names.notice" ); 44 info( message ); 45 verifyNames( policy ); 46 47 message = REZ.getString( "valid-keyStoreReferences.notice" ); 48 info( message ); 49 verifyKeyStoreReferences( policy ); 50 51 message = REZ.getString( "valid-actions.notice" ); 52 info( message ); 53 verifyActions( policy ); 54 } 55 56 62 protected void info( final String message ) 63 { 64 } 66 67 72 private void verifyNames( final PolicyMetaData policy ) 73 throws Exception 74 { 75 final KeyStoreMetaData[] keyStores = policy.getKeyStores(); 76 for( int i = 0; i < keyStores.length; i++ ) 77 { 78 final String name = keyStores[ i ].getName(); 79 verifyName( name ); 80 } 81 } 82 83 88 private void verifyKeyStoreReferences( final PolicyMetaData policy ) 89 throws Exception 90 { 91 final GrantMetaData[] grants = policy.getGrants(); 92 for( int i = 0; i < grants.length; i++ ) 93 { 94 verifyKeyStore( policy, grants[ i ] ); 95 } 96 } 97 98 103 private void verifyKeyStore( final PolicyMetaData policy, 104 final GrantMetaData grant ) 105 throws Exception 106 { 107 verifyKeyStoreReference( policy, grant.getKeyStore() ); 108 final PermissionMetaData[] permissions = grant.getPermissions(); 109 for( int j = 0; j < permissions.length; j++ ) 110 { 111 final PermissionMetaData permission = permissions[ j ]; 112 verifyKeyStoreReference( policy, permission.getKeyStore() ); 113 } 114 } 115 116 121 private void verifyKeyStoreReference( final PolicyMetaData policy, 122 final String keyStoreName ) 123 throws Exception 124 { 125 if( null == keyStoreName ) 127 { 128 return; 129 } 130 final KeyStoreMetaData[] keyStores = policy.getKeyStores(); 131 for( int i = 0; i < keyStores.length; i++ ) 132 { 133 final KeyStoreMetaData keyStore = keyStores[ i ]; 134 if( keyStore.getName().equals( keyStoreName ) ) 135 { 136 return; 137 } 138 } 139 140 final String message = 141 REZ.format( "bad-keystore-reference.error", 142 keyStoreName ); 143 throw new Exception ( message ); 144 } 145 146 151 private void verifyName( final String name ) 152 throws Exception 153 { 154 final int size = name.length(); 155 if( 0 == size ) 156 { 157 final String message = 158 REZ.format( "empty-name.error", 159 name ); 160 throw new Exception ( message ); 161 } 162 final char ch = name.charAt( 0 ); 163 if( !Character.isLetter( ch ) && 164 '_' != ch ) 165 { 166 final String message = 167 REZ.format( "name-invalid-start.error", 168 name ); 169 throw new Exception ( message ); 170 } 171 172 for( int i = 1; i < size; i++ ) 173 { 174 final char c = name.charAt( i ); 175 if( !Character.isLetterOrDigit( c ) && 176 '_' != c && 177 '-' != c && 178 '.' != c ) 179 { 180 final String message = 181 REZ.format( "name-invalid-char.error", 182 name, 183 String.valueOf( c ) ); 184 throw new Exception ( message ); 185 } 186 } 187 } 188 189 194 private void verifyActions( final PolicyMetaData policy ) 195 throws Exception 196 { 197 final GrantMetaData[] grants = policy.getGrants(); 198 for( int i = 0; i < grants.length; i++ ) 199 { 200 final GrantMetaData grant = grants[ i ]; 201 final PermissionMetaData[] permissions = grant.getPermissions(); 202 for( int j = 0; j < permissions.length; j++ ) 203 { 204 final PermissionMetaData permission = permissions[ j ]; 205 final String target = permission.getTarget(); 206 final String action = permission.getAction(); 207 if( null == target && null != action ) 208 { 209 final String message = 210 REZ.format( "permission-missing-action.error", 211 grant.getCodebase(), 212 permission.getClassname() ); 213 throw new Exception ( message ); 214 } 215 } 216 } 217 } 218 } 219 | Popular Tags |