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  package org.apache.directory.api.ldap.schema.extractor;
20  
21  
22  import java.net.URL;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.List;
27  
28  
29  /**
30   * Exception for when we detect more than one unqiue schema LDIF file resource
31   * on the classpath.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class UniqueResourceException extends RuntimeException
36  {
37  
38      /** The serialVersionUID. */
39      private static final long serialVersionUID = 1L;
40  
41      /** The resource name. */
42      private final String resourceName;
43  
44      /** The urls. */
45      private final List<URL> urls;
46  
47      /** The resource description. */
48      private final String resourceDescription;
49  
50  
51      /**
52       * Instantiates a new unique resource exception.
53       *
54       * @param resourceName the resource name
55       * @param resourceDescription the resource description
56       */
57      public UniqueResourceException( String resourceName, String resourceDescription )
58      {
59          this( resourceName, null, resourceDescription );
60      }
61  
62  
63      /**
64       * Instantiates a new unique resource exception.
65       *
66       * @param resourceName the resource name
67       * @param urls the URLs
68       * @param resourceDescription the resource description
69       */
70      public UniqueResourceException( String resourceName, List<URL> urls, String resourceDescription )
71      {
72          this.resourceName = resourceName;
73          this.urls = urls;
74          this.resourceDescription = resourceDescription;
75      }
76  
77  
78      /**
79       * Instantiates a new unique resource exception.
80       *
81       * @param resourceName the resource name
82       * @param first the first
83       * @param urlEnum the enum with URLs
84       * @param resourceDescription the resource description
85       */
86      public UniqueResourceException( String resourceName, URL first, Enumeration<URL> urlEnum, String resourceDescription )
87      {
88          this( resourceName, toList( first, urlEnum ), resourceDescription );
89      }
90  
91  
92      private static List<URL> toList( URL first, Enumeration<URL> urlEnum )
93      {
94          ArrayList<URL> urls = new ArrayList<>();
95          urls.add( first );
96          while ( urlEnum.hasMoreElements() )
97          {
98              urls.add( urlEnum.nextElement() );
99          }
100         return urls;
101     }
102 
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public String getMessage()
109     {
110         StringBuilder buf = new StringBuilder( "Problem locating " ).append( resourceDescription ).append( "\n" );
111         
112         if ( urls == null )
113         {
114             buf.append( "No resources named '" ).append( resourceName ).append( "' located on classpath" );
115         }
116         else
117         {
118             buf.append( "Multiple copies of resource named '" ).append( resourceName ).append(
119                 "' located on classpath at urls" );
120             
121             for ( URL url : urls )
122             {
123                 buf.append( "\n    " ).append( url );
124             }
125         }
126         
127         return buf.toString();
128     }
129 
130 
131     /**
132      * Gets the resource name.
133      *
134      * @return the resource name
135      */
136     public String getResourceName()
137     {
138         return resourceName;
139     }
140 
141 
142     /**
143      * Gets the URLs.
144      *
145      * @return the URLs
146      */
147     public List<URL> getUrls()
148     {
149         return Collections.unmodifiableList( urls );
150     }
151 }