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; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.List; 024import java.util.Map; 025 026import org.nuxeo.common.utils.FileUtils; 027import org.nuxeo.connect.update.LocalPackage; 028import org.nuxeo.connect.update.PackageException; 029import org.nuxeo.connect.update.PackageState; 030import org.nuxeo.connect.update.PackageUpdateService; 031import org.nuxeo.connect.update.task.live.commands.Flush; 032import org.nuxeo.connect.update.task.standalone.InstallTask; 033import org.nuxeo.runtime.api.Framework; 034import org.nuxeo.runtime.model.ComponentName; 035import org.nuxeo.runtime.model.impl.RegistrationInfoImpl; 036import org.nuxeo.runtime.reload.ReloadService; 037 038/** 039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 040 */ 041public class LiveInstallTask extends InstallTask { 042 043 public LiveInstallTask(PackageUpdateService pus) { 044 super(pus); 045 } 046 047 @Override 048 protected void doRun(Map<String, String> params) throws PackageException { 049 super.doRun(params); 050 // reload components declared in 'reload' file 051 reloadComponents(getPackage()); 052 } 053 054 @Override 055 protected void taskDone() throws PackageException { 056 Framework.getService(ReloadService.class).reload(); 057 if (isRestartRequired()) { 058 service.setPackageState(pkg, PackageState.INSTALLED); 059 } else { 060 service.setPackageState(pkg, PackageState.STARTED); 061 } 062 } 063 064 /** 065 * @deprecated since 5.6: this way of reloading components is smarter because the package installed can declare what 066 * needs to be reloaded exactly, but this is too complicated to handle, and risky given potential 067 * dependency issues => make components listen for the "flush" event instead, @see {@link ReloadService} 068 */ 069 @Deprecated 070 protected static void reloadComponents(LocalPackage localPackage) throws PackageException { 071 File file = localPackage.getData().getEntry("reload"); 072 if (file.isFile()) { 073 try { 074 List<String> lines = FileUtils.readLines(file); 075 for (String line : lines) { 076 line = line.trim(); 077 if (line.startsWith("#") || line.length() == 0) { 078 continue; 079 } 080 reloadComponent(line); 081 } 082 } catch (IOException e) { 083 throw new PackageException("Failed to read the 'reload' file", e); 084 } 085 } 086 } 087 088 /** 089 * @deprecated since 5.6: see {@link #reloadComponents(LocalPackage)} 090 */ 091 @Deprecated 092 protected static void reloadComponent(String name) throws PackageException { 093 RegistrationInfoImpl ri = (RegistrationInfoImpl) Framework.getRuntime().getComponentManager().getRegistrationInfo( 094 new ComponentName(name)); 095 if (ri != null) { 096 ri.reload(); 097 } 098 } 099 100 @Override 101 protected void flush() throws PackageException { 102 Flush.flush(); 103 } 104 105}