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.util.Map; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.connect.update.PackageException; 027import org.nuxeo.connect.update.task.Command; 028import org.nuxeo.connect.update.task.Task; 029import org.nuxeo.connect.update.task.standalone.commands.UndeployPlaceholder; 030import org.nuxeo.runtime.api.Framework; 031import org.nuxeo.runtime.reload.ReloadService; 032import org.osgi.framework.BundleException; 033 034/** 035 * Undeploy a runtime bundle, or a directory containing runtime bundles. 036 * <p> 037 * The inverse of this command is Deploy. 038 * 039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 040 */ 041public class Undeploy extends UndeployPlaceholder { 042 043 private static final Log log = LogFactory.getLog(Undeploy.class); 044 045 public Undeploy() { 046 super(); 047 } 048 049 public Undeploy(File file) { 050 super(file); 051 } 052 053 protected void undeployFile(File file, ReloadService service) throws PackageException { 054 String name = service.getOSGIBundleName(file); 055 if (name == null) { 056 // not an OSGI bundle => ignore 057 return; 058 } 059 try { 060 service.undeployBundle(file, true); 061 } catch (BundleException e) { 062 throw new PackageException("Failed to undeploy bundle " + file, e); 063 } 064 } 065 066 protected void undeployDirectory(File dir, ReloadService service) throws PackageException { 067 File[] files = dir.listFiles(); 068 if (files != null) { 069 for (File fileInDir : files) { 070 undeployFile(fileInDir, service); 071 } 072 } 073 } 074 075 @Override 076 protected Command doRun(Task task, Map<String, String> prefs) throws PackageException { 077 if (!file.exists()) { 078 log.warn("Can't undeploy file " + file + ". File is missing."); 079 return null; 080 } 081 try { 082 ReloadService srv = Framework.getLocalService(ReloadService.class); 083 if (file.isDirectory()) { 084 undeployDirectory(file, srv); 085 } else { 086 undeployFile(file, srv); 087 } 088 } catch (PackageException e) { 089 // ignore uninstall -> this may break the entire chain. Usually 090 // uninstall is done only when rollbacking or uninstalling => force 091 // restart required 092 task.setRestartRequired(true); 093 throw new PackageException("Failed to undeploy bundle " + file, e); 094 } 095 return new Deploy(file); 096 } 097 098}