Package org.jboss.forge.arquillian.maven

Source Code of org.jboss.forge.arquillian.maven.PlexusContainer

/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.arquillian.maven;

import java.util.concurrent.Callable;

import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.DefaultContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
import org.jboss.forge.furnace.util.ClassLoaders;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
class PlexusContainer
{
   private static org.codehaus.plexus.PlexusContainer plexusContainer;

   public <T> T lookup(final Class<T> type)
   {
      try
      {
         return ClassLoaders.executeIn(Thread.currentThread().getContextClassLoader(), new Callable<T>()
         {
            @Override
            public T call() throws Exception
            {
               return getPlexusContainer().lookup(type);
            }
         });
      }
      catch (Exception e)
      {
         throw new RuntimeException("Could not look up component of type [" + type.getName() + "]", e);
      }
   }

   public void shutdown()
   {
      if (plexusContainer != null)
      {
         plexusContainer.dispose();
         plexusContainer = null;
      }
   }

   private org.codehaus.plexus.PlexusContainer getPlexusContainer() throws Exception
   {
      if (plexusContainer == null)
      {
         plexusContainer = ClassLoaders.executeIn(Thread.currentThread().getContextClassLoader(),
                  new Callable<DefaultPlexusContainer>()
                  {

                     @Override
                     public DefaultPlexusContainer call() throws Exception
                     {
                        try
                        {
                           ContainerConfiguration config = new DefaultContainerConfiguration().setAutoWiring(true);
                           plexusContainer = new DefaultPlexusContainer(config);
                           ConsoleLoggerManager loggerManager = new ConsoleLoggerManager();
                           loggerManager.setThreshold("ERROR");
                           ((DefaultPlexusContainer) plexusContainer).setLoggerManager(loggerManager);
                           return (DefaultPlexusContainer) plexusContainer;
                        }
                        catch (Exception e)
                        {
                           throw new RuntimeException(
                                    "Could not initialize Maven", e);
                        }
                     }
                  });
      }
      return plexusContainer;
   }

}
TOP

Related Classes of org.jboss.forge.arquillian.maven.PlexusContainer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.