Package net.sourceforge.javautil.common.io

Source Code of net.sourceforge.javautil.common.io.VirtualArtifactURLStreamHandler$VirtualArtifactURLConnection

package net.sourceforge.javautil.common.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;

import net.sourceforge.javautil.common.CollectionUtil;
import net.sourceforge.javautil.common.StringUtil;
import net.sourceforge.javautil.common.io.impl.SimplePath;

/**
* This will handle URL stream's for {@link IVirtualArtifact}'s.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: VirtualArtifactURLStreamHandler.java 2297 2010-06-16 00:13:14Z ponderator $
*/
public class VirtualArtifactURLStreamHandler extends URLStreamHandler {
 
  protected final IVirtualArtifact artifact;

  public VirtualArtifactURLStreamHandler() { this(null); }
  public VirtualArtifactURLStreamHandler(IVirtualArtifact artifact) {
    this.artifact = artifact;
  }

  @Override protected URLConnection openConnection(URL u) throws IOException {
    return new VirtualArtifactURLConnection(u);
  }
 
  public class VirtualArtifactURLConnection extends URLConnection {

    public VirtualArtifactURLConnection(URL url) { super(url); }

    @Override public void connect() throws IOException {
      this.connected = true;
    }

    @Override public InputStream getInputStream() throws IOException {
      IVirtualArtifact artifact = this.getArtifact();
      if (artifact instanceof IVirtualArtifactIO) {
        return ((IVirtualArtifactIO)artifact).getInputStream();
      } else {
        throw new UnsupportedOperationException("Cannot do I/O on artifacts of type: " + artifact.getClass());
      }
    }

    @Override public long getLastModified() { return this.getArtifact().getLastModified(); }

    @Override public OutputStream getOutputStream() throws IOException {
      IVirtualArtifact artifact = this.getArtifact();
      if (artifact instanceof IVirtualArtifactIO) {
        return ((IVirtualArtifactIO)artifact).getOutputStream();
      } else {
        throw new UnsupportedOperationException("Cannot do I/O on artifacts of type: " + artifact.getClass());
      }
    }
   
    /**
     * @return The artifact related to the URL
     */
    public IVirtualArtifact getArtifact () {
      if (VirtualArtifactURLStreamHandler.this.artifact != null) {
        return VirtualArtifactURLStreamHandler.this.artifact;
      } else {
        VirtualArtifactSystem vas = VirtualArtifactSystem.get(url.getHost(), false);
        if (vas == null) throw new IllegalArgumentException("No mounted VAS by the name: " + url.getHost() + " could be found.");
       
        return vas.getArtifact( new SimplePath(url.getPath()) );
      }
    }
   
  }

}
TOP

Related Classes of net.sourceforge.javautil.common.io.VirtualArtifactURLStreamHandler$VirtualArtifactURLConnection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.