Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.maven.classrealm;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
Manages the class realms used by Maven. Warning: This is an internal utility class that is only public for technical reasons, it is not part of the public API. In particular, this class can be changed or deleted without prior notice.

Author(s):
Benjamin Bentmann
 
 @Component( role = ClassRealmManager.class )
     implements ClassRealmManager
 {
 
     @Requirement
     private Logger logger;
 
     @Requirement
     protected PlexusContainer container;
 
     private ClassRealm mavenRealm;
 
     private ClassWorld getClassWorld()
     {
         return ( (MutablePlexusContainer ).getClassWorld();
     }
 
     private ClassRealm newRealmString id )
     {
         ClassWorld world = getClassWorld();
 
         synchronized ( world )
         {
             String realmId = id;
 
             Random random = new Random();
 
             while ( true )
             {
                 try
                 {
                     ClassRealm classRealm = world.newRealmrealmIdnull );
 
                     if ( .isDebugEnabled() )
                     {
                         .debug"Created new class realm " + realmId );
                     }
 
                     return classRealm;
                 }
                 catch ( DuplicateRealmException e )
                 {
                    realmId = id + '-' + random.nextInt();
                }
            }
        }
    }
    public synchronized ClassRealm getMavenApiRealm()
    {
        if (  == null )
        {
             = newRealm"maven.api" );
            List<ClassRealmConstituentconstituents = new ArrayList<ClassRealmConstituent>();
            List<StringparentImports = new ArrayList<String>();
            Map<StringClassLoaderforeignImports = new HashMap<StringClassLoader>();
            importMavenApiforeignImports );
            callDelegates..getParentClassLoader(), parentImports,
                           foreignImportsconstituents );
            wireRealmparentImportsforeignImports );
            populateRealmconstituents );
        }
        return ;
    }
    private void importMavenApiMap<StringClassLoaderimports )
    {
        ClassRealm coreRealm = getCoreRealm();
        // maven-*
        imports.put"org.apache.maven.*"coreRealm );
        imports.put"org.apache.maven.artifact"coreRealm );
        imports.put"org.apache.maven.classrealm"coreRealm );
        imports.put"org.apache.maven.cli"coreRealm );
        imports.put"org.apache.maven.configuration"coreRealm );
        imports.put"org.apache.maven.exception"coreRealm );
        imports.put"org.apache.maven.execution"coreRealm );
        imports.put"org.apache.maven.lifecycle"coreRealm );
        imports.put"org.apache.maven.model"coreRealm );
        imports.put"org.apache.maven.monitor"coreRealm );
        imports.put"org.apache.maven.plugin"coreRealm );
        imports.put"org.apache.maven.profiles"coreRealm );
        imports.put"org.apache.maven.project"coreRealm );
        imports.put"org.apache.maven.reporting"coreRealm );
        imports.put"org.apache.maven.repository"coreRealm );
        imports.put"org.apache.maven.rtinfo"coreRealm );
        imports.put"org.apache.maven.settings"coreRealm );
        imports.put"org.apache.maven.toolchain"coreRealm );
        imports.put"org.apache.maven.usability"coreRealm );
        // wagon-api
        imports.put"org.apache.maven.wagon.*"coreRealm );
        imports.put"org.apache.maven.wagon.authentication"coreRealm );
        imports.put"org.apache.maven.wagon.authorization"coreRealm );
        imports.put"org.apache.maven.wagon.events"coreRealm );
        imports.put"org.apache.maven.wagon.observers"coreRealm );
        imports.put"org.apache.maven.wagon.proxy"coreRealm );
        imports.put"org.apache.maven.wagon.repository"coreRealm );
        imports.put"org.apache.maven.wagon.resource"coreRealm );
        // aether-api, aether-spi, aether-impl
        imports.put"org.eclipse.aether.*"coreRealm );
        imports.put"org.eclipse.aether.artifact"coreRealm );
        imports.put"org.eclipse.aether.collection"coreRealm );
        imports.put"org.eclipse.aether.deployment"coreRealm );
        imports.put"org.eclipse.aether.graph"coreRealm );
        imports.put"org.eclipse.aether.impl"coreRealm );
        imports.put"org.eclipse.aether.installation"coreRealm );
        imports.put"org.eclipse.aether.metadata"coreRealm );
        imports.put"org.eclipse.aether.repository"coreRealm );
        imports.put"org.eclipse.aether.resolution"coreRealm );
        imports.put"org.eclipse.aether.spi"coreRealm );
        imports.put"org.eclipse.aether.transfer"coreRealm );
        imports.put"org.eclipse.aether.version"coreRealm );
        // plexus-classworlds
        imports.put"org.codehaus.plexus.classworlds"coreRealm );
        // classworlds (for legacy code)
        imports.put"org.codehaus.classworlds"coreRealm );
        // plexus-utils (for DOM-type fields in maven-model)
        imports.put"org.codehaus.plexus.util.xml.Xpp3Dom"coreRealm );
        imports.put"org.codehaus.plexus.util.xml.pull.XmlPullParser"coreRealm );
        imports.put"org.codehaus.plexus.util.xml.pull.XmlPullParserException"coreRealm );
        imports.put"org.codehaus.plexus.util.xml.pull.XmlSerializer"coreRealm );
        // plexus-container, plexus-component-annotations
        imports.put"org.codehaus.plexus.*"coreRealm );
        imports.put"org.codehaus.plexus.component"coreRealm );
        imports.put"org.codehaus.plexus.configuration"coreRealm );
        imports.put"org.codehaus.plexus.container"coreRealm );
        imports.put"org.codehaus.plexus.context"coreRealm );
        imports.put"org.codehaus.plexus.lifecycle"coreRealm );
        imports.put"org.codehaus.plexus.logging"coreRealm );
        imports.put"org.codehaus.plexus.personality"coreRealm );
        // javax.inject (JSR-330)
        imports.put"javax.inject.*"coreRealm );
        imports.put"javax.enterprise.inject.*"coreRealm );
        // com.google
        //
        // We may potentially want to export these, but right now I'm not sure that anything Guice specific needs
        // to be made available to plugin authors. If we find people are getting fancy and want to take advantage
        // of Guice specifics we can expose that later. Really some testing needs to be done to see full hiding
        // of Guice has any impact on what we may categorize as a standard JSR-330 based Tesla/Maven plugin.
        //
        // imports.put( "com.google.inject.*", coreRealm );
        // imports.put( "com.google.inject.binder.*", coreRealm );
        // imports.put( "com.google.inject.matcher.*", coreRealm );
        // imports.put( "com.google.inject.name.*", coreRealm );
        // imports.put( "com.google.inject.spi.*", coreRealm );
        // imports.put( "com.google.inject.util.*", coreRealm );
        // SLF4J
        imports.put"org.slf4j.*"coreRealm );
    }

    
