1515
1616package com .appdynamics .extensions ;
1717
18- import com .appdynamics .extensions .conf .MonitorConfiguration ;
18+ import com .appdynamics .extensions .conf .MonitorContextConfiguration ;
19+ import com .appdynamics .extensions .conf .MonitorContext ;
20+ import com .appdynamics .extensions .file .FileWatchListener ;
21+ import com .appdynamics .extensions .util .AssertUtils ;
22+ import com .appdynamics .extensions .util .PathResolver ;
1923import com .singularity .ee .agent .systemagent .api .AManagedMonitor ;
2024import com .singularity .ee .agent .systemagent .api .TaskExecutionContext ;
2125import com .singularity .ee .agent .systemagent .api .TaskOutput ;
3539 * <p>An {@code ABaseMonitor} is a wrapper on top of
3640 * {@link AManagedMonitor} to remove the boiler plate code of
3741 * creating a {@link AMonitorJob} and initializing the
38- * {@link MonitorConfiguration }.
42+ * {@link MonitorContextConfiguration }.
3943 *
4044 * <p>The MA or SIM agent loads all the {@link AManagedMonitor}s
4145 * from their respective subdirectories in the monitors directory
@@ -87,11 +91,13 @@ public abstract class ABaseMonitor extends AManagedMonitor{
8791 */
8892 protected String monitorName ;
8993
94+ private File installDir ;
95+
9096 /**
91- * A configuration object that reads the monitor's config file
97+ * A contextConfiguration object that reads the monitor's config file
9298 * and initializes the different bits required by the monitor.
9399 */
94- protected MonitorConfiguration configuration ;
100+ private MonitorContextConfiguration contextConfiguration ;
95101
96102 /**
97103 * A runnable which does all the leg work for fetching the
@@ -104,21 +110,39 @@ public ABaseMonitor(){
104110 logger .info ("Using {} Version [{}]" ,monitorName , getImplementationVersion ());
105111 }
106112
107- protected void initialize (Map <String , String > args ) {
108- if (configuration == null ){
113+ protected void initialize (final Map <String , String > args ) {
114+ if (contextConfiguration == null ){
115+ installDir = getInstallDirectory ();
109116 monitorJob = createMonitorJob ();
110- MonitorConfiguration conf = new MonitorConfiguration (monitorName ,getDefaultMetricPrefix (), monitorJob );
111- conf .setConfigYml (args .get ("config-file" ), new MonitorConfiguration .FileWatchListener () {
112- @ Override
113- public void onFileChange (File file ) {
114- onConfigReload (file );
115- }
116- });
117- initializeMoreStuff (args , conf );
118- this .configuration = conf ;
117+ contextConfiguration = createContextConfiguration ();
118+ contextConfiguration .registerListener (args .get ("config-file" ), createYmlFileListener (args .get ("config-file" )));
119+ initializeMoreStuff (args );
119120 }
120121 }
121122
123+ private FileWatchListener createYmlFileListener (final String ymlFile ) {
124+ FileWatchListener fileWatchListener = new FileWatchListener () {
125+ @ Override
126+ public void onFileChange (File file ) {
127+ contextConfiguration .setConfigYml (ymlFile );
128+ onConfigReload (file );
129+ }
130+ };
131+ return fileWatchListener ;
132+ }
133+
134+ private File getInstallDirectory () {
135+ File installDir = PathResolver .resolveDirectory (AManagedMonitor .class );
136+ if (installDir == null ){
137+ throw new RuntimeException ("The install directory cannot be null" );
138+ }
139+ return installDir ;
140+ }
141+
142+ private MonitorContextConfiguration createContextConfiguration () {
143+ return new MonitorContextConfiguration (getMonitorName (), getDefaultMetricPrefix (), installDir , monitorJob );
144+ }
145+
122146 protected void onConfigReload (File file ){};
123147
124148 protected AMonitorJob createMonitorJob () {
@@ -128,9 +152,9 @@ protected AMonitorJob createMonitorJob() {
128152 /**
129153 * A placeholder method which should be overridden if there are
130154 * custom objects to be initialized in the monitor.
131- * @param conf
155+ * @param args
132156 */
133- protected void initializeMoreStuff (Map <String , String > args , MonitorConfiguration conf ) {
157+ protected void initializeMoreStuff (Map <String , String > args ) {
134158 ;
135159 }
136160
@@ -154,8 +178,10 @@ public TaskOutput execute(Map<String, String> args, TaskExecutionContext taskExe
154178 }
155179
156180 protected void executeMonitor () {
157- if (configuration .isEnabled ()){
158- if (configuration .isScheduledModeEnabled ()){ //scheduled mode
181+ if (contextConfiguration .isEnabled ()){
182+ MonitorContext context = contextConfiguration .getContext ();
183+ AssertUtils .assertNotNull (context , "The context of the extension has not been initialised!!!! Please check your contextConfiguration" );
184+ if (context .isScheduledModeEnabled ()){ //scheduled mode
159185 logger .debug ("Task scheduler is enabled, printing the metrics from the cache" );
160186 monitorJob .printAllFromCache ();
161187 }
@@ -175,8 +201,8 @@ protected static String getImplementationVersion() {
175201
176202 public abstract String getMonitorName ();
177203
178- public MonitorConfiguration getConfiguration () {
179- return configuration ;
204+ public MonitorContextConfiguration getContextConfiguration () {
205+ return contextConfiguration ;
180206 }
181207
182208 protected abstract void doRun (TasksExecutionServiceProvider tasksExecutionServiceProvider );
@@ -186,4 +212,5 @@ public MonitorConfiguration getConfiguration(){
186212 protected void onComplete () {
187213 logger .info ("Finished processing all tasks in the job for {}" ,getMonitorName ());
188214 }
215+
189216}
0 commit comments