-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstall.cfc
More file actions
318 lines (281 loc) · 9.43 KB
/
Install.cfc
File metadata and controls
318 lines (281 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
component {
public void function fill (config) {
_configurator().configure(config);
}
/**
* called from Railo to validate entered values
*/
public void function validate (struct error, string path, struct config, numeric step) {
local.minVersion = "4.1.1.000";
if ( server.railo.version lt local.minVersion ) {
throw(
type:"coldfunctional.incompatable",
message="ColdFunctional is not compatible with Railo version #server.railo.version#, must be version #local.minVersion# or greater"
);
}
}
/**
* called from Railo to install the extension
*/
public string function install ( struct error, string path, struct config ){
if ( structKeyExists( arguments, 'config' ) ) {
_applyPreferencesToFLD( arguments.config );
}
local.filesToDelete = [];
_directories().each(
function(source,destination) {
filesToDelete.append(
_installDirectory( path & "/" & source, destination, _extractVersion(path) )
,true
);
}
);
local.successMessage = "Successfully installed #_name()# version #_extractVersion(path)#.";
local.successMessage = _addDeleteFilesMessage(local.successMessage,local.filesToDelete);
return local.successMessage;
}
/**
* called from Railo to update the already installed extension
*/
public string function update ( struct error, string path, struct config ) {
return install(argumentCollection=arguments);
}
/**
* called from Railo to uninstall the extension
*/
public string function uninstall ( string path, struct config ) {
local.filesToDelete = [];
_directories().each(
function(source,destination) {
filesToDelete.append(
_uninstallDirectory( path & "/" & source, destination, _extractVersion(path))
,true
);
}
);
return _addDeleteFilesMessage('#_Name()# version #_extractVersion(path)# was successfully uninstalled.',local.filesToDelete);
}
public boolean function hasExtension (string name) {
admin
action="getExtensions"
type="#_adminType()#"
password="#_password()#"
returnVariable="local.extensions";
for( local.row in local.extensions ) {
if ( local.row.name eq _name() ) return true;
}
return false;
}
/*
* privates
*/
private string function _addDeleteFilesMessage ( string successMessage, array filesToDelete ) {
local.result = arguments.successMessage;
if ( arguments.filesToDelete.size() gt 0 ) {
local.s = "s";
if ( arguments.filesToDelete.size() eq 1 ) {
local.s = "";
}
local.result &= '<br /> <span style="color:red;font-weight:bold;">Please stop your Servlet Container and delete the following file#local.s#: <br />';
for( local.fileName in arguments.filesToDelete ) {
local.result &= local.fileName & " <br /> "; /*"*/
}
local.result &= " </span> "; /*"*/
} else {
local.result &= " <br /> Please restart Railo.<br /> ";/*"*/
}
return local.result;
}
private string function _contextPath () {
switch (_adminType()) {
case "web":
return expandPath('{railo-web}');
break;
case "server":
return expandPath('{railo-server}');
break;
}
}
private string function _adminType() {
if ( structKeyExists( request,"adminType" ) ) {
return request.adminType;
} else {
return "web";
}
}
private string function _password() {
local.key = "password"&_adminType();
if ( structKeyExists(session,local.key) ) {
return session[local.key];
} else {
return "sysadmin";
}
}
/**
* given the source and destination, this function will install a directory to the destination
*/
private array function _installDirectory() {
arguments.callback = _installFile;
return _processDirectory(argumentCollection=arguments);
}
/**
* given the source and destination, this function will uninstall a directory to the destination
*/
private array function _uninstallDirectory(){
arguments.callback=_uninstallFile;
return _processDirectory(argumentCollection=arguments);
}
/**
* helper function to process a directory with a callback on each file
* @source absolute source folder
* @destination absolute destinatoin folder
* @version the version number to install or
* @callback a function to execute for each file
*/
private array function _processDirectory ( string source, string destination, string version, function callback ) {
local.files = directoryList( path:arguments.source, listinfo:"name" );
local.result = [];
local.files.each(
function (sourceFile) {
result.append(
callback( source & "/" & sourceFile, destination, version )
,true
);
}
);
return local.result;
}
/**
* given the source and directory destination, this function will install a file to the directory destination,
* the magical part is it will overwrite files with different version numbers
* @source absolute source file
* @destination absolute destination folder
* @version the version number
*/
private array function _installFile ( string source, string destination, string version ) {
// find destination files
local.fileName= getFileFromPath( arguments.source );
local.versionedFileName = _addVersionToFileName( local.fileName, arguments.version );
local.lockedFiles = [];
local.versionedFiles = _getVersionedFiles( arguments.destination, local.fileName );
for (local.file in local.versionedFiles) {
local.fullpath = arguments.destination & "/" & local.file;
if ( local.versionedFileName neq local.file ) {
if ( not _deleteFile( local.fullPath ) ) {
local.lockedFiles.append( local.fullPath );
}
}
}
local.destinedFile = arguments.destination & "/" & local.versionedFileName;
if ( not fileExists( local.destinedFile ) ) {
fileCopy( arguments.source, local.destinedFile );
}
return local.lockedFiles;
}
/**
* given the source and directory destination, this function will install a file to the directory destination,
* the magical part is it will overwrite files with different version numbers
* @source absolute source file
* @destination absolute destination folder
* @version the version
*/
private array function _uninstallFile (string source, string destination, string version) {
<!--- find destination files --->
local.fileName= getFileFromPath(arguments.source);
local.versionedFileName = _addVersionToFileName(local.fileName,arguments.version);
local.versionedFiles = _getVersionedFiles(arguments.destination,local.fileName);
local.lockedFiles = [];
for( local.file in local.versionedFiles ) {
local.fullpath = arguments.destination & "/" & local.file;
if (not _deleteFile( local.fullPath ) ) {
local.lockedFiles.append( local.fullPath );
}
}
return local.lockedFiles;
}
/**
* @directory directory to search for files
* @rootFilename the root file name, without the version
*/
private array function _getVersionedFiles ( string directory, string rootFileName ) {
local.fileNameWithoutExtension = REreplaceNoCase( arguments.rootFileName, "\.[^.]*$", "");
return directoryList( listInfo:"name", path:arguments.directory, filter:local.fileNamewithoutExtension & ".*" );/*"*/
}
private string function _name() {
return "ColdFunctional";
}
/**
* adds the version name to the file
* @fileName the file name to change
* @version the version number to add
*/
private string function _addVersionToFileName ( string fileName, string version ) {
local.arr = ListToArray( arguments.fileName, "." );
arrayInsertAt( local.arr, local.arr.size(), arguments.version );
return arrayToList( local.arr, "." );
}
/**
* returns the version of the config xml in the path provided.
* @path the path to the directory of the extension xml file
*/
private string function _extractVersion ( string path ) {
return xmlParse( arguments.path & "/config.xml" ).config.info.version.xmlText;
}
/**
* returns a map of directory sources and destinations that the extension will install to.
*/
private struct function _directories () {
return {
'content/jar': _contextPath() &"/lib",
'content/fld': _destinationFLDPath()
};
}
private string function _destinationFLDPath() {
return _contextPath() & "/library/fld";
}
/**
* attempts to delete a file, if it can't it marks it for deletion
* @file the file to delete
*/
private boolean function _deleteFile ( string file ){
try {
fileDelete( arguments.file );
}
catch (local.e) {
return false;
}
return true;
}
private cfc.InstallStepConfigurator function _configurator() {
return new cfc.InstallStepConfigurator(_functionSettings());
}
private struct function _functionSettings() {
return new cfc.FunctionLibraryDescriptorReader().readFunctionSettingsFromFLD(
_getMaximumVersionedFilePath(
_destinationFLDPath() & "/coldfunctional.fld"
),
_sourceFLD()
);
}
private string function _getMaximumVersionedFilePath ( string path ) {
local.directory = getDirectoryFromPath( arguments.path );
local.file = getFileFromPath( arguments.path );
local.files = _getVersionedFiles( local.directory, local.file );
local.files.sort("textnocase");
if ( local.files.size() eq 0 ) {
local.result = arguments.path
} else {
local.result = local.directory & "/" & local.files[ local.files.size() ];
}
return local.result;
}
private void function _applyPreferencesToFLD ( struct config ) {
new cfc.FunctionLibraryDescriptorApplier().apply(
_sourceFLD(),
arguments.config
);
}
private string function _sourceFLD() {
return getDirectoryFromPath ( getCurrentTemplatePath() ) & "/content/fld/coldfunctional.fld";
}
}