1 package com.atlassian.sal.core.component;
2
3 import java.util.Collection;
4 import java.util.Map;
5
6 import org.apache.log4j.Logger;
7
8 import com.atlassian.sal.api.component.ComponentLocator;
9 import com.atlassian.sal.spi.HostContextAccessor;
10
11 public class DefaultComponentLocator extends ComponentLocator
12 {
13 private final HostContextAccessor hostContextAccessor;
14 private static final Logger log = Logger.getLogger(DefaultComponentLocator.class);
15
16 public DefaultComponentLocator(final HostContextAccessor accessor)
17 {
18 this.hostContextAccessor = accessor;
19 ComponentLocator.setComponentLocator(this);
20 }
21
22 @Override
23 protected <T> T getComponentInternal(final Class<T> iface)
24 {
25 final Map<String, T> beansOfType = hostContextAccessor.getComponentsOfType(iface);
26
27 if (beansOfType == null || beansOfType.isEmpty())
28 {
29 return null;
30 } else if (beansOfType.size()>1)
31 {
32
33 final String shortClassName = convertClassToName(iface);
34 final T implementation = beansOfType.get(shortClassName);
35 if (implementation == null)
36 {
37 log.warn("More than one instance of " + iface.getName() + " found but none of them has key " + shortClassName);
38 }
39 return implementation;
40 }
41 return beansOfType.values().iterator().next();
42 }
43
44 @Override
45 protected <T> T getComponentInternal(final Class<T> iface, final String componentId)
46 {
47 final Map<String, T> beansOfType = hostContextAccessor.getComponentsOfType(iface);
48
49 return beansOfType.get(componentId);
50 }
51
52 @Override
53 protected <T> Collection<T> getComponentsInternal(final Class<T> iface)
54 {
55 final Map<String, T> beansOfType = hostContextAccessor.getComponentsOfType(iface);
56 return (beansOfType != null ? beansOfType.values() : null);
57 }
58 }