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.samples.importer; 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 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 ModelImporter.RESOURCES_ROOT; 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 this.context = context; 073 initDataDirPath(); 074 expandResources(); 075 } 076 077 @Override 078 public void stop(BundleContext context) { 079 this.context = null; 080 cleanupDataDirPath(); 081 } 082 083 /* Note that this may be called twice, because several activators inherit from this class. */ 084 protected static void initDataDirPath() { 085 if (dataDirPath != null) { 086 return; 087 } 088 String dataDir = Environment.getDefault().getData().getPath(); 089 Path path = new Path(dataDir); 090 path = path.append("resources"); 091 dataDirPath = path.toString(); 092 } 093 094 @SuppressWarnings("deprecation") 095 protected static void cleanupDataDirPath() { 096 if (tmpDir != null) { 097 FileUtils.deleteTree(tmpDir); 098 tmpDir = null; 099 } 100 dataDirPath = null; 101 } 102 103 protected static Path getDataDirPath() { 104 return new Path(dataDirPath); 105 } 106 107 public void expandResources() { 108 log.info("Deploying templates for bundle " + context.getBundle().getSymbolicName()); 109 110 URL sampleRootURL = getResource(getTemplateResourcesRootPath()); 111 if (sampleRootURL == null) { 112 return; 113 } 114 115 Path path = getDataDirPath(); 116 path = path.append(getTemplateResourcesRootPath()); 117 File dataDir = new File(path.toString()); 118 if (!dataDir.exists()) { 119 dataDir.mkdirs(); 120 } 121 122 Enumeration<?> urls = findEntries(getTemplateResourcesRootPath()); 123 while (urls.hasMoreElements()) { 124 URL resourceURL = (URL) urls.nextElement(); 125 try { 126 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.copyToFile(is, f); 136 is.close(); 137 } catch (IOException e) { 138 throw new NuxeoException("Failed for template: " + resourceURL, e); 139 } 140 } 141 } 142}