/*******************************************************************************
 * Copyright (c) 2009 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.xtext.internal;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.resource.IResourceServiceProvider;

/**
 * @author Sven Efftinge - Initial contribution and API
 */
public class ResourceServiceProviderDescriptor implements IResourceServiceProvider.Provider {

	private final static Logger log = Logger.getLogger(ResourceServiceProviderDescriptor.class);

	private IConfigurationElement element;
	private String attClass;
	private Object extension;

	public ResourceServiceProviderDescriptor(IConfigurationElement element, String attClass) {
		this.element = element;
		this.attClass = attClass;
	}

	public IConfigurationElement getElement() {
		return element;
	}

	public synchronized IResourceServiceProvider get(URI uri, String contentType) {
		if (this.extension == null) {
			try {
				this.extension = element.createExecutableExtension(attClass);
			} catch (CoreException e) {
				log.error(e.getMessage(), e);
			}
		}
		if (this.extension instanceof IResourceServiceProvider.Provider) {
			IResourceServiceProvider.Provider provider = ((IResourceServiceProvider.Provider) extension);
			IResourceServiceProvider result = provider.get(uri, contentType);
			return result;
		} else if (this.extension instanceof IResourceServiceProvider) {
			return (IResourceServiceProvider) this.extension;
		}
		String type = this.extension == null ? "null" : this.extension.getClass().getName();
		String valid = IResourceServiceProvider.class + " or " + IResourceServiceProvider.Provider.class;
		throw new ClassCastException("The type " + type + " is not a valid " + valid);
	}

}

@@ -2132,16 +2132,252 @@
 .class;%0A
+%09%09String suffix = %22%22;%0A%09%09if (element != null && element.getDeclaringExtension() != null && element.getDeclaringExtension().getContributor() != null) %7B%0A%09%09%09suffix += %22 contributed by %22+element.getDeclaringExtension().getContributor();%0A%09%09%7D%0A
 %09%09throw 
@@ -2446,16 +2446,25 @@
  + valid
+ + suffix
 );%0A%09%7D%0A%0A%7D

put the contributing plugin into the error message for problematic extensions of ResourceServiceProviders.