View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  
21  package org.apache.directory.server.schema.bootstrap.partition;
22  
23  import java.net.URL;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.List;
28  
29  /**
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev: 664295 $ $Date: 2008-06-07 09:48:16 +0200 (Sa, 07 Jun 2008) $
32   */
33  public class UniqueResourceException extends RuntimeException
34  {
35      private static final long serialVersionUID = 1L;
36  
37      private final String resourceName;
38      private final List<URL> urls;
39      private final String resourceDescription;
40  
41      public UniqueResourceException( String resourceName, String resourceDescription )
42      {
43          this( resourceName, null, resourceDescription );
44      }
45  
46      public UniqueResourceException( String resourceName, List<URL> urls, String resourceDescription )
47      {
48          this.resourceName = resourceName;
49          this.urls = urls;
50          this.resourceDescription = resourceDescription;
51      }
52  
53      public UniqueResourceException( String resourceName, URL first, Enumeration<URL> urlEnum, String resourceDescription )
54      {
55          this( resourceName, toList( first, urlEnum ), resourceDescription );
56      }
57  
58      private static List<URL> toList( URL first, Enumeration<URL> urlEnum )
59      {
60          ArrayList<URL> urls = new ArrayList<URL>();
61          urls.add( first );
62          while( urlEnum.hasMoreElements() )
63          {
64              urls.add( urlEnum.nextElement() );
65          }
66          return urls;
67      }
68  
69      public String getMessage()
70      {
71          StringBuffer buf = new StringBuffer( "Problem locating " ).append( resourceDescription ).append( "\n" );
72          if ( urls == null )
73          {
74              buf.append( "No resources named '" ).append( resourceName ).append( "' located on classpath" );
75          } else
76          {
77              buf.append( "Multiple copies of resource named '" ).append( resourceName ).append(
78                      "' located on classpath at urls" );
79              for ( URL url : urls )
80              {
81                  buf.append( "\n    " ).append( url );
82              }
83          }
84          return buf.toString();
85      }
86  
87  
88      public String getResourceName()
89      {
90          return resourceName;
91      }
92  
93      public List<URL> getUrls()
94      {
95          return Collections.unmodifiableList( urls );
96      }
97  }