001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *  http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.directory.api.ldap.schema.extractor;
020
021
022import java.net.URL;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.Enumeration;
026import java.util.List;
027
028
029/**
030 * Exception for when we detect more than one unqiue schema LDIF file resource
031 * on the classpath.
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class UniqueResourceException extends RuntimeException
036{
037
038    /** The serialVersionUID. */
039    private static final long serialVersionUID = 1L;
040
041    /** The resource name. */
042    private final String resourceName;
043
044    /** The urls. */
045    private final List<URL> urls;
046
047    /** The resource description. */
048    private final String resourceDescription;
049
050
051    /**
052     * Instantiates a new unique resource exception.
053     *
054     * @param resourceName the resource name
055     * @param resourceDescription the resource description
056     */
057    public UniqueResourceException( String resourceName, String resourceDescription )
058    {
059        this( resourceName, null, resourceDescription );
060    }
061
062
063    /**
064     * Instantiates a new unique resource exception.
065     *
066     * @param resourceName the resource name
067     * @param urls the URLs
068     * @param resourceDescription the resource description
069     */
070    public UniqueResourceException( String resourceName, List<URL> urls, String resourceDescription )
071    {
072        this.resourceName = resourceName;
073        this.urls = urls;
074        this.resourceDescription = resourceDescription;
075    }
076
077
078    /**
079     * Instantiates a new unique resource exception.
080     *
081     * @param resourceName the resource name
082     * @param first the first
083     * @param urlEnum the enum with URLs
084     * @param resourceDescription the resource description
085     */
086    public UniqueResourceException( String resourceName, URL first, Enumeration<URL> urlEnum, String resourceDescription )
087    {
088        this( resourceName, toList( first, urlEnum ), resourceDescription );
089    }
090
091
092    private static List<URL> toList( URL first, Enumeration<URL> urlEnum )
093    {
094        ArrayList<URL> urls = new ArrayList<>();
095        urls.add( first );
096        while ( urlEnum.hasMoreElements() )
097        {
098            urls.add( urlEnum.nextElement() );
099        }
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}