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     */
019    package org.apache.directory.shared.ldap.schema.ldif.extractor;
020    
021    
022    import java.net.URL;
023    import java.util.ArrayList;
024    import java.util.Collections;
025    import java.util.Enumeration;
026    import 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     * @version $Rev: 664295 $ $Date: 2008-06-07 10:48:16 +0300 (Sat, 07 Jun 2008) $
035     */
036    public class UniqueResourceException extends RuntimeException
037    {
038        private static final long serialVersionUID = 1L;
039    
040        private final String resourceName;
041        private final List<URL> urls;
042        private final String resourceDescription;
043    
044        
045        public UniqueResourceException( String resourceName, String resourceDescription )
046        {
047            this( resourceName, null, resourceDescription );
048        }
049    
050        
051        public UniqueResourceException( String resourceName, List<URL> urls, String resourceDescription )
052        {
053            this.resourceName = resourceName;
054            this.urls = urls;
055            this.resourceDescription = resourceDescription;
056        }
057    
058        
059        public UniqueResourceException( String resourceName, URL first, Enumeration<URL> urlEnum, String resourceDescription )
060        {
061            this( resourceName, toList( first, urlEnum ), resourceDescription );
062        }
063    
064        
065        private static List<URL> toList( URL first, Enumeration<URL> urlEnum )
066        {
067            ArrayList<URL> urls = new ArrayList<URL>();
068            urls.add( first );
069            while( urlEnum.hasMoreElements() )
070            {
071                urls.add( urlEnum.nextElement() );
072            }
073            return urls;
074        }
075    
076        
077        public String getMessage()
078        {
079            StringBuffer buf = new StringBuffer( "Problem locating " ).append( resourceDescription ).append( "\n" );
080            if ( urls == null )
081            {
082                buf.append( "No resources named '" ).append( resourceName ).append( "' located on classpath" );
083            } else
084            {
085                buf.append( "Multiple copies of resource named '" ).append( resourceName ).append(
086                        "' located on classpath at urls" );
087                for ( URL url : urls )
088                {
089                    buf.append( "\n    " ).append( url );
090                }
091            }
092            return buf.toString();
093        }
094    
095    
096        public String getResourceName()
097        {
098            return resourceName;
099        }
100    
101        
102        public List<URL> getUrls()
103        {
104            return Collections.unmodifiableList( urls );
105        }
106    }