001/*
002 * (C) Copyright 2006-2013 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 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.template.deckjs;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.net.URL;
025import java.util.Enumeration;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.Environment;
030import org.nuxeo.common.utils.FileUtils;
031import org.nuxeo.common.utils.Path;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.osgi.framework.BundleActivator;
034import org.osgi.framework.BundleContext;
035
036/**
037 * The activator expand the sample documents in the data directory.
038 *
039 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
040 * @author <a href="mailto:ldoguin@nuxeo.com">Laurent Doguin</a>
041 */
042public class TemplateBundleActivator implements BundleActivator {
043
044    protected static TemplateBundleActivator instance;
045
046    private BundleContext context;
047
048    protected static final Log log = LogFactory.getLog(TemplateBundleActivator.class);
049
050    private static File tmpDir;
051
052    private static String dataDirPath;
053
054    protected static String getTemplateResourcesRootPath() {
055        return "templatesamples";
056    }
057
058    public URL getResource(String path) {
059        return this.context.getBundle().getResource(path);
060    }
061
062    public Enumeration<?> findEntries(String path) {
063        return this.context.getBundle().findEntries(path, null, true);
064    }
065
066    public BundleContext getContext() {
067        return context;
068    }
069
070    @Override
071    public void start(BundleContext context) {
072        instance = this;
073        this.context = context;
074        initDataDirPath();
075        expandResources();
076    }
077
078    @Override
079    public void stop(BundleContext context) {
080        this.context = null;
081        cleanupDataDirPath();
082    }
083
084    /* Note that this may be called twice, because several activators inherit from this class. */
085    protected static void initDataDirPath() {
086        if (dataDirPath != null) {
087            return;
088        }
089        String dataDir = Environment.getDefault().getData().getPath();
090        Path path = new Path(dataDir);
091        path = path.append("resources");
092        dataDirPath = path.toString();
093    }
094
095    @SuppressWarnings("deprecation")
096    protected static void cleanupDataDirPath() {
097        if (tmpDir != null) {
098            FileUtils.deleteTree(tmpDir);
099            tmpDir = null;
100        }
101        dataDirPath = null;
102    }
103
104    protected static Path getDataDirPath() {
105        return new Path(dataDirPath);
106    }
107
108    public void expandResources() {
109        log.info("Deploying templates for bundle " + context.getBundle().getSymbolicName());
110
111        URL sampleRootURL = getResource(getTemplateResourcesRootPath());
112        if (sampleRootURL == null) {
113            return;
114        }
115
116        Path path = getDataDirPath();
117        path = path.append(getTemplateResourcesRootPath());
118        File dataDir = new File(path.toString());
119        if (!dataDir.exists()) {
120            dataDir.mkdirs();
121        }
122
123        Enumeration<?> urls = findEntries(getTemplateResourcesRootPath());
124        while (urls.hasMoreElements()) {
125            URL resourceURL = (URL) urls.nextElement();
126            try {
127                InputStream is = resourceURL.openStream();
128                String filePath = resourceURL.getFile();
129                filePath = filePath.split("/" + getTemplateResourcesRootPath() + "/")[1];
130                filePath = "/" + filePath;
131                File f = new File(dataDir, filePath);
132                File parent = f.getParentFile();
133                if (!parent.exists()) {
134                    parent.mkdirs();
135                }
136                FileUtils.copyToFile(is, f);
137                is.close();
138            } catch (IOException e) {
139                throw new NuxeoException("Failed for template: " + resourceURL, e);
140            }
141        }
142    }
143
144    public static InputStream getResourceAsStream(String path) throws IOException {
145        URL url = instance.getResource(path);
146        return url != null ? url.openStream() : null;
147    }
148
149}