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