Skip to Content.
Sympa Menu

notify-dpml - svn commit: r2157 - in development/main/transit/core/handler/src: main/net/dpml/transit main/net/dpml/transit/artifact main/net/dpml/transit/link main/net/dpml/transit/util test/net/dpml/transit/test

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: niclas AT hedhman.org
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: svn commit: r2157 - in development/main/transit/core/handler/src: main/net/dpml/transit main/net/dpml/transit/artifact main/net/dpml/transit/link main/net/dpml/transit/util test/net/dpml/transit/test
  • Date: Sun, 27 Mar 2005 13:31:46 -0500

Author: niclas AT hedhman.org
Date: Sun Mar 27 13:31:45 2005
New Revision: 2157

Added:

development/main/transit/core/handler/src/main/net/dpml/transit/link/ArtifactLinkManager.java
(contents, props changed)

development/main/transit/core/handler/src/main/net/dpml/transit/link/FileLinkManager.java
(contents, props changed)

development/main/transit/core/handler/src/main/net/dpml/transit/link/Handler.java
(contents, props changed)

development/main/transit/core/handler/src/main/net/dpml/transit/link/Link.java
(contents, props changed)

development/main/transit/core/handler/src/main/net/dpml/transit/link/LinkManager.java
(contents, props changed)

development/main/transit/core/handler/src/main/net/dpml/transit/link/LinkURLConnection.java
(contents, props changed)

development/main/transit/core/handler/src/test/net/dpml/transit/test/LinkTestCase.java
(contents, props changed)
Modified:

development/main/transit/core/handler/src/main/net/dpml/transit/SecuredTransitContext.java

development/main/transit/core/handler/src/main/net/dpml/transit/Transit.java

development/main/transit/core/handler/src/main/net/dpml/transit/artifact/ArtifactURLConnection.java

development/main/transit/core/handler/src/main/net/dpml/transit/util/MimeTypeHandler.java

development/main/transit/core/handler/src/main/net/dpml/transit/util/StreamUtils.java

development/main/transit/core/handler/src/test/net/dpml/transit/test/MimeHandlerTestCase.java

development/main/transit/core/handler/src/test/net/dpml/transit/test/OfflineTestCase.java
Log:
Added a link: handler, which is similar to symlinks.

Modified:
development/main/transit/core/handler/src/main/net/dpml/transit/SecuredTransitContext.java
==============================================================================
---
development/main/transit/core/handler/src/main/net/dpml/transit/SecuredTransitContext.java
(original)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/SecuredTransitContext.java
Sun Mar 27 13:31:45 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2004 Niclas Hedhman
+ * Copyright 2004-2005 Niclas Hedhman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
import net.dpml.transit.cache.CacheHandler;
import net.dpml.transit.cache.RemoteCacheHandler;
import net.dpml.transit.host.ResourceManager;
+import net.dpml.transit.link.FileLinkManager;
+import net.dpml.transit.link.LinkManager;
import net.dpml.transit.monitors.SystemMonitor;
import net.dpml.transit.network.DelegatingAuthenticator;
import net.dpml.transit.network.RequestIdentifier;
@@ -143,6 +145,11 @@
private CacheHandler m_cacheHandler;

/**
+ * The LinkManager instance.
+ */
+ private LinkManager m_linkManager;
+
+ /**
* The authoritive anchor url.
*/
private URL m_authorativeHost;
@@ -182,32 +189,6 @@
}
}

