001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.node.protocol;
020
021import org.jppf.location.Location;
022
023/**
024 * A simple implementation of the {@link ClassPathElement} interface.
025 * @author Laurent Cohen
026 */
027public class ClassPathElementImpl implements ClassPathElement {
028  /**
029   * Explicit serialVersionUID.
030   */
031  private static final long serialVersionUID = 1L;
032  /**
033   * The location of this classpath element in the client environment.
034   */
035  private final Location<?> sourceLocation;
036  /**
037   * The location of this classpath element in the node environment.
038   */
039  private final Location<?> targetLocation;
040  /**
041   * The name of this classpath element.
042   * @deprecated the {@code name} attribute has no clearly defined, consistent semantics. It is no longer used.
043   */
044  private final String name;
045  /**
046   * Whether to copy the source to the target if the target is a file and if it already exists on the file system.
047   */
048  private final boolean copyToExistingFile;
049
050  /**
051   * Initialize this classpath element with the specified name and local location.
052   * The remote location is set to the same location.
053   * @param name the name of this classpath element.
054   * @param location the location of this classpath element.
055   * @deprecated the {@code name} attribute has no clearly defined, consistent semantics. It is no longer used.
056   */
057  ClassPathElementImpl(final String name, final Location<?> location) {
058    this(name, location, location);
059  }
060
061  /**
062   * Initialize this classpath element with the specified name and local location.
063   * The remote location is set to the same location.
064   * @param name the name of this classpath element.
065   * @param sourceLocation the location of this classpath element in the client environment.
066   * @param targetLocation the location of this classpath element in the node environment.
067   * @deprecated the {@code name} attribute has no clearly defined, consistent semantics. It is no longer used.
068   */
069  ClassPathElementImpl(final String name, final Location<?> sourceLocation, final Location<?> targetLocation) {
070    this.name = name;
071    this.sourceLocation = sourceLocation;
072    this.targetLocation = targetLocation;
073    this.copyToExistingFile = true;
074  }
075
076  /**
077   * Initialize this classpath element with the specified name and local location.
078   * The remote location is set to the same location.
079   * @param location the location of this classpath element.
080   */
081  ClassPathElementImpl(final Location<?> location) {
082    this(location, location, true);
083  }
084
085  /**
086   * Initialize this classpath element with the specified name and local location.
087   * The remote location is set to the same location.
088   * @param sourceLocation the location of this classpath element in the client environment.
089   * @param targetLocation the location of this classpath element in the node environment.
090   */
091  ClassPathElementImpl(final Location<?> sourceLocation, final Location<?> targetLocation) {
092    this(sourceLocation, targetLocation, true);
093  }
094
095  /**
096   * Initialize this classpath element with the specified name and local location.
097   * The remote location is set to the same location.
098   * @param sourceLocation the location of this classpath element in the client environment.
099   * @param targetLocation the location of this classpath element in the node environment.
100   * @param copyToExistingFileLocation whether to copy the source to the target if the target is a file and if it already exists on the file system.
101   */
102  ClassPathElementImpl(final Location<?> sourceLocation, final Location<?> targetLocation, final boolean copyToExistingFileLocation) {
103    this.name = null;
104    this.sourceLocation = sourceLocation;
105    this.targetLocation = targetLocation;
106    this.copyToExistingFile = copyToExistingFileLocation;
107  }
108
109  /**
110   * {@inheritDoc}
111   * @deprecated the {@code name} attribute has no clearly defined, consistent semantics. It is no longer used.
112   */
113  @Override
114  public String getName() {
115    return name;
116  }
117
118  @Override
119  public Location<?> getSourceLocation() {
120    return sourceLocation;
121  }
122
123  /**
124   * {@inheritDoc}
125   * @deprecated use {@link #getSourceLocation()} instead.
126   * @exclude
127   */
128  @Override
129  public Location<?> getLocalLocation() {
130    return sourceLocation;
131  }
132
133  @Override
134  public Location<?> getTargetLocation() {
135    return targetLocation;
136  }
137
138  /**
139   * {@inheritDoc}
140   * @deprecated use {@link #getTargetLocation()} instead.
141   * @exclude
142   */
143  @Override
144  public Location<?> getRemoteLocation() {
145    return targetLocation;
146  }
147
148  /**
149   * This default implementation always return true.
150   * @return <code>true</code>.
151   */
152  @Override
153  public boolean validate() {
154    return true;
155  }
156
157  @Override
158  public String toString() {
159    return new StringBuilder(getClass().getSimpleName()).append('[')
160      .append("source=").append(sourceLocation)
161      .append(", target=").append(targetLocation)
162      .append(']').toString();
163  }
164
165  @Override
166  public boolean isCopyToExistingFile() {
167    return copyToExistingFile;
168  }
169}