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