1 87 package org.codehaus.loom.components.monitor; 88 89 import java.io.File ; 90 import java.util.Arrays ; 91 import java.util.HashSet ; 92 import java.util.Iterator ; 93 import java.util.Set ; 94 95 import org.codehaus.loom.components.util.monitor.DirectoryChangeListener; 96 import org.codehaus.loom.components.util.monitor.DirectoryScanner; 97 import org.codehaus.loom.components.util.ExtensionFileFilter; 98 import org.codehaus.loom.interfaces.Deployer; 99 100 import org.codehaus.spice.salt.i18n.ResourceManager; 101 import org.codehaus.spice.salt.i18n.Resources; 102 import org.codehaus.spice.salt.io.FileUtil; 103 104 import org.codehaus.dna.AbstractLogEnabled; 105 import org.codehaus.dna.Active; 106 import org.codehaus.dna.Composable; 107 import org.codehaus.dna.Configurable; 108 import org.codehaus.dna.Configuration; 109 import org.codehaus.dna.ConfigurationException; 110 import org.codehaus.dna.MissingResourceException; 111 import org.codehaus.dna.ResourceLocator; 112 113 121 public class DefaultDeploymentMonitor 122 extends AbstractLogEnabled 123 implements Configurable, Composable, Active, DirectoryChangeListener 124 { 125 private final static Resources REZ = 126 ResourceManager.getPackageResources( DefaultDeploymentMonitor.class ); 127 128 129 private File m_appsDir; 130 131 132 private DirectoryScanner m_scanner; 133 134 135 private Deployer m_deployer; 136 137 138 private long m_frequency; 139 140 151 public void configure( final Configuration configuration ) 152 throws ConfigurationException 153 { 154 m_frequency = 1000L 155 * configuration.getChild( "scanner-frequency" ).getValueAsLong( 5L ); 156 if( m_frequency <= 0 ) 157 { 158 m_frequency = 5000L; 159 } 160 161 final String appsDir = 162 configuration.getChild( "base-application-directory" ).getValue(); 163 m_appsDir = new File ( appsDir ); 164 } 165 166 171 public void compose( final ResourceLocator locator ) 172 throws MissingResourceException 173 { 174 m_deployer = (Deployer)locator.lookup( Deployer.class.getName() ); 175 } 176 177 180 public void initialize() 181 throws Exception  182 { 183 deployDefaultApplications(); 184 m_scanner = new DirectoryScanner(); 185 m_scanner.setDirectory( m_appsDir.getPath() ); 186 m_scanner.setFrequency( m_frequency ); 187 m_scanner.setDirectoryChangeListener( this ); 188 m_scanner.start(); 189 } 190 191 194 public void dispose() 195 throws Exception  196 { 197 m_scanner.stop(); 198 } 199 200 public void unableToListContents() 201 { 202 getLogger().error( REZ.format( "monitor.no-list-contents.error", m_appsDir.toString() ) ); 203 } 204 205 212 public void directoryChange( final int type, final Set fileSet ) 213 { 214 final Set deployments = getDeployments( fileSet ); 215 final Iterator iterator = deployments.iterator(); 216 if( getLogger().isDebugEnabled() ) 217 { 218 final String message = 219 REZ.format( "monitor.directory-change.notice", 220 new Integer ( type ), 221 new Integer ( fileSet.size() ), 222 new Integer ( deployments.size() ) ); 223 getLogger().debug( message ); 224 } 225 if( deployments.isEmpty() ) 226 { 227 return; 228 } 229 230 if( DirectoryChangeListener.ADDITION == type ) 231 { 232 while( iterator.hasNext() ) 233 { 234 final File file = (File )iterator.next(); 235 deployApplication( file ); 236 } 237 } 238 else if( DirectoryChangeListener.REMOVAL == type ) 239 { 240 while( iterator.hasNext() ) 241 { 242 final File file = (File )iterator.next(); 243 undeployApplication( file ); 244 } 245 } 246 else if( DirectoryChangeListener.MODIFICATION == type ) 247 { 248 while( iterator.hasNext() ) 249 { 250 final File file = (File )iterator.next(); 251 redeployApplication( file ); 252 } 253 } 254 } 255 256 261 private void deployApplication( final File file ) 262 { 263 final String name = FileUtil.removeExtension( file.getName() ); 264 try 265 { 266 final String message = 267 REZ.format( "monitor.deploy.notice", name, file ); 268 getLogger().info( message ); 269 m_deployer.deploy( name, file.toURL() ); 270 } 271 catch( final Exception e ) 272 { 273 final String message = 274 REZ.format( "monitor.no-deploy.error", file, e ); 275 getLogger().warn( message, e ); 276 } 277 } 278 279 284 private void undeployApplication( final File file ) 285 { 286 final String name = FileUtil.removeExtension( file.getName() ); 287 try 288 { 289 final String message = 290 REZ.format( "monitor.undeploy.notice", name ); 291 getLogger().info( message ); 292 m_deployer.undeploy( name ); 293 } 294 catch( final Exception e ) 295 { 296 final String message = 297 REZ.format( "monitor.no-undeploy.error", file, e ); 298 getLogger().warn( message, e ); 299 } 300 } 301 302 307 private void redeployApplication( final File file ) 308 { 309 final String name = 310 FileUtil.removeExtension( file.getName() ); 311 try 312 { 313 final String message = 314 REZ.format( "monitor.redeploy.notice", name, file ); 315 getLogger().info( message ); 316 m_deployer.redeploy( name, file.toURL() ); 317 } 318 catch( final Exception e ) 319 { 320 final String message = 321 REZ.format( "monitor.no-redeploy.error", file, e ); 322 getLogger().warn( message, e ); 323 } 324 } 325 326 331 private Set getDeployments( final Set newValue ) 332 { 333 final Set deployments = new HashSet (); 334 final Iterator iterator = newValue.iterator(); 335 while( iterator.hasNext() ) 336 { 337 final File file = (File )iterator.next(); 338 if( isDeployment( file ) ) 339 { 340 deployments.add( file ); 341 } 342 else 343 { 344 final String message = 345 REZ.format( "monitor.skipping-file.notice", file ); 346 getLogger().info( message ); 347 } 348 } 349 return deployments; 350 } 351 352 358 private boolean isDeployment( final File file ) 359 { 360 return !file.isDirectory() && file.getName().endsWith( ".sar" ); 361 } 362 363 369 protected void deployDefaultApplications() 370 throws Exception  371 { 372 final ExtensionFileFilter filter = new ExtensionFileFilter( ".sar" ); 373 final File [] files = m_appsDir.listFiles( filter ); 374 if( null != files ) 375 { 376 deployFiles( files ); 377 } 378 } 379 380 386 private void deployFiles( final File [] files ) 387 throws Exception  388 { 389 Arrays.sort( files ); 390 for( int i = 0; i < files.length; i++ ) 391 { 392 final File file = files[i]; 393 deployApplication( file ); 394 } 395 } 396 } 397 | Popular Tags |