    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
    21
    22  import java.io.File;
    23  import java.net.URI;
    24  import java.util.Iterator;
    25  import java.util.regex.Pattern;
    26
    27  import org.apache.maven.artifact.Artifact;
    28  import org.apache.maven.artifact.repository.ArtifactRepository;
    29  import org.apache.maven.model.Resource;
    30  import org.apache.maven.project.MavenProject;
    31
    32
    33  /**
    34   * Various OBR utility methods
    35   *
    36   * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
    37   */
    38  public 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
    54          Pattern ignoredNames = Pattern.compile( "^(true|false|none|null)?$", Pattern.CASE_INSENSITIVE );
    55
    56          // Combine location settings into a single repository location
    57          if ( null == targetPath || ignoredNames.matcher( targetPath ).matches() )
    58          {
    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      /**
    88       * @param project current project
    89       * @return URI pointing to correct obr.xml, null if not found
    90       */
    91      public static URI findObrXml( MavenProject project )
    92      {
    93          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(); )
    99          {
    100             Resource resource = ( Resource ) i.next();
    101             obrFile = new File( resource.getDirectory(), OBR_XML );
    102             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      */
    116     public static URI getArtifactURI( ArtifactRepository repository, Artifact artifact )
    117     {
    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 }