protected boolean doUnregister( String uri ) { // Read the store ... ValueFactory<String> stringFactory = store.getContext().getValueFactories().getStringFactory(); boolean result = false; try { Subgraph nsGraph = store.getSubgraphOfDepth(2).at(parentOfNamespaceNodes); // Iterate over the existing mappings, looking for one that uses the prefix and uri ... Set<Location> locationsToRemove = new HashSet<Location>(); for (Location nsLocation : nsGraph.getRoot().getChildren()) { Node ns = nsGraph.getNode(nsLocation); String actualUri = stringFactory.create(ns.getProperty(uriPropertyName).getFirstValue()); if (actualUri.equals(uri)) { locationsToRemove.add(ns.getLocation()); result = true; } } // Remove any other nodes that have the same URI ... Graph.Batch batch = store.batch(); for (Location namespaceToRemove : locationsToRemove) { batch.delete(namespaceToRemove).and(); } // Execute all these changes ... batch.execute(); } catch (PathNotFoundException e) { // Nothing stored yet, so do nothing ... } return result; }
createSubgraph(graph, initialPath, depth, numChildrenPerNode, numPropertiesPerNode, batch, sw, output, null); Subgraph source = graph.getSubgraphOfDepth(3).at("/node1"); Subgraph target = graph.getSubgraphOfDepth(3).at("/newUuids/node1"); assertThat(target, is(notNullValue())); assertThat(target.getNode(".").getChildren(), hasChildren(segment("node1"), segment("node2"), segment("node3")));
setPropertiesToReadOn(child122, validIdProperty2); Subgraph subgraph = graph.getSubgraphOfDepth(2).at(validPath); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getMaximumDepth(), is(2));
@Ignore @Test public void shouldReadSubgraphStartingAtRootAndWithMaximumDepthOfThree() { Subgraph subgraph = graph.getSubgraphOfDepth(3).at("/"); assertThat(subgraph, is(notNullValue())); // Verify that the root node is the same as getting it directly ... Node root = subgraph.getRoot(); assertSameNode(root, graph.getNodeAt("/")); // Verify the first-level children ... List<Location> children = graph.getChildren().of("/"); assertThat(children, is(notNullValue())); for (Location childLocation : children) { // Verify the child in the subgraph matches the same node obtained directly from the graph ... Node child = subgraph.getNode(childLocation); assertSameNode(child, graph.getNodeAt(childLocation)); // Now get the second-level children ... List<Location> grandChildren = graph.getChildren().of(childLocation); assertThat(grandChildren, is(notNullValue())); for (Location grandchildLocation : grandChildren) { // Verify the grandchild in the subgraph matches the same node obtained directly from the graph ... Node grandchild = subgraph.getNode(grandchildLocation); assertSameNode(grandchild, graph.getNodeAt(grandchildLocation)); // The subgraph should contain the children locations and properties for the grandchildren. // However, the subgraph should not a node for the children of the grandchildren ... for (Location greatGrandchild : grandchild.getChildren()) { assertThat(subgraph.getNode(greatGrandchild), is(nullValue())); } } } }
protected void assertInitialData() { // Now check that the data is all there ... Subgraph subgraph = graph.getSubgraphOfDepth(10).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("/a").getProperty("propB").isSingle(), is(true)); assertThat(subgraph.getNode("/a").getProperty("propB").getFirstValue(), is((Object)"valueB")); assertThat(subgraph.getNode("/a").getProperty("propC").isSingle(), is(true)); assertThat(subgraph.getNode("/a").getProperty("propC").getFirstValue(), is((Object)"valueC")); assertThat(subgraph.getNode("/a/b").getProperty("propB").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b").getProperty("propB").getFirstValue(), is((Object)"valueB")); assertThat(subgraph.getNode("/a/b").getProperty("propC").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b").getProperty("propC").getFirstValue(), is((Object)"valueC")); assertThat(subgraph.getNode("/a/b/c").getProperty("propB").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b/c").getProperty("propB").getFirstValue(), is((Object)"valueB")); assertThat(subgraph.getNode("/a/b/c").getProperty("propC").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b/c").getProperty("propC").getFirstValue(), is((Object)"valueC")); assertThat(subgraph.getNode("/a/b/d").getProperty("propB").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b/d").getProperty("propB").getFirstValue(), is((Object)"valueB")); assertThat(subgraph.getNode("/a/b/d").getProperty("propC").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b/d").getProperty("propC").getFirstValue(), is((Object)"valueC")); assertThat(subgraph.getNode("/a").getChildrenSegments().size(), is(1)); assertThat(subgraph.getNode("/a").getChildrenSegments().get(0), is(segment("b"))); assertThat(subgraph.getNode("/a/b").getChildrenSegments().size(), is(2)); assertThat(subgraph.getNode("/a/b").getChildrenSegments().get(0), is(segment("c"))); assertThat(subgraph.getNode("/a/b").getChildrenSegments().get(1), is(segment("d"))); assertThat(subgraph.getNode("/a/b/c").getChildrenSegments().isEmpty(), is(true)); assertThat(subgraph.getNode("/a/b/d").getChildrenSegments().isEmpty(), is(true)); }
protected synchronized void startService() { if (this.started.get() == false) { // ------------------------------------------------------------------------------------ // Read the configuration ... // ------------------------------------------------------------------------------------ // Read the configuration and repository source nodes (children under "/dna:sources") ... Graph graph = Graph.create(getConfigurationSourceName(), sources, context); Path pathToSourcesNode = context.getValueFactories() .getPathFactory() .create(pathToConfigurationRoot, ModeShapeLexicon.SOURCES); try { String workspaceName = getConfigurationWorkspaceName(); if (workspaceName != null) graph.useWorkspace(workspaceName); Subgraph sourcesGraph = graph.getSubgraphOfDepth(ReadBranchRequest.NO_MAXIMUM_DEPTH).at(pathToSourcesNode); // Iterate over each of the children, and create the RepositorySource ... for (Location location : sourcesGraph.getRoot().getChildren()) { sources.addSource(createRepositorySource(sourcesGraph, location, problems)); } } catch (PathNotFoundException e) { // No sources were found, and this is okay! } catch (Throwable err) { throw new ModeShapeConfigurationException(RepositoryI18n.errorStartingRepositoryService.text(), err); } this.started.set(true); } }
/** * Traverse the saved configuration graph treating it as an XML document and calling the corresponding SAX event on the * provided {@link ContentHandler}. Changes made without calling {@link #save()} will not be written to the stream. * * @param handler the content handler that will receive the SAX events * @throws SAXException if there is an error saving the configuration */ public void storeTo( ContentHandler handler ) throws SAXException { Subgraph allContent = configurationGraph().getSubgraphOfDepth(ReadBranchRequest.NO_MAXIMUM_DEPTH).at("/"); Set<NamespaceRegistry.Namespace> namespaces = this.context.getNamespaceRegistry().getNamespaces(); Stack<String> mappedNamespacePrefixes = new Stack<String>(); handler.startDocument(); for (NamespaceRegistry.Namespace namespace : namespaces) { handler.startPrefixMapping(namespace.getPrefix(), namespace.getNamespaceUri()); mappedNamespacePrefixes.push(namespace.getPrefix()); } exportNode(handler, allContent, allContent.getRoot()); while (!mappedNamespacePrefixes.isEmpty()) { handler.endPrefixMapping(mappedNamespacePrefixes.pop()); } handler.endDocument(); }
@Test public void shouldAddValuesToExistingProperty() { String initialPath = ""; int depth = 1; int numChildrenPerNode = 1; int numPropertiesPerNode = 1; Stopwatch sw = new Stopwatch(); boolean batch = true; createSubgraph(graph, initialPath, depth, numChildrenPerNode, numPropertiesPerNode, batch, sw, output, null); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); Subgraph subgraph = graph.getSubgraphOfDepth(2).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("node1"), hasProperty("property1", "The quick brown fox jumped over the moon. What? ")); graph.addValue("foo").andValue("bar").to("property1").on("node1"); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); subgraph = graph.getSubgraphOfDepth(2).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("node1"), hasProperty("property1", "The quick brown fox jumped over the moon. What? ", "foo", "bar")); }
@Test public void shouldAllowAddingSequencer() { // Update the configuration and save it ... configuration.sequencer("sequencerA") .usingClass(MockStreamSequencerA.class) .setDescription("Mock Sequencer A") .sequencingFrom("/foo/source") .andOutputtingTo("/foo/target") .sequencingFrom("/bar/source") .andOutputtingTo("/bar/target") .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sequencers").getChildren(), hasChild(segment("sequencerA"))); assertThat(subgraph.getNode("/mode:sequencers/sequencerA"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sequencers/sequencerA"), hasProperty(ModeShapeLexicon.DESCRIPTION, "Mock Sequencer A")); assertThat(subgraph.getNode("/mode:sequencers/sequencerA"), hasProperty(ModeShapeLexicon.CLASSNAME, MockStreamSequencerA.class.getName())); System.out.println(subgraph.getNode("/mode:sequencers/sequencerA").getProperty(ModeShapeLexicon.PATH_EXPRESSION)); assertThat(subgraph.getNode("/mode:sequencers/sequencerA"), hasProperty(ModeShapeLexicon.PATH_EXPRESSION, "/foo/source => /foo/target", "/bar/source => /bar/target")); }
/** * {@inheritDoc} * * @see org.modeshape.graph.connector.RepositoryContext#getConfiguration(int) */ @Override public Subgraph getConfiguration( int depth ) { Subgraph result = null; RepositorySource configSource = getConfigurationSource(); if (configSource != null) { Graph config = Graph.create(configSource, getExecutionContext()); String workspaceName = getConfigurationWorkspaceName(); if (workspaceName != null) { config.useWorkspace(workspaceName); } Path configPath = getPathToConfigurationRoot(); Path sourcePath = getExecutionContext().getValueFactories().getPathFactory().create(configPath, sourceName); result = config.getSubgraphOfDepth(depth).at(sourcePath); } return result; } };
@Test public void shouldSetPropertyWithPath() { Path path1 = createPath("/a"); Name prop1_name = createName("prop1"); String value_1 = "blue"; output.setProperty(path1, prop1_name, value_1); Path path2 = createPath("/a/b"); Name prop2_name = createName("prop2"); String value_2 = "red"; output.setProperty(path2, prop2_name, value_2); output.close(); Subgraph result = graph.getSubgraphOfDepth(10).at("/"); String v1 = (String)result.getNode(path1).getProperty(prop1_name).getFirstValue(); assertThat(v1, is(value_1)); String v2 = (String)result.getNode(path2).getProperty(prop2_name).getFirstValue(); assertThat(v2, is(value_2)); assertNull(result.getNode("/c")); } }
@Test public void shouldAllowAddingRepositorySourceByClassNameAndClasspathAndSettingProperties() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFrom("cp1", "cp2") .setProperty("retryLimit", 5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSNAME, InMemoryRepositorySource.class.getName())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSPATH, "cp1", "cp2")); }
@Test public void shouldAllowAddingMimeTypeDetector() { // Update the configuration and save it ... configuration.mimeTypeDetector("detector") .usingClass(ExtensionBasedMimeTypeDetector.class) .setDescription("default detector") .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:mimeTypeDetectors").getChildren(), hasChild(segment("detector"))); assertThat(subgraph.getNode("/mode:mimeTypeDetectors/detector"), is(notNullValue())); assertThat(subgraph.getNode("/mode:mimeTypeDetectors/detector"), hasProperty(ModeShapeLexicon.DESCRIPTION, "default detector")); assertThat(subgraph.getNode("/mode:mimeTypeDetectors/detector"), hasProperty(ModeShapeLexicon.CLASSNAME, ExtensionBasedMimeTypeDetector.class.getName())); }
@Test public void shouldAllowAddingRepositorySourceByClassNameAndSettingProperties() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setDescription("description") .setRetryLimit(5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.DESCRIPTION, "description")); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSNAME, InMemoryRepositorySource.class.getName())); }
@Test public void shouldSetReferenceWithString() { String path1 = "/a"; String prop1_name = "prop1"; String value_1 = "blue"; output.setReference(path1, prop1_name, value_1); String path2 = "/a/b"; String prop2_name = "prop2"; String value_2 = "red"; output.setReference(path2, prop2_name, value_2); output.close(); Subgraph result = graph.getSubgraphOfDepth(10).at("/"); String v1 = (String)result.getNode(path1).getProperty(prop1_name).getFirstValue(); assertThat(v1, is(value_1)); String v2 = (String)result.getNode(path2).getProperty(prop2_name).getFirstValue(); assertThat(v2, is(value_2)); assertNull(result.getNode("/c")); }
@Test public void shouldAllowAddingRepositorySourceByClassReferenceAndSettingProperties() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class) .setProperty("retryLimit", 5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSNAME, InMemoryRepositorySource.class.getName())); }
@Test public void shouldSaveSimpleConfigurationToContentHandler() throws Exception { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setProperty("retryLimit", 5) .and() .save(); GraphHandler handler = new GraphHandler(); configuration.storeTo(handler); Subgraph subgraph = handler.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, "5")); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSNAME, InMemoryRepositorySource.class.getName())); }
@Test public void shouldAllowSettingRetryLimitOnRepositorySourceUsingPrefixedPropertyName() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setProperty("mode:retryLimit", 5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); }
@Test public void shouldReturnSameStructureForRepeatedReadBranchRequestsOnLargeRepository() { // Initialize workspace one with some content ... String initialPath = ""; int depth = 4; int numChildrenPerNode = 10; int numPropertiesPerNode = 7; Stopwatch sw = new Stopwatch(); boolean batch = true; createSubgraph(graph, initialPath, depth, numChildrenPerNode, numPropertiesPerNode, batch, sw, output, null); // Verify that the content doesn't change Location root = graph.getCurrentWorkspace().getRoot(); Subgraph subgraph1 = graph.getSubgraphOfDepth(10).at(root); Subgraph subgraph2 = graph.getSubgraphOfDepth(10).at(root); assertEquivalentSubgraphs(subgraph1, subgraph2, true, true); }
@Test public void shouldSetPropertyWithString() { String path1 = "/a"; String prop1_name = "prop1"; String value_1 = "blue"; output.setProperty(path1, prop1_name, value_1); String path2 = "/a/b"; String prop2_name = "prop2"; String value_2 = "red"; output.setProperty(path2, prop2_name, value_2); output.close(); Subgraph result = graph.getSubgraphOfDepth(10).at("/"); String v1 = (String)result.getNode(path1).getProperty(prop1_name).getFirstValue(); assertThat(v1, is(value_1)); String v2 = (String)result.getNode(path2).getProperty(prop2_name).getFirstValue(); assertThat(v2, is(value_2)); assertNull(result.getNode("/c")); }