|
65 | 65 | import com.marklogic.mgmt.databases.DatabaseManager; |
66 | 66 | import com.marklogic.rest.util.Fragment; |
67 | 67 | import com.marklogic.rest.util.ResourcesFragment; |
| 68 | +import org.springframework.core.io.Resource; |
| 69 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 70 | +import org.springframework.core.io.support.ResourcePatternResolver; |
68 | 71 | import org.springframework.web.client.ResourceAccessException; |
69 | 72 |
|
| 73 | +import java.io.IOException; |
70 | 74 | import java.nio.file.Paths; |
71 | 75 | import java.util.ArrayList; |
72 | 76 | import java.util.HashMap; |
@@ -291,21 +295,38 @@ public void installUserModules() { |
291 | 295 | * Removes user's modules from the modules db |
292 | 296 | */ |
293 | 297 | public void clearUserModules() { |
| 298 | + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(DataHub.class.getClassLoader()); |
294 | 299 | try { |
295 | | - runInDatabase( |
| 300 | + ArrayList<String> options = new ArrayList<>(); |
| 301 | + for (Resource r : resolver.getResources("classpath*:/ml-modules/options/*.xml")) { |
| 302 | + options.add(r.getFilename().replace(".xml", "")); |
| 303 | + } |
| 304 | + |
| 305 | + ArrayList<String> services = new ArrayList<>(); |
| 306 | + for (Resource r : resolver.getResources("classpath*:/ml-modules/services/*.xqy")) { |
| 307 | + services.add(r.getFilename().replaceAll("\\.(xqy|sjs)", "")); |
| 308 | + } |
| 309 | + |
| 310 | + |
| 311 | + ArrayList<String> transforms = new ArrayList<>(); |
| 312 | + for (Resource r : resolver.getResources("classpath*:/ml-modules/transforms/*")) { |
| 313 | + transforms.add(r.getFilename().replaceAll("\\.(xqy|sjs)", "")); |
| 314 | + } |
| 315 | + |
| 316 | + String query = |
296 | 317 | "cts:uris((),(),cts:not-query(cts:collection-query('hub-core-module')))[\n" + |
297 | 318 | " fn:not(\n" + |
298 | | - " fn:ends-with(., \"options/spring-batch.xml\") or\n" + |
299 | | - " fn:ends-with(., \"options/traces.xml\") or\n" + |
300 | | - " fn:matches(., \"/marklogic.rest.resource/(collector|debug|entity|flow|tracing|validate|writer)/assets/(metadata\\.xml|resource\\.(xqy|sjs))\") or\n" + |
301 | | - " fn:matches(., \"/marklogic.rest.transform/(get-content|run-flow|trace-json|trace-search)/assets/(metadata\\.xml|transform\\.(xqy|sjs))\")\n" + |
| 319 | + " fn:matches(., \"^.+options/(" + String.join("|", options) + ").xml$\") or\n" + |
| 320 | + " fn:matches(., \"/marklogic.rest.resource/(" + String.join("|", services) + ")/assets/(metadata\\.xml|resource\\.(xqy|sjs))\") or\n" + |
| 321 | + " fn:matches(., \"/marklogic.rest.transform/(" + String.join("|", transforms) + ")/assets/(metadata\\.xml|transform\\.(xqy|sjs))\")\n" + |
302 | 322 | " )\n" + |
303 | | - "] ! xdmp:document-delete(.)\n", |
304 | | - hubConfig.modulesDbName |
305 | | - ); |
| 323 | + "] ! xdmp:document-delete(.)\n"; |
| 324 | + runInDatabase(query, hubConfig.modulesDbName); |
306 | 325 | } |
307 | 326 | catch(FailedRequestException e) { |
308 | 327 | logger.error("Failed to clear user modules"); |
| 328 | + } catch (IOException e) { |
| 329 | + e.printStackTrace(); |
309 | 330 | } |
310 | 331 | } |
311 | 332 |
|
|
0 commit comments