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