View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.connector.federation;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.UUID;
33  import net.jcip.annotations.Immutable;
34  import org.modeshape.graph.ExecutionContext;
35  import org.modeshape.graph.Location;
36  import org.modeshape.graph.property.Name;
37  import org.modeshape.graph.property.Path;
38  import org.modeshape.graph.property.PathFactory;
39  import org.modeshape.graph.property.Property;
40  
41  /**
42   * A Projector for federated repository configurations that are an offset, direct one-for-one mirror against a single source
43   * repository that is projected below the federated root. In other words, the federated repository has a single projection with a
44   * single "/something/below/root => /" rule.
45   */
46  @Immutable
47  abstract class ProjectorWithPlaceholders implements Projector {
48  
49      private Map<Path, PlaceholderNode> placeholderNodesByPath;
50      private Map<UUID, PlaceholderNode> placeholderNodesByUuid;
51  
52      ProjectorWithPlaceholders( ExecutionContext context,
53                                 List<Projection> projections ) {
54          placeholderNodesByPath = new HashMap<Path, PlaceholderNode>();
55          placeholderNodesByUuid = new HashMap<UUID, PlaceholderNode>();
56          // Use the execution context of the source to load the projected nodes ...
57          Collection<PlaceholderNode> placeholders = new ArrayList<PlaceholderNode>();
58          loadPlaceholderNodes(context, projections, placeholders);
59          for (PlaceholderNode placeholder : placeholders) {
60              placeholderNodesByPath.put(placeholder.location().getPath(), placeholder);
61              placeholderNodesByUuid.put(placeholder.location().getUuid(), placeholder);
62          }
63      }
64  
65      /**
66       * Determine whether the specified location is a placeholder node.
67       * 
68       * @param location the location of the node; may not be null
69       * @return the placeholder node, or null if the supplied location does not designate a placeholder node
70       */
71      public PlaceholderNode isPlaceholder( Location location ) {
72          Path path = location.getPath();
73          if (path != null) {
74              return placeholderNodesByPath.get(path);
75          }
76          UUID uuid = location.getUuid();
77          if (uuid != null) {
78              return placeholderNodesByUuid.get(uuid);
79          }
80          return null;
81      }
82  
83      /**
84       * Load the placeholder nodes for this repository. This method does not modify any state of this instance, but instead
85       * populates the supplied map.
86       * <p>
87       * A placeholder is created for each node above the projections' {@link Projection#getTopLevelPathsInRepository(PathFactory)
88       * top-level paths} in the federated repository. Thus, the projected node immediately above a top-level path will contain the
89       * top-level path as a child.
90       * </p>
91       * 
92       * @param context the context in which the placeholder nodes should be materialized; may not be null
93       * @param projections the projections; may not be null
94       * @param placeholderNodes the collection into which should be placed the materialized placeholders
95       */
96      protected static void loadPlaceholderNodes( ExecutionContext context,
97                                                  Iterable<Projection> projections,
98                                                  Collection<PlaceholderNode> placeholderNodes ) {
99          final PathFactory pathFactory = context.getValueFactories().getPathFactory();
100         Map<Path, ProxyNode> proxyNodesByPath = new HashMap<Path, ProxyNode>();
101         Map<Path, PlaceholderNode> placeholdersByPath = new HashMap<Path, PlaceholderNode>();
102         for (Projection projection : projections) {
103             // Create for all of the top-level nodes ...
104             for (Path path : projection.getTopLevelPathsInRepository(pathFactory)) {
105                 if (path.isRoot()) continue;
106                 // Create ProxyNodes for each corresponding path-in-source ...
107                 Location inRepository = Location.create(path);
108                 ProxyNode previous = null;
109                 for (Path pathInSource : projection.getPathsInSource(path, pathFactory)) {
110                     Location inSource = Location.create(pathInSource);
111                     ProxyNode proxy = new ProxyNode(projection, inSource, inRepository);
112                     if (previous == null) {
113                         previous = proxy;
114                         proxyNodesByPath.put(path, proxy);
115                     } else {
116                         previous.add(proxy);
117                     }
118                 }
119                 // Walk up the in-repository path to create the placeholder nodes ...
120                 ProjectedNode child = previous;
121                 while (!path.isRoot()) {
122                     // Create a projected node for the parent of this path ...
123                     Path parent = path.getParent();
124                     PlaceholderNode parentPlaceholder = placeholdersByPath.get(parent);
125                     if (parentPlaceholder == null) {
126                         // Need to create the placeholder ...
127                         Map<Name, Property> properties = Collections.emptyMap();
128                         Location location = Location.create(parent, UUID.randomUUID());
129                         parentPlaceholder = new PlaceholderNode(location, properties, new ArrayList<ProjectedNode>());
130                         placeholdersByPath.put(parent, parentPlaceholder);
131                         placeholderNodes.add(parentPlaceholder);
132                         parentPlaceholder.children().add(child);
133                     } else {
134                         // The placeholder already exists, so make sure we're not adding it to this parent twice ...
135                         List<ProjectedNode> children = parentPlaceholder.children();
136                         if (!children.contains(child)) children.add(child);
137                     }
138                     child = parentPlaceholder;
139                     path = parent;
140                 }
141             }
142         }
143     }
144 }