@@ -45,7 +45,12 @@ public AbstractAppDeployer(ManageClient manageClient, AdminManager adminManager)
45
45
*/
46
46
protected abstract List <Command > getCommands ();
47
47
48
- public void deploy (AppConfig appConfig ) {
48
+ /**
49
+ * Calls execute on each of the configured commands.
50
+ *
51
+ * @param appConfig
52
+ */
53
+ public void deploy (AppConfig appConfig ) {
49
54
logger .info (format ("Deploying app %s with config dir of: %s\n " , appConfig .getName (), appConfig .getConfigDir ()
50
55
.getBaseDir ().getAbsolutePath ()));
51
56
@@ -54,35 +59,67 @@ public void deploy(AppConfig appConfig) {
54
59
55
60
CommandContext context = new CommandContext (appConfig , manageClient , adminManager );
56
61
57
- String [] filenamesToIgnore = appConfig .getResourceFilenamesToIgnore ();
58
- Pattern excludePattern = appConfig .getResourceFilenamesExcludePattern ();
59
- Pattern includePattern = appConfig .getResourceFilenamesIncludePattern ();
60
-
61
62
for (Command command : commands ) {
62
63
String name = command .getClass ().getName ();
63
64
logger .info (format ("Executing command [%s] with sort order [%d]" , name , command .getExecuteSortOrder ()));
64
-
65
- if (command instanceof AbstractCommand ) {
66
- AbstractCommand abstractCommand = (AbstractCommand )command ;
67
- if (filenamesToIgnore != null ) {
68
- abstractCommand .setFilenamesToIgnore (filenamesToIgnore );
69
- }
70
- if (excludePattern != null ) {
71
- abstractCommand .setResourceFilenamesExcludePattern (excludePattern );
72
- }
73
- if (includePattern != null ) {
74
- abstractCommand .setResourceFilenamesIncludePattern (includePattern );
75
- }
76
- }
77
-
78
- command .execute (context );
65
+ prepareCommand (command , context );
66
+ executeCommand (command , context );
79
67
logger .info (format ("Finished executing command [%s]\n " , name ));
80
68
}
81
69
82
70
logger .info (format ("Deployed app %s" , appConfig .getName ()));
83
71
}
84
72
85
- public void undeploy (AppConfig appConfig ) {
73
+ /**
74
+ * Prepare the given command before either execute or undo is called on it.
75
+ *
76
+ * @param command
77
+ * @param context
78
+ */
79
+ protected void prepareCommand (Command command , CommandContext context ) {
80
+ if (command instanceof AbstractCommand ) {
81
+ AppConfig appConfig = context .getAppConfig ();
82
+ String [] filenamesToIgnore = appConfig .getResourceFilenamesToIgnore ();
83
+ Pattern excludePattern = appConfig .getResourceFilenamesExcludePattern ();
84
+ Pattern includePattern = appConfig .getResourceFilenamesIncludePattern ();
85
+
86
+ AbstractCommand abstractCommand = (AbstractCommand )command ;
87
+ if (filenamesToIgnore != null ) {
88
+ abstractCommand .setFilenamesToIgnore (filenamesToIgnore );
89
+ }
90
+ if (excludePattern != null ) {
91
+ abstractCommand .setResourceFilenamesExcludePattern (excludePattern );
92
+ }
93
+ if (includePattern != null ) {
94
+ abstractCommand .setResourceFilenamesIncludePattern (includePattern );
95
+ }
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Executes the command, catching an exception if desired.
101
+ *
102
+ * @param command
103
+ * @param context
104
+ */
105
+ protected void executeCommand (Command command , CommandContext context ) {
106
+ try {
107
+ command .execute (context );
108
+ } catch (RuntimeException ex ) {
109
+ if (context .getAppConfig ().isCatchDeployExceptions ()) {
110
+ logger .error (format ("Command [%s] threw exception that was caught; cause: %s" , command .getClass ().getName (), ex .getMessage ()), ex );
111
+ } else {
112
+ throw ex ;
113
+ }
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Calls undo on each of the configured commands that implements the UndoableCommand interface.
119
+ *
120
+ * @param appConfig
121
+ */
122
+ public void undeploy (AppConfig appConfig ) {
86
123
logger .info (format ("Undeploying app %s with config dir: %s\n " , appConfig .getName (), appConfig .getConfigDir ()
87
124
.getBaseDir ().getAbsolutePath ()));
88
125
@@ -96,16 +133,36 @@ public void undeploy(AppConfig appConfig) {
96
133
}
97
134
98
135
Collections .sort (undoableCommands , new UndoComparator ());
136
+ CommandContext context = new CommandContext (appConfig , manageClient , adminManager );
99
137
100
138
for (UndoableCommand command : undoableCommands ) {
101
139
String name = command .getClass ().getName ();
102
140
logger .info (format ("Undoing command [%s] with sort order [%d]" , name , command .getUndoSortOrder ()));
103
- command .undo (new CommandContext (appConfig , manageClient , adminManager ));
141
+ prepareCommand (command , context );
142
+ undoCommand (command , context );
104
143
logger .info (format ("Finished undoing command [%s]\n " , name ));
105
144
}
106
145
107
146
logger .info (format ("Undeployed app %s" , appConfig .getName ()));
108
147
}
148
+
149
+ /**
150
+ * Calls undo on the command, catching an exception if desired.
151
+ *
152
+ * @param command
153
+ * @param context
154
+ */
155
+ protected void undoCommand (UndoableCommand command , CommandContext context ) {
156
+ try {
157
+ command .undo (context );
158
+ } catch (RuntimeException ex ) {
159
+ if (context .getAppConfig ().isCatchUndeployExceptions ()) {
160
+ logger .error (format ("Command [%s] threw exception that was caught; cause: %s" , command .getClass ().getName (), ex .getMessage ()), ex );
161
+ } else {
162
+ throw ex ;
163
+ }
164
+ }
165
+ }
109
166
}
110
167
111
168
class ExecuteComparator implements Comparator <Command > {
0 commit comments