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