Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 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.obrplugin; |
| 20 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 21 | |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 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; |
Carsten Ziegeler | 318c2cb | 2015-03-09 13:57:23 +0000 | [diff] [blame] | 41 | import org.apache.maven.plugins.annotations.Mojo; |
| 42 | import org.apache.maven.plugins.annotations.Parameter; |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 43 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 44 | |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Index the content of a maven repository using OBR |
| 47 | * |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 48 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 49 | */ |
Carsten Ziegeler | 318c2cb | 2015-03-09 13:57:23 +0000 | [diff] [blame] | 50 | @Mojo( name = "index", requiresProject = false ) |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 51 | public final class ObrIndex extends AbstractMojo |
| 52 | { |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * OBR Repository. |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 56 | */ |
Carsten Ziegeler | 318c2cb | 2015-03-09 13:57:23 +0000 | [diff] [blame] | 57 | @Parameter( property = "obrRepository" ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 58 | private String obrRepository; |
| 59 | |
| 60 | /** |
| 61 | * Template for urls |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 62 | */ |
Carsten Ziegeler | 318c2cb | 2015-03-09 13:57:23 +0000 | [diff] [blame] | 63 | @Parameter( property = "urlTemplate" ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 64 | private String urlTemplate; |
| 65 | |
| 66 | /** |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 67 | * The repository to index |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 68 | */ |
Carsten Ziegeler | 318c2cb | 2015-03-09 13:57:23 +0000 | [diff] [blame] | 69 | @Parameter( property = "mavenRepository" ) |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 70 | private String mavenRepository; |
| 71 | |
| 72 | /** |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 73 | * Local Repository. |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 74 | */ |
Carsten Ziegeler | 318c2cb | 2015-03-09 13:57:23 +0000 | [diff] [blame] | 75 | @Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 76 | private ArtifactRepository localRepository; |
| 77 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 78 | |
| 79 | public void execute() throws MojoExecutionException |
| 80 | { |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 81 | Log log = getLog(); |
| 82 | try |
| 83 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 84 | log.info( "Indexing..." ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 85 | |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 86 | String repo = mavenRepository; |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 87 | if ( repo == null ) |
| 88 | { |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 89 | repo = localRepository.getBasedir(); |
| 90 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 91 | URI mavenRepoUri = new File( repo ).toURI(); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 92 | |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 93 | URI repositoryXml = ObrUtils.findRepositoryXml( repo, obrRepository ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 94 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 95 | log.info( "Repository: " + mavenRepoUri ); |
| 96 | log.info( "OBR xml: " + repositoryXml ); |
| 97 | log.info( "URL template: " + urlTemplate ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 98 | |
| 99 | List<File> files = new ArrayList<File>(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 100 | findAllJars( new File( repo ), files ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 101 | |
| 102 | DataModelHelperImpl dmh = new DataModelHelperImpl(); |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 103 | RepositoryImpl repository; |
| 104 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 105 | File obrRepoFile = new File( repositoryXml ); |
| 106 | if ( obrRepoFile.isFile() ) |
| 107 | { |
| 108 | repository = ( RepositoryImpl ) dmh.repository( repositoryXml.toURL() ); |
| 109 | } |
| 110 | else |
| 111 | { |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 112 | repository = new RepositoryImpl(); |
| 113 | } |
| 114 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 115 | for ( File file : files ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 116 | { |
| 117 | try |
| 118 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 119 | ResourceImpl resource = ( ResourceImpl ) dmh.createResource( file.toURI().toURL() ); |
| 120 | if ( resource != null ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 121 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 122 | repository.addResource( resource ); |
| 123 | doTemplate( mavenRepoUri, file, resource ); |
| 124 | log.info( "Adding resource: " + file ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 125 | } |
| 126 | else |
| 127 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 128 | log.info( "Ignoring non OSGi bundle: " + file ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 131 | catch ( Exception e ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 132 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 133 | log.warn( "Error processing bundle: " + file + " " + e.getMessage() ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
Guillaume Nodet | 2d81f33 | 2010-04-09 16:29:20 +0000 | [diff] [blame] | 136 | Writer writer = new FileWriter( obrRepoFile ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 137 | 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 McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 152 | |
| 153 | protected void doTemplate( URI root, File path, ResourceImpl resource ) throws IOException, URISyntaxException |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 154 | { |
| 155 | path = path.getAbsoluteFile().getCanonicalFile(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 156 | String finalUri = root.relativize( path.toURI() ).toString(); |
| 157 | if ( "maven".equals( urlTemplate ) ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 158 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 159 | 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 Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 162 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 163 | String artifactId = p[p.length - 3]; |
| 164 | String version = p[p.length - 2]; |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 165 | String classifier; |
| 166 | String type; |
| 167 | String artifactIdVersion = artifactId + "-" + version; |
| 168 | StringBuffer sb = new StringBuffer(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 169 | if ( p[p.length - 1].charAt( artifactIdVersion.length() ) == '-' ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 170 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 171 | classifier = p[p.length - 1].substring( artifactIdVersion.length() + 1, |
| 172 | p[p.length - 1].lastIndexOf( '.' ) ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 173 | } |
| 174 | else |
| 175 | { |
| 176 | classifier = null; |
| 177 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 178 | 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 Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 181 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 182 | if ( j > 0 ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 183 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 184 | sb.append( '.' ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 185 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 186 | sb.append( p[j] ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 187 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 188 | sb.append( '/' ).append( artifactId ).append( '/' ).append( version ); |
| 189 | if ( !"jar".equals( type ) || classifier != null ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 190 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 191 | sb.append( '/' ); |
| 192 | if ( !"jar".equals( type ) ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 193 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 194 | sb.append( type ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 195 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 196 | if ( classifier != null ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 197 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 198 | sb.append( '/' ).append( classifier ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | finalUri = sb.toString(); |
| 202 | } |
| 203 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 204 | else if ( urlTemplate != null ) |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 205 | { |
Stuart McCulloch | f621f77 | 2011-10-11 17:01:19 +0000 | [diff] [blame] | 206 | URI parentDir = path.getParentFile().toURI(); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 207 | |
Stuart McCulloch | f621f77 | 2011-10-11 17:01:19 +0000 | [diff] [blame] | 208 | String absoluteDir = trim( root.toString(), parentDir.toURL().toString() ); |
| 209 | String relativeDir = trim( root.toString(), root.relativize( parentDir ).toString() ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 210 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 211 | String url = urlTemplate.replaceAll( "%v", "" + resource.getVersion() ); |
| 212 | url = url.replaceAll( "%s", resource.getSymbolicName() ); |
| 213 | url = url.replaceAll( "%f", path.getName() ); |
Stuart McCulloch | f621f77 | 2011-10-11 17:01:19 +0000 | [diff] [blame] | 214 | url = url.replaceAll( "%p", absoluteDir ); |
| 215 | url = url.replaceAll( "%rp", relativeDir ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 216 | finalUri = url; |
| 217 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 218 | resource.put( Resource.URI, finalUri, Property.URI ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Stuart McCulloch | f621f77 | 2011-10-11 17:01:19 +0000 | [diff] [blame] | 221 | |
| 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 McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 234 | private final FileFilter filter = new FileFilter() |
| 235 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 236 | public boolean accept( File pathname ) |
| 237 | { |
| 238 | return pathname.getName().endsWith( "ar" ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 239 | } |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 243 | private void findAllJars( File mainRoot, List<File> files ) |
| 244 | { |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 245 | List<File> roots = new ArrayList<File>(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 246 | roots.add( mainRoot ); |
| 247 | while ( !roots.isEmpty() ) |
| 248 | { |
| 249 | File root = roots.remove( 0 ); |
Guillaume Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 250 | File[] children = root.listFiles(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 251 | 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 Nodet | 8d5faf6 | 2010-03-19 15:32:17 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } |