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 a direct one-for-one mirror against a single source repository. In 35 * other words, the federated repository has a single projection with a single "/ => /" rule. 36 */ 37 @Immutable 38 final class MirrorProjector implements Projector { 39 40 /** 41 * Attempt to create an instance of the {@link MirrorProjector} with the supplied projections using the supplied context. 42 * 43 * @param context the context; may not be null 44 * @param projections the projections in the federated repository; may not be null 45 * @return the mirror projector, or null if the projections didn't match the criteria for such a projector 46 */ 47 static MirrorProjector with( ExecutionContext context, 48 List<Projection> projections ) { 49 assert projections != null; 50 assert context != null; 51 if (projections.size() != 1) return null; 52 Projection projection = projections.get(0); 53 assert projection != null; 54 if (projection.getRules().size() != 1) return null; 55 PathFactory pathFactory = context.getValueFactories().getPathFactory(); 56 List<Path> topLevelPaths = projection.getRules().get(0).getTopLevelPathsInRepository(pathFactory); 57 if (topLevelPaths.size() != 1) return null; 58 if (!topLevelPaths.get(0).isRoot()) return null; 59 return new MirrorProjector(projection); 60 } 61 62 private final Projection projection; 63 64 private MirrorProjector( Projection projection ) { 65 this.projection = projection; 66 } 67 68 /** 69 * {@inheritDoc} 70 * <p> 71 * This implementation <i>always<i> returns a single {@link ProxyNode} for the location in the single projection. 72 * </p> 73 * 74 * @see org.modeshape.graph.connector.federation.Projector#project(ExecutionContext, Location, boolean) 75 */ 76 public ProjectedNode project( ExecutionContext context, 77 Location location, 78 boolean requiresUpdate ) { 79 if (requiresUpdate && projection.isReadOnly()) return null; 80 return new ProxyNode(projection, location, location); 81 } 82 83 }