- /** Set the Transit instance into Server mode.
- * @param serverMode true to operate Transit in Server mode.
- * @exception TransitException if not possible to make this Transit into
a server.
- */
- public void setServer( boolean serverMode )
- throws TransitException
- {
- m_server = serverMode;
- if( m_server ^ serverMode ) // Is there a change?
- {
- if( m_server )
- {
- bindServer();
- }
- else
- {
- unbindServer();
- }
- }
- }
-
- public boolean isServer()
- {
- return m_server;
- }
-
/**
* Creation of the transit context. If the transit context has already
* been established the method returns the singeton context otherwise a
new context
@@ -288,6 +269,32 @@
}
}

+ /** Set the Transit instance into Server mode.
+ * @param serverMode true to operate Transit in Server mode.
+ * @exception TransitException if not possible to make this Transit into
a server.
+ */
+ public void setServer( boolean serverMode )
+ throws TransitException
+ {
+ m_server = serverMode;
+ if( m_server ^ serverMode ) // Is there a change?
+ {
+ if( m_server )
+ {
+ bindServer();
+ }
+ else
+ {
+ unbindServer();
+ }
+ }
+ }
+
+ public boolean isServer()
+ {
+ return m_server;
+ }
+
/**
* Create the resource manager that will be responsible for the
establishment
* of the transit cache handler. The class implementating the resource
manager
@@ -351,6 +358,11 @@
{
m_authorativeHost = null;
m_cacheHandler = handler;
+
+ // Niclas; Hardcoding the LinkManager for now. When/If pluggable
+ // LinkManagers becomes a reality, we'll worry about how
+ // to install them.
+ m_linkManager = new FileLinkManager();
}

/**
@@ -363,8 +375,7 @@
private SecuredTransitContext( URL authorative, CacheHandler handler,
Properties props )
throws TransitException
{
- m_authorativeHost = authorative;
- m_cacheHandler = handler;
+ this( handler );
try
{
setupAuthenticator( props );
@@ -400,6 +411,11 @@
return m_cacheHandler;
}

+ public LinkManager getLinkManager()
+ {
+ return m_linkManager;
+ }
+
/**
* Return the authorative host URL.
* @return the authorative host url

Modified:
development/main/transit/core/handler/src/main/net/dpml/transit/Transit.java
==============================================================================
---
development/main/transit/core/handler/src/main/net/dpml/transit/Transit.java
(original)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/Transit.java
Sun Mar 27 13:31:45 2005
@@ -1,6 +1,6 @@
/*
* Copyright 2004 Stephen McConnell.
- * Copyright 2004 Niclas Hedhman.
+ * Copyright 2004-2005 Niclas Hedhman.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

Modified:
development/main/transit/core/handler/src/main/net/dpml/transit/artifact/ArtifactURLConnection.java
==============================================================================
---
development/main/transit/core/handler/src/main/net/dpml/transit/artifact/ArtifactURLConnection.java
(original)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/artifact/ArtifactURLConnection.java
Sun Mar 27 13:31:45 2005
@@ -175,7 +175,7 @@
}
catch( UnknownServiceException use )
{
- boolean ignore = true;
+ // Ignore
}

//

Added:
development/main/transit/core/handler/src/main/net/dpml/transit/link/ArtifactLinkManager.java
==============================================================================
--- (empty file)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/link/ArtifactLinkManager.java
Sun Mar 27 13:31:45 2005
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2005 Niclas Hedhman
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.dpml.transit.link;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import java.net.URI;
+import java.net.URL;
+
+import java.util.Properties;
+
+import net.dpml.transit.SecuredTransitContext;
+import net.dpml.transit.Transit;
+
+import net.dpml.transit.util.StreamUtils;
+
+/** The LinkManager is responsible for storing the mapping between a Link
+ * instance and a URI.
+ * <p>
+ * Applications should never call the methods for the LinkManager directly,
+ * and it is likely that the LinkManager remains outside the reachability of
+ * applications.
+ * </p>
+ * <p>
+ * This LinkManager stores all the link: to URI mappings in artifact:link:
+ * cache objects.
+ * </p>
+ */
+public class ArtifactLinkManager
+ implements LinkManager
+{
+
+ public ArtifactLinkManager()
+ {
+ }
+
+ /** Sets the URI for the provided Link.
+ * The LinkManager is required to persist this information between
+ * JVM restarts and should be persisted on a scope larger than a
+ * single JVM, typically a host or a local area network. LinkManagers
+ * are encouraged to establish other virtual scopes independent of
+ * network topologies.
+ * @exception IOException if the mapping could not be updated.
+ */
+ public void setTargetURI( URI linkUri, URI targetUri )
+ throws IOException
+ {
+ String artifact = "artifact:" + linkUri.toString();
+ URL store = new URL( artifact );
+ OutputStream out = store.openConnection().getOutputStream();
+ byte[] array = targetUri.toString().getBytes( "ISO8859-1" );
+ ByteArrayInputStream in = new ByteArrayInputStream( array );
+ StreamUtils.copyStream( in, out, true );
+ }
+
+ /** Returns the URI that the provided link: URI instance is pointing to.
+ * @exception IOException if the mapping could not be retrieved, due to
+ * a IOException.
+ * @return target URI that the link points to, or null if it could not
+ * be found.
+ */
+ public URI getTargetURI( URI linkUri )
+ throws IOException
+ {
+ String artifact = "artifact:" + linkUri.toString();
+ URL store = new URL( artifact );
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ InputStream in = store.openConnection().getInputStream();
+ StreamUtils.copyStream( in, out, true );
+ String target = out.toString( "ISO8859-1" );
+ return URI.create( target );
+ }
+}

