View Javadoc
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.osgiplugin;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.util.Properties;
25  import java.util.jar.JarFile;
26  import java.util.jar.Manifest;
27  
28  import org.apache.maven.plugin.eclipse.InstallPluginsMojo;
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.archiver.jar.JarArchiver;
31  
32  /**
33   * Represents an Eclipse plugin that it's exploded in a directory
34   * 
35   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
36   * @version $Id: ExplodedPlugin.java 728546 2008-12-21 22:56:51Z bentmann $
37   */
38  public class ExplodedPlugin
39      extends AbstractEclipseOsgiPlugin
40  {
41  
42      private File tempJarFile;
43  
44      public ExplodedPlugin( File folder )
45      {
46          super( folder );
47      }
48  
49      private File getManifestFile()
50      {
51          return new File( getFile(), "META-INF/MANIFEST.MF" );
52      }
53  
54      public Manifest getManifest()
55          throws IOException
56      {
57          if ( !getManifestFile().exists() )
58          {
59              return null;
60          }
61  
62          FileInputStream is = new FileInputStream( getManifestFile() );
63          try
64          {
65              return new Manifest( is );
66          }
67          finally
68          {
69              is.close();
70          }
71      }
72  
73      public boolean hasManifest()
74      {
75          return getManifestFile().exists();
76      }
77  
78      public File getJarFile()
79          throws IOException
80      {
81          if ( tempJarFile == null )
82          {
83              try
84              {
85                  tempJarFile = File.createTempFile( "mvn-eclipse", null );
86                  tempJarFile.deleteOnExit();
87  
88                  JarArchiver jarArchiver = new JarArchiver();
89  
90                  jarArchiver.setDestFile( tempJarFile );
91                  jarArchiver.addDirectory( getFile() );
92                  jarArchiver.setManifest( getManifestFile() );
93                  jarArchiver.createArchive();
94  
95                  return tempJarFile;
96              }
97              catch ( ArchiverException e )
98              {
99                  // TODO only accepts the cause as parameter in JDK 1.6+
100                 throw new IOException( e.getMessage() );
101             }
102         }
103         return tempJarFile;
104     }
105 
106     public JarFile getJar()
107         throws IOException
108     {
109         return new JarFile( getJarFile(), false );
110     }
111 
112     /**
113      * set the pom property to install unpacked if it was unpacked
114      */
115     public Properties getPomProperties()
116     {
117         Properties properties = new Properties();
118         properties.setProperty( InstallPluginsMojo.PROP_UNPACK_PLUGIN, Boolean.TRUE.toString() );
119         return properties;
120     }
121 
122 }