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