/**
 * Copyright (C) 2015 Red Hat, Inc. (jdcasey@commonjava.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.pnc.causeway.inject;

import org.commonjava.util.jhttpc.HttpFactory;
import org.commonjava.util.jhttpc.auth.MemoryPasswordManager;
import org.commonjava.util.jhttpc.auth.PasswordManager;
import org.commonjava.util.jhttpc.auth.PasswordType;
import org.jboss.pnc.causeway.config.CausewayConfig;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import java.io.Closeable;
import java.io.IOException;

/**
 * Created by jdcasey on 11/10/15.
 */
@ApplicationScoped
public class CausewayProducer
    implements Closeable
{
    @Inject
    private CausewayConfig config;

    private HttpFactory httpFactory;

    protected CausewayProducer(){}

    public CausewayProducer(CausewayConfig config){
        this.config = config;
        setup();
    }

    @PostConstruct
    public void setup()
    {
        PasswordManager passwords = new MemoryPasswordManager();
        passwords.bind( config.getKojiClientCertificatePassword(), CausewayConfig.KOJI_SITE_ID, PasswordType.KEY );

        httpFactory = new HttpFactory( passwords );

    }

    @PreDestroy
    public void close()
            throws IOException
    {
        if ( httpFactory != null )
        {
            httpFactory.close();
        }
    }

    @Produces
    @Default
    public HttpFactory getHttpFactory()
    {
        return httpFactory;
    }
}

@@ -1908,45 +1908,14 @@
 e()%0A
-            throws IOException%0A
     %7B%0A
+
     
@@ -1951,24 +1951,46 @@
 )%0A        %7B%0A
+            try %7B%0A    
             
@@ -2006,24 +2006,168 @@
 ry.close();%0A
+            %7D catch (IOException e) %7B%0A                throw new RuntimeException(%22Close httpFactory error %22 + e.getMessage(), e);%0A            %7D%0A
         %7D%0A  

fix for WELD warning about checked exception