001/* 002 * (C) Copyright 2006-2016 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 */ 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.io.FileUtils; 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.common.Environment; 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:[email protected]">Tiry</a> 040 * @author <a href="mailto:[email protected]">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.deleteQuietly(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 (InputStream is = resourceURL.openStream()) { 127 String filePath = resourceURL.getFile(); 128 filePath = filePath.split("/" + getTemplateResourcesRootPath() + "/")[1]; 129 filePath = "/" + filePath; 130 File f = new File(dataDir, filePath); 131 File parent = f.getParentFile(); 132 if (!parent.exists()) { 133 parent.mkdirs(); 134 } 135 FileUtils.copyInputStreamToFile(is, f); 136 } catch (IOException e) { 137 throw new NuxeoException("Failed for template: " + resourceURL, e); 138 } 139 } 140 } 141 142 public static InputStream getResourceAsStream(String path) throws IOException { 143 URL url = instance.getResource(path); 144 return url != null ? url.openStream() : null; 145 } 146 147}