Added:
development/main/transit/core/handler/src/main/net/dpml/transit/link/FileLinkManager.java
==============================================================================
--- (empty file)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/link/FileLinkManager.java
Sun Mar 27 13:31:45 2005
@@ -0,0 +1,179 @@
+/*
+ * Copyright 2005 Niclas Hedhman
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.dpml.transit.link;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import java.net.URI;
+
+import java.util.Properties;
+
+import net.dpml.transit.SecuredTransitContext;
+import net.dpml.transit.Transit;
+
+/** The LinkManager is responsible for storing the mapping between a Link
+ * instance and a URI.
+ * <p>
+ * Applications should never call the methods for the LinkManager directly,
+ * and it is likely that the LinkManager remains outside the reachability of
+ * applications.
+ * </p>
+ * <p>
+ * This LinkManager stores all the link: to URI mappings in text files,
+ * which are stored in $DPML_PREFS/transit/links.
+ * </p>
+ * <p>
+ * The SchemeSpecificPart of the link: URI, i.e. the value between the colon
+ * (:) and the hash (#) in "link:something#3.1", will be used as the
filename.
+ * That file is in the standard java.util.Properties format and
+ * the key is the string representation of the link URI and the value is the
+ * string representation of the target URI.
+ * </p>
+ */
+public class FileLinkManager
+ implements LinkManager
+{
+ private File m_linksDir;
+
+ public FileLinkManager()
+ {
+ m_linksDir = new File( Transit.DPML_PREFS, "transit/links" );
+ m_linksDir.mkdirs();
+
+ }
+
+ /** Sets the URI for the provided Link.
+ * The LinkManager is required to persist this information between
+ * JVM restarts and should be persisted on a scope larger than a
+ * single JVM, typically a host or a local area network. LinkManagers
+ * are encouraged to establish other virtual scopes independent of
+ * network topologies.
+ * @exception IOException if the mapping could not be updated.
+ */
+ public void setTargetURI( URI linkUri, URI targetUri )
+ throws IOException
+ {
+ File linkFile = getLinkFile( linkUri );
+ lockWrite( linkFile );
+ try
+ {
+ setTargetURI( linkFile, linkUri, targetUri );
+ } finally
+ {
+ unlock( linkFile );
+ }
+ }
+
+ /** Returns the URI that the provided link: URI instance is pointing to.
+ * @exception IOException if the mapping could not be retrieved, due to
+ * a IOException.
+ * @return target URI that the link points to, or null if it could not
+ * be found.
+ */
+ public URI getTargetURI( URI linkUri )
+ throws IOException
+ {
+ File linkFile = getLinkFile( linkUri );
+ lockRead( linkFile );
+ try
+ {
+ return getTargetURI( linkFile, linkUri );
+ } finally
+ {
+ unlock( linkFile );
+ }
+ }
+
+ private File getLinkFile( URI linkUri )
+ {
+ String filename = linkUri.getSchemeSpecificPart();
+ File f = new File( m_linksDir, filename );
+ return f;
+ }
+
+ private void setTargetURI( File linkFile, URI linkUri, URI targetUri )
+ throws IOException
+ {
+ Properties props = new Properties();
+ if( linkFile.exists() )
+ {
+ FileInputStream in = new FileInputStream( linkFile );
+ try
+ {
+ props.load( in );
+ } finally
+ {
+ in.close();
+ }
+ }
+ String link = linkUri.toString();
+ String target = targetUri.toString();
+ props.put( link, target );
+ File parent = linkFile.getParentFile();
+ parent.mkdirs();
+ FileOutputStream out = new FileOutputStream( linkFile );
+ try
+ {
+ props.store( out, "Links for " + linkUri.getSchemeSpecificPart()
);
+ } finally
+ {
+ out.close();
+ }
+ }
+
+ private URI getTargetURI( File linkFile, URI linkUri )
+ throws IOException
+ {
+ if( linkFile.exists() == false )
+ return null;
+
+ FileInputStream in = new FileInputStream( linkFile );
+ Properties props = new Properties();
+ try
+ {
+ props.load( in );
+ } finally
+ {
+ in.close();
+ }
+ in.close();
+ String link = linkUri.toString();
+ String target = props.getProperty( link );
+ if( target == null )
+ return null;
+ return URI.create( target );
+ }
+
+ private void lockWrite( File file )
+ {
+ // TODO
+ }
+
+ private void lockRead( File file )
+ {
+ // TODO
+ }
+
+ private void unlock( File file )
+ {
+ // TODO
+ }
+}

