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.List;
27  import net.jcip.annotations.Immutable;
28  import org.modeshape.graph.ExecutionContext;
29  import org.modeshape.graph.Location;
30  import org.modeshape.graph.property.Path;
31  import org.modeshape.graph.property.PathFactory;
32  
33  /**
34   * A Projector for federated repository configurations that are an offset, direct one-for-one mirror against a single source
35   * repository with a subgraph projected below the federated root. In other words, the federated repository has a single projection
36   * with a single "/something/below/root => /source/node" rule. Note that this project works even when the path in source is the
37   * root node, such as "/something/below/root => /".
38   */
39  @Immutable
40  final class OffsetMirrorProjector extends ProjectorWithPlaceholders {
41  
42      /**
43       * Attempt to create an instance of the {@link OffsetMirrorProjector} with the supplied projections using the supplied
44       * context.
45       * 
46       * @param context the context; may not be null
47       * @param projections the projections in the federated repository; may not be null
48       * @return the offset mirror projector, or null if the projections didn't match the criteria for such a projector
49       */
50      static OffsetMirrorProjector with( ExecutionContext context,
51                                         List<Projection> projections ) {
52          assert projections != null;
53          assert context != null;
54          // There must be a single projection ...
55          if (projections.size() != 1) return null;
56          Projection projection = projections.get(0);
57          assert projection != null;
58          // That projection may only have one rule ...
59          if (projection.getRules().size() != 1) return null;
60          PathFactory pathFactory = context.getValueFactories().getPathFactory();
61  
62          // The # of paths in repository must be 1 ...
63          List<Path> topLevelPaths = projection.getRules().get(0).getTopLevelPathsInRepository(pathFactory);
64          if (topLevelPaths.size() != 1) return null;
65          Path topLevelPath = topLevelPaths.get(0);
66          assert topLevelPath != null;
67          // The federated path may not be the root ...
68          if (topLevelPath.isRoot()) return null;
69  
70          // The corresponding source path may or may not be the root ...
71          Path sourcePath = projection.getRules().get(0).getPathInSource(topLevelPath, pathFactory);
72  
73          return new OffsetMirrorProjector(context, projections, topLevelPath, sourcePath);
74      }
75  
76      private final Projection projection;
77      private final Path offset;
78      private final int offsetSize;
79      private final Path sourcePath;
80  
81      private OffsetMirrorProjector( ExecutionContext context,
82                                     List<Projection> projections,
83                                     Path offset,
84                                     Path sourcePath ) {
85          super(context, projections);
86          this.projection = projections.get(0);
87          this.offset = offset;
88          this.offsetSize = offset.size();
89          this.sourcePath = sourcePath;
90      }
91  
92      /**
93       * {@inheritDoc}
94       * 
95       * @see org.modeshape.graph.connector.federation.Projector#project(ExecutionContext, Location, boolean)
96       */
97      public ProjectedNode project( ExecutionContext context,
98                                    Location location,
99                                    boolean requiresUpdate ) {
100         if (requiresUpdate && projection.isReadOnly()) return null;
101         PlaceholderNode placeholder = isPlaceholder(location);
102         if (placeholder != null) return placeholder;
103 
104         Path path = location.getPath();
105         Location locationInSource = location;
106         if (path != null) {
107             if (path.size() == offsetSize) {
108                 // Make sure the path is the same ...
109                 if (path.equals(offset)) {
110                     locationInSource = location.with(sourcePath);
111                 } else {
112                     return null; // not in the path
113                 }
114             } else {
115                 // Make sure the path begins with the offset ...
116                 if (path.isDecendantOf(offset)) {
117                     Path pathBelowOffset = path.relativeTo(offset);
118                     PathFactory pathFactory = context.getValueFactories().getPathFactory();
119                     Path pathInSource = pathFactory.create(sourcePath, pathBelowOffset);
120                     locationInSource = location.with(pathInSource);
121                 } else {
122                     // Not in the path
123                     return null;
124                 }
125             }
126         }
127         return new ProxyNode(projection, locationInSource, location);
128     }
129 
130 }