Richard S. Hall | 85bafab | 2009-07-13 13:25:46 +0000 | [diff] [blame^] | 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 | |
| 20 | package org.cauldron.bld.ant; |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.IOException; |
| 24 | import java.util.jar.JarFile; |
| 25 | import java.util.jar.Manifest; |
| 26 | |
| 27 | import org.apache.tools.ant.BuildException; |
| 28 | import org.apache.tools.ant.Task; |
| 29 | |
| 30 | public class BundleInfoTask extends Task { |
| 31 | private File bundle; |
| 32 | private String header; |
| 33 | private String property; |
| 34 | private String defaultValue; |
| 35 | |
| 36 | @Override |
| 37 | public void execute() throws BuildException { |
| 38 | if (bundle == null) |
| 39 | throw new BuildException("missing attribute: bundle"); |
| 40 | if (header == null) |
| 41 | throw new BuildException("missing attribute: header"); |
| 42 | |
| 43 | try { |
| 44 | JarFile jar = new JarFile(bundle); |
| 45 | Manifest mf = jar.getManifest(); |
| 46 | String value = mf.getMainAttributes().getValue(header); |
| 47 | if ( property == null ) { |
| 48 | log(header + "=" + value); |
| 49 | } |
| 50 | else { |
| 51 | if ("Bundle-SymbolicName".equals(header) && value != null) { |
| 52 | // remove singleton flag |
| 53 | int semi = value.indexOf(';'); |
| 54 | if (semi > 0) |
| 55 | value = value.substring(0, semi); |
| 56 | } |
| 57 | if ( value == null ) { |
| 58 | value = defaultValue; |
| 59 | } |
| 60 | if ( value != null ) { |
| 61 | getProject().setNewProperty(property, value); |
| 62 | } |
| 63 | } |
| 64 | } catch (IOException e) { |
| 65 | throw new BuildException( "Failed to access bundle", e); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public void setBundle(String bundle) { |
| 70 | this.bundle = new File( bundle ); |
| 71 | } |
| 72 | |
| 73 | public void setHeader(String header) { |
| 74 | this.header = header; |
| 75 | } |
| 76 | |
| 77 | public void setProperty(String property) { |
| 78 | this.property = property; |
| 79 | } |
| 80 | |
| 81 | public void setDefaultValue(String defaultValue) { |
| 82 | this.defaultValue = defaultValue; |
| 83 | } |
| 84 | } |