Added:
development/main/transit/core/handler/src/main/net/dpml/transit/link/Handler.java
==============================================================================
--- (empty file)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/link/Handler.java
Sun Mar 27 13:31:45 2005
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2005 Niclas Hedhman
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.dpml.transit.link;
+
+import java.io.IOException;
+
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/** link: URL protocol handler.
+ * <p>
+ * The <strong>link:</strong> protocol is similar to the symlink concept
+ * in Linux/Unix, where something that looks like a file points to another
+ * file. In this case, the link URL points to another URL. The resolution
+ * of the link translation is picked up from managed Transit server(s)
+ * </p>
+ */
+public class Handler extends URLStreamHandler
+{
+ /**
+ * Creation of a new transit link: protocol handler.
+ * @exception TransitException if a transit system error occurs
+ */
+ public Handler()
+ {
+ }
+
+ //
------------------------------------------------------------------------
+ // implementation
+ //
------------------------------------------------------------------------
+
+ /**
+ * Opens a connection to the specified URL.
+ *
+ * @param url A URL to open a connection to.
+ * @return The established connection.
+ * @throws IOException If a connection failure occurs.
+ */
+ protected URLConnection openConnection( final URL url )
+ throws IOException
+ {
+ return new LinkURLConnection( url );
+ }
+}

Added:
development/main/transit/core/handler/src/main/net/dpml/transit/link/Link.java
==============================================================================
--- (empty file)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/link/Link.java
Sun Mar 27 13:31:45 2005
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2005 Niclas Hedhman
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.dpml.transit.link;
+
+import java.io.IOException;
+
+import java.net.URI;
+
+/** The Link class represents the content of the link: URL and not the
content
+ * of the target that the link: points to.
+ * It is not intended that the applications instantiates this class
directly,
+ * but obtains it as a object from the <code>URL.getContent()</code> method.
+ * Example;
+ * <pre><code>
+ * URL url = new URL( "link:some/opague/pointer" );
+ * Class[] type = new Class[] { Link.class };
+ *
+ * // Get the Link object from the URL
+ * Link link = (Link) url.getContent( type );
+ *
+ * // Get the URI that this link is pointing to at the moment
+ * URI uri = link.getURI();
+ *
+ * // Change the link to point somewhere else.
+ * URI newUri = new URI( "artifact:jar:abc/def/hoopla#3.1.2" );
+ * link.setURI( newUri );
+ * </code></pre>
+ */
+public class Link
+{
+ private LinkManager m_manager;
+ private URI m_uri;
+
+ /** Constructor for the Link.
+ */
+ public Link( URI linkURI, LinkManager manager )
+ {
+ m_manager = manager;
+ m_uri = linkURI;
+ }
+
+ public URI getLinkURI()
+ {
+ return m_uri;
+ }
+
+ /** Return the URI that is currently bound to the Link.
+ * @return the current URI the link: is pointing to.
+ */
+ public URI getTargetURI( URI defaultUri )
+ throws IOException
+ {
+ URI uri = m_manager.getTargetURI( m_uri );
+ if( uri == null )
+ return defaultUri;
+ return uri;
+ }
+
+ /** Sets (and permanently remembers) the Link to point to a new URI.
+ * @param uri the URI that this link: should be pointing to.
+ */
+ public void setTargetURI( URI uri )
+ throws IOException
+ {
+ m_manager.setTargetURI( m_uri, uri );
+ }
+}
+

