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