2323 */
2424public abstract class AbstractAppDeployer extends LoggingObject implements AppDeployer {
2525
26- private ManageClient manageClient ;
27- private AdminManager adminManager ;
28-
29- /**
30- * Can use this constructor when the default config used by ManageClient and AdminManager will work.
31- */
32- public AbstractAppDeployer () {
33- this (new ManageClient (), new AdminManager ());
34- }
35-
36- public AbstractAppDeployer (ManageClient manageClient , AdminManager adminManager ) {
37- super ();
38- this .manageClient = manageClient ;
39- this .adminManager = adminManager ;
40- }
41-
42- /**
43- * The subclass just needs to define the list of commands to be invoked.
44- *
45- * @return
46- */
47- protected abstract List <Command > getCommands ();
26+ private ManageClient manageClient ;
27+ private AdminManager adminManager ;
28+ private List <DeployerListener > deployerListeners ;
29+
30+ /**
31+ * Can use this constructor when the default config used by ManageClient and AdminManager will work.
32+ */
33+ public AbstractAppDeployer () {
34+ this (new ManageClient (), new AdminManager ());
35+ }
36+
37+ public AbstractAppDeployer (ManageClient manageClient , AdminManager adminManager ) {
38+ super ();
39+ this .manageClient = manageClient ;
40+ this .adminManager = adminManager ;
41+
42+ this .deployerListeners = new ArrayList <>();
43+ this .deployerListeners .add (new AddHostNameTokensDeployerListener ());
44+ }
45+
46+ /**
47+ * The subclass just needs to define the list of commands to be invoked.
48+ *
49+ * @return
50+ */
51+ protected abstract List <Command > getCommands ();
4852
4953 /**
5054 * Calls execute on each of the configured commands.
@@ -56,23 +60,33 @@ public void deploy(AppConfig appConfig) {
5660 for (ConfigDir configDir : appConfig .getConfigDirs ()) {
5761 configPaths .add (configDir .getBaseDir ().getAbsolutePath ());
5862 }
59- logger .info (format ("Deploying app %s with config dirs: %s\n " , appConfig .getName (), configPaths ));
63+ logger .info (format ("Deploying app %s with config dirs: %s\n " , appConfig .getName (), configPaths ));
6064
61- List <Command > commands = getCommands ();
62- Collections .sort (commands , new ExecuteComparator ());
65+ List <Command > commands = getCommands ();
66+ Collections .sort (commands , new ExecuteComparator ());
6367
64- CommandContext context = new CommandContext (appConfig , manageClient , adminManager );
68+ CommandContext context = new CommandContext (appConfig , manageClient , adminManager );
6569
66- for (Command command : commands ) {
67- String name = command .getClass ().getName ();
68- logger .info (format ("Executing command [%s] with sort order [%d]" , name , command .getExecuteSortOrder ()));
69- prepareCommand (command , context );
70- executeCommand (command , context );
71- logger .info (format ("Finished executing command [%s]\n " , name ));
72- }
70+ invokeDeployerListenersBeforeCommandsAreExecuted (new DeploymentContext (context , appConfig , commands ));
7371
74- logger .info (format ("Deployed app %s" , appConfig .getName ()));
75- }
72+ for (Command command : commands ) {
73+ String name = command .getClass ().getName ();
74+ logger .info (format ("Executing command [%s] with sort order [%d]" , name , command .getExecuteSortOrder ()));
75+ prepareCommand (command , context );
76+ executeCommand (command , context );
77+ logger .info (format ("Finished executing command [%s]\n " , name ));
78+ }
79+
80+ logger .info (format ("Deployed app %s" , appConfig .getName ()));
81+ }
82+
83+ protected void invokeDeployerListenersBeforeCommandsAreExecuted (DeploymentContext context ) {
84+ if (deployerListeners != null ) {
85+ for (DeployerListener listener : deployerListeners ) {
86+ listener .beforeCommandsExecuted (context );
87+ }
88+ }
89+ }
7690
7791 /**
7892 * Prepare the given command before either execute or undo is called on it.
@@ -81,24 +95,24 @@ public void deploy(AppConfig appConfig) {
8195 * @param context
8296 */
8397 protected void prepareCommand (Command command , CommandContext context ) {
84- if (command instanceof AbstractCommand ) {
85- AppConfig appConfig = context .getAppConfig ();
86- String [] filenamesToIgnore = appConfig .getResourceFilenamesToIgnore ();
87- Pattern excludePattern = appConfig .getResourceFilenamesExcludePattern ();
88- Pattern includePattern = appConfig .getResourceFilenamesIncludePattern ();
89-
90- AbstractCommand abstractCommand = (AbstractCommand )command ;
91- if (filenamesToIgnore != null ) {
92- abstractCommand .setFilenamesToIgnore (filenamesToIgnore );
93- }
94- if (excludePattern != null ) {
95- abstractCommand .setResourceFilenamesExcludePattern (excludePattern );
96- }
97- if (includePattern != null ) {
98- abstractCommand .setResourceFilenamesIncludePattern (includePattern );
99- }
100- }
101- }
98+ if (command instanceof AbstractCommand ) {
99+ AppConfig appConfig = context .getAppConfig ();
100+ String [] filenamesToIgnore = appConfig .getResourceFilenamesToIgnore ();
101+ Pattern excludePattern = appConfig .getResourceFilenamesExcludePattern ();
102+ Pattern includePattern = appConfig .getResourceFilenamesIncludePattern ();
103+
104+ AbstractCommand abstractCommand = (AbstractCommand ) command ;
105+ if (filenamesToIgnore != null ) {
106+ abstractCommand .setFilenamesToIgnore (filenamesToIgnore );
107+ }
108+ if (excludePattern != null ) {
109+ abstractCommand .setResourceFilenamesExcludePattern (excludePattern );
110+ }
111+ if (includePattern != null ) {
112+ abstractCommand .setResourceFilenamesIncludePattern (includePattern );
113+ }
114+ }
115+ }
102116
103117 /**
104118 * Executes the command, catching an exception if desired.
@@ -107,16 +121,16 @@ protected void prepareCommand(Command command, CommandContext context) {
107121 * @param context
108122 */
109123 protected void executeCommand (Command command , CommandContext context ) {
110- try {
111- command .execute (context );
112- } catch (RuntimeException ex ) {
113- if (context .getAppConfig ().isCatchDeployExceptions ()) {
114- logger .error (format ("Command [%s] threw exception that was caught; cause: %s" , command .getClass ().getName (), ex .getMessage ()), ex );
115- } else {
116- throw ex ;
117- }
118- }
119- }
124+ try {
125+ command .execute (context );
126+ } catch (RuntimeException ex ) {
127+ if (context .getAppConfig ().isCatchDeployExceptions ()) {
128+ logger .error (format ("Command [%s] threw exception that was caught; cause: %s" , command .getClass ().getName (), ex .getMessage ()), ex );
129+ } else {
130+ throw ex ;
131+ }
132+ }
133+ }
120134
121135 /**
122136 * Calls undo on each of the configured commands that implements the UndoableCommand interface.
@@ -128,30 +142,30 @@ public void undeploy(AppConfig appConfig) {
128142 for (ConfigDir configDir : appConfig .getConfigDirs ()) {
129143 configPaths .add (configDir .getBaseDir ().getAbsolutePath ());
130144 }
131- logger .info (format ("Undeploying app %s with config dirs: %s\n " , appConfig .getName (), configPaths ));
145+ logger .info (format ("Undeploying app %s with config dirs: %s\n " , appConfig .getName (), configPaths ));
132146
133- List <Command > commands = getCommands ();
147+ List <Command > commands = getCommands ();
134148
135- List <UndoableCommand > undoableCommands = new ArrayList <UndoableCommand >();
136- for (Command command : commands ) {
137- if (command instanceof UndoableCommand ) {
138- undoableCommands .add ((UndoableCommand ) command );
139- }
140- }
149+ List <UndoableCommand > undoableCommands = new ArrayList <UndoableCommand >();
150+ for (Command command : commands ) {
151+ if (command instanceof UndoableCommand ) {
152+ undoableCommands .add ((UndoableCommand ) command );
153+ }
154+ }
141155
142- Collections .sort (undoableCommands , new UndoComparator ());
143- CommandContext context = new CommandContext (appConfig , manageClient , adminManager );
156+ Collections .sort (undoableCommands , new UndoComparator ());
157+ CommandContext context = new CommandContext (appConfig , manageClient , adminManager );
144158
145- for (UndoableCommand command : undoableCommands ) {
146- String name = command .getClass ().getName ();
147- logger .info (format ("Undoing command [%s] with sort order [%d]" , name , command .getUndoSortOrder ()));
148- prepareCommand (command , context );
149- undoCommand (command , context );
150- logger .info (format ("Finished undoing command [%s]\n " , name ));
151- }
159+ for (UndoableCommand command : undoableCommands ) {
160+ String name = command .getClass ().getName ();
161+ logger .info (format ("Undoing command [%s] with sort order [%d]" , name , command .getUndoSortOrder ()));
162+ prepareCommand (command , context );
163+ undoCommand (command , context );
164+ logger .info (format ("Finished undoing command [%s]\n " , name ));
165+ }
152166
153- logger .info (format ("Undeployed app %s" , appConfig .getName ()));
154- }
167+ logger .info (format ("Undeployed app %s" , appConfig .getName ()));
168+ }
155169
156170 /**
157171 * Calls undo on the command, catching an exception if desired.
@@ -170,18 +184,26 @@ protected void undoCommand(UndoableCommand command, CommandContext context) {
170184 }
171185 }
172186 }
187+
188+ public List <DeployerListener > getDeployerListeners () {
189+ return deployerListeners ;
190+ }
191+
192+ public void setDeployerListeners (List <DeployerListener > deployerListeners ) {
193+ this .deployerListeners = deployerListeners ;
194+ }
173195}
174196
175197class ExecuteComparator implements Comparator <Command > {
176- @ Override
177- public int compare (Command o1 , Command o2 ) {
178- return o1 .getExecuteSortOrder ().compareTo (o2 .getExecuteSortOrder ());
179- }
198+ @ Override
199+ public int compare (Command o1 , Command o2 ) {
200+ return o1 .getExecuteSortOrder ().compareTo (o2 .getExecuteSortOrder ());
201+ }
180202}
181203
182204class UndoComparator implements Comparator <UndoableCommand > {
183- @ Override
184- public int compare (UndoableCommand o1 , UndoableCommand o2 ) {
185- return o1 .getUndoSortOrder ().compareTo (o2 .getUndoSortOrder ());
186- }
205+ @ Override
206+ public int compare (UndoableCommand o1 , UndoableCommand o2 ) {
207+ return o1 .getUndoSortOrder ().compareTo (o2 .getUndoSortOrder ());
208+ }
187209}
0 commit comments