001/* 002 * (C) Copyright 2006-2010 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 * bstefanescu 016 */ 017package org.nuxeo.connect.update.task.live.commands; 018 019import java.io.File; 020import java.io.IOException; 021import java.util.Map; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.connect.update.PackageException; 026import org.nuxeo.connect.update.task.Command; 027import org.nuxeo.connect.update.task.Task; 028import org.nuxeo.connect.update.task.standalone.commands.CompositeCommand; 029import org.nuxeo.connect.update.task.standalone.commands.DeployPlaceholder; 030import org.nuxeo.runtime.api.Framework; 031import org.nuxeo.runtime.reload.ReloadService; 032import org.osgi.framework.BundleException; 033 034/** 035 * Deploy a runtime bundle, or a directory containing runtime bundles. 036 * <p> 037 * The inverse of this command is Undeploy. 038 * 039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 040 */ 041public class Deploy extends DeployPlaceholder { 042 043 private static final Log log = LogFactory.getLog(Deploy.class); 044 045 public Deploy() { 046 super(); 047 } 048 049 public Deploy(File file) { 050 super(file); 051 } 052 053 protected Undeploy deployFile(File file, ReloadService service) throws PackageException { 054 String name = service.getOSGIBundleName(file); 055 if (name == null) { 056 // not an OSGI bundle => ignore 057 return null; 058 } 059 try { 060 service.deployBundle(file, true); 061 } catch (BundleException e) { 062 throw new PackageException("Failed to deploy bundle " + file, e); 063 } 064 return new Undeploy(file); 065 } 066 067 protected CompositeCommand deployDirectory(File dir, ReloadService service) throws PackageException { 068 CompositeCommand cmd = new CompositeCommand(); 069 File[] files = dir.listFiles(); 070 if (files != null) { 071 for (File fileInDir : files) { 072 Command ud = deployFile(fileInDir, service); 073 if (ud != null) { 074 cmd.addCommand(ud); 075 } 076 } 077 } 078 if (cmd.isEmpty()) { 079 // nothing to rollback 080 return null; 081 } 082 return cmd; 083 } 084 085 @Override 086 protected Command doRun(Task task, Map<String, String> prefs) throws PackageException { 087 if (!file.exists()) { 088 log.warn("Can't deploy file " + file + ". File is missing."); 089 return null; 090 } 091 ReloadService srv = Framework.getLocalService(ReloadService.class); 092 Command rollback; 093 if (file.isDirectory()) { 094 rollback = deployDirectory(file, srv); 095 } else { 096 rollback = deployFile(file, srv); 097 } 098 if (rollback != null) { 099 // some deployments where done 100 try { 101 srv.runDeploymentPreprocessor(); 102 } catch (IOException e) { 103 throw new PackageException(e.getMessage(), e); 104 } 105 } 106 return rollback; 107 } 108 109}