Re: [PR] SLING-12265 - Improve node type registration in JCR_OAK [sling-org-apache-sling-testing-sling-mock]
stefanseifert merged PR #37: URL: https://github.com/apache/sling-org-apache-sling-testing-sling-mock/pull/37 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
Re: [PR] SLING-12265 - Improve node type registration in JCR_OAK [sling-org-apache-sling-testing-sling-mock]
sonarcloud[bot] commented on PR #37: URL: https://github.com/apache/sling-org-apache-sling-testing-sling-mock/pull/37#issuecomment-2045784232 ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-testing-sling-mock=37) **Quality Gate passed** Issues ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-testing-sling-mock=37=false=true) ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-testing-sling-mock=37=new_accepted_issues=list) Measures ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-testing-sling-mock=37=false=true) ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [95.2% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-testing-sling-mock=37=new_coverage=list) ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-testing-sling-mock=37=new_duplicated_lines_density=list) [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-testing-sling-mock=37) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
Re: [PR] SLING-12265 - Improve node type registration in JCR_OAK [sling-org-apache-sling-testing-sling-mock]
stefanseifert commented on code in PR #37: URL: https://github.com/apache/sling-org-apache-sling-testing-sling-mock/pull/37#discussion_r1557767226 ## core/src/main/java/org/apache/sling/testing/mock/sling/NodeTypeDefinitionScanner.java: ## @@ -161,50 +160,75 @@ private void registerNodeTypes(Session session, List nodeTypeResources) NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager(); NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry(); ValueFactory valueFactory = session.getValueFactory(); + DefinitionBuilderFactory factory = +new TemplateBuilderFactory(nodeTypeManager, valueFactory, namespaceRegistry); - // try registering node types multiple times because the exact order is not known - int iteration = 0; - List remainingNodeTypeResources = new ArrayList(nodeTypeResources); - while (!remainingNodeTypeResources.isEmpty()) { - registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, false); - iteration++; - if (iteration >= MAX_ITERATIONS) { - break; - } + Map nodeTypes = new HashMap<>(); + for (String resource : nodeTypeResources) { + nodeTypes.putAll(parseNodeTypesFromResource(resource, classLoader, factory)); } - if (!remainingNodeTypeResources.isEmpty()) { - registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, true); + for (NodeTypeTemplate template : nodeTypes.values()) { + ensureNtBase(template, nodeTypes, nodeTypeManager); } + + nodeTypeManager.registerNodeTypes(nodeTypes.values().toArray(new NodeTypeTemplate[0]), true); } /** - * Register node types found in classpath in JCR repository, and remove those that succeeded to register from the list. - * @param nodeTypeResources List of nodetype classpath resources - * @param classLoader - * @param nodeTypeManager - * @param namespaceRegistry - * @param valueFactory - * @param logError if true, and error is logged if node type registration failed. Otherwise it is ignored. + * Parses a CND file present on the classpath and returns the node types found within. + * @param resource The resource name. + * @param classLoader The classloader to load resources with. + * @param factory The factory to build node type definitions with. + * @return A mapping from node type names to node type definitions. */ -private void registerNodeTypesAndRemoveSucceeds(List nodeTypeResources, ClassLoader classLoader, -NodeTypeManager nodeTypeManager, NamespaceRegistry namespaceRegistry, ValueFactory valueFactory, -boolean logError) { -Iterator nodeTypeResourcesIterator = nodeTypeResources.iterator(); -while (nodeTypeResourcesIterator.hasNext()) { -String nodeTypeResource = nodeTypeResourcesIterator.next(); -try (InputStream is = classLoader.getResourceAsStream(nodeTypeResource)) { -if (is == null) { -continue; -} -Reader reader = new InputStreamReader(is); -CndImporter.registerNodeTypes(reader, nodeTypeResource, nodeTypeManager, namespaceRegistry, valueFactory, true); -nodeTypeResourcesIterator.remove(); +private Map parseNodeTypesFromResource(String resource, ClassLoader classLoader, +DefinitionBuilderFactory factory) { +try (InputStream is = classLoader.getResourceAsStream(resource)) { +if (is == null) { +return Map.of(); +} +CompactNodeTypeDefReader cndReader = +new CompactNodeTypeDefReader<>(new InputStreamReader(is), resource, factory); +Map result = new HashMap<>(); +for (NodeTypeTemplate template : cndReader.getNodeTypeDefinitions()) { +result.put(template.getName(), template); } -catch (Throwable ex) { -if (logError) { -log.warn("Unable to register node type: " + nodeTypeResource, ex); +return result; +} catch (Throwable ex) { +log.warn("Failed to parse CND resource: " + resource, ex); +return Map.of(); +} +} + +/** + * Add an implied nt:base supertype explicitly to the node type definition. + * @param nodeTypeTemplate The definition to update. + * @param templates The mapping of all definitions that are going to be added. + * @param nodeTypeManager Node type manager of the target repository, for looking up existing types. + * @throws RepositoryException If any issues happen while querying type information from the repository. + */ +private static void
Re: [PR] SLING-12265 - Improve node type registration in JCR_OAK [sling-org-apache-sling-testing-sling-mock]
sonarcloud[bot] commented on PR #37: URL: https://github.com/apache/sling-org-apache-sling-testing-sling-mock/pull/37#issuecomment-1990863390 ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-testing-sling-mock=37) **Quality Gate passed** Issues ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-testing-sling-mock=37=false=true) ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-testing-sling-mock=37=new_accepted_issues=list) Measures ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-testing-sling-mock=37=false=true) ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [94.9% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-testing-sling-mock=37=new_coverage=list) ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-testing-sling-mock=37=new_duplicated_lines_density=list) [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-testing-sling-mock=37) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org