aboutsummaryrefslogtreecommitdiffstats
path: root/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java
blob: 0de63185eac03b849faca9ae91fca3253363c501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright 2012 Decebal Suiu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
 * the License. You may obtain a copy of the License in the LICENSE file, or 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.
 */
package ro.fortsoft.pf4j;

import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.java.sezpoz.Index;
import net.java.sezpoz.IndexItem;

/**
 * Using Sezpoz(http://sezpoz.java.net/) for extensions discovery.
 * 
 * @author Decebal Suiu
 */
public class DefaultExtensionFinder implements ExtensionFinder {

	private static final Logger log = LoggerFactory.getLogger(DefaultExtensionFinder.class);
	
	private volatile List<IndexItem<Extension, Object>> indices;
	private ClassLoader classLoader;
	
	public DefaultExtensionFinder(ClassLoader classLoader) {
		this.classLoader = classLoader;
	}
	
	@Override
	public <T> List<ExtensionWrapper<T>> find(Class<T> type) {
		log.debug("Find extensions for {}", type);
        List<ExtensionWrapper<T>> result = new ArrayList<ExtensionWrapper<T>>();
		getIndices();
//		System.out.println("indices =  "+ indices);
		for (IndexItem<Extension, Object> item : indices) {
            try {
            	AnnotatedElement element = item.element();
            	Class<?> extensionType = (Class<?>) element;
            	log.debug("Checking extension type {}", extensionType);
            	if (type.isAssignableFrom(extensionType)) {
                    Object instance = item.instance();
                    if (instance != null) {
                		log.debug("Added extension {}", extensionType);
						result.add(new ExtensionWrapper<T>(type.cast(instance), item.annotation().ordinal()));
                    }
                }
            } catch (InstantiationException e) {
            	log.error(e.getMessage(), e);
			}
		}
		
		return result;
	}

	 private List<IndexItem<Extension, Object>> getIndices() {
         if (indices == null) {
             indices = new ArrayList<IndexItem<Extension, Object>>(); 
             Iterator<IndexItem<Extension, Object>> it = Index.load(Extension.class, Object.class, classLoader).iterator();
             while (it.hasNext()) {
            	 indices.add(it.next());
             }
         }
         
         return indices;
     }
	 
}