Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +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.bundleplugin; |
| 20 | |
| 21 | |
| 22 | import java.io.BufferedReader; |
| 23 | import java.io.ByteArrayInputStream; |
| 24 | import java.io.ByteArrayOutputStream; |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.io.InputStreamReader; |
| 28 | import java.net.URL; |
Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +0000 | [diff] [blame] | 29 | import java.util.HashSet; |
Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +0000 | [diff] [blame] | 30 | import java.util.Map; |
| 31 | import java.util.Set; |
Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +0000 | [diff] [blame] | 32 | |
| 33 | import javax.xml.transform.Transformer; |
| 34 | import javax.xml.transform.TransformerFactory; |
| 35 | import javax.xml.transform.stream.StreamResult; |
| 36 | import javax.xml.transform.stream.StreamSource; |
| 37 | |
| 38 | import aQute.bnd.osgi.Analyzer; |
Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +0000 | [diff] [blame] | 39 | import aQute.bnd.osgi.Processor; |
| 40 | import aQute.bnd.osgi.Resource; |
| 41 | import aQute.bnd.service.AnalyzerPlugin; |
| 42 | import aQute.libg.generics.Create; |
Guillaume Nodet | 7bf509f | 2014-05-19 07:12:48 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | public class ScrPlugin implements AnalyzerPlugin |
| 46 | { |
| 47 | |
| 48 | Transformer transformer; |
| 49 | |
| 50 | public ScrPlugin() throws Exception |
| 51 | { |
| 52 | transformer = getTransformer( getClass().getResource( "scr.xsl" ) ); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | public boolean analyzeJar( Analyzer analyzer ) throws Exception |
| 57 | { |
| 58 | Set<String> headers = Create.set(); |
| 59 | |
| 60 | String bpHeader = analyzer.getProperty( "Service-Component" ); |
| 61 | |
| 62 | Map<String, ? extends Map<String, String>> map = Processor.parseHeader( bpHeader, null ); |
| 63 | for ( String root : map.keySet() ) |
| 64 | { |
| 65 | Resource resource = analyzer.getJar().getResource(root); |
| 66 | if ( resource != null ) { |
| 67 | process(analyzer, root, resource, headers); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Group and analyze |
| 72 | for ( String str : headers ) |
| 73 | { |
| 74 | int idx = str.indexOf( ':' ); |
| 75 | if ( idx < 0 ) |
| 76 | { |
| 77 | analyzer.warning( ( new StringBuilder( "Error analyzing services in scr resource: " ) ).append( str ).toString() ); |
| 78 | continue; |
| 79 | } |
| 80 | String h = str.substring( 0, idx ).trim(); |
| 81 | String v = str.substring( idx + 1 ).trim(); |
| 82 | |
| 83 | StringBuilder sb = new StringBuilder(); |
| 84 | String header = analyzer.getProperty( h ); |
| 85 | if (header != null && !header.isEmpty()) |
| 86 | { |
| 87 | sb.append(header); |
| 88 | sb.append(","); |
| 89 | } |
| 90 | sb.append( v ); |
| 91 | analyzer.setProperty(h, sb.toString()); |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | private void process( Analyzer analyzer, String path, Resource resource, Set<String> headers ) |
| 98 | { |
| 99 | InputStream in = null; |
| 100 | try |
| 101 | { |
| 102 | in = resource.openInputStream(); |
| 103 | |
| 104 | // Retrieve headers |
| 105 | Set<String> set = analyze( in ); |
| 106 | headers.addAll( set ); |
| 107 | } |
| 108 | catch ( Exception e ) |
| 109 | { |
| 110 | analyzer.error( ( new StringBuilder( "Unexpected exception in processing scr resources(" ) ) |
| 111 | .append( path ).append( "): " ).append( e ).toString() ); |
| 112 | } |
| 113 | finally |
| 114 | { |
| 115 | try |
| 116 | { |
| 117 | if ( in != null ) |
| 118 | { |
| 119 | in.close(); |
| 120 | } |
| 121 | } |
| 122 | catch ( IOException e ) |
| 123 | { |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | public Set<String> analyze( InputStream in ) throws Exception |
| 130 | { |
| 131 | Set<String> refers = new HashSet<String>(); |
| 132 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 133 | javax.xml.transform.Result r = new StreamResult( bout ); |
| 134 | javax.xml.transform.Source s = new StreamSource( in ); |
| 135 | transformer.transform( s, r ); |
| 136 | ByteArrayInputStream bin = new ByteArrayInputStream( bout.toByteArray() ); |
| 137 | bout.close(); |
| 138 | BufferedReader br = new BufferedReader( new InputStreamReader( bin ) ); |
| 139 | for ( String line = br.readLine(); line != null; line = br.readLine() ) |
| 140 | { |
| 141 | line = line.trim(); |
| 142 | if ( line.length() > 0 ) |
| 143 | { |
| 144 | refers.add( line ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | br.close(); |
| 149 | return refers; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | protected Transformer getTransformer( URL url ) throws Exception |
| 154 | { |
| 155 | TransformerFactory tf = TransformerFactory.newInstance(); |
| 156 | javax.xml.transform.Source source = new StreamSource( url.openStream() ); |
| 157 | return tf.newTransformer( source ); |
| 158 | } |
| 159 | |
| 160 | } |