001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util.ant.bnd;
016    
017    import aQute.bnd.ant.BndTask;
018    import aQute.bnd.build.Workspace;
019    
020    import java.io.File;
021    
022    import java.util.Properties;
023    
024    import org.apache.tools.ant.BuildException;
025    import org.apache.tools.ant.Project;
026    
027    /**
028     * @author Raymond Augé
029     */
030    public abstract class BaseBndTask extends BndTask {
031    
032            @Override
033            public void execute() throws BuildException {
034                    try {
035                            project = getProject();
036    
037                            doBeforeExecute();
038                            doExecute();
039                    }
040                    catch (Exception e) {
041                            if (e instanceof BuildException) {
042                                    throw (BuildException)e;
043                            }
044    
045                            throw new BuildException(e);
046                    }
047            }
048    
049            @SuppressWarnings("unchecked")
050            public aQute.bnd.build.Project getBndProject() throws Exception {
051                    File bndRootFile = getBndRootFile();
052    
053                    Workspace workspace = Workspace.getWorkspace(
054                            bndRootFile.getParentFile(), getBndDirName());
055    
056                    aQute.bnd.build.Project bndProject = new aQute.bnd.build.Project(
057                            workspace, bndRootFile.getParentFile());
058    
059                    bndProject.setFileMustExist(true);
060                    bndProject.setProperties(bndRootFile);
061    
062                    Properties properties = new Properties();
063    
064                    properties.putAll(project.getProperties());
065                    properties.putAll(bndProject.getProperties());
066    
067                    bndProject.setProperties(properties);
068    
069                    return bndProject;
070            }
071    
072            public File getBndRootFile() {
073                    return _bndRootFile;
074            }
075    
076            public void setBndRootFile(File bndRootFile) {
077                    _bndRootFile = bndRootFile;
078            }
079    
080            protected void doBeforeExecute() throws Exception {
081                    if ((_bndRootFile == null) || !_bndRootFile.exists() ||
082                            _bndRootFile.isDirectory()) {
083    
084                            if (_bndRootFile != null) {
085                                    project.log(
086                                            "bndRootFile is either missing or is a directory " +
087                                                    _bndRootFile.getAbsolutePath(),
088                                            Project.MSG_ERR);
089                            }
090    
091                            throw new Exception("bndRootFile is invalid");
092                    }
093    
094                    _bndRootFile = _bndRootFile.getAbsoluteFile();
095            }
096    
097            protected abstract void doExecute() throws Exception;
098    
099            protected String getBndDirName() {
100                    if (_bndDirName != null) {
101                            return _bndDirName;
102                    }
103    
104                    _bndDirName = project.getProperty("baseline.jar.bnddir.name");
105    
106                    if (_bndDirName == null) {
107                            _bndDirName = _BND_DIR;
108                    }
109    
110                    return _bndDirName;
111            }
112    
113            protected Project project;
114    
115            private static final String _BND_DIR = ".bnd";
116    
117            private String _bndDirName;
118            private File _bndRootFile;
119    
120    }