001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }