001package org.javasimon.spring; 002 003import java.io.FileNotFoundException; 004import java.io.IOException; 005import java.io.InputStream; 006import java.io.InputStreamReader; 007 008import org.javasimon.Manager; 009import org.javasimon.SimonManager; 010import org.javasimon.SwitchingManager; 011 012import org.springframework.beans.factory.InitializingBean; 013 014/** 015 * Spring bean that configures Simon manager using {@link org.javasimon.ManagerConfiguration} facility. 016 * 017 * @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a> 018 */ 019public class SimonConfigurationBean implements InitializingBean { 020 private Manager simonManager = SimonManager.manager(); 021 private String configurationPath; 022 023 public Manager getSimonManager() { 024 return simonManager; 025 } 026 027 public void setSimonManager(Manager simonManager) { 028 this.simonManager = simonManager; 029 } 030 031 /** 032 * Creates new {@link org.javasimon.SwitchingManager} instead of using {@link org.javasimon.SimonManager#manager()}. 033 */ 034 public void setNewManager() { 035 simonManager = new SwitchingManager(); 036 } 037 038 /** 039 * Loads configuration for the manager from the specified resource path. 040 * 041 * @param configurationPath resource path to the configuration XML 042 * @throws IOException thrown if the resource is not found or the configuration XML is not well formed 043 */ 044 public void setConfiguration(String configurationPath) throws IOException { 045 this.configurationPath = configurationPath; 046 } 047 048 @Override 049 public void afterPropertiesSet() throws Exception { 050 if (configurationPath != null) { 051 InputStream is = getClass().getClassLoader().getResourceAsStream(configurationPath); 052 if (is == null) { 053 throw new FileNotFoundException(configurationPath); 054 } 055 simonManager.configuration().readConfig(new InputStreamReader(is)); 056 } 057 } 058}