001 /* 002 * Copyright (C) 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.codehaus.gmaven.runtime.loader; 018 019 import org.codehaus.gmaven.feature.Provider; 020 import org.codehaus.gmaven.feature.ProviderLoader; 021 import org.codehaus.plexus.PlexusConstants; 022 import org.codehaus.plexus.PlexusContainer; 023 import org.codehaus.plexus.context.Context; 024 import org.codehaus.plexus.context.ContextException; 025 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; 026 import org.slf4j.Logger; 027 import org.slf4j.LoggerFactory; 028 029 import java.util.Collections; 030 import java.util.HashMap; 031 import java.util.Iterator; 032 import java.util.Map; 033 import java.util.Set; 034 035 /** 036 * Default {@link ProviderLoader}. 037 * 038 * @plexus.component role="org.codehaus.gmaven.feature.ProviderLoader" role-hint="default" 039 * 040 * @version $Id: DefaultProviderLoader.java 21 2009-07-16 09:42:35Z user57 $ 041 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 042 */ 043 public class DefaultProviderLoader 044 implements ProviderLoader, Contextualizable 045 { 046 private final Logger log = LoggerFactory.getLogger(getClass()); 047 048 private PlexusContainer container; 049 050 public void contextualize(final Context context) throws ContextException { 051 assert context != null; 052 053 container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY); 054 } 055 056 protected PlexusContainer getContainer() { 057 if (container == null) { 058 throw new IllegalStateException("Container not bound"); 059 } 060 061 return container; 062 } 063 064 public Map load(final String key) throws Exception { 065 assert key != null; 066 067 Map found = null; 068 069 Map providers = findProviders(); 070 071 if (providers == null || providers.isEmpty()) { 072 log.debug("No providers were found"); 073 } 074 else { 075 log.debug("Looking for provider {} in {}", key, providers); 076 077 Provider provider = (Provider) providers.get(key); 078 079 if (provider != null) { 080 found = Collections.singletonMap(key, provider); 081 } 082 } 083 084 return found; 085 } 086 087 /** 088 * Find any providers which are available in the container. 089 */ 090 private Map findProviders() { 091 Map providers = getContainer().getComponentDescriptorMap(Provider.class.getName()); 092 if (providers == null) { 093 throw new Error("No providers discovered"); 094 } 095 096 Set keys = providers.keySet(); 097 Map found = null; 098 099 for (Iterator iter = keys.iterator(); iter.hasNext();) { 100 String key = (String)iter.next(); 101 102 Provider provider; 103 104 try { 105 provider = (Provider) getContainer().lookup(Provider.class.getName(), key); 106 } 107 catch (Exception e) { 108 log.warn("Failed to lookup provider for key: {}", key, e); 109 continue; 110 } 111 112 if (provider != null) { 113 if (found == null) { 114 found = new HashMap(); 115 } 116 117 found.put(key, provider); 118 } 119 } 120 121 return found; 122 } 123 }