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.request; 25 26 import org.modeshape.common.util.CheckArg; 27 import org.modeshape.common.util.HashCode; 28 import org.modeshape.graph.GraphI18n; 29 import org.modeshape.graph.Location; 30 31 /** 32 * Instruction to verify the existance of a node at the specified location. This request also returns the actual location. 33 */ 34 public class VerifyNodeExistsRequest extends CacheableRequest { 35 36 private static final long serialVersionUID = 1L; 37 38 private final Location at; 39 private final String workspaceName; 40 private Location actualLocation; 41 42 /** 43 * Create a request to verify the existance and location of a node at the supplied location. 44 * 45 * @param at the location of the node to be verified 46 * @param workspaceName the name of the workspace containing the node 47 * @throws IllegalArgumentException if the location or workspace name is null 48 */ 49 public VerifyNodeExistsRequest( Location at, 50 String workspaceName ) { 51 CheckArg.isNotNull(at, "at"); 52 CheckArg.isNotNull(workspaceName, "workspaceName"); 53 this.workspaceName = workspaceName; 54 this.at = at; 55 } 56 57 /** 58 * {@inheritDoc} 59 * 60 * @see org.modeshape.graph.request.Request#isReadOnly() 61 */ 62 @Override 63 public boolean isReadOnly() { 64 return true; 65 } 66 67 /** 68 * Get the location defining the node that is to be read. 69 * 70 * @return the location of the node; never null 71 */ 72 public Location at() { 73 return at; 74 } 75 76 /** 77 * Get the name of the workspace in which the node exists. 78 * 79 * @return the name of the workspace; never null 80 */ 81 public String inWorkspace() { 82 return workspaceName; 83 } 84 85 /** 86 * Sets the actual and complete location of the node whose properties have been read. This method must be called when 87 * processing the request, and the actual location must have a {@link Location#getPath() path}. 88 * 89 * @param actual the actual location of the node being read, or null if the {@link #at() current location} should be used 90 * @throws IllegalArgumentException if the actual location is null or does not have a path. 91 * @throws IllegalStateException if the request is frozen 92 */ 93 public void setActualLocationOfNode( Location actual ) { 94 checkNotFrozen(); 95 CheckArg.isNotNull(actual, "actual"); 96 if (!actual.hasPath()) { 97 throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual)); 98 } 99 this.actualLocation = actual; 100 } 101 102 /** 103 * Get the actual location of the node whose properties were read. 104 * 105 * @return the actual location, or null if the actual location was not set 106 */ 107 public Location getActualLocationOfNode() { 108 return actualLocation; 109 } 110 111 /** 112 * Return whether this node is known to exist. If the request has been processed, it will have an 113 * {@link #getActualLocationOfNode() actual location} or an {@link #getError() error}. 114 * 115 * @return true if this node is known to exist 116 * @see #getActualLocationOfNode() 117 * @see #getError() 118 */ 119 public boolean exists() { 120 return actualLocation != null; 121 } 122 123 /** 124 * {@inheritDoc} 125 * 126 * @see org.modeshape.graph.request.Request#cancel() 127 */ 128 @Override 129 public void cancel() { 130 super.cancel(); 131 this.actualLocation = null; 132 } 133 134 /** 135 * {@inheritDoc} 136 * 137 * @see java.lang.Object#hashCode() 138 */ 139 @Override 140 public int hashCode() { 141 return HashCode.compute(at, workspaceName); 142 } 143 144 /** 145 * {@inheritDoc} 146 * 147 * @see java.lang.Object#equals(java.lang.Object) 148 */ 149 @Override 150 public boolean equals( Object obj ) { 151 if (obj == this) return true; 152 if (this.getClass().isInstance(obj)) { 153 VerifyNodeExistsRequest that = (VerifyNodeExistsRequest)obj; 154 if (!this.at().isSame(that.at())) return false; 155 if (!this.inWorkspace().equals(that.inWorkspace())) return false; 156 return true; 157 } 158 return false; 159 } 160 161 /** 162 * {@inheritDoc} 163 * 164 * @see java.lang.Object#toString() 165 */ 166 @Override 167 public String toString() { 168 return "verify node exists at " + at() + " in the \"" + workspaceName + "\" workspace"; 169 } 170 171 @Override 172 public RequestType getType() { 173 return RequestType.VERIFY_NODE_EXISTS; 174 } 175 }