Creates a new class realm with the specified parent and imports.

Parameters:
baseRealmId The base id to use for the new realm, must not be null.
type The type of the class realm, must not be null.
parent The parent realm for the new realm, may be null.
parentImports The packages/types to import from the parent realm, may be null.
foreignImports The packages/types to import from foreign realms, may be null.
artifacts The artifacts to add to the realm, may be null. Unresolved artifacts (i.e. with a missing file) will automatically be excluded from the realm.
Returns:
The created class realm, never null.
    private ClassRealm createRealmString baseRealmIdRealmType typeClassLoader parentList<StringparentImports,
                                    Map<StringClassLoaderforeignImportsList<Artifactartifacts )
    {
        Set<StringartifactIds = new LinkedHashSet<String>();
        List<ClassRealmConstituentconstituents = new ArrayList<ClassRealmConstituent>();
        if ( artifacts != null )
        {
            for ( Artifact artifact : artifacts )
            {
                artifactIds.addgetIdartifact ) );
                if ( artifact.getFile() != null )
                {
                    constituents.addnew ArtifactClassRealmConstituentartifact ) );
                }
            }
        }
        if ( parentImports != null )
        {
            parentImports = new ArrayList<String>( parentImports );
        }
        else
        {
            parentImports = new ArrayList<String>();
        }
        if ( foreignImports != null )
        {
            foreignImports = new TreeMap<StringClassLoader>( foreignImports );
        }
        else
        {
            foreignImports = new TreeMap<StringClassLoader>();
        }
        ClassRealm classRealm = newRealmbaseRealmId );
        if ( parent != null )
        {
            classRealm.setParentClassLoaderparent );
        }
        callDelegatesclassRealmtypeparentparentImportsforeignImportsconstituents );
        wireRealmclassRealmparentImportsforeignImports );
        Set<StringincludedIds = populateRealmclassRealmconstituents );
        if ( .isDebugEnabled() )
        {
            artifactIds.removeAllincludedIds );
            for ( String id : artifactIds )
            {
                .debug"  Excluded: " + id );
            }
        }
        return classRealm;
    }
    public ClassRealm getCoreRealm()
    {
        return .getContainerRealm();
    }
    public ClassRealm createProjectRealmModel modelList<Artifactartifacts )
    {
        if ( model == null )
        {
            throw new IllegalArgumentException"model missing" );
        }
        ClassLoader parent = getMavenApiRealm();
        return createRealmgetKeymodel ), .parentnullnullartifacts );
    }
    private static String getKeyModel model )
    {
        return "project>" + model.getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion();
    }
    public ClassRealm createExtensionRealmPlugin pluginList<Artifactartifacts )
    {
        if ( plugin == null )
        {
            throw new IllegalArgumentException"extension plugin missing" );
        }
        ClassLoader parent = ClassLoader.getSystemClassLoader();
        Map<StringClassLoaderforeignImports =
            Collections.<StringClassLoadersingletonMap""getMavenApiRealm() );
        return createRealmgetKeyplugintrue ), .parentnullforeignImportsartifacts );
    }
    public ClassRealm createPluginRealmPlugin pluginClassLoader parentList<StringparentImports,
                                         Map<StringClassLoaderforeignImportsList<Artifactartifacts )
    {
        if ( plugin == null )
        {
            throw new IllegalArgumentException"plugin missing" );
        }
        if ( parent == null )
        {
            parent = ClassLoader.getSystemClassLoader();
        }
        return createRealmgetKeypluginfalse ), .parentparentImportsforeignImportsartifacts );
    }
    private static String getKeyPlugin pluginboolean extension )
    {
        String version = ArtifactUtils.toSnapshotVersionplugin.getVersion() );
        return ( extension ? "extension>" : "plugin>" ) + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":"
            + version;
    }
    private static String getIdArtifact artifact )
    {
        return getIdartifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
                      artifact.getClassifier(), artifact.getBaseVersion() );
    }
    private static String getIdClassRealmConstituent constituent )
    {
        return getIdconstituent.getGroupId(), constituent.getArtifactId(), constituent.getType(),
                      constituent.getClassifier(), constituent.getVersion() );
    }
    private static String getIdString gidString aidString typeString clsString ver )
    {
        return gid + ':' + aid + ':' + type + ( StringUtils.isNotEmptycls ) ? ':' + cls : "" ) + ':' + ver;
    }
    {
        try
        {
            return .lookupListClassRealmManagerDelegate.class );
        }
        catch ( ComponentLookupException e )
        {
            .error"Failed to lookup class realm delegates: " + e.getMessage(), e );
            return Collections.emptyList();
        }
    }
    private void callDelegatesClassRealm classRealmRealmType typeClassLoader parentList<StringparentImports,
                                Map<StringClassLoaderforeignImportsList<ClassRealmConstituentconstituents )
    {
        List<ClassRealmManagerDelegatedelegates = getDelegates();
        if ( !delegates.isEmpty() )
        {
            ClassRealmRequest request =
                new DefaultClassRealmRequesttypeparentparentImportsforeignImportsconstituents );
            for ( ClassRealmManagerDelegate delegate : delegates )
            {
                try
                {
                    delegate.setupRealmclassRealmrequest );
                }
                catch ( Exception e )
                {
                    .errordelegate.getClass().getName() + " failed to setup class realm " + classRealm + ": "
                        + e.getMessage(), e );
                }
            }
        }
    }
    private Set<StringpopulateRealmClassRealm classRealmList<ClassRealmConstituentconstituents )
    {
        Set<StringincludedIds = new LinkedHashSet<String>();
        if ( .isDebugEnabled() )
        {
            .debug"Populating class realm " + classRealm.getId() );
        }
        for ( ClassRealmConstituent constituent : constituents )
        {
            File file = constituent.getFile();
            String id = getIdconstituent );
            includedIds.addid );
            if ( .isDebugEnabled() )
            {
                .debug"  Included: " + id );
            }
            try
            {
                classRealm.addURLfile.toURI().toURL() );
            }
            catch ( MalformedURLException e )
            {
                // Not going to happen
                .errore.getMessage(), e );
            }
        }
        return includedIds;
    }
    private void wireRealmClassRealm classRealmList<StringparentImportsMap<StringClassLoaderforeignImports )
    {
        if ( foreignImports != null && !foreignImports.isEmpty() )
        {
            if ( .isDebugEnabled() )
            {
                .debug"Importing foreign packages into class realm " + classRealm.getId() );
            }
            for ( Map.Entry<StringClassLoaderentry : foreignImports.entrySet() )
            {
                ClassLoader importedRealm = entry.getValue();
                String imp = entry.getKey();
                if ( .isDebugEnabled() )
                {
                    .debug"  Imported: " + imp + " < " + getIdimportedRealm ) );
                }
                classRealm.importFromimportedRealmimp );
            }
        }
        if ( parentImports != null && !parentImports.isEmpty() )
        {
            if ( .isDebugEnabled() )
            {
                .debug"Importing parent packages into class realm " + classRealm.getId() );
            }
            for ( String imp : parentImports )
            {
                if ( .isDebugEnabled() )
                {
                    .debug"  Imported: " + imp + " < " + getIdclassRealm.getParentClassLoader() ) );
                }
                classRealm.importFromParentimp );
            }
        }
    }
    private String getIdClassLoader classLoader )
    {
        if ( classLoader instanceof ClassRealm )
        {
            return ( (ClassRealmclassLoader ).getId();
        }
        return String.valueOfclassLoader );
    }
New to GrepCode? Check out our FAQ X