blob: ef7100f47dff6d5d8daf02f7ee80dd14fc5abf2c [file] [log] [blame]
Guillaume Nodet8d5faf62010-03-19 15:32:17 +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 */
19package org.apache.felix.obrplugin;
20
Stuart McCullochd98b02e2011-06-28 23:20:37 +000021
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000022import java.io.File;
23import java.io.FileFilter;
24import java.io.FileWriter;
25import java.io.IOException;
26import java.io.Writer;
27import java.net.URI;
28import java.net.URISyntaxException;
29import java.util.ArrayList;
30import java.util.List;
31
32import org.apache.felix.bundlerepository.Property;
33import org.apache.felix.bundlerepository.Resource;
34import org.apache.felix.bundlerepository.impl.DataModelHelperImpl;
35import org.apache.felix.bundlerepository.impl.RepositoryImpl;
36import org.apache.felix.bundlerepository.impl.ResourceImpl;
37import org.apache.maven.artifact.repository.ArtifactRepository;
38import org.apache.maven.plugin.AbstractMojo;
39import org.apache.maven.plugin.MojoExecutionException;
40import org.apache.maven.plugin.logging.Log;
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000041import org.apache.maven.plugins.annotations.Mojo;
42import org.apache.maven.plugins.annotations.Parameter;
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000043
Stuart McCullochd98b02e2011-06-28 23:20:37 +000044
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000045/**
46 * Index the content of a maven repository using OBR
47 *
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000048 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
49 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000050@Mojo( name = "index", requiresProject = false )
Stuart McCullochd98b02e2011-06-28 23:20:37 +000051public final class ObrIndex extends AbstractMojo
52{
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000053
54 /**
55 * OBR Repository.
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000056 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000057 @Parameter( property = "obrRepository" )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000058 private String obrRepository;
59
60 /**
61 * Template for urls
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000062 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000063 @Parameter( property = "urlTemplate" )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000064 private String urlTemplate;
65
66 /**
Guillaume Nodet2d81f332010-04-09 16:29:20 +000067 * The repository to index
Guillaume Nodet2d81f332010-04-09 16:29:20 +000068 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000069 @Parameter( property = "mavenRepository" )
Guillaume Nodet2d81f332010-04-09 16:29:20 +000070 private String mavenRepository;
71
72 /**
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000073 * Local Repository.
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000074 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000075 @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000076 private ArtifactRepository localRepository;
77
Stuart McCullochd98b02e2011-06-28 23:20:37 +000078
79 public void execute() throws MojoExecutionException
80 {
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000081 Log log = getLog();
82 try
83 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +000084 log.info( "Indexing..." );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000085
Guillaume Nodet2d81f332010-04-09 16:29:20 +000086 String repo = mavenRepository;
Stuart McCullochd98b02e2011-06-28 23:20:37 +000087 if ( repo == null )
88 {
Guillaume Nodet2d81f332010-04-09 16:29:20 +000089 repo = localRepository.getBasedir();
90 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +000091 URI mavenRepoUri = new File( repo ).toURI();
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000092
Guillaume Nodet2d81f332010-04-09 16:29:20 +000093 URI repositoryXml = ObrUtils.findRepositoryXml( repo, obrRepository );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000094
Stuart McCullochd98b02e2011-06-28 23:20:37 +000095 log.info( "Repository: " + mavenRepoUri );
96 log.info( "OBR xml: " + repositoryXml );
97 log.info( "URL template: " + urlTemplate );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +000098
99 List<File> files = new ArrayList<File>();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000100 findAllJars( new File( repo ), files );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000101
102 DataModelHelperImpl dmh = new DataModelHelperImpl();
Guillaume Nodet2d81f332010-04-09 16:29:20 +0000103 RepositoryImpl repository;
104
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000105 File obrRepoFile = new File( repositoryXml );
106 if ( obrRepoFile.isFile() )
107 {
108 repository = ( RepositoryImpl ) dmh.repository( repositoryXml.toURL() );
109 }
110 else
111 {
Guillaume Nodet2d81f332010-04-09 16:29:20 +0000112 repository = new RepositoryImpl();
113 }
114
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000115 for ( File file : files )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000116 {
117 try
118 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000119 ResourceImpl resource = ( ResourceImpl ) dmh.createResource( file.toURI().toURL() );
120 if ( resource != null )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000121 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000122 repository.addResource( resource );
123 doTemplate( mavenRepoUri, file, resource );
124 log.info( "Adding resource: " + file );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000125 }
126 else
127 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000128 log.info( "Ignoring non OSGi bundle: " + file );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000129 }
130 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000131 catch ( Exception e )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000132 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000133 log.warn( "Error processing bundle: " + file + " " + e.getMessage() );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000134 }
135 }
Guillaume Nodet2d81f332010-04-09 16:29:20 +0000136 Writer writer = new FileWriter( obrRepoFile );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000137 try
138 {
139 dmh.writeRepository( repository, writer );
140 }
141 finally
142 {
143 writer.close();
144 }
145 }
146 catch ( Exception e )
147 {
148 log.warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
149 }
150 }
151
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000152
153 protected void doTemplate( URI root, File path, ResourceImpl resource ) throws IOException, URISyntaxException
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000154 {
155 path = path.getAbsoluteFile().getCanonicalFile();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000156 String finalUri = root.relativize( path.toURI() ).toString();
157 if ( "maven".equals( urlTemplate ) )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000158 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000159 String dir = root.relativize( path.toURI() ).toString();
160 String[] p = dir.split( "/" );
161 if ( p.length >= 4 && p[p.length - 1].startsWith( p[p.length - 3] + "-" + p[p.length - 2] ) )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000162 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000163 String artifactId = p[p.length - 3];
164 String version = p[p.length - 2];
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000165 String classifier;
166 String type;
167 String artifactIdVersion = artifactId + "-" + version;
168 StringBuffer sb = new StringBuffer();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000169 if ( p[p.length - 1].charAt( artifactIdVersion.length() ) == '-' )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000170 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000171 classifier = p[p.length - 1].substring( artifactIdVersion.length() + 1,
172 p[p.length - 1].lastIndexOf( '.' ) );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000173 }
174 else
175 {
176 classifier = null;
177 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000178 type = p[p.length - 1].substring( p[p.length - 1].lastIndexOf( '.' ) + 1 );
179 sb.append( "mvn:" );
180 for ( int j = 0; j < p.length - 3; j++ )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000181 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000182 if ( j > 0 )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000183 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000184 sb.append( '.' );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000185 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000186 sb.append( p[j] );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000187 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000188 sb.append( '/' ).append( artifactId ).append( '/' ).append( version );
189 if ( !"jar".equals( type ) || classifier != null )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000190 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000191 sb.append( '/' );
192 if ( !"jar".equals( type ) )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000193 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000194 sb.append( type );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000195 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000196 if ( classifier != null )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000197 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000198 sb.append( '/' ).append( classifier );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000199 }
200 }
201 finalUri = sb.toString();
202 }
203 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000204 else if ( urlTemplate != null )
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000205 {
Stuart McCullochf621f772011-10-11 17:01:19 +0000206 URI parentDir = path.getParentFile().toURI();
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000207
Stuart McCullochf621f772011-10-11 17:01:19 +0000208 String absoluteDir = trim( root.toString(), parentDir.toURL().toString() );
209 String relativeDir = trim( root.toString(), root.relativize( parentDir ).toString() );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000210
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000211 String url = urlTemplate.replaceAll( "%v", "" + resource.getVersion() );
212 url = url.replaceAll( "%s", resource.getSymbolicName() );
213 url = url.replaceAll( "%f", path.getName() );
Stuart McCullochf621f772011-10-11 17:01:19 +0000214 url = url.replaceAll( "%p", absoluteDir );
215 url = url.replaceAll( "%rp", relativeDir );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000216 finalUri = url;
217 }
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000218 resource.put( Resource.URI, finalUri, Property.URI );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000219 }
220
Stuart McCullochf621f772011-10-11 17:01:19 +0000221
222 private String trim( String prefix, String path )
223 {
224 if ( path.endsWith( "/" ) )
225 path = path.substring( 0, path.length() - 1 );
226
227 if ( path.startsWith( prefix ) )
228 path = path.substring( prefix.length() );
229
230 return path;
231 }
232
233
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000234 private final FileFilter filter = new FileFilter()
235 {
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000236 public boolean accept( File pathname )
237 {
238 return pathname.getName().endsWith( "ar" );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000239 }
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000240 };
241
242
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000243 private void findAllJars( File mainRoot, List<File> files )
244 {
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000245 List<File> roots = new ArrayList<File>();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000246 roots.add( mainRoot );
247 while ( !roots.isEmpty() )
248 {
249 File root = roots.remove( 0 );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000250 File[] children = root.listFiles();
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000251 if ( children != null )
252 {
253 for ( File child : children )
254 {
255 if ( child.isFile() && filter.accept( child ) )
256 {
257 files.add( child );
258 }
259 else if ( child.isDirectory() )
260 {
261 roots.add( child );
Guillaume Nodet8d5faf62010-03-19 15:32:17 +0000262 }
263 }
264 }
265 }
266 }
267
268}