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 that is projected below the federated root. In other words, the federated repository has a single projection with a
36 * single "/something/below/root => /" rule.
37 */
38 @Immutable
39 final class OffsetMirrorProjector extends ProjectorWithPlaceholders {
40
41 /**
42 * Attempt to create an instance of the {@link OffsetMirrorProjector} with the supplied projections using the supplied
43 * context.
44 *
45 * @param context the context; may not be null
46 * @param projections the projections in the federated repository; may not be null
47 * @return the offset mirror projector, or null if the projections didn't match the criteria for such a projector
48 */
49 static OffsetMirrorProjector with( ExecutionContext context,
50 List<Projection> projections ) {
51 assert projections != null;
52 assert context != null;
53 if (projections.size() != 1) return null;
54 Projection projection = projections.get(0);
55 assert projection != null;
56 if (projection.getRules().size() != 1) return null;
57 PathFactory pathFactory = context.getValueFactories().getPathFactory();
58 List<Path> topLevelPaths = projection.getRules().get(0).getTopLevelPathsInRepository(pathFactory);
59 if (topLevelPaths.size() != 1) return null;
60 Path topLevelPath = topLevelPaths.get(0);
61 assert topLevelPath != null;
62 if (topLevelPath.isRoot()) return null;
63 return new OffsetMirrorProjector(context, projections, topLevelPath);
64 }
65
66 private final Projection projection;
67 private final Path offset;
68 private final int offsetSize;
69
70 private OffsetMirrorProjector( ExecutionContext context,
71 List<Projection> projections,
72 Path offset ) {
73 super(context, projections);
74 this.projection = projections.get(0);
75 this.offset = offset;
76 this.offsetSize = offset.size();
77 }
78
79 /**
80 * {@inheritDoc}
81 *
82 * @see org.modeshape.graph.connector.federation.Projector#project(ExecutionContext, Location, boolean)
83 */
84 public ProjectedNode project( ExecutionContext context,
85 Location location,
86 boolean requiresUpdate ) {
87 if (requiresUpdate && projection.isReadOnly()) return null;
88 PlaceholderNode placeholder = isPlaceholder(location);
89 if (placeholder != null) return placeholder;
90
91 Path path = location.getPath();
92 Location locationInSource = location;
93 if (path != null) {
94 if (path.size() == offsetSize) {
95 // Make sure the path is the same ...
96 if (path.equals(offset)) {
97 locationInSource = location.with(context.getValueFactories().getPathFactory().createRootPath());
98 } else {
99 return null; // not in the path
100 }
101 } else {
102 // Make sure the path begins with the offset ...
103 if (path.isAtOrBelow(offset)) {
104 locationInSource = location.with(path.subpath(offsetSize));
105 } else {
106 // Not in the path
107 return null;
108 }
109 }
110 }
111 return new ProxyNode(projection, locationInSource, location);
112 }
113
114 }