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 java.util.*;
022
023import org.jppf.location.Location;
024
025/**
026 * A simple implementation of the {@link ClassPath} interface
027 * @author Laurent Cohen
028 */
029public class ClassPathImpl implements ClassPath {
030  /**
031   * Explicit serialVersionUID.
032   */
033  private static final long serialVersionUID = 1L;
034  /**
035   * Mapping of classpath elements to their names.
036   */
037  private final List<ClassPathElement> elementList = new ArrayList<>();
038  /**
039   * Determines whether the node should force a reset of the class loader before executing the tasks.
040   */
041  private boolean forceClassLoaderReset = false;
042
043  @Override
044  public Iterator<ClassPathElement> iterator() {
045    return elementList.iterator();
046  }
047
048  @Override
049  public ClassPath add(final ClassPathElement element) {
050    elementList.add(element);
051    return this;
052  }
053
054  @Override
055  public ClassPath add(final Location<?> location) {
056    elementList.add(new ClassPathElementImpl(location, location));
057    return this;
058  }
059
060  @Override
061  public ClassPath add(final Location<?> sourceLocation, final Location<?> targetLocation) {
062    elementList.add(new ClassPathElementImpl(sourceLocation, targetLocation));
063    return this;
064  }
065
066  @Override
067  public ClassPath add(final Location<?> sourceLocation, final Location<?> targetLocation, final boolean copyToExistingFileLocation) {
068    elementList.add(new ClassPathElementImpl(sourceLocation, targetLocation, copyToExistingFileLocation));
069    return this;
070  }
071
072  /**
073   * {@inheritDoc}
074   * @deprecated the {@code name} attribute has no clearly defined, consistent semantics. It is no longer used.
075   */
076  @Override
077  public ClassPath add(final String name, final Location<?> location) {
078    elementList.add(new ClassPathElementImpl(location));
079    return this;
080  }
081
082  /**
083   * {@inheritDoc}
084   * @deprecated the {@code name} attribute has no clearly defined, consistent semantics. It is no longer used.
085   */
086  @Override
087  public ClassPath add(final String name, final Location<?> localLocation, final Location<?> remoteLocation) {
088    elementList.add(new ClassPathElementImpl(localLocation, remoteLocation));
089    return this;
090  }
091
092  @Override
093  public ClassPath remove(final ClassPathElement element) {
094    elementList.remove(element);
095    return this;
096  }
097
098  /**
099   * {@inheritDoc}
100   * @deprecated the {@code name} attribute has no clearly defined, consistent semantics. It is no longer used.
101   */
102  @Override
103  public ClassPath remove(final String name) {
104    ClassPathElement toRemove = null;
105    if (name == null) return null;
106    for (final ClassPathElement elt: elementList) {
107      if (name.equals(elt.getName())) {
108        toRemove = elt;
109        break;
110      }
111    }
112    if (toRemove != null) elementList.remove(toRemove);
113    return this;
114  }
115
116  @Override
117  public ClassPath clear() {
118    elementList.clear();
119    return this;
120  }
121
122  /**
123   * {@inheritDoc}
124   * @deprecated the {@code name} attribute has no clearly defined, consistent semantics. It is no longer used.
125   */
126  @Override
127  public ClassPathElement element(final String name) {
128    if (name == null) return null;
129    for (final ClassPathElement elt: elementList) {
130      if (name.equals(elt.getName())) return elt;
131    }
132    return null;
133  }
134
135  @Override
136  public Collection<ClassPathElement> allElements() {
137    return new ArrayList<>(elementList);
138  }
139
140  @Override
141  public boolean isEmpty() {
142    return elementList.isEmpty();
143  }
144
145  @Override
146  public boolean isForceClassLoaderReset() {
147    return forceClassLoaderReset;
148  }
149
150  @Override
151  public ClassPath setForceClassLoaderReset(final boolean forceClassLoaderReset) {
152    this.forceClassLoaderReset = forceClassLoaderReset;
153    return this;
154  }
155
156  @Override
157  public String toString() {
158    final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('[');
159    int count = 0;
160    for (final ClassPathElement elt: this) {
161      if (count > 0) sb.append(", ClassPathElement[");
162      sb.append("source=").append(elt.getSourceLocation().getPath());
163      sb.append(", target=").append(elt.getTargetLocation().getPath());
164      sb.append(']');
165      count++;
166    }
167    sb.append(']');
168    return sb.toString();
169  }
170}