Coverage Report - org.apache.maven.plugin.eclipse.writers.EclipseOSGiManifestWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseOSGiManifestWriter
15%
12/79
13%
5/38
6,25
 
 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.maven.plugin.eclipse.writers;
 20  
 
 21  
 import java.io.BufferedReader;
 22  
 import java.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.FileNotFoundException;
 25  
 import java.io.FileOutputStream;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStreamReader;
 28  
 import java.io.OutputStreamWriter;
 29  
 import java.io.Writer;
 30  
 import java.util.Arrays;
 31  
 
 32  
 import org.apache.maven.plugin.MojoExecutionException;
 33  
 import org.apache.maven.plugin.eclipse.Messages;
 34  
 import org.apache.maven.plugin.ide.IdeDependency;
 35  
 import org.codehaus.plexus.util.IOUtil;
 36  
 import org.codehaus.plexus.util.StringUtils;
 37  
 
 38  
 /**
 39  
  * The <code>EclipseOSGiManifestWriter</code> ensures that value of the "Bundle-Classpath" property in
 40  
  * META-INF/MANIFEST.MF is synchronized with the POM by adding all dependencies that don't have the scope provided.
 41  
  * 
 42  
  * @deprecated use <a href="http://cwiki.apache.org/FELIX/bundle-plugin-for-maven-bnd.html/">Maven Bundle plugin</a>
 43  
  *             from Felix
 44  
  */
 45  0
 public class EclipseOSGiManifestWriter
 46  
     extends AbstractEclipseWriter
 47  
 {
 48  
 
 49  
     /**
 50  
      * Constant used for newline.
 51  
      * 
 52  
      * @todo check if we should use system-dependent newlines or if eclipse prefers a common format
 53  
      */
 54  
     private static final String NEWLINE = "\n";
 55  
 
 56  
     /**
 57  
      * Bundle classpath: updated with the list of dependencies.
 58  
      */
 59  
     public final static String ENTRY_BUNDLE_CLASSPATH = "Bundle-ClassPath:";
 60  
 
 61  
     /**
 62  
      * Bundle name: updated with the project name.
 63  
      */
 64  
     public final static String ENTRY_BUNDLE_NAME = "Bundle-Name:";
 65  
 
 66  
     /**
 67  
      * Bundle symbolic name: updated with the artifact id.
 68  
      */
 69  
     public final static String ENTRY_BUNDLE_SYMBOLICNAME = "Bundle-SymbolicName:";
 70  
 
 71  
     /**
 72  
      * Bundle version: updated with the project version.
 73  
      */
 74  
     public final static String ENTRY_BUNDLE_VERSION = "Bundle-Version:";
 75  
 
 76  
     /**
 77  
      * Bundle vendor: updated with the organization name (if set in the POM).
 78  
      */
 79  
     public final static String ENTRY_BUNDLE_VENDOR = "Bundle-Vendor:";
 80  
 
 81  
     /**
 82  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 83  
      */
 84  
     public void write()
 85  
         throws MojoExecutionException
 86  
     {
 87  
         // check for existence
 88  0
         if ( !config.getOSGIManifestFile().exists() )
 89  
         {
 90  0
             log.warn( Messages.getString( "EclipseOSGiManifestWriter.nomanifestfile",
 91  
                                           config.getOSGIManifestFile().getAbsolutePath() ) );
 92  0
             return;
 93  
         }
 94  
 
 95  0
         StringBuffer manifestSb = rewriteManifest( config.getOSGIManifestFile() );
 96  0
         Writer out = null;
 97  
         try
 98  
         {
 99  0
             out = new OutputStreamWriter( new FileOutputStream( config.getOSGIManifestFile() ), "UTF-8" );
 100  0
             out.write( manifestSb.toString() );
 101  
         }
 102  0
         catch ( FileNotFoundException e )
 103  
         {
 104  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile",
 105  
                                                                   config.getOSGIManifestFile().getAbsolutePath() ) );
 106  
         }
 107  0
         catch ( IOException e )
 108  
         {
 109  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile",
 110  
                                                                   config.getOSGIManifestFile().getAbsolutePath() ), e );
 111  
         }
 112  
         finally
 113  
         {
 114  0
             IOUtil.close( out );
 115  0
         }
 116  0
     }
 117  
 
 118  
     protected StringBuffer rewriteManifest( File manifestFile )
 119  
         throws MojoExecutionException
 120  
     {
 121  
 
 122  
         // warning: we read and rewrite the file line by line in order to preserve formatting
 123  0
         boolean inBundleClasspathEntry = false;
 124  0
         StringBuffer manifestSb = new StringBuffer();
 125  
         try
 126  
         {
 127  0
             BufferedReader in =
 128  
                 new BufferedReader( new InputStreamReader( new FileInputStream( manifestFile ), "UTF-8" ) );
 129  
             String line;
 130  0
             while ( ( line = in.readLine() ) != null )
 131  
             {
 132  0
                 if ( inBundleClasspathEntry && line.indexOf( ":" ) > -1 )
 133  
                 {
 134  0
                     inBundleClasspathEntry = false;
 135  
                 }
 136  0
                 else if ( inBundleClasspathEntry )
 137  
                 {
 138  
                     // skip it
 139  0
                     continue;
 140  
                 }
 141  
 
 142  
                 // Note that this could be the empty string, if we encounter
 143  
                 // a field that we weren't expecting to be multi-line.
 144  0
                 String name = line.substring( 0, line.indexOf( ":" ) + 1 );
 145  
 
 146  0
                 if ( name.equalsIgnoreCase( ENTRY_BUNDLE_CLASSPATH ) )
 147  
                 {
 148  0
                     inBundleClasspathEntry = true;
 149  
                 }
 150  0
                 else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_NAME ) )
 151  
                 {
 152  0
                     manifestSb.append( ENTRY_BUNDLE_NAME );
 153  0
                     manifestSb.append( " " );
 154  0
                     manifestSb.append( config.getProject().getName() );
 155  0
                     manifestSb.append( NEWLINE );
 156  
                 }
 157  0
                 else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_SYMBOLICNAME ) )
 158  
                 {
 159  0
                     manifestSb.append( ENTRY_BUNDLE_SYMBOLICNAME );
 160  0
                     manifestSb.append( " " );
 161  0
                     manifestSb.append( config.getEclipseProjectName() );
 162  0
                     manifestSb.append( ";singleton:=true" );
 163  0
                     manifestSb.append( NEWLINE );
 164  
                 }
 165  0
                 else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_VERSION ) )
 166  
                 {
 167  0
                     manifestSb.append( ENTRY_BUNDLE_VERSION );
 168  0
                     manifestSb.append( " " );
 169  0
                     manifestSb.append( getNormalizedVersion( config.getProject().getVersion() ) );
 170  0
                     manifestSb.append( NEWLINE );
 171  
                 }
 172  0
                 else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_VENDOR ) && config.getProject().getOrganization() != null )
 173  
                 {
 174  0
                     manifestSb.append( ENTRY_BUNDLE_VENDOR );
 175  0
                     manifestSb.append( " " );
 176  0
                     manifestSb.append( config.getProject().getOrganization().getName() );
 177  0
                     manifestSb.append( NEWLINE );
 178  
                 }
 179  
                 else
 180  
                 {
 181  0
                     manifestSb.append( line + NEWLINE );
 182  
                 }
 183  0
             }
 184  
 
 185  0
             IOUtil.close( in );
 186  
         }
 187  0
         catch ( IOException e )
 188  
         {
 189  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantreadfile",
 190  
                                                                   manifestFile.getAbsolutePath() ) );
 191  0
         }
 192  0
         manifestSb.append( addBundleClasspathEntries() );
 193  
 
 194  
         // OSGi manifest headers need to end with a line break
 195  0
         manifestSb.append( NEWLINE );
 196  0
         return manifestSb;
 197  
     }
 198  
 
 199  
     /**
 200  
      * Normalize a version number, by moving snapshot identifier to the 5th token (first 4 tokens must be numeric for
 201  
      * OSGI bundles)
 202  
      * 
 203  
      * @param version original version
 204  
      * @return a normalized version number
 205  
      */
 206  
     protected static String getNormalizedVersion( String version )
 207  
     {
 208  
 
 209  18
         if ( version.endsWith( "-SNAPSHOT" ) )
 210  
         {
 211  18
             String[] versionTokens = StringUtils.split( StringUtils.stripEnd( version, "-SNAPSHOT" ), "." );
 212  
 
 213  18
             int j = 0;
 214  18
             StringBuffer newVersion = new StringBuffer( 20 );
 215  126
             for ( ; j < versionTokens.length; j++ )
 216  
             {
 217  54
                 newVersion.append( versionTokens[j] );
 218  54
                 newVersion.append( "." );
 219  
             }
 220  30
             for ( ; j < 3; j++ )
 221  
             {
 222  6
                 newVersion.append( "0." );
 223  
             }
 224  
 
 225  18
             newVersion.append( "SNAPSHOT" );
 226  18
             version = newVersion.toString();
 227  
 
 228  
         }
 229  18
         return version;
 230  
     }
 231  
 
 232  
     /**
 233  
      * Add all libraries that don't have the scope "provided" to the "Bundle-Classpath".
 234  
      * 
 235  
      * @return complete "Bundle-ClassPath:" entry for manifest
 236  
      */
 237  
     protected String addBundleClasspathEntries()
 238  
     {
 239  0
         StringBuffer bundleClasspathSb = new StringBuffer( ENTRY_BUNDLE_CLASSPATH );
 240  
 
 241  
         // local classes, if the plugin is jarred
 242  
         // @todo handle expanded plugins
 243  0
         bundleClasspathSb.append( " ." );
 244  
 
 245  0
         IdeDependency[] deps = config.getDepsOrdered();
 246  
 
 247  
         // since Manifest is supposed to be in SVN, having the order of classpath entries shuffled at each run is very
 248  
         // annoying. For now just sort them by using groupId/artifactId
 249  0
         Arrays.sort( deps );
 250  
 
 251  0
         for ( int j = 0; j < deps.length; j++ )
 252  
         {
 253  0
             IdeDependency dep = deps[j];
 254  0
             if ( !dep.isProvided() && !dep.isReferencedProject() && !dep.isTestDependency() && !dep.isOsgiBundle() )
 255  
             {
 256  0
                 bundleClasspathSb.append( "," + NEWLINE );
 257  
 
 258  0
                 log.debug( "Adding artifact to manifest: " + dep.getArtifactId() );
 259  
 
 260  0
                 bundleClasspathSb.append( " " + dep.getFile().getName() );
 261  
             }
 262  
         }
 263  
         // only insert the name of the property if there are local libraries
 264  0
         return bundleClasspathSb.toString();
 265  
     }
 266  
 
 267  
 }