1 87 package org.codehaus.loom.components.util.verifier; 88 89 import org.codehaus.loom.components.util.metadata.ComponentTemplate; 90 import org.codehaus.loom.components.util.profile.ComponentProfile; 91 import org.codehaus.loom.components.util.profile.PartitionProfile; 92 import org.codehaus.loom.interfaces.ContainerConstants; 93 import org.codehaus.spice.salt.i18n.ResourceManager; 94 import org.codehaus.spice.salt.i18n.Resources; 95 import org.codehaus.dna.Logger; 96 97 113 public class SarVerifier 114 extends AssemblyVerifier 115 { 116 119 private static final Resources REZ = 120 ResourceManager.getPackageResources( SarVerifier.class ); 121 122 125 private static final Class BLOCK_CLASS = 126 getClass( "org.apache.avalon.phoenix.Block" ); 127 128 131 private static final Class BLOCKLISTENER_CLASS = 132 getClass( "org.apache.avalon.phoenix.BlockListener" ); 133 134 137 private final ComponentVerifier m_infoVerifier = new ComponentVerifier(); 138 139 public void enableLogging( final Logger logger ) 140 { 141 super.enableLogging( logger ); 142 143 setupLogger( m_infoVerifier ); 144 } 145 146 156 public void verifySar( final PartitionProfile profile, 157 final ClassLoader classLoader ) 158 throws Exception 159 { 160 final ComponentProfile[] blocks = 161 profile.getPartition( ContainerConstants.BLOCK_PARTITION ) 162 .getComponents(); 163 final ComponentProfile[] listeners = 164 profile.getPartition( ContainerConstants.LISTENER_PARTITION ) 165 .getComponents(); 166 167 String message; 168 169 message = REZ.getString( "verify-valid-names" ); 170 getLogger().info( message ); 171 verifySarName( profile.getMetaData().getName() ); 172 verifyValidNames( listeners ); 173 174 super.verifyAssembly( blocks ); 175 176 message = REZ.getString( "verify-unique-names" ); 177 getLogger().info( message ); 178 checkNamesUnique( blocks, listeners ); 179 180 message = REZ.getString( "verify-block-type" ); 181 getLogger().info( message ); 182 verifyBlocksType( blocks, classLoader ); 183 184 message = REZ.getString( "verify-listener-type" ); 185 getLogger().info( message ); 186 verifyListenersType( listeners, classLoader ); 187 } 188 189 197 private void verifyBlocksType( final ComponentProfile[] blocks, 198 final ClassLoader classLoader ) 199 throws Exception 200 { 201 for( int i = 0; i < blocks.length; i++ ) 202 { 203 verifyBlockType( blocks[i], classLoader ); 204 } 205 } 206 207 215 private void verifyBlockType( final ComponentProfile block, 216 final ClassLoader classLoader ) 217 throws Exception 218 { 219 final ComponentTemplate metaData = block.getTemplate(); 220 final Class clazz = loadClass( "block", metaData, classLoader ); 221 222 m_infoVerifier.verifyType( metaData.getName(), 223 block.getInfo().getType() ); 224 225 if( BLOCK_CLASS.isAssignableFrom( clazz ) ) 226 { 227 final String message = 228 REZ.format( "verifier.implements-block.error", 229 metaData.getName(), 230 metaData.getImplementationKey() ); 231 getLogger().error( message ); 232 System.err.println( message ); 233 } 234 } 235 236 private Class loadClass( final String type, 237 final ComponentTemplate metaData, 238 final ClassLoader classLoader ) 239 throws Exception 240 { 241 try 242 { 243 return classLoader.loadClass( metaData.getImplementationKey() ); 244 } 245 catch( final Exception e ) 246 { 247 final String message = 248 REZ.format( "bad-" + type + "-class", 249 metaData.getName(), 250 metaData.getImplementationKey(), 251 e.getMessage() ); 252 throw new Exception ( message ); 253 } 254 } 255 256 264 private void verifyListenersType( final ComponentProfile[] listeners, 265 final ClassLoader classLoader ) 266 throws Exception 267 { 268 for( int i = 0; i < listeners.length; i++ ) 269 { 270 verifyListenerType( listeners[i], classLoader ); 271 } 272 } 273 274 282 private void verifyListenerType( final ComponentProfile listener, 283 final ClassLoader classLoader ) 284 throws Exception 285 { 286 final ComponentTemplate metaData = listener.getTemplate(); 287 final Class clazz = loadClass( "listener", metaData, classLoader ); 288 if( !BLOCKLISTENER_CLASS.isAssignableFrom( clazz ) ) 289 { 290 final String message = 291 REZ.format( "listener-noimpl-listener", 292 metaData, 293 metaData ); 294 throw new Exception ( message ); 295 } 296 } 297 298 305 private void verifySarName( final String name ) 306 throws Exception 307 { 308 if( !isValidName( name ) ) 309 { 310 final String message = REZ.format( "invalid-sar-name", name ); 311 throw new Exception ( message ); 312 } 313 } 314 315 322 public boolean isValidName( final String name ) 323 { 324 final int size = name.length(); 325 for( int i = 0; i < size; i++ ) 326 { 327 final char ch = name.charAt( i ); 328 329 if( !Character.isLetterOrDigit( ch ) && '-' != ch && '.' != ch ) 330 { 331 return false; 332 } 333 } 334 335 return true; 336 } 337 338 347 private void checkNamesUnique( final ComponentProfile[] blocks, 348 final ComponentProfile[] listeners ) 349 throws Exception 350 { 351 for( int i = 0; i < blocks.length; i++ ) 352 { 353 final String name = blocks[i].getTemplate().getName(); 354 checkNameUnique( name, blocks, listeners, i, -1 ); 355 } 356 357 for( int i = 0; i < listeners.length; i++ ) 358 { 359 final String name = listeners[i].getTemplate().getName(); 360 checkNameUnique( name, blocks, listeners, -1, i ); 361 } 362 } 363 364 376 private void checkNameUnique( final String name, 377 final ComponentProfile[] blocks, 378 final ComponentProfile[] listeners, 379 final int blockIndex, 380 final int listenerIndex ) 381 throws Exception 382 { 383 for( int i = 0; i < blocks.length; i++ ) 385 { 386 final String other = blocks[i].getTemplate().getName(); 387 if( blockIndex != i && name.equals( other ) ) 388 { 389 final String message = REZ.format( "duplicate-name", name ); 390 throw new Exception ( message ); 391 } 392 } 393 394 for( int i = 0; i < listeners.length; i++ ) 396 { 397 final String other = listeners[i].getTemplate().getName(); 398 if( listenerIndex != i && name.equals( other ) ) 399 { 400 final String message = REZ.format( "duplicate-name", name ); 401 throw new Exception ( message ); 402 } 403 } 404 } 405 406 413 private static Class getClass( final String classname ) 414 { 415 try 416 { 417 return Class.forName( classname ); 418 } 419 catch( final ClassNotFoundException cnfe ) 420 { 421 final String message = "Unable to locate class " + 422 classname + " due to " + cnfe; 423 throw new IllegalStateException ( message ); 424 } 425 } 426 } 427 | Popular Tags |