Added:
development/main/transit/core/handler/src/main/net/dpml/transit/link/LinkManager.java
==============================================================================
--- (empty file)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/link/LinkManager.java
Sun Mar 27 13:31:45 2005
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2005 Niclas Hedhman
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.dpml.transit.link;
+
+import java.io.IOException;
+
+import java.net.URI;
+
+/** The LinkManager is responsible for storing the mapping between a Link
+ * instance and a URI.
+ * Applications should never call the methods for the LinkManager directly,
+ * and it is likely that the LinkManager remains outside the reachability of
+ * applications.
+ */
+public interface LinkManager
+{
+ /** Sets the URI for the provided link URI.
+ * The LinkManager is required to persist this information between
+ * JVM restarts and should be persisted on a scope larger than a
+ * single JVM, typically a host or a local area network. LinkManagers
+ * are encouraged to establish other virtual scopes independent of
+ * network topologies.
+ */
+ void setTargetURI( URI link, URI target )
+ throws IOException;
+
+ /** Returns the URI that the provided link URI is pointing to.
+ */
+ URI getTargetURI( URI link )
+ throws IOException;
+
+}

Added:
development/main/transit/core/handler/src/main/net/dpml/transit/link/LinkURLConnection.java
==============================================================================
--- (empty file)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/link/LinkURLConnection.java
Sun Mar 27 13:31:45 2005
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2005 Niclas Hedhman
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.dpml.transit.link;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import java.net.UnknownServiceException;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
+
+import net.dpml.transit.Transit;
+import net.dpml.transit.SecuredTransitContext;
+import net.dpml.transit.util.MimeTypeHandler;
+
+/**
+ * link: URL protocol connection processor.
+ */
+public class LinkURLConnection extends URLConnection
+{
+ private boolean m_connected;
+ private URL m_targetURL;
+
+ /**
+ * Creation of a new handler.
+ * @param url the url to establish a connection with
+ * @param context the transit context
+ * @exception NullPointerException if the url argument is null
+ * @exception IOException if the url argument is not a valid,
+ * i.e. the path must contain a group and an artifactId
+ * separated by a slash.
+ */
+ LinkURLConnection( URL url )
+ throws NullPointerException, IOException
+ {
+ super( url );
+ m_connected = false;
+ }
+
+ /**
+ * Establish a connection.
+ *
+ * @exception IOException is an error occurs while attempting to establish
+ * the connection.
+ */
+ public void connect()
+ throws IOException
+ {
+ if( m_connected )
+ return;
+ m_connected = true;
+ LinkManager linkManager =
SecuredTransitContext.create().getLinkManager();
+ URI linkUri = URI.create( url.toExternalForm() );
+ URI targetUri = linkManager.getTargetURI( linkUri );
+ if( targetUri != null )
+ m_targetURL = new URL( targetUri.toString() );
+ else
+ m_targetURL = null;
+ }
+
+ /**
+ * Return an input stream to the resource.
+ * @return the input stream
+ * @exception IOException is an error occurs
+ */
+ public InputStream getInputStream()
+ throws IOException
+ {
+ connect();
+ if( m_targetURL == null )
+ return null;
+ URLConnection conn = m_targetURL.openConnection();
+ InputStream in = conn.getInputStream();
+ return in;
+ }
+
+ /**
+ * Return an output stream to the resource.
+ * @return the output stream
+ * @exception IOException if any I/O problems occur.
+ */
+ public OutputStream getOutputStream()
+ throws IOException
+ {
+ connect();
+ if( m_targetURL == null )
+ return null;
+ URLConnection conn = m_targetURL.openConnection();
+ OutputStream out = conn.getOutputStream();
+ return out;
+ }
+
+ /**
+ * Reutrn the mimetype of the content.
+ * @return the content mimetype
+ */
+ public String getContentType()
+ {
+ return MimeTypeHandler.getMimeType( "link" );
+ }
+
+ /**
+ * Return the content for this Link.
+ * @param classes a sequence of classes against which the
+ * implementation will attempt to establish a known match
+ * @return the content object (possibly null)
+ * @exception IOException is an error occurs
+ */
+ public Object getContent( Class[] classes )
+ throws IOException
+ {
+ try
+ {
+ Object content = super.getContent( classes );
+ if( content != null )
+ {
+ return content;
+ }
+ }
+ catch( UnknownServiceException use )
+ {
+ // Ignore
+ }
+
+ //
+ // attempt to resolve this locally
+ //
+
+ for( int i=0; i < classes.length; i++ )
+ {
+ Class c = classes[i];
+ if( c.equals( Link.class ) )
+ {
+ LinkManager linkManager =
SecuredTransitContext.getInstance().getLinkManager();
+ String extUri = getURL().toString();
+ URI uri = URI.create( extUri );
+ Link link = new Link( uri, linkManager );
+ return link;
+ }
+ if( c.equals( URI.class ) )
+ {
+ String extUri = getURL().toString();
+ URI uri = URI.create( extUri );
+ return uri;
+ }
+ }
+ return null;
+ }
+}

