1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.felix.obrplugin;
20
21
22 import java.io.File;
23 import java.io.FileFilter;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.io.Writer;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.apache.felix.bundlerepository.Property;
33 import org.apache.felix.bundlerepository.Resource;
34 import org.apache.felix.bundlerepository.impl.DataModelHelperImpl;
35 import org.apache.felix.bundlerepository.impl.RepositoryImpl;
36 import org.apache.felix.bundlerepository.impl.ResourceImpl;
37 import org.apache.maven.artifact.repository.ArtifactRepository;
38 import org.apache.maven.plugin.AbstractMojo;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.plugin.logging.Log;
41
42
43
44
45
46
47
48
49
50
51 public final class ObrIndex extends AbstractMojo
52 {
53
54
55
56
57
58
59 private String obrRepository;
60
61
62
63
64
65
66 private String urlTemplate;
67
68
69
70
71
72
73 private String mavenRepository;
74
75
76
77
78
79
80
81
82 private ArtifactRepository localRepository;
83
84
85 public void execute() throws MojoExecutionException
86 {
87 Log log = getLog();
88 try
89 {
90 log.info( "Indexing..." );
91
92 String repo = mavenRepository;
93 if ( repo == null )
94 {
95 repo = localRepository.getBasedir();
96 }
97 URI mavenRepoUri = new File( repo ).toURI();
98
99 URI repositoryXml = ObrUtils.findRepositoryXml( repo, obrRepository );
100
101 log.info( "Repository: " + mavenRepoUri );
102 log.info( "OBR xml: " + repositoryXml );
103 log.info( "URL template: " + urlTemplate );
104
105 List<File> files = new ArrayList<File>();
106 findAllJars( new File( repo ), files );
107
108 DataModelHelperImpl dmh = new DataModelHelperImpl();
109 RepositoryImpl repository;
110
111 File obrRepoFile = new File( repositoryXml );
112 if ( obrRepoFile.isFile() )
113 {
114 repository = ( RepositoryImpl ) dmh.repository( repositoryXml.toURL() );
115 }
116 else
117 {
118 repository = new RepositoryImpl();
119 }
120
121 for ( File file : files )
122 {
123 try
124 {
125 ResourceImpl resource = ( ResourceImpl ) dmh.createResource( file.toURI().toURL() );
126 if ( resource != null )
127 {
128 repository.addResource( resource );
129 doTemplate( mavenRepoUri, file, resource );
130 log.info( "Adding resource: " + file );
131 }
132 else
133 {
134 log.info( "Ignoring non OSGi bundle: " + file );
135 }
136 }
137 catch ( Exception e )
138 {
139 log.warn( "Error processing bundle: " + file + " " + e.getMessage() );
140 }
141 }
142 Writer writer = new FileWriter( obrRepoFile );
143 try
144 {
145 dmh.writeRepository( repository, writer );
146 }
147 finally
148 {
149 writer.close();
150 }
151 }
152 catch ( Exception e )
153 {
154 log.warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
155 }
156 }
157
158
159 protected void doTemplate( URI root, File path, ResourceImpl resource ) throws IOException, URISyntaxException
160 {
161 path = path.getAbsoluteFile().getCanonicalFile();
162 String finalUri = root.relativize( path.toURI() ).toString();
163 if ( "maven".equals( urlTemplate ) )
164 {
165 String dir = root.relativize( path.toURI() ).toString();
166 String[] p = dir.split( "/" );
167 if ( p.length >= 4 && p[p.length - 1].startsWith( p[p.length - 3] + "-" + p[p.length - 2] ) )
168 {
169 String artifactId = p[p.length - 3];
170 String version = p[p.length - 2];
171 String classifier;
172 String type;
173 String artifactIdVersion = artifactId + "-" + version;
174 StringBuffer sb = new StringBuffer();
175 if ( p[p.length - 1].charAt( artifactIdVersion.length() ) == '-' )
176 {
177 classifier = p[p.length - 1].substring( artifactIdVersion.length() + 1,
178 p[p.length - 1].lastIndexOf( '.' ) );
179 }
180 else
181 {
182 classifier = null;
183 }
184 type = p[p.length - 1].substring( p[p.length - 1].lastIndexOf( '.' ) + 1 );
185 sb.append( "mvn:" );
186 for ( int j = 0; j < p.length - 3; j++ )
187 {
188 if ( j > 0 )
189 {
190 sb.append( '.' );
191 }
192 sb.append( p[j] );
193 }
194 sb.append( '/' ).append( artifactId ).append( '/' ).append( version );
195 if ( !"jar".equals( type ) || classifier != null )
196 {
197 sb.append( '/' );
198 if ( !"jar".equals( type ) )
199 {
200 sb.append( type );
201 }
202 if ( classifier != null )
203 {
204 sb.append( '/' ).append( classifier );
205 }
206 }
207 finalUri = sb.toString();
208 }
209 }
210 else if ( urlTemplate != null )
211 {
212 String dir = path.getParentFile().toURI().toURL().toString();
213 if ( dir.endsWith( "/" ) )
214 dir = dir.substring( 0, dir.length() - 1 );
215
216 if ( dir.startsWith( root.toString() ) )
217 dir = dir.substring( root.toString().length() );
218
219 String url = urlTemplate.replaceAll( "%v", "" + resource.getVersion() );
220 url = url.replaceAll( "%s", resource.getSymbolicName() );
221 url = url.replaceAll( "%f", path.getName() );
222 url = url.replaceAll( "%p", dir );
223 finalUri = url;
224 }
225 resource.put( Resource.URI, finalUri, Property.URI );
226 }
227
228 private final FileFilter filter = new FileFilter()
229 {
230
231 public boolean accept( File pathname )
232 {
233 return pathname.getName().endsWith( "ar" );
234 }
235 };
236
237
238 private void findAllJars( File mainRoot, List<File> files )
239 {
240 List<File> roots = new ArrayList<File>();
241 roots.add( mainRoot );
242 while ( !roots.isEmpty() )
243 {
244 File root = roots.remove( 0 );
245 File[] children = root.listFiles();
246 if ( children != null )
247 {
248 for ( File child : children )
249 {
250 if ( child.isFile() && filter.accept( child ) )
251 {
252 files.add( child );
253 }
254 else if ( child.isDirectory() )
255 {
256 roots.add( child );
257 }
258 }
259 }
260 }
261 }
262
263 }