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