KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > AbstractURLConnection


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.repository ;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLConnection JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.apache.avalon.repository.Artifact;
27
28 /**
29  * Abstract artifact URL protocol handler.
30  * @since 3.3
31  */

32 public abstract class AbstractURLConnection extends URLConnection JavaDoc
33 {
34     /**
35      * Creation of a new handler.
36      * @param url the url to establish a connection with
37      */

38      AbstractURLConnection( URL JavaDoc url )
39        throws IOException JavaDoc
40      {
41          super( url );
42  
43          String JavaDoc path = url.getPath();
44          int i = path.lastIndexOf( "/" );
45          if( i<0 )
46          {
47              final String JavaDoc error =
48                "Artifact specification does not contain a [group]/[name] seperator.";
49              throw new MalformedURLException JavaDoc( error );
50          }
51      }
52
53     /**
54      * Return the Artifact specified by this URL.
55      * @param classes a set of classes (ignored)
56      * @return the artifact instance
57      * @see org.apache.avalon.repository.Artifact
58      */

59      public Object JavaDoc getContent( Class JavaDoc[] classes ) throws IOException JavaDoc
60      {
61          return getContent();
62      }
63
64     /**
65      * Null implementation of the conect protocol.
66      */

67      public void connect()
68      {
69          // nothing to do
70
}
71
72     /**
73      * Return the Artifact specified by this URL.
74      * @param type the artifact type (e.g. "jar", "block", "xml", etc.)
75      * @return the artifact instance
76      * @see org.apache.avalon.repository.Artifact
77      */

78      protected Object JavaDoc getContent( String JavaDoc type ) throws IOException JavaDoc
79      {
80          try
81          {
82              final String JavaDoc path = getURL().getPath();
83              final int i = path.lastIndexOf( "/" );
84              final String JavaDoc group = path.substring( 0, i );
85              final String JavaDoc name = path.substring( i+1 );
86              final String JavaDoc version = getVersion( url );
87              return Artifact.createArtifact( group, name, version, type );
88          }
89          catch( Throwable JavaDoc e )
90          {
91              final String JavaDoc error =
92                "Unexpected exception while resolving url [" + super.getURL() + "].";
93              throw new CascadingIOException( error );
94          }
95      }
96
97     /**
98      * Utility method to return the version field with the url query.
99      * @param url the url containing the query
100      * @return the version value if declared else null
101      */

102      protected String JavaDoc getVersion( URL JavaDoc url )
103      {
104          if( null != url.getRef() ) return url.getRef();
105          return getQueryField( url, "version", null );
106      }
107
108      /**
109      * Utility method to return the value of a field within the url query.
110      * @param url the url containing the query
111      * @param field the query field name
112      * @param fallback the default value if not query parameter available
113      * @return the value of the query field
114      */

115      protected String JavaDoc getQueryField( URL JavaDoc url, String JavaDoc field, String JavaDoc fallback )
116      {
117          String JavaDoc query = url.getQuery();
118          if( null != query )
119          {
120              StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( query, "&" );
121              while( tokenizer.hasMoreElements() )
122              {
123                  String JavaDoc token = tokenizer.nextToken();
124                  if( token.startsWith( field + "=" ) )
125                  {
126                      return token.substring( (field.length() + 1) );
127                  }
128              }
129          }
130          return fallback;
131      }
132 }
133
Popular Tags