PluginUpdatePlugin pup = (PluginUpdatePlugin)pup_pi.getPlugin();
UpdateManagerImpl uman = (UpdateManagerImpl)manager.getDefaultPluginInterface().getUpdateManager();
UpdateCheckInstanceImpl inst =
uman.createEmptyUpdateCheckInstance(
UpdateCheckInstance.UCI_INSTALL,
"update.instance.install",
low_noise );
if ( properties != null ){
for ( Map.Entry<Integer,Object> entry: properties.entrySet()){
inst.setProperty( entry.getKey(), entry.getValue());
}
}
if ( listener != null ){
inst.addListener(
new UpdateCheckInstanceListener()
{
public void
cancelled(
UpdateCheckInstance instance )
{
listener.cancelled();
}
public void
complete(
UpdateCheckInstance instance )
{
final Update[] updates = instance.getUpdates();
if ( updates.length == 0 ){
listener.failed( new PluginException( "No updates were added during check process" ));
}else{
for (int i=0;i<updates.length;i++){
updates[i].addListener(
new UpdateListener()
{
private boolean cancelled;
public void
cancelled(
Update update)
{
cancelled = true;
check();
}
public void
complete(
Update update )
{
check();
}
protected void
check()
{
Update failed_update = null;
for ( Update update: updates ){
if ( !update.isCancelled() && !update.isComplete()){
return;
}
if ( !update.wasSuccessful()){
failed_update = update;
}
}
if ( cancelled ){
listener.cancelled();
}else{
if ( failed_update == null ){
// flush plugin events through before reporting complete
PluginInitializer.waitForPluginEvents();
listener.completed();
}else{
listener.failed(
new PluginException(
"Install of " + failed_update.getName() + " failed" ));
}
}
}
});
}
}
}
});
}
try{
for (int i=0;i<plugins.length;i++){
InstallablePlugin plugin = plugins[i];
final String plugin_id = plugin.getId();
PluginInterface existing_plugin_interface = manager.getPluginInterfaceByID( plugin_id, false );
Plugin existing_plugin = null;
if ( existing_plugin_interface != null ){
existing_plugin = existing_plugin_interface.getPlugin();
// try to check that the new version is higher than the old one!
String old_version = existing_plugin_interface.getPluginVersion();
if ( old_version != null ){
int res = Constants.compareVersions( plugin.getVersion(), old_version );
if ( res < 0 ){
throw( new PluginException( "A higher version (" + old_version + ") of Plugin '" + plugin_id + "' is already installed" ));
}else if ( res == 0 ){
throw( new PluginException( "Version (" + old_version + ") of Plugin '" + plugin_id + "' is already installed" ));
}
}
}
String target_dir;
if ( shared ){
target_dir = FileUtil.getApplicationFile( "plugins" ).toString();
}else{
target_dir = FileUtil.getUserFile( "plugins" ).toString();
}
target_dir += File.separator + plugin_id;
// this may fail on Vista but it doesn't matter as we recover this later
// on. So *don't* check for success here
new File( target_dir ).mkdir();
if ( existing_plugin == null ){
// create a dummy plugin at version 0.0 to trigger the "upgrade" to the new
// installed version
FailedPlugin dummy_plugin = new FailedPlugin( plugin_id, target_dir );
PluginManager.registerPlugin( dummy_plugin, plugin_id );
final PluginInterface dummy_plugin_interface = manager.getPluginInterfaceByID( plugin_id, false );
((InstallablePluginImpl)plugin).addUpdate( inst, pup, dummy_plugin, dummy_plugin_interface );
inst.addListener(
new UpdateCheckInstanceListener()
{
public void
cancelled(
UpdateCheckInstance instance )
{
try{
dummy_plugin_interface.getPluginState().unload();
}catch( Throwable e ){
Debug.out( "Failed to unload plugin", e );
}
}
public void
complete(
UpdateCheckInstance instance )
{
PluginInterface pi = manager.getPluginInterfaceByID( plugin_id, false );
if ( pi != null && pi.getPlugin() instanceof FailedPlugin ){
try{
pi.getPluginState().unload();
}catch( Throwable e ){
Debug.out( "Failed to unload plugin", e );
}
}
}
});
}else{
((InstallablePluginImpl)plugin).addUpdate( inst, pup, existing_plugin, existing_plugin_interface );
}
}
inst.start();
return( inst );
}catch( Throwable e ){
inst.cancel();
if ( e instanceof PluginException ){
throw((PluginException)e);
}else{