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