blob: 9ece6c1e92dee1d48601e7575be651cb0015fc86 [file] [log] [blame]
Stuart McCullochf26260b2011-06-16 17:33:04 +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 */
Guillaume Nodet09157a92010-03-10 17:40:45 +000019package org.apache.felix.bundleplugin;
20
Stuart McCullochf26260b2011-06-16 17:33:04 +000021
Carsten Ziegeler82d87402015-03-04 13:26:21 +000022import static org.apache.felix.utils.manifest.Parser.parseHeader;
23
Guillaume Nodet09157a92010-03-10 17:40:45 +000024import java.io.BufferedReader;
25import java.io.ByteArrayInputStream;
26import java.io.ByteArrayOutputStream;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.InputStreamReader;
30import java.net.URL;
Guillaume Nodet7bf509f2014-05-19 07:12:48 +000031import java.util.Arrays;
Guillaume Nodet09157a92010-03-10 17:40:45 +000032import java.util.HashSet;
Guillaume Nodet09157a92010-03-10 17:40:45 +000033import java.util.Map;
34import java.util.Set;
Guillaume Nodet09157a92010-03-10 17:40:45 +000035import java.util.regex.Pattern;
Stuart McCullochd98b02e2011-06-28 23:20:37 +000036
Guillaume Nodet09157a92010-03-10 17:40:45 +000037import javax.xml.transform.Transformer;
38import javax.xml.transform.TransformerFactory;
39import javax.xml.transform.stream.StreamResult;
40import javax.xml.transform.stream.StreamSource;
41
Carsten Ziegeler82d87402015-03-04 13:26:21 +000042import org.apache.felix.utils.manifest.Attribute;
43import org.apache.felix.utils.manifest.Clause;
44import org.osgi.framework.Constants;
45
Guillaume Nodet7bf509f2014-05-19 07:12:48 +000046import aQute.bnd.header.Attrs;
Stuart McCulloch42151ee2012-07-16 13:43:38 +000047import aQute.bnd.osgi.Analyzer;
48import aQute.bnd.osgi.Descriptors.PackageRef;
49import aQute.bnd.osgi.Jar;
50import aQute.bnd.osgi.Processor;
51import aQute.bnd.osgi.Resource;
Carsten Ziegeler82d87402015-03-04 13:26:21 +000052import aQute.bnd.service.AnalyzerPlugin;
Guillaume Nodet09157a92010-03-10 17:40:45 +000053import aQute.libg.generics.Create;
Guillaume Nodet09157a92010-03-10 17:40:45 +000054
Stuart McCulloch340a8dd2011-06-28 22:47:10 +000055
Stuart McCullochd98b02e2011-06-28 23:20:37 +000056public class BlueprintPlugin implements AnalyzerPlugin
57{
Guillaume Nodet09157a92010-03-10 17:40:45 +000058
Stuart McCullochd98b02e2011-06-28 23:20:37 +000059 static Pattern QN = Pattern.compile( "[_A-Za-z$][_A-Za-z0-9$]*(\\.[_A-Za-z$][_A-Za-z0-9$]*)*" );
60 static Pattern PATHS = Pattern.compile( ".*\\.xml" );
Guillaume Nodet09157a92010-03-10 17:40:45 +000061
62 Transformer transformer;
63
Stuart McCullochd98b02e2011-06-28 23:20:37 +000064
65 public BlueprintPlugin() throws Exception
66 {
67 transformer = getTransformer( getClass().getResource( "blueprint.xsl" ) );
Guillaume Nodet09157a92010-03-10 17:40:45 +000068 }
69
Stuart McCullochd98b02e2011-06-28 23:20:37 +000070
71 public boolean analyzeJar( Analyzer analyzer ) throws Exception
72 {
Guillaume Nodet7bf509f2014-05-19 07:12:48 +000073 String mode = analyzer.getProperty("service_mode");
74 if (mode == null) {
Guillaume Nodet2acc5b52014-06-17 08:04:47 +000075 mode = "service";
Guillaume Nodet7bf509f2014-05-19 07:12:48 +000076 }
77
Stuart McCullochd98b02e2011-06-28 23:20:37 +000078 transformer.setParameter( "nsh_interface",
79 analyzer.getProperty( "nsh_interface" ) != null ? analyzer.getProperty( "nsh_interface" ) : "" );
80 transformer.setParameter( "nsh_namespace",
81 analyzer.getProperty( "nsh_namespace" ) != null ? analyzer.getProperty( "nsh_namespace" ) : "" );
Guillaume Nodet09157a92010-03-10 17:40:45 +000082
83 Set<String> headers = Create.set();
84
Stuart McCullochd98b02e2011-06-28 23:20:37 +000085 String bpHeader = analyzer.getProperty( "Bundle-Blueprint", "OSGI-INF/blueprint" );
Stuart McCullochf3173222012-06-07 21:57:32 +000086 Map<String, ? extends Map<String, String>> map = Processor.parseHeader( bpHeader, null );
Guillaume Nodetf092e1b2012-07-18 21:24:06 +000087 bpHeader = "";
Stuart McCullochd98b02e2011-06-28 23:20:37 +000088 for ( String root : map.keySet() )
Guillaume Nodetf51cc6b2010-03-10 19:37:34 +000089 {
Guillaume Nodet09157a92010-03-10 17:40:45 +000090 Jar jar = analyzer.getJar();
Stuart McCullochd98b02e2011-06-28 23:20:37 +000091 Map<String, Resource> dir = jar.getDirectories().get( root );
92 if ( dir == null || dir.isEmpty() )
Guillaume Nodet09157a92010-03-10 17:40:45 +000093 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +000094 Resource resource = jar.getResource( root );
Carsten Ziegeler82d87402015-03-04 13:26:21 +000095 if ( resource != null )
Guillaume Nodetf092e1b2012-07-18 21:24:06 +000096 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +000097 process( analyzer, root, resource, headers );
Guillaume Nodetf092e1b2012-07-18 21:24:06 +000098 if (bpHeader.length() > 0) {
99 bpHeader += ",";
100 }
101 bpHeader += root;
102 }
Stuart McCullochb2815082012-11-22 01:24:13 +0000103 continue;
Guillaume Nodet09157a92010-03-10 17:40:45 +0000104 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000105 for ( Map.Entry<String, Resource> entry : dir.entrySet() )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000106 {
107 String path = entry.getKey();
108 Resource resource = entry.getValue();
Carsten Ziegeler82d87402015-03-04 13:26:21 +0000109 if ( PATHS.matcher( path ).matches() )
Guillaume Nodetf092e1b2012-07-18 21:24:06 +0000110 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000111 process( analyzer, path, resource, headers );
Guillaume Nodetf092e1b2012-07-18 21:24:06 +0000112 if (bpHeader.length() > 0) {
113 bpHeader += ",";
114 }
115 bpHeader += path;
116 }
Guillaume Nodet09157a92010-03-10 17:40:45 +0000117 }
Guillaume Nodet09157a92010-03-10 17:40:45 +0000118 }
Carsten Ziegeler82d87402015-03-04 13:26:21 +0000119 if( !map.isEmpty() )
Guillaume Nodetf092e1b2012-07-18 21:24:06 +0000120 {
121 analyzer.setProperty("Bundle-Blueprint", bpHeader);
122 }
Guillaume Nodet09157a92010-03-10 17:40:45 +0000123
124 // Group and analyze
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000125 Set<String> caps = Create.set();
126 Set<String> reqs = Create.set();
127 Map<String, Set<Clause>> hdrs = Create.map();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000128 for ( String str : headers )
129 {
130 int idx = str.indexOf( ':' );
131 if ( idx < 0 )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000132 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000133 analyzer.warning( ( new StringBuilder( "Error analyzing services in blueprint resource: " ) ).append(
134 str ).toString() );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000135 continue;
136 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000137 String h = str.substring( 0, idx ).trim();
138 String v = str.substring( idx + 1 ).trim();
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000139 Clause[] hc = parseHeader(v);
140 // Convert generic caps/reqs
141 if ("Import-Service".equals(h))
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000142 {
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000143 if (!"service".equals(mode))
144 {
145 Clause clause = hc[0];
146 String multiple = clause.getDirective("multiple");
147 String avail = clause.getDirective("availability");
148 String filter = clause.getAttribute("filter");
149
150 StringBuilder sb = new StringBuilder();
151 sb.append("osgi.service;effective:=active;");
152 if ("optional".equals(avail)) {
153 sb.append("resolution:=optional;");
154 }
155 if ("true".equals(multiple)) {
156 sb.append("cardinality:=multiple;");
157 }
158 if (filter == null) {
159 filter = "(" + Constants.OBJECTCLASS + "=" + clause.getName() + ")";
160 } else if (!filter.startsWith("(") && !filter.endsWith(")")) {
161 filter = "(&(" + Constants.OBJECTCLASS + "=" + clause.getName() + ")(" + filter + "))";
162 } else {
163 filter = "(&(" + Constants.OBJECTCLASS + "=" + clause.getName() + ")" + filter + ")";
164 }
165 sb.append("filter:=\"").append(filter).append("\"");
166 reqs.add(sb.toString());
167 }
168 else if (!"generic".equals(mode))
169 {
170 Set<Clause> clauses = hdrs.get(h);
171 if (clauses == null) {
172 clauses = new HashSet<Clause>();
173 hdrs.put(h, clauses);
174 }
175 clauses.addAll(Arrays.asList(hc));
176 }
Guillaume Nodet09157a92010-03-10 17:40:45 +0000177 }
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000178 else if ("Export-Service".equals(h))
179 {
180 if (!"service".equals(mode))
181 {
182 StringBuilder sb = new StringBuilder();
183 sb.append("osgi.service;effective:=active;objectClass");
184 if (hc.length > 1) {
185 sb.append(":List<String>=\"");
186 } else {
187 sb.append("=\"");
188 }
189 for (int i = 0; i < hc.length; i++)
190 {
191 if (i > 0)
192 {
193 sb.append(",");
194 }
195 sb.append(hc[i].getName());
196 }
197 sb.append("\"");
198 for (int i = 0; i < hc[0].getAttributes().length; i++)
199 {
200 sb.append(";");
201 sb.append(hc[0].getAttributes()[i].getName());
202 sb.append("=\"");
203 sb.append(hc[0].getAttributes()[i].getValue());
204 sb.append("\"");
205 }
206 caps.add(sb.toString());
207 }
208 else if (!"generic".equals(mode))
209 {
210 Set<Clause> clauses = hdrs.get(h);
211 if (clauses == null) {
212 clauses = new HashSet<Clause>();
213 hdrs.put(h, clauses);
214 }
215 clauses.addAll(Arrays.asList(hc));
216 }
217 }
218 else
219 {
220 Set<Clause> clauses = hdrs.get(h);
221 if (clauses == null)
222 {
223 clauses = new HashSet<Clause>();
224 hdrs.put(h, clauses);
225 }
226 clauses.addAll(Arrays.asList( hc ) );
227 }
228 }
229 if (!caps.isEmpty())
230 {
231 StringBuilder sb = new StringBuilder();
232 String header = analyzer.getProperty("Provide-Capability");
233 if (header != null)
234 {
235 sb.append(header);
236 }
237 for (String cap : caps) {
238 if (sb.length() > 0) {
239 sb.append(",");
240 }
241 sb.append(cap);
242 }
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000243 analyzer.setProperty("Provide-Capability", sb.toString());
244 }
245 if (!reqs.isEmpty())
246 {
247 StringBuilder sb = new StringBuilder();
248 String header = analyzer.getProperty("Require-Capability");
249 if (header != null)
250 {
251 sb.append(header);
252 }
253 for (String req : reqs) {
254 if (sb.length() > 0) {
255 sb.append(",");
256 }
257 sb.append(req);
258 }
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000259 analyzer.setProperty("Require-Capability", sb.toString());
Guillaume Nodet09157a92010-03-10 17:40:45 +0000260 }
261 // Merge
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000262 for ( String header : hdrs.keySet() )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000263 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000264 if ( "Import-Class".equals( header ) || "Import-Package".equals( header ) )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000265 {
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000266 Set<Clause> newAttr = hdrs.get(header);
267 for ( Clause a : newAttr )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000268 {
269 String pkg = a.getName();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000270 if ( "Import-Class".equals( header ) )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000271 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000272 int n = a.getName().lastIndexOf( '.' );
273 if ( n > 0 )
274 {
275 pkg = pkg.subSequence( 0, n ).toString();
276 }
277 else
278 {
Guillaume Nodet09157a92010-03-10 17:40:45 +0000279 continue;
280 }
281 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000282 PackageRef pkgRef = analyzer.getPackageRef( pkg );
283 if ( !analyzer.getReferred().containsKey( pkgRef ) )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000284 {
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000285 Attrs attrs = analyzer.getReferred().put(pkgRef);
286 for (Attribute attribute : a.getAttributes())
287 {
288 attrs.put(attribute.getName(), attribute.getValue());
289 }
Guillaume Nodet09157a92010-03-10 17:40:45 +0000290 }
291 }
292 }
293 else
294 {
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000295 Set<String> merge = Create.set();
Guillaume Nodet0290f492014-06-17 11:25:40 +0000296 String org = analyzer.getProperty(header);
297 if (org != null && !org.isEmpty())
Guillaume Nodet09157a92010-03-10 17:40:45 +0000298 {
Guillaume Nodet0290f492014-06-17 11:25:40 +0000299 for (Clause clause : parseHeader(org))
300 {
301 merge.add(clause.toString());
302 }
Guillaume Nodet09157a92010-03-10 17:40:45 +0000303 }
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000304 for (Clause clause : hdrs.get(header))
305 {
306 merge.add(clause.toString());
307 }
Guillaume Nodet09157a92010-03-10 17:40:45 +0000308 StringBuilder sb = new StringBuilder();
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000309 for (String clause : merge)
Guillaume Nodet09157a92010-03-10 17:40:45 +0000310 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000311 if ( sb.length() > 0 )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000312 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000313 sb.append( "," );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000314 }
Guillaume Nodet7bf509f2014-05-19 07:12:48 +0000315 sb.append(clause);
316
Guillaume Nodet09157a92010-03-10 17:40:45 +0000317 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000318 analyzer.setProperty( header, sb.toString() );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000319 }
320 }
321 return false;
322 }
323
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000324
325 private void process( Analyzer analyzer, String path, Resource resource, Set<String> headers )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000326 {
327 InputStream in = null;
328 try
329 {
330 in = resource.openInputStream();
331
332 // Retrieve headers
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000333 Set<String> set = analyze( in );
334 headers.addAll( set );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000335 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000336 catch ( Exception e )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000337 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000338 analyzer.error( ( new StringBuilder( "Unexpected exception in processing spring resources(" ) )
339 .append( path ).append( "): " ).append( e ).toString() );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000340 }
341 finally
342 {
343 try
344 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000345 if ( in != null )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000346 {
347 in.close();
348 }
349 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000350 catch ( IOException e )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000351 {
352 }
353 }
354 }
355
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000356
357 public Set<String> analyze( InputStream in ) throws Exception
Guillaume Nodet09157a92010-03-10 17:40:45 +0000358 {
359 Set<String> refers = new HashSet<String>();
360 ByteArrayOutputStream bout = new ByteArrayOutputStream();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000361 javax.xml.transform.Result r = new StreamResult( bout );
362 javax.xml.transform.Source s = new StreamSource( in );
363 transformer.transform( s, r );
364 ByteArrayInputStream bin = new ByteArrayInputStream( bout.toByteArray() );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000365 bout.close();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000366 BufferedReader br = new BufferedReader( new InputStreamReader( bin ) );
367 for ( String line = br.readLine(); line != null; line = br.readLine() )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000368 {
369 line = line.trim();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000370 line = line.replace( ";availability:=mandatory", "" );
371 if ( line.length() > 0 )
Guillaume Nodet09157a92010-03-10 17:40:45 +0000372 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000373 refers.add( line );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000374 }
375 }
376
377 br.close();
378 return refers;
379 }
380
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000381
382 protected Transformer getTransformer( URL url ) throws Exception
Guillaume Nodet09157a92010-03-10 17:40:45 +0000383 {
384 TransformerFactory tf = TransformerFactory.newInstance();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000385 javax.xml.transform.Source source = new StreamSource( url.openStream() );
386 return tf.newTransformer( source );
Guillaume Nodet09157a92010-03-10 17:40:45 +0000387 }
388
Guillaume Nodet09157a92010-03-10 17:40:45 +0000389}