001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.install;
021
022import java.io.File;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.utils.FileUtils;
030import org.nuxeo.common.utils.ZipUtils;
031import org.nuxeo.common.xmap.annotation.XNode;
032import org.nuxeo.common.xmap.annotation.XNodeList;
033import org.nuxeo.common.xmap.annotation.XObject;
034import org.nuxeo.ecm.webengine.model.exceptions.WebDeployException;
035import org.nuxeo.runtime.api.Framework;
036import org.nuxeo.runtime.model.RuntimeContext;
037import org.osgi.framework.Bundle;
038
039/**
040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
041 */
042@XObject("install")
043public class Installer {
044
045    private static final Log log = LogFactory.getLog(Installer.class);
046
047    @XNode("@module")
048    public String module;
049
050    @XNodeList(value = "copy", type = ArrayList.class, componentType = CopyOperation.class)
051    private List<CopyOperation> copyOperations;
052
053    @XNodeList(value = "append", type = ArrayList.class, componentType = AppendOperation.class)
054    private List<AppendOperation> appendOperations;
055
056    protected RuntimeContext ctx;
057
058    public Bundle getBundle() {
059        return ctx.getBundle();
060    }
061
062    public RuntimeContext getContext() {
063        return ctx;
064    }
065
066    public void logError(String message, Throwable t) {
067        log.error(message, t);
068    }
069
070    public void logError(String message) {
071        log.error(message);
072    }
073
074    public void logInfo(String message) {
075        log.error(message);
076    }
077
078    public void logWarning(String message) {
079        log.error(message);
080    }
081
082    public void logTrace(String message) {
083        log.error(message);
084    }
085
086    public void install(RuntimeContext ctx, File installDir) {
087        this.ctx = ctx;
088        if (module != null) {
089            if (new File(installDir, module).exists()) {
090                return;
091            }
092        }
093        boolean deleteDir = false;
094        File bundleDir = null;
095        try {
096            Bundle bundle = ctx.getBundle();
097            File file = getBundleFile(bundle);
098            if (file == null) {
099                throw new IOException("Couldn't transform the bundle location into a file " + bundle);
100            }
101            if (file.isDirectory()) {
102                bundleDir = file;
103            } else {
104                deleteDir = true;
105                bundleDir = getTempBundleDir(bundle);
106                ZipUtils.unzip(file, bundleDir);
107            }
108            if (copyOperations != null) {
109                for (CopyOperation copy : copyOperations) {
110                    copy.run(this, bundleDir, installDir);
111                }
112            }
113            if (appendOperations != null) {
114                for (AppendOperation append : appendOperations) {
115                    append.run(this, bundleDir, installDir);
116                }
117            }
118        } catch (IOException e) {
119            throw new WebDeployException("Installation failed for bundle: " + ctx.getBundle().getSymbolicName(), e);
120        } finally {
121            if (deleteDir && bundleDir != null) {
122                FileUtils.deleteTree(bundleDir);
123            }
124        }
125    }
126
127    public void uninstall(RuntimeContext ctx, File installDir) {
128        // TODO
129    }
130
131    public static void copyResources(Bundle bundle, String path, File root) throws IOException {
132        File file = Framework.getRuntime().getBundleFile(bundle);
133        if (file == null) {
134            throw new UnsupportedOperationException("Couldn't transform the bundle location into a file");
135        }
136        root.mkdirs();
137        if (file.isDirectory()) {
138            file = new File(file, path);
139            FileUtils.copy(file.listFiles(), root);
140        } else {
141            ZipUtils.unzip(path, file, root);
142        }
143    }
144
145    protected File getTempBundleDir(Bundle bundle) {
146        return new File(Framework.getRuntime().getHome(), "tmp/bundles/" + bundle.getSymbolicName());
147    }
148
149    protected File getBundleFile(Bundle bundle) {
150        return Framework.getRuntime().getBundleFile(bundle);
151    }
152
153}