/*******************************************************************************
 * 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.service;

import org.eclipse.xtext.Constants;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.XtextPackage;
import org.eclipse.xtext.parser.BaseEPackageAccess;
import org.eclipse.xtext.resource.ClasspathUriUtil;
import org.eclipse.xtext.resource.XtextResourceSet;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

/**
 * @author Sebastian Zarnekow - Initial contribution and API
 */
@Singleton
public class GrammarProvider {

	private final String languageName;

	private volatile Grammar grammar;

	private final Provider<XtextResourceSet> resourceSetProvider;

	@Inject
	public GrammarProvider(@Named(Constants.LANGUAGE_NAME) String languageName, Provider<XtextResourceSet> resourceSetProvider) {
		this.languageName = languageName;
		this.resourceSetProvider = resourceSetProvider;
	}

	public Grammar getGrammar(Object requestor) {
		if (grammar==null) {
			XtextPackage.eINSTANCE.getClass();
			// comment shameless copied from Scopes.Singleton
			// DCL on a volatile is safe as of Java 5, which we obviously require.
			synchronized(this) {
				if (grammar == null) {
					XtextResourceSet resourceSet = resourceSetProvider.get();
					resourceSet.setClasspathURIContext(requestor == null ? getClass().getClassLoader() : requestor.getClass().getClassLoader());
					grammar = (Grammar) BaseEPackageAccess.loadGrammarFile(
							ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + languageName.replace('.', '/') + ".xmi",
							resourceSet);
				}
			}
		}
		return grammar;
	}
}

@@ -1160,16 +1160,76 @@
 rovider;
+%0A%09%0A%09@Inject(optional=true)%0A%09private ClassLoader classLoader;
 %0A%0A%09@Inje
@@ -1797,16 +1797,49 @@
 .get();%0A
+%09%09%09%09%09if (classLoader != null) %7B%0A%09
 %09%09%09%09%09res
@@ -1870,16 +1870,87 @@
 Context(
+classLoader);%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09final ClassLoader classLoaderToUse = 
 requesto
@@ -2028,18 +2028,84 @@
 Loader()
-)
 ;
+%0A%09%09%09%09%09%09resourceSet.setClasspathURIContext(classLoaderToUse);%0A%09%09%09%09%09%7D
 %0A%09%09%09%09%09gr

use an injected ClassLoader to load the xmi from classpath.