001    package org.kuali.common.deploy.monitoring.spring;
002    
003    import org.kuali.common.deploy.DeployContext;
004    import org.kuali.common.deploy.Deployable;
005    import org.kuali.common.deploy.channel.spring.DefaultSecureChannelConfig;
006    import org.kuali.common.deploy.monitoring.AppDynamicsMonitoring;
007    import org.kuali.common.deploy.monitoring.MachineAgent;
008    import org.kuali.common.deploy.monitoring.Monitoring;
009    import org.kuali.common.deploy.monitoring.ServerAgent;
010    import org.kuali.common.deploy.spring.DefaultDeployContextConfig;
011    import org.kuali.common.util.secure.channel.SecureChannel;
012    import org.kuali.common.util.spring.PropertySourceUtils;
013    import org.kuali.common.util.spring.SpringUtils;
014    import org.kuali.common.util.spring.env.EnvironmentService;
015    import org.kuali.common.util.spring.service.SpringServiceConfig;
016    import org.springframework.beans.factory.annotation.Autowired;
017    import org.springframework.context.annotation.Bean;
018    import org.springframework.context.annotation.Configuration;
019    import org.springframework.context.annotation.Import;
020    import org.springframework.core.env.ConfigurableEnvironment;
021    
022    @Configuration
023    @Import({ DefaultDeployContextConfig.class, SpringServiceConfig.class, DefaultSecureChannelConfig.class })
024    public class AppDynamicsConfig implements MonitoringConfig {
025    
026            @Autowired
027            DeployContext context;
028    
029            @Autowired
030            EnvironmentService env;
031    
032            @Autowired
033            ConfigurableEnvironment configurableEnvironment;
034    
035            @Autowired
036            SecureChannel channel;
037    
038            @Override
039            @Bean
040            public Monitoring applicationMonitoring() {
041                    boolean enabled = env.getBoolean("monitoring.enabled", false);
042                    AppDynamicsMonitoring adm = new AppDynamicsMonitoring();
043                    adm.setUser(env.getString("tomcat.user"));
044                    adm.setGroup(env.getString("tomcat.group"));
045                    adm.setChannel(channel);
046                    adm.setMachineAgent(getMachineAgent());
047                    adm.setServerAgent(getServerAgent());
048                    adm.setEnabled(enabled);
049                    adm.setFilterProperties(PropertySourceUtils.getAllEnumerableProperties(configurableEnvironment));
050                    return adm;
051            }
052    
053            protected Deployable getMachineAgentController() {
054                    String local = env.getString("appdynamics.ma.controller.local");
055                    String remote = env.getString("appdynamics.ma.controller");
056                    return new Deployable.Builder(local, remote).filter(true).build();
057            }
058    
059            protected Deployable getServerAgentController() {
060                    String local = env.getString("appdynamics.sa.controller.local");
061                    String remote = env.getString("appdynamics.sa.controller");
062                    return new Deployable.Builder(local, remote).filter(true).build();
063            }
064    
065            protected MachineAgent getMachineAgent() {
066                    MachineAgent agent = new MachineAgent();
067                    agent.setStartupCommand(env.getString("appdynamics.ma.cmd"));
068                    agent.setBaseDir(env.getString("appdynamics.ma.base"));
069                    agent.setTmpDir(env.getString("appdynamics.ma.tmp"));
070                    agent.setLogsDir(env.getString("appdynamics.ma.logs"));
071                    agent.setController(getMachineAgentController());
072                    agent.setLogFile(env.getString("appdynamics.ma.logFile"));
073                    agent.setStartupToken(env.getString("appdynamics.ma.logFile.startupToken"));
074                    agent.setStartupTimeoutMillis(new Long(SpringUtils.getMillis(env, "appdynamics.ma.logFile.monitor.timeout", "5m")).intValue());
075                    agent.setLogFileIntervalMillis(new Long(SpringUtils.getMillis(env, "appdynamics.ma.logFile.monitor.interval", "500ms")).intValue());
076                    agent.setLogFileEncoding(env.getString("appdynamics.ma.logFile.encoding"));
077                    return agent;
078            }
079    
080            protected ServerAgent getServerAgent() {
081                    String startupOptions = env.getString("appdynamics.sa.tomcat.java.options");
082                    String baseDir = env.getString("appdynamics.sa.base");
083                    String logsDir = env.getString("appdynamics.sa.logs");
084                    Deployable controller = getServerAgentController();
085                    return new ServerAgent(startupOptions, controller, baseDir, logsDir);
086            }
087    
088    }