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.IOException;
23  import java.io.InputStream;
24  import java.util.Properties;
25  import java.util.jar.JarFile;
26  import java.util.zip.ZipEntry;
27  
28  /**
29   * Common functionality for both exploded and packaged plugins.
30   * 
31   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
32   * @version $Id: AbstractEclipseOsgiPlugin.java 728546 2008-12-21 22:56:51Z bentmann $
33   */
34  public abstract class AbstractEclipseOsgiPlugin
35      implements EclipseOsgiPlugin
36  {
37  
38      private File file;
39  
40      private Properties pluginProperties;
41  
42      public AbstractEclipseOsgiPlugin( File file )
43      {
44          this.setFile( file );
45      }
46  
47      public void setFile( File file )
48      {
49          this.file = file;
50      }
51  
52      public File getFile()
53      {
54          return file;
55      }
56  
57      public String toString()
58      {
59          return getFile().getAbsolutePath();
60      }
61  
62      public Properties getPluginProperties()
63          throws IOException
64      {
65          if ( pluginProperties == null )
66          {
67              JarFile file = getJar();
68              InputStream pluginPropertiesStream = null;
69              try
70              {
71                  pluginProperties = new Properties();
72                  ZipEntry jarEntry = file.getEntry( "plugin.properties" );
73                  if ( jarEntry != null )
74                  {
75                      pluginPropertiesStream = file.getInputStream( jarEntry );
76                      pluginProperties.load( pluginPropertiesStream );
77                  }
78              }
79              finally
80              {
81                  if ( pluginPropertiesStream != null )
82                  {
83                      try
84                      {
85                          pluginPropertiesStream.close();
86                      }
87                      catch ( IOException e )
88                      {
89                          // ignore
90                      }
91                  }
92              }
93          }
94          return pluginProperties;
95      }
96  
97      public Properties getPomProperties()
98      {
99          return new Properties();
100     }
101 
102     public String getManifestAttribute( String key )
103         throws IOException
104     {
105         String value = getManifest().getMainAttributes().getValue( key );
106 
107         if ( value == null )
108         {
109             return null;
110         }
111 
112         /* check the plugin properties for translations */
113         if ( value.startsWith( "%" ) )
114         {
115             String valueFromProperties = getPluginProperties().getProperty( value.substring( 1 ) );
116             if ( valueFromProperties != null )
117             {
118                 value = valueFromProperties;
119             }
120         }
121 
122         return value;
123     }
124 }