View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar;
21  
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.util.Properties;
28  import java.util.jar.JarEntry;
29  import java.util.jar.JarFile;
30  
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.logging.Log;
33  
34  /**
35   * @author Mark Donszelmann
36   */
37  public class NarInfo {
38  
39    public static final String NAR_PROPERTIES = "nar.properties";
40  
41    private final String groupId, artifactId, version;
42  
43    private final Properties info;
44  
45    private final Log log;
46  
47    public NarInfo(final String groupId, final String artifactId, final String version, final Log log)
48        throws MojoExecutionException {
49      this(groupId, artifactId, version, log, null);
50    }
51  
52    public NarInfo(final String groupId, final String artifactId, final String version, final Log log, File propertiesFile)
53        throws MojoExecutionException {
54      this.groupId = groupId;
55      this.artifactId = artifactId;
56      this.version = version;
57      this.log = log;
58      this.info = new Properties();
59  
60      // Fill with general properties.nar file
61      if (propertiesFile != null) {
62        try {
63          if (propertiesFile.isDirectory()) {
64            propertiesFile = new File(propertiesFile, getNarInfoFileName());
65          }
66          this.info.load(new FileInputStream(propertiesFile));
67        } catch (final FileNotFoundException e) {
68          // ignored
69        } catch (final IOException e) {
70          throw new MojoExecutionException("Problem loading " + propertiesFile, e);
71        }
72      }
73    }
74  
75    public final void addNar(final AOL aol, final String type, final String nar) {
76      String nars = getProperty(aol, NarConstants.NAR + "." + type);
77      nars = nars == null ? nar : nars + ", " + nar;
78      setProperty(aol, NarConstants.NAR + "." + type, nars);
79    }
80  
81    public final boolean exists(final JarFile jar) {
82      return getNarPropertiesEntry(jar) != null;
83    }
84  
85    public final AOL getAOL(final AOL aol) {
86      return aol == null ? null : new AOL(getProperty(aol, aol.toString(), aol.toString()));
87    }
88  
89    // FIXME replace with list of AttachedNarArtifacts
90    public final String[] getAttachedNars(final AOL aol, final String type) {
91      final String attachedNars = getProperty(aol, NarConstants.NAR + "." + type);
92      return attachedNars != null ? attachedNars.split(",") : null;
93    }
94  
95    /**
96     * No binding means default binding.
97     *
98     * @param aol
99     * @return
100    */
101   public final String getBinding(final AOL aol, final String defaultBinding) {
102     return getProperty(aol, "libs.binding", defaultBinding);
103   }
104 
105   public final String getExactProperty(final AOL aol, final String key, final String defaultValue) {
106     if (key == null) {
107       throw new NullPointerException();
108     }
109     final String value = this.info.getProperty((aol == null ? "" : aol.toString() + ".") + key, defaultValue);
110     this.log.debug("getExactProperty(" + aol + ", " + key + ", " + defaultValue + ") = " + value);
111     return value;
112   }
113 
114   public final String getLibs(final AOL aol) {
115     // resolve output Vs libs.names
116     return getProperty(aol, "libs.names", getOutput(aol, this.artifactId + "-" + this.version));
117   }
118 
119   public String getNarInfoFileName() {
120     return "META-INF/nar/" + this.groupId + "/" + this.artifactId + "/" + NAR_PROPERTIES;
121   }
122 
123   private JarEntry getNarPropertiesEntry(final JarFile jar) {
124     return jar.getJarEntry(getNarInfoFileName());
125   }
126 
127   public final String getOptions(final AOL aol) {
128     return getProperty(aol, "linker.options");
129   }
130 
131   public final String getOutput(final AOL aol, final String defaultOutput) {
132     return getExactProperty(aol, "output", defaultOutput);
133   }
134 
135   public final String getProperty(final AOL aol, final String key) {
136     return getProperty(aol, key, (String) null);
137   }
138 
139   public final boolean getProperty(final AOL aol, final String key, final boolean defaultValue) {
140     return Boolean.valueOf(getProperty(aol, key, String.valueOf(defaultValue))).booleanValue();
141   }
142 
143   public final File getProperty(final AOL aol, final String key, final File defaultValue) {
144     return new File(getProperty(aol, key, defaultValue.getPath()));
145   }
146 
147   public final int getProperty(final AOL aol, final String key, final int defaultValue) {
148     return Integer.parseInt(getProperty(aol, key, Integer.toString(defaultValue)));
149   }
150 
151   public final String getProperty(final AOL aol, final String key, final String defaultValue) {
152     if (key == null) {
153       return defaultValue;
154     }
155     String value = this.info.getProperty(key, defaultValue);
156     value = aol == null ? value : this.info.getProperty(aol.toString() + "." + key, value);
157     this.log.debug("getProperty(" + aol + ", " + key + ", " + defaultValue + ") = " + value);
158     return value;
159   }
160 
161   public final String getSysLibs(final AOL aol) {
162     return getProperty(aol, "syslibs.names");
163   }
164 
165   public final void read(final JarFile jar) throws IOException {
166     this.info.load(jar.getInputStream(getNarPropertiesEntry(jar)));
167   }
168 
169   public final void setBinding(final AOL aol, final String value) {
170     setProperty(aol, "libs.binding", value);
171   }
172 
173   public final void setNar(final AOL aol, final String type, final String nar) {
174     setProperty(aol, NarConstants.NAR + "." + type, nar);
175   }
176 
177   public final void setOutput(final AOL aol, final String value) {
178     setProperty(aol, "output", value);
179   }
180 
181   public final void setLibs(final AOL aol, final String value) {
182     setProperty(aol, "libs.names", value);
183   }
184 
185   private void setProperty(final AOL aol, final String key, final String value) {
186     if (aol == null) {
187       this.info.setProperty(key, value);
188     } else {
189       this.info.setProperty(aol.toString() + "." + key, value);
190     }
191   }
192 
193   @Override
194   public final String toString() {
195     final StringBuffer s = new StringBuffer("NarInfo for ");
196     s.append(this.groupId);
197     s.append(":");
198     s.append(this.artifactId);
199     s.append("-");
200     s.append(this.version);
201     s.append(" {\n");
202 
203     for (final Object element : this.info.keySet()) {
204       final String key = (String) element;
205       s.append("   ");
206       s.append(key);
207       s.append("='");
208       s.append(this.info.getProperty(key, "<null>"));
209       s.append("'\n");
210     }
211 
212     s.append("}\n");
213     return s.toString();
214   }
215 
216   public final void writeToDirectory(final File directory) throws MojoExecutionException {
217     try {
218       writeToFile(new File(directory, getNarInfoFileName()));
219     } catch (final IOException ioe) {
220       throw new MojoExecutionException("Cannot write nar properties file to " + directory, ioe);
221     }
222   }
223 
224   public final void writeToFile(final File file) throws IOException {
225     final File parent = file.getParentFile();
226     if (parent != null) {
227       parent.mkdirs();
228     }
229     this.info.store(new FileOutputStream(file), "NAR Properties for " + this.groupId + "." + this.artifactId + "-"
230         + this.version);
231   }
232 }