Modified:
development/main/transit/core/handler/src/main/net/dpml/transit/util/MimeTypeHandler.java
==============================================================================
---
development/main/transit/core/handler/src/main/net/dpml/transit/util/MimeTypeHandler.java
(original)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/util/MimeTypeHandler.java
Sun Mar 27 13:31:45 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2004 Niclas Hedhman.
+ * Copyright 2004-2005 Niclas Hedhman.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@
MIME_TYPES.put( "png", "image/png" );
MIME_TYPES.put( "gif", "image/gif" );
MIME_TYPES.put( "jpg", "image/jpg" );
+ MIME_TYPES.put( "link", "application/x-dpml-link" );
}

/**

Modified:
development/main/transit/core/handler/src/main/net/dpml/transit/util/StreamUtils.java
==============================================================================
---
development/main/transit/core/handler/src/main/net/dpml/transit/util/StreamUtils.java
(original)
+++
development/main/transit/core/handler/src/main/net/dpml/transit/util/StreamUtils.java
Sun Mar 27 13:31:45 2005
@@ -124,4 +124,22 @@
}
}
}
+
+ static public boolean compareStreams( InputStream in1, InputStream in2 )
+ throws IOException
+ {
+ boolean result = true;
+ do
+ {
+ int v1 = in1.read();
+ int v2 = in2.read();
+ if( v1 != v2 )
+ {
+ return false;
+ }
+ if( v1 == -1 )
+ break;
+ } while( true );
+ return result;
+ }
}

Added:
development/main/transit/core/handler/src/test/net/dpml/transit/test/LinkTestCase.java
==============================================================================
--- (empty file)
+++
development/main/transit/core/handler/src/test/net/dpml/transit/test/LinkTestCase.java
Sun Mar 27 13:31:45 2005
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2005 Niclas Hedhman
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.dpml.transit.test;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import java.net.URI;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+import net.dpml.transit.artifact.ArtifactNotFoundException;
+import net.dpml.transit.link.Link;
+import net.dpml.transit.util.StreamUtils;
+
+/**
+ *
+ * @author <a href="http://www.dpml.net/";>The Digital Product Meta
Library</a>
+ * @version $Id$
+ */
+public class LinkTestCase extends TestCase
+{
+ protected void setUp() throws Exception
+ {
+ System.setProperty( "java.protocol.handler.pkgs", "net.dpml.transit"
);
+ System.setProperty( "dpml.transit.secured.disable", "true" );
+ System.setProperty( "dpml.transit.authority.file",
"target/test/test.authority" );
+ String test = System.getProperty( "project.dir" );
+ File old = new File( test, "target/test/cache" );
+ old.delete();
+ }
+
+ /**
+ * Constructor
+ * @param name the test name
+ */
+ public LinkTestCase( String name )
+ {
+ super( name );
+ }
+
+ public void testDefaultValue()
+ throws Exception
+ {
+ URL url = new URL( "link:transit/test/aaa/bbb" );
+ Class[] args = new Class[] { Link.class };
+ Link link = (Link) url.getContent( args );
+ URI defUri = URI.create( "artifact:bla:abc/def#142" );
+ URI uri = link.getTargetURI( defUri );
+ assertEquals( "default", defUri, uri );
+ }
+
+ public void testContentUri()
+ throws Exception
+ {
+ URI orig = URI.create( "link:transit/test/aaa/bbb" );
+ URL url = orig.toURL();
+ Class[] args = new Class[] { URI.class };
+ URI found = (URI) url.getContent( args );
+ assertEquals( "default", orig, found );
+ }
+
+ public void testSetValue()
+ throws Exception
+ {
+ URL url = new URL( "link:transit/test/aaa/ccc" );
+ Class[] args = new Class[] { Link.class };
+ Link link = (Link) url.getContent( args );
+ URI newUri = URI.create( "artifact:testfile:dpml/test/abc#1.0.1" );
+ link.setTargetURI( newUri );
+
+ url = new URL( "link:transit/test/aaa/ccc" );
+ link = (Link) url.getContent( args );
+ URI defUri = URI.create( "artifact:bla:ccc/ddd#222" );
+ URI uri = link.getTargetURI( defUri );
+
+ assertEquals( "setting", newUri, uri );
+ }
+
+ public void testGetValue()
+ throws Exception
+ {
+ URL url = new URL( "link:transit/test/aaa/ccc" );
+ Class[] args = new Class[] { Link.class };
+ Link link = (Link) url.getContent( args );
+ URI newUri = URI.create( "artifact:testfile:dpml/test/abc#1.0.1" );
+ link.setTargetURI( newUri );
+
+ url = new URL( "link:transit/test/aaa/ccc" );
+ URL original = newUri.toURL();
+ InputStream in1 = url.openConnection().getInputStream();
+ InputStream in2 = original.openConnection().getInputStream();
+ assertTrue( "different content", StreamUtils.compareStreams( in1,
in2 ) );
+ }
+
+ public void testIllegalArtifact()
+ throws Exception
+ {
+ URL url = new URL( "link:transit/test/errors/aaa" );
+ Class[] args = new Class[] { Link.class };
+ URI newUri = URI.create( "artifact:abc:aaa/bbb/def" );
+
+ url = new URL( "link:transit/test/errors/aaa" );
+ try
+ {
+ Link link = (Link) url.getContent( args );
+ link.setTargetURI( newUri );
+
+ link = (Link) url.getContent( args );
+ fail( "No ArtifactNotFoundException" );
+ } catch( ArtifactNotFoundException e )
+ {
+ // should throw an exception if we try to read a against a
+ // non existent artifact.
+ }
+ }
+}

