blob: b63a112a74a0e1d3022ce7bcd5983731622b9f00 [file] [log] [blame]
Carsten Ziegeler55c96d32012-06-13 12:03:35 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.scrplugin.helper;
20
21
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.Iterator;
27import java.util.List;
28import java.util.Map;
29
30import javax.imageio.spi.ServiceRegistry;
31
Carsten Ziegeler15e73682012-06-17 13:17:53 +000032import org.apache.felix.scrplugin.Log;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000033import org.apache.felix.scrplugin.SCRDescriptorException;
34import org.apache.felix.scrplugin.SCRDescriptorFailureException;
Carsten Ziegeler9452bb22012-06-13 13:08:05 +000035import org.apache.felix.scrplugin.annotations.AnnotationProcessor;
36import org.apache.felix.scrplugin.annotations.ScannedClass;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000037import org.apache.felix.scrplugin.description.ClassDescription;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000038
39
40/**
41 * Supports mapping of built-in and custom java annotations to
42 * descriptions.
43 */
44public class AnnotationProcessorManager implements AnnotationProcessor {
45
46 /**
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000047 * Ordered list of processors
48 */
Carsten Ziegeler561e4e22012-06-27 06:38:28 +000049 private final List<AnnotationProcessor> processors = new ArrayList<AnnotationProcessor>();
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000050
51 /**
Carsten Ziegeler15e73682012-06-17 13:17:53 +000052 * Create annotation processor manager.
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000053 * @throws SCRDescriptorFailureException
54 */
Carsten Ziegeler15e73682012-06-17 13:17:53 +000055 public AnnotationProcessorManager(final Log log,
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000056 final ClassLoader classLoader )
57 throws SCRDescriptorFailureException {
58 // search for providers
Carsten Ziegeler561e4e22012-06-27 06:38:28 +000059 final Map<String, AnnotationProcessor> processorMap = new HashMap<String, AnnotationProcessor>();
60
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000061 final Iterator<AnnotationProcessor> serviceIter = ServiceRegistry.lookupProviders(AnnotationProcessor.class, classLoader);
62 while ( serviceIter.hasNext() ) {
Carsten Ziegeler561e4e22012-06-27 06:38:28 +000063 final AnnotationProcessor processor = serviceIter.next();
64 // check if this processor is already loaded
65 final String key = processor.getClass().getName();
66 if ( !processorMap.containsKey(key) ) {
67 processorMap.put(key, processor);
68 }
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000069 }
70
Carsten Ziegeler561e4e22012-06-27 06:38:28 +000071 // create ordered list sorted by ranking
72 for(final AnnotationProcessor pro : processorMap.values() ) {
73 this.processors.add(pro);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000074 }
Carsten Ziegeler561e4e22012-06-27 06:38:28 +000075 Collections.sort(this.processors, new Comparator<AnnotationProcessor>() {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000076
77 public int compare(AnnotationProcessor o1, AnnotationProcessor o2) {
78 return Integer.valueOf(o1.getRanking()).compareTo(Integer.valueOf(o2.getRanking()));
79 }
80 });
Carsten Ziegeler561e4e22012-06-27 06:38:28 +000081 if ( this.processors.size() == 0 ) {
Carsten Ziegeler15e73682012-06-17 13:17:53 +000082 throw new SCRDescriptorFailureException("No annotation processors found in classpath.");
83 }
Carsten Ziegeler561e4e22012-06-27 06:38:28 +000084 log.debug("..using annotation processors: ");
85 for(final AnnotationProcessor pro : this.processors) {
86 log.debug(" - " + pro.getName() + " - " + pro.getRanking());
Carsten Ziegeler15e73682012-06-17 13:17:53 +000087 }
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000088 }
89
90 /**
Carsten Ziegeler9452bb22012-06-13 13:08:05 +000091 * @see org.apache.felix.scrplugin.annotations.AnnotationProcessor#process(org.apache.felix.scrplugin.annotations.ScannedClass, org.apache.felix.scrplugin.description.ClassDescription)
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000092 */
93 public void process(final ScannedClass scannedClass,
94 final ClassDescription describedClass)
95 throws SCRDescriptorException, SCRDescriptorFailureException {
Carsten Ziegeler561e4e22012-06-27 06:38:28 +000096 // forward do all processors
97 for(final AnnotationProcessor ap : this.processors) {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000098 ap.process(scannedClass, describedClass);
99 }
100 }
101
102 /**
Carsten Ziegeler9452bb22012-06-13 13:08:05 +0000103 * @see org.apache.felix.scrplugin.annotations.AnnotationProcessor#getRanking()
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000104 */
105 public int getRanking() {
106 return 0;
107 }
108
109 /**
Carsten Ziegeler15e73682012-06-17 13:17:53 +0000110 * @see org.apache.felix.scrplugin.annotations.AnnotationProcessor#getName()
111 */
112 public String getName() {
113 return "Annotation Processor Manager";
114 }
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000115}