blob: 3cf2e6659d2279f04951f85de5d42d95bd4e9635 [file] [log] [blame]
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +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 */
Stuart McCullochc792b372008-02-17 16:12:24 +000019package org.apache.felix.obrplugin;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000020
21
22import java.io.File;
23import java.net.URI;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000024import java.util.Iterator;
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000025import java.util.regex.Pattern;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000026
27import org.apache.maven.artifact.Artifact;
28import org.apache.maven.artifact.repository.ArtifactRepository;
29import org.apache.maven.model.Resource;
Stuart McCulloch25687f82011-06-28 00:15:26 +000030import org.apache.maven.project.MavenProject;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000031
32
33/**
34 * Various OBR utility methods
35 *
36 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
37 */
38public class ObrUtils
39{
40 private static final String DOT_XML = ".xml";
41 private static final String REPO_XML = "repository.xml";
42 private static final String OBR_XML = "obr.xml";
43
44
45 /**
46 * @param mavenRepository path to local maven repository
47 * @param obrRepository path to specific repository.xml
48 * @return URI pointing to correct repository.xml
49 */
50 public static URI findRepositoryXml( String mavenRepository, String obrRepository )
51 {
52 String targetPath = obrRepository;
53
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000054 Pattern ignoredNames = Pattern.compile( "^(true|false|none|null)?$", Pattern.CASE_INSENSITIVE );
55
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000056 // Combine location settings into a single repository location
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000057 if ( null == targetPath || ignoredNames.matcher( targetPath ).matches() )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000058 {
59 targetPath = mavenRepository + '/' + REPO_XML;
60 }
61 else if ( !targetPath.toLowerCase().endsWith( DOT_XML ) )
62 {
63 targetPath = targetPath + '/' + REPO_XML;
64 }
65
66 URI uri;
67 try
68 {
69 uri = new URI( targetPath );
70 uri.toURL(); // check protocol
71 }
72 catch ( Exception e )
73 {
74 uri = null;
75 }
76
77 // fall-back to file-system approach
78 if ( null == uri || !uri.isAbsolute() )
79 {
80 uri = new File( targetPath ).toURI();
81 }
82
83 return uri;
84 }
85
86
87 /**
Stuart McCulloch25687f82011-06-28 00:15:26 +000088 * @param project current project
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000089 * @return URI pointing to correct obr.xml, null if not found
90 */
Stuart McCulloch25687f82011-06-28 00:15:26 +000091 public static URI findObrXml( MavenProject project )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000092 {
Stuart McCulloch25687f82011-06-28 00:15:26 +000093 File obrFile = new File( project.getBuild().getOutputDirectory(), OBR_XML );
94 if ( obrFile.exists() )
95 {
96 return obrFile.toURI();
97 }
98 for ( Iterator i = project.getResources().iterator(); i.hasNext(); )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000099 {
100 Resource resource = ( Resource ) i.next();
Stuart McCulloch25687f82011-06-28 00:15:26 +0000101 obrFile = new File( resource.getDirectory(), OBR_XML );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000102 if ( obrFile.exists() )
103 {
104 return obrFile.toURI();
105 }
106 }
107 return null;
108 }
109
110
111 /**
112 * @param repository maven repository
113 * @param artifact maven artifact
114 * @return file URI pointing to artifact in repository
115 */
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000116 public static URI getArtifactURI( ArtifactRepository repository, Artifact artifact )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000117 {
118 String baseDir = repository.getBasedir();
119 String artifactPath = repository.pathOf( artifact );
120
121 return toFileURI( baseDir + '/' + artifactPath );
122 }
123
124
125 /**
126 * @param path filesystem path
127 * @return file URI for the path
128 */
129 public static URI toFileURI( String path )
130 {
131 if ( null == path )
132 {
133 return null;
134 }
135 else if ( path.startsWith( "file:" ) )
136 {
137 return URI.create( path );
138 }
139 else
140 {
141 return new File( path ).toURI();
142 }
143 }
144
145
146 /**
147 * @param repositoryXml URI pointing to repository.xml, or directory containing it
148 * @param bundleJar URI pointing to bundle jarfile
149 * @return relative URI to bundle jarfile
150 */
151 public static URI getRelativeURI( URI repositoryXml, URI bundleJar )
152 {
153 try
154 {
155 String repositoryPath = repositoryXml.getPath();
156 if ( repositoryPath.toLowerCase().endsWith( DOT_XML ) )
157 {
158 // remove filename to get containing directory
159 int dirnameIndex = repositoryPath.lastIndexOf( '/' );
160 repositoryPath = repositoryPath.substring( 0, dirnameIndex );
161 }
162
163 URI rootURI = new URI( null, repositoryPath, null );
164 URI localURI = new URI( null, bundleJar.getPath(), null );
165
166 return rootURI.relativize( localURI );
167 }
168 catch ( Exception e )
169 {
170 return bundleJar;
171 }
172 }
173}