Modified:
development/main/transit/core/handler/src/test/net/dpml/transit/test/MimeHandlerTestCase.java
==============================================================================
---
development/main/transit/core/handler/src/test/net/dpml/transit/test/MimeHandlerTestCase.java
(original)
+++
development/main/transit/core/handler/src/test/net/dpml/transit/test/MimeHandlerTestCase.java
Sun Mar 27 13:31:45 2005
@@ -35,7 +35,8 @@
{ "pdf", "application/pdf" },
{ "png", "image/png" },
{ "jpg", "image/jpg" },
- { "gif", "image/gif" }
+ { "gif", "image/gif" },
+ { "link", "application/x-dpml-link" }
};

static public Test suite()

Modified:
development/main/transit/core/handler/src/test/net/dpml/transit/test/OfflineTestCase.java
==============================================================================
---
development/main/transit/core/handler/src/test/net/dpml/transit/test/OfflineTestCase.java
(original)
+++
development/main/transit/core/handler/src/test/net/dpml/transit/test/OfflineTestCase.java
Sun Mar 27 13:31:45 2005
@@ -44,6 +44,9 @@
System.setProperty( "java.protocol.handler.pkgs", "net.dpml.transit"
);
System.setProperty( "dpml.transit.secured.disable", "true" );
System.setProperty( "dpml.transit.authority.file",
"target/test/test.authority" );
+ String test = System.getProperty( "project.dir" );
+ File old = new File( test, "target/test/cache/" );
+ delete( old );
}

public void testDownloadNoVersion()
@@ -117,10 +120,6 @@
public void testWriteNewArtifact()
throws Exception
{
- String test = System.getProperty( "project.dir" );
- File old = new File( test,
"target/test/cache/dpml/test/txts/sample.txt" );
- System.out.println( old.getAbsolutePath() );
- old.delete();
URL url = new URL( "artifact:txt:dpml/test/sample" );
OutputStream out = url.openConnection().getOutputStream();
for( int i = 65 ; i < 91 ; i++ )
@@ -166,6 +165,19 @@
stream.close();
}
}
+
+ private void delete( File dir )
+ {
+ File[] entries = dir.listFiles();
+ for( int i = 0; i < entries.length; i++ )
+ {
+ File f = entries[ i ];
+ if( f.isDirectory() )
+ delete( f );
+ f.delete();
+ }
+ }
+
/* TODO: not used
private void print( URL url )
{



  • svn commit: r2157 - in development/main/transit/core/handler/src: main/net/dpml/transit main/net/dpml/transit/artifact main/net/dpml/transit/link main/net/dpml/transit/util test/net/dpml/transit/test, niclas, 03/27/2005

Archive powered by MHonArc 2.6.24.

Top of Page