Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | package org.apache.felix.scrplugin.helper; |
| 20 | |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Collections; |
| 24 | import java.util.Comparator; |
| 25 | import java.util.HashMap; |
| 26 | import java.util.Iterator; |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import javax.imageio.spi.ServiceRegistry; |
| 31 | |
Carsten Ziegeler | 15e7368 | 2012-06-17 13:17:53 +0000 | [diff] [blame] | 32 | import org.apache.felix.scrplugin.Log; |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 33 | import org.apache.felix.scrplugin.SCRDescriptorException; |
| 34 | import org.apache.felix.scrplugin.SCRDescriptorFailureException; |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 35 | import org.apache.felix.scrplugin.annotations.AnnotationProcessor; |
| 36 | import org.apache.felix.scrplugin.annotations.ScannedClass; |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 37 | import org.apache.felix.scrplugin.description.ClassDescription; |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | /** |
| 41 | * Supports mapping of built-in and custom java annotations to |
| 42 | * descriptions. |
| 43 | */ |
| 44 | public class AnnotationProcessorManager implements AnnotationProcessor { |
| 45 | |
| 46 | /** |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 47 | * Ordered list of processors |
| 48 | */ |
Carsten Ziegeler | 561e4e2 | 2012-06-27 06:38:28 +0000 | [diff] [blame] | 49 | private final List<AnnotationProcessor> processors = new ArrayList<AnnotationProcessor>(); |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
Carsten Ziegeler | 15e7368 | 2012-06-17 13:17:53 +0000 | [diff] [blame] | 52 | * Create annotation processor manager. |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 53 | * @throws SCRDescriptorFailureException |
| 54 | */ |
Carsten Ziegeler | 15e7368 | 2012-06-17 13:17:53 +0000 | [diff] [blame] | 55 | public AnnotationProcessorManager(final Log log, |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 56 | final ClassLoader classLoader ) |
| 57 | throws SCRDescriptorFailureException { |
| 58 | // search for providers |
Carsten Ziegeler | 561e4e2 | 2012-06-27 06:38:28 +0000 | [diff] [blame] | 59 | final Map<String, AnnotationProcessor> processorMap = new HashMap<String, AnnotationProcessor>(); |
| 60 | |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 61 | final Iterator<AnnotationProcessor> serviceIter = ServiceRegistry.lookupProviders(AnnotationProcessor.class, classLoader); |
| 62 | while ( serviceIter.hasNext() ) { |
Carsten Ziegeler | 561e4e2 | 2012-06-27 06:38:28 +0000 | [diff] [blame] | 63 | 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 Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Carsten Ziegeler | 561e4e2 | 2012-06-27 06:38:28 +0000 | [diff] [blame] | 71 | // create ordered list sorted by ranking |
| 72 | for(final AnnotationProcessor pro : processorMap.values() ) { |
| 73 | this.processors.add(pro); |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 74 | } |
Carsten Ziegeler | 561e4e2 | 2012-06-27 06:38:28 +0000 | [diff] [blame] | 75 | Collections.sort(this.processors, new Comparator<AnnotationProcessor>() { |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 76 | |
| 77 | public int compare(AnnotationProcessor o1, AnnotationProcessor o2) { |
| 78 | return Integer.valueOf(o1.getRanking()).compareTo(Integer.valueOf(o2.getRanking())); |
| 79 | } |
| 80 | }); |
Carsten Ziegeler | 561e4e2 | 2012-06-27 06:38:28 +0000 | [diff] [blame] | 81 | if ( this.processors.size() == 0 ) { |
Carsten Ziegeler | 15e7368 | 2012-06-17 13:17:53 +0000 | [diff] [blame] | 82 | throw new SCRDescriptorFailureException("No annotation processors found in classpath."); |
| 83 | } |
Carsten Ziegeler | 561e4e2 | 2012-06-27 06:38:28 +0000 | [diff] [blame] | 84 | log.debug("..using annotation processors: "); |
| 85 | for(final AnnotationProcessor pro : this.processors) { |
| 86 | log.debug(" - " + pro.getName() + " - " + pro.getRanking()); |
Carsten Ziegeler | 15e7368 | 2012-06-17 13:17:53 +0000 | [diff] [blame] | 87 | } |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 91 | * @see org.apache.felix.scrplugin.annotations.AnnotationProcessor#process(org.apache.felix.scrplugin.annotations.ScannedClass, org.apache.felix.scrplugin.description.ClassDescription) |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 92 | */ |
| 93 | public void process(final ScannedClass scannedClass, |
| 94 | final ClassDescription describedClass) |
| 95 | throws SCRDescriptorException, SCRDescriptorFailureException { |
Carsten Ziegeler | 561e4e2 | 2012-06-27 06:38:28 +0000 | [diff] [blame] | 96 | // forward do all processors |
| 97 | for(final AnnotationProcessor ap : this.processors) { |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 98 | ap.process(scannedClass, describedClass); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 103 | * @see org.apache.felix.scrplugin.annotations.AnnotationProcessor#getRanking() |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 104 | */ |
| 105 | public int getRanking() { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /** |
Carsten Ziegeler | 15e7368 | 2012-06-17 13:17:53 +0000 | [diff] [blame] | 110 | * @see org.apache.felix.scrplugin.annotations.AnnotationProcessor#getName() |
| 111 | */ |
| 112 | public String getName() { |
| 113 | return "Annotation Processor Manager"; |
| 114 | } |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 115 | } |