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.location;
020
021import java.io.*;
022import java.util.List;
023import java.util.concurrent.CopyOnWriteArrayList;
024
025import org.jppf.utils.streams.*;
026
027/**
028 * Instances of this class represent the location of an artifact, generally a file or the data found at a url.
029 * @param <T> the type of this location.
030 * @author Laurent Cohen
031 */
032public abstract class AbstractLocation<T> implements Location<T> {
033  /**
034   * Explicit serialVersionUID.
035   */
036  private static final long serialVersionUID = 1L;
037  /**
038   * The path for this location.
039   */
040  protected final T path;
041  /**
042   * The list of listeners to this location.
043   */
044  protected final List<LocationEventListener> listeners = new CopyOnWriteArrayList<>();
045  /**
046   * Boolean flag that determines if at least one listener is registered.
047   * Used to minimize the overhead of sending events if there is no listener.
048   */
049  protected boolean eventsEnabled = false;
050
051  /**
052   * Initialize this location with the specified type and path.
053   * @param path the path for this location.
054   */
055  public AbstractLocation(final T path) {
056    this.path = path;
057  }
058
059  @Override
060  public T getPath() {
061    return path;
062  }
063
064  @Override
065  public <V> Location<V> copyTo(final Location<V> location) throws Exception {
066    copyStream(getInputStream(), location.getOutputStream(), eventsEnabled);
067    return location;
068  }
069
070  @Override
071  public byte[] toByteArray() throws Exception {
072    final JPPFByteArrayOutputStream os = new JPPFByteArrayOutputStream();
073    copyStream(getInputStream(), os, false);
074    return os.toByteArray();
075  }
076
077  @Override
078  public String toString() {
079    final StringBuilder sb = new StringBuilder();
080    sb.append(getClass().getSimpleName()).append('[');
081    sb.append("path=").append(path);
082    sb.append(']');
083    return sb.toString();
084  }
085
086  @Override
087  public void addLocationEventListener(final LocationEventListener listener) {
088    if (listener == null) throw new NullPointerException("null listener not accepted");
089    listeners.add(listener);
090    if (!eventsEnabled) eventsEnabled = true;
091  }
092
093  @Override
094  public void removeLocationEventListener(final LocationEventListener listener) {
095    if (listener == null) throw new IllegalArgumentException("null listener not accepted");
096    listeners.remove(listener);
097    if (listeners.isEmpty()) eventsEnabled = false;
098  }
099
100  /**
101   * Notify all listeners that a data transfer has occurred.
102   * @param n the size of the data that was transferred.
103   */
104  protected void fireLocationEvent(final long n) {
105    if (listeners.isEmpty()) return;
106    final LocationEvent event = new LocationEvent(this, n);
107    for (final LocationEventListener l : listeners) l.dataTransferred(event);
108  }
109
110  /**
111   * Copy the data read from the specified input stream to the specified output stream.
112   * @param is the input stream to read from.
113   * @param os the output stream to write to.
114   * @param withEvt if <code>true</code>, then {@link LocationEvent}s will be generated during the transfer.
115   * @throws IOException if an I/O error occurs.
116   */
117  private void copyStream(final InputStream is, final OutputStream os, final boolean withEvt) throws IOException {
118    final OutputStream tmpos = !withEvt ? os : new NotifyingOutputStream(os, new NotifyingStreamCallback() {
119      @Override
120      public void bytesNotification(final long length) throws IOException {
121        fireLocationEvent(length);
122      }
123    });
124    StreamUtils.copyStream(is, tmpos, true);
125  }
126}