KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > resolver > AbstractArtifactResolutionException


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

18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.repository.ArtifactRepository;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * Base class for artifact resolution exceptions.
28  *
29  * @author <a HREF="mailto:brett@apache.org">Brett Porter</a>
30  * @version $Id: AbstractArtifactResolutionException.java 379549 2006-02-21 18:06:25Z jdcasey $
31  */

32 public class AbstractArtifactResolutionException
33     extends Exception JavaDoc
34 {
35     private String JavaDoc groupId;
36
37     private String JavaDoc artifactId;
38
39     private String JavaDoc version;
40
41     private String JavaDoc type;
42
43     private List JavaDoc remoteRepositories;
44
45     private final String JavaDoc originalMessage;
46
47     private final String JavaDoc path;
48
49     static final String JavaDoc LS = System.getProperty( "line.separator" );
50
51     protected AbstractArtifactResolutionException( String JavaDoc message, String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version,
52                                                    String JavaDoc type, List JavaDoc remoteRepositories, List JavaDoc path )
53     {
54         super( constructMessageBase( message, groupId, artifactId, version, type, remoteRepositories, path ) );
55
56         this.originalMessage = message;
57         this.groupId = groupId;
58         this.artifactId = artifactId;
59         this.type = type;
60         this.version = version;
61         this.remoteRepositories = remoteRepositories;
62         this.path = constructArtifactPath( path, "" );
63     }
64
65     protected AbstractArtifactResolutionException( String JavaDoc message, String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version,
66                                                    String JavaDoc type, List JavaDoc remoteRepositories, List JavaDoc path, Throwable JavaDoc t )
67     {
68         super( constructMessageBase( message, groupId, artifactId, version, type, remoteRepositories, path ), t );
69
70         this.originalMessage = message;
71         this.groupId = groupId;
72         this.artifactId = artifactId;
73         this.type = type;
74         this.version = version;
75         this.remoteRepositories = remoteRepositories;
76         this.path = constructArtifactPath( path, "" );
77     }
78
79     protected AbstractArtifactResolutionException( String JavaDoc message, Artifact artifact )
80     {
81         this( message, artifact, null );
82     }
83
84     protected AbstractArtifactResolutionException( String JavaDoc message, Artifact artifact, List JavaDoc remoteRepositories )
85     {
86         this( message, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(),
87               remoteRepositories, artifact.getDependencyTrail() );
88     }
89
90     protected AbstractArtifactResolutionException( String JavaDoc message, Artifact artifact, List JavaDoc remoteRepositories,
91                                                    Throwable JavaDoc t )
92     {
93         this( message, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(),
94               remoteRepositories, artifact.getDependencyTrail(), t );
95     }
96
97     public String JavaDoc getGroupId()
98     {
99         return groupId;
100     }
101
102     public String JavaDoc getArtifactId()
103     {
104         return artifactId;
105     }
106
107     public String JavaDoc getVersion()
108     {
109         return version;
110     }
111
112     public String JavaDoc getType()
113     {
114         return type;
115     }
116
117     public List JavaDoc getRemoteRepositories()
118     {
119         return remoteRepositories;
120     }
121
122     public String JavaDoc getOriginalMessage()
123     {
124         return originalMessage;
125     }
126
127     protected static String JavaDoc constructArtifactPath( List JavaDoc path, String JavaDoc indentation )
128     {
129         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
130
131         if ( path != null )
132         {
133             sb.append( LS );
134             sb.append( indentation );
135             sb.append( "Path to dependency: " );
136             sb.append( LS );
137             int num = 1;
138             for ( Iterator JavaDoc i = path.iterator(); i.hasNext(); num++ )
139             {
140                 sb.append( indentation );
141                 sb.append( "\t" );
142                 sb.append( num );
143                 sb.append( ") " );
144                 sb.append( i.next() );
145                 sb.append( LS );
146             }
147         }
148
149         return sb.toString();
150     }
151
152     private static String JavaDoc constructMessageBase( String JavaDoc message, String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version,
153                                                 String JavaDoc type, List JavaDoc remoteRepositories, List JavaDoc path )
154     {
155         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
156
157         sb.append( message );
158         sb.append( LS );
159         sb.append( " " + groupId + ":" + artifactId + ":" + type + ":" + version );
160         sb.append( LS );
161         if ( remoteRepositories != null && !remoteRepositories.isEmpty() )
162         {
163             sb.append( LS );
164             sb.append( "from the specified remote repositories:" );
165             sb.append( LS + " " );
166
167             for ( Iterator JavaDoc i = new HashSet JavaDoc( remoteRepositories ).iterator(); i.hasNext(); )
168             {
169                 ArtifactRepository remoteRepository = (ArtifactRepository) i.next();
170
171                 sb.append( remoteRepository.getId() );
172                 sb.append( " (" );
173                 sb.append( remoteRepository.getUrl() );
174                 sb.append( ")" );
175                 if ( i.hasNext() )
176                 {
177                     sb.append( ",\n " );
178                 }
179             }
180         }
181
182         sb.append( constructArtifactPath( path, "" ) );
183         sb.append( LS );
184         return sb.toString();
185     }
186
187     protected static String JavaDoc constructMissingArtifactMessage( String JavaDoc message, String JavaDoc indentation, String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version,
188                                               String JavaDoc type, String JavaDoc downloadUrl, List JavaDoc path )
189     {
190         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( message );
191
192         if ( !"pom".equals( type ) )
193         {
194             if ( downloadUrl != null )
195             {
196                 sb.append( LS );
197                 sb.append( LS );
198                 sb.append( indentation );
199                 sb.append( "Try downloading the file manually from: " );
200                 sb.append( LS );
201                 sb.append( indentation );
202                 sb.append( " " );
203                 sb.append( downloadUrl );
204             }
205             else
206             {
207                 sb.append( LS );
208                 sb.append( LS );
209                 sb.append( indentation );
210                 sb.append( "Try downloading the file manually from the project website." );
211             }
212             
213             sb.append( LS );
214             sb.append( LS );
215             sb.append( indentation );
216             sb.append( "Then, install it using the command: " );
217             sb.append( LS );
218             sb.append( indentation );
219             sb.append( " mvn install:install-file -DgroupId=" );
220             sb.append( groupId );
221             sb.append( " -DartifactId=" );
222             sb.append( artifactId );
223             sb.append( " \\\n");
224             sb.append( indentation );
225             sb.append( " " );
226             sb.append( "-Dversion=" );
227             sb.append( version );
228             sb.append( " -Dpackaging=" );
229             sb.append( type );
230             sb.append( " -Dfile=/path/to/file" );
231             sb.append( LS );
232         }
233
234         sb.append( constructArtifactPath( path, indentation ) );
235         sb.append( LS );
236
237         return sb.toString();
238     }
239     
240     public String JavaDoc getArtifactPath()
241     {
242         return path;
243     }
244 }
245
Popular Tags