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.shared.ldap.schemaextractor; 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<URL>(); 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 public String getMessage() 108 { 109 StringBuffer buf = new StringBuffer( "Problem locating " ).append( resourceDescription ).append( "\n" ); 110 if ( urls == null ) 111 { 112 buf.append( "No resources named '" ).append( resourceName ).append( "' located on classpath" ); 113 } 114 else 115 { 116 buf.append( "Multiple copies of resource named '" ).append( resourceName ).append( 117 "' located on classpath at urls" ); 118 for ( URL url : urls ) 119 { 120 buf.append( "\n " ).append( url ); 121 } 122 } 123 return buf.toString(); 124 } 125 126 127 /** 128 * Gets the resource name. 129 * 130 * @return the resource name 131 */ 132 public String getResourceName() 133 { 134 return resourceName; 135 } 136 137 138 /** 139 * Gets the URLs. 140 * 141 * @return the URLs 142 */ 143 public List<URL> getUrls() 144 { 145 return Collections.unmodifiableList( urls ); 146 } 147}