/*
 * Copyright (c) 2012 Sonatype, Inc. All rights reserved.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
 * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package org.sonatype.ci.jenkins;

import hudson.Plugin;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.Hudson;
import hudson.model.UpdateCenter;
import hudson.model.UpdateSite;
import hudson.security.ACL;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;

public class PluginImpl
    extends Plugin
{
    private static final Logger log = Logger.getLogger( PluginImpl.class.getName() );

    @Initializer( after = InitMilestone.EXTENSIONS_AUGMENTED, attains = "sonatype-ci-update-site" )
    public static void addUpdateSite()
        throws Exception
    {
        log.info( "Adding Sonatype CI update site" );

        UpdateCenter updateCenter = Hudson.getInstance().getUpdateCenter();
        if ( updateCenter.getSites().isEmpty() )
        {
            updateCenter.load();
        }
        if ( updateCenter.getById( "sonatype-ci" ) == null )
        {
            updateCenter.getSites().add( new UpdateSite( "sonatype-ci", "http://links.sonatype.com/products/insight/ci/update-site" ) );
        }
    }

    @Initializer( requires = "sonatype-ci-update-site" )
    public static void installPlugins()
    {
        log.info( "Installing Sonatype CI plugins" );

        UpdateCenter updateCenter = Hudson.getInstance().getUpdateCenter();

        installPlugin( updateCenter, "insight-ci" );
    }

    private static void installPlugin( UpdateCenter updateCenter, String name )
    {
        UpdateSite.Plugin plugin = updateCenter.getPlugin( name );
        if ( plugin != null && plugin.getInstalled() == null )
        {
            @SuppressWarnings( "static-access" )
            Authentication authentication = Hudson.getAuthentication();
            try
            {
                SecurityContextHolder.getContext().setAuthentication( ACL.SYSTEM );

                log.info( "Installing " + name + "..." );
                plugin.deploy().get();
                log.info( "Installed " + name );
            }
            catch ( Exception e )
            {
                log.log( Level.WARNING, "Cannot install " + name, e );
            }
            finally
            {
                SecurityContextHolder.getContext().setAuthentication( authentication );
            }
        }
    }
}

@@ -903,35 +903,32 @@
 ins = %22sonatype-
-ci-
 update-site%22 )%0A 
@@ -914,32 +914,33 @@
 type-update-site
+s
 %22 )%0A    public s
@@ -963,16 +963,17 @@
 dateSite
+s
 ()%0A     
@@ -1033,19 +1033,16 @@
 onatype 
-CI 
 update s
@@ -1044,16 +1044,19 @@
 ate site
+(s)
 %22 );%0A%0A  
@@ -1231,24 +1231,25 @@
       %7D%0A
+%0A
         
 if ( upd
@@ -1244,137 +1244,45 @@
     
-if ( updateCenter.getById( %22sonatype-ci%22 ) == null )%0A        %7B%0A            updateCenter.getSites().add( new UpdateSite( %22sonatype
+addUpdateSite( updateCenter, %22insight
 -ci%22
@@ -1348,21 +1348,9 @@
 e%22 )
- );%0A        %7D
+;
 %0A   
@@ -1396,11 +1396,8 @@
 ype-
-ci-
 upda
@@ -1403,16 +1403,17 @@
 ate-site
+s
 %22 )%0A    
@@ -1497,18 +1497,17 @@
 ype 
-CI 
 plugin
-s
+(s)
 %22 );
@@ -1641,24 +1641,891 @@
 %22 );%0A    %7D%0A%0A
+    private static void addUpdateSite( UpdateCenter updateCenter, String name, String url )%0A    %7B%0A        if ( updateCenter.getById( name ) == null )%0A        %7B%0A            @SuppressWarnings( %22static-access%22 )%0A            Authentication authentication = Hudson.getAuthentication();%0A            try%0A            %7B%0A                SecurityContextHolder.getContext().setAuthentication( ACL.SYSTEM );%0A%0A                log.info( %22Adding %22 + name + %22...%22 );%0A                updateCenter.getSites().add( new UpdateSite( name, url ) );%0A                log.info( %22Added %22 + name );%0A            %7D%0A            catch ( Exception e )%0A            %7B%0A                log.log( Level.WARNING, %22Cannot add %22 + name, e );%0A            %7D%0A            finally%0A            %7B%0A                SecurityContextHolder.getContext().setAuthentication( authentication );%0A            %7D%0A        %7D%0A    %7D%0A%0A
     private 

Clean up update site code