@@ -0,0 +1,3355 @@
+/*-%0A * Copyright (c) 2014 Red Hat, Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage org.fedoraproject.xmvn;%0A%0Aimport java.io.IOException;%0Aimport java.lang.reflect.Method;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.concurrent.Callable;%0A%0A/**%0A * @author Mikolaj Izdebski%0A */%0Aclass IsolatedXMvnServiceLocator%0A%7B%0A    static%0A    %7B%0A        INSTANCE = new IsolatedXMvnServiceLocator();%0A    %7D%0A%0A    private static IsolatedXMvnServiceLocator INSTANCE;%0A%0A    private static Path getHome()%0A    %7B%0A        String home = System.getProperty( %22xmvn.home%22 );%0A        if ( home == null )%0A            home = System.getenv( %22XMVN_HOME%22 );%0A        if ( home == null )%0A            home = System.getenv( %22M2_HOME%22 );%0A        if ( home == null )%0A            home = %22/usr/share/xmvn%22;%0A        return Paths.get( home );%0A    %7D%0A%0A    private final IsolatedClassRealm realm;%0A%0A    public static IsolatedClassRealm getRealm()%0A    %7B%0A        return INSTANCE.realm;%0A    %7D%0A%0A    private final Method getServiceMethod;%0A%0A    private IsolatedXMvnServiceLocator()%0A    %7B%0A        try%0A        %7B%0A            realm = new IsolatedClassRealm( XMvn.class.getClassLoader() );%0A%0A            realm.addJarDirectory( getHome().resolve( %22isolated%22 ) );%0A%0A            realm.importPackage( %22org.fedoraproject.xmvn%22 );%0A            realm.importPackage( %22org.fedoraproject.xmvn.deployer%22 );%0A            realm.importPackage( %22org.fedoraproject.xmvn.resolver%22 );%0A            realm.importPackage( %22org.eclipse.aether.artifact%22 );%0A        %7D%0A        catch ( IOException e )%0A        %7B%0A            throw new RuntimeException( %22Unable to initialize XMvn class realm%22, e );%0A        %7D%0A%0A        try%0A        %7B%0A            getServiceMethod = realm.execute( new Callable%3CMethod%3E()%0A            %7B%0A                @Override%0A                public Method call()%0A                    throws ReflectiveOperationException%0A                %7B%0A                    Class%3C?%3E locatorClass = realm.loadClass( %22org.fedoraproject.xmvn.locator.XMvnServiceLocator%22 );%0A                    return locatorClass.getDeclaredMethod( %22getService%22, Class.class );%0A                %7D%0A            %7D );%0A        %7D%0A        catch ( Exception e )%0A        %7B%0A            throw new RuntimeException( %22Unable to initialize XMvn class space%22, e );%0A        %7D%0A    %7D%0A%0A    @SuppressWarnings( %22unchecked%22 )%0A    public static %3CT%3E T getService( final Class%3CT%3E role )%0A    %7B%0A        try%0A        %7B%0A            return INSTANCE.realm.execute( new Callable%3CT%3E()%0A            %7B%0A                @Override%0A                public T call()%0A                    throws ReflectiveOperationException%0A                %7B%0A                    return (T) INSTANCE.getServiceMethod.invoke( null, role );%0A                %7D%0A            %7D );%0A        %7D%0A        catch ( Exception e )%0A        %7B%0A            throw new RuntimeException( e );%0A        %7D%0A    %7D%0A%7D%0A

Implement isolated service locator