001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.install;
023
024import java.io.File;
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.List;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.common.utils.FileUtils;
032import org.nuxeo.common.utils.ZipUtils;
033import org.nuxeo.common.xmap.annotation.XNode;
034import org.nuxeo.common.xmap.annotation.XNodeList;
035import org.nuxeo.common.xmap.annotation.XObject;
036import org.nuxeo.ecm.webengine.model.exceptions.WebDeployException;
037import org.nuxeo.runtime.api.Framework;
038import org.nuxeo.runtime.model.RuntimeContext;
039import org.osgi.framework.Bundle;
040
041/**
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044@XObject("install")
045public class Installer {
046
047    private static final Log log = LogFactory.getLog(Installer.class);
048
049    @XNode("@module")
050    public String module;
051
052    @XNodeList(value = "copy", type = ArrayList.class, componentType = CopyOperation.class)
053    private List<CopyOperation> copyOperations;
054
055    @XNodeList(value = "append", type = ArrayList.class, componentType = AppendOperation.class)
056    private List<AppendOperation> appendOperations;
057
058    protected RuntimeContext ctx;
059
060    public Bundle getBundle() {
061        return ctx.getBundle();
062    }
063
064    public RuntimeContext getContext() {
065        return ctx;
066    }
067
068    public void logError(String message, Throwable t) {
069        log.error(message, t);
070    }
071
072    public void logError(String message) {
073        log.error(message);
074    }
075
076    public void logInfo(String message) {
077        log.error(message);
078    }
079
080    public void logWarning(String message) {
081        log.error(message);
082    }
083
084    public void logTrace(String message) {
085        log.error(message);
086    }
087
088    public void install(RuntimeContext ctx, File installDir) {
089        this.ctx = ctx;
090        if (module != null) {
091            if (new File(installDir, module).exists()) {
092                return;
093            }
094        }
095        boolean deleteDir = false;
096        File bundleDir = null;
097        try {
098            Bundle bundle = ctx.getBundle();
099            File file = getBundleFile(bundle);
100            if (file == null) {
101                throw new IOException("Couldn't transform the bundle location into a file " + bundle);
102            }
103            if (file.isDirectory()) {
104                bundleDir = file;
105            } else {
106                deleteDir = true;
107                bundleDir = getTempBundleDir(bundle);
108                ZipUtils.unzip(file, bundleDir);
109            }
110            if (copyOperations != null) {
111                for (CopyOperation copy : copyOperations) {
112                    copy.run(this, bundleDir, installDir);
113                }
114            }
115            if (appendOperations != null) {
116                for (AppendOperation append : appendOperations) {
117                    append.run(this, bundleDir, installDir);
118                }
119            }
120        } catch (IOException e) {
121            throw new WebDeployException("Installation failed for bundle: " + ctx.getBundle().getSymbolicName(), e);
122        } finally {
123            if (deleteDir && bundleDir != null) {
124                org.apache.commons.io.FileUtils.deleteQuietly(bundleDir);
125            }
126        }
127    }
128
129    public void uninstall(RuntimeContext ctx, File installDir) {
130        // TODO
131    }
132
133    public static void copyResources(Bundle bundle, String path, File root) throws IOException {
134        File file = Framework.getRuntime().getBundleFile(bundle);
135        if (file == null) {
136            throw new UnsupportedOperationException("Couldn't transform the bundle location into a file");
137        }
138        root.mkdirs();
139        if (file.isDirectory()) {
140            file = new File(file, path);
141            FileUtils.copy(file.listFiles(), root);
142        } else {
143            ZipUtils.unzip(path, file, root);
144        }
145    }
146
147    protected File getTempBundleDir(Bundle bundle) {
148        return new File(Framework.getRuntime().getHome(), "tmp/bundles/" + bundle.getSymbolicName());
149    }
150
151    protected File getBundleFile(Bundle bundle) {
152        return Framework.getRuntime().getBundleFile(bundle);
153    }
154
155}