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.samples.importer; 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 private BundleContext context; 045 046 protected static final Log log = LogFactory.getLog(TemplateBundleActivator.class); 047 048 private static File tmpDir; 049 050 private static String dataDirPath; 051 052 protected static String getTemplateResourcesRootPath() { 053 return ModelImporter.RESOURCES_ROOT; 054 } 055 056 public URL getResource(String path) { 057 return this.context.getBundle().getResource(path); 058 } 059 060 public Enumeration<?> findEntries(String path) { 061 return this.context.getBundle().findEntries(path, null, true); 062 } 063 064 public BundleContext getContext() { 065 return context; 066 } 067 068 @Override 069 public void start(BundleContext context) { 070 this.context = context; 071 initDataDirPath(); 072 expandResources(); 073 } 074 075 @Override 076 public void stop(BundleContext context) { 077 this.context = null; 078 cleanupDataDirPath(); 079 } 080 081 /* Note that this may be called twice, because several activators inherit from this class. */ 082 protected static void initDataDirPath() { 083 if (dataDirPath != null) { 084 return; 085 } 086 String dataDir = Environment.getDefault().getData().getPath(); 087 Path path = new Path(dataDir); 088 path = path.append("resources"); 089 dataDirPath = path.toString(); 090 } 091 092 @SuppressWarnings("deprecation") 093 protected static void cleanupDataDirPath() { 094 if (tmpDir != null) { 095 FileUtils.deleteQuietly(tmpDir); 096 tmpDir = null; 097 } 098 dataDirPath = null; 099 } 100 101 protected static Path getDataDirPath() { 102 return new Path(dataDirPath); 103 } 104 105 public void expandResources() { 106 log.info("Deploying templates for bundle " + context.getBundle().getSymbolicName()); 107 108 URL sampleRootURL = getResource(getTemplateResourcesRootPath()); 109 if (sampleRootURL == null) { 110 return; 111 } 112 113 Path path = getDataDirPath(); 114 path = path.append(getTemplateResourcesRootPath()); 115 File dataDir = new File(path.toString()); 116 if (!dataDir.exists()) { 117 dataDir.mkdirs(); 118 } 119 120 Enumeration<?> urls = findEntries(getTemplateResourcesRootPath()); 121 while (urls.hasMoreElements()) { 122 URL resourceURL = (URL) urls.nextElement(); 123 try (InputStream is = resourceURL.openStream()) { 124 String filePath = resourceURL.getFile(); 125 filePath = filePath.split("/" + getTemplateResourcesRootPath() + "/")[1]; 126 filePath = "/" + filePath; 127 File f = new File(dataDir, filePath); 128 File parent = f.getParentFile(); 129 if (!parent.exists()) { 130 parent.mkdirs(); 131 } 132 FileUtils.copyInputStreamToFile(is, f); 133 } catch (IOException e) { 134 throw new NuxeoException("Failed for template: " + resourceURL, e); 135 } 136 } 137 } 138}