/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.apache.ivy.osgi.updatesite;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;

import org.apache.ivy.core.cache.CacheResourceOptions;
import org.apache.ivy.osgi.repo.AbstractOSGiResolver;
import org.apache.ivy.osgi.repo.RepoDescriptor;
import org.apache.ivy.util.Message;
import org.xml.sax.SAXException;

public class UpdateSiteResolver extends AbstractOSGiResolver {

    private String url;

    private Long metadataTtl;

    private Boolean forceMetadataUpdate;

    private String logLevel;

    public void setUrl(String url) {
        this.url = url;
    }

    public void setMetadataTtl(Long metadataTtl) {
        this.metadataTtl = metadataTtl;
    }

    public void setForceMetadataUpdate(Boolean forceMetadataUpdate) {
        this.forceMetadataUpdate = forceMetadataUpdate;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }

    protected void init() {
        if (url == null) {
            throw new RuntimeException("Missing url");
        }
        CacheResourceOptions options = new CacheResourceOptions();
        if (metadataTtl != null) {
            options.setTtl(metadataTtl.longValue());
        }
        if (forceMetadataUpdate != null) {
            options.setForce(forceMetadataUpdate.booleanValue());
        }
        int log = Message.MSG_INFO;
        if (logLevel != null) {
            if ("debug".equalsIgnoreCase(logLevel)) {
                log = Message.MSG_DEBUG;
            } else if ("verbose".equalsIgnoreCase(logLevel)) {
                log = Message.MSG_VERBOSE;
            } else if ("info".equalsIgnoreCase(logLevel)) {
                log = Message.MSG_INFO;
            } else if ("warn".equalsIgnoreCase(logLevel)) {
                log = Message.MSG_WARN;
            } else if ("error".equalsIgnoreCase(logLevel)) {
                log = Message.MSG_ERR;
            } else {
                throw new RuntimeException("Unknown log level: " + logLevel);
            }
        }
        UpdateSiteLoader loader = new UpdateSiteLoader(getRepositoryCacheManager(),
                getEventManager(), options);
        loader.setLogLevel(log);
        RepoDescriptor repoDescriptor;
        try {
            repoDescriptor = loader.load(new URI(url));
        } catch (IOException e) {
            throw new RuntimeException("IO issue while trying to read the update site ("
                    + e.getMessage() + ")");
        } catch (ParseException e) {
            throw new RuntimeException("Failed to parse the updatesite (" + e.getMessage() + ")", e);
        } catch (SAXException e) {
            throw new RuntimeException("Illformed updatesite (" + e.getMessage() + ")", e);
        } catch (URISyntaxException e) {
            throw new RuntimeException("Illformed url (" + e.getMessage() + ")", e);
        }
        if (repoDescriptor == null) {
            setRepoDescriptor(FAILING_REPO_DESCRIPTOR);
            throw new RuntimeException("No update site was found at the location: " + url);
        }
        setRepoDescriptor(repoDescriptor);
    }
}

@@ -851,16 +851,37 @@
 esite;%0A%0A
+import java.io.File;%0A
 import j
@@ -892,32 +892,32 @@
 io.IOException;%0A
-
 import java.net.
@@ -991,16 +991,65 @@
 ption;%0A%0A
+import org.apache.ivy.core.cache.ArtifactOrigin;%0A
 import o
@@ -1095,16 +1095,237 @@
 ptions;%0A
+import org.apache.ivy.core.cache.DownloadListener;%0Aimport org.apache.ivy.core.cache.RepositoryCacheManager;%0Aimport org.apache.ivy.core.module.descriptor.Artifact;%0Aimport org.apache.ivy.core.report.ArtifactDownloadReport;%0A
 import o
@@ -1418,16 +1418,78 @@
 riptor;%0A
+import org.apache.ivy.plugins.resolver.util.ResolvedResource;%0A
 import o
@@ -2545,34 +2545,21 @@
     
+final 
 int log
- = Message.MSG_INFO
 ;%0A  
@@ -3194,32 +3194,32 @@
 ;%0A            %7D%0A
-
         %7D%0A      
@@ -3203,32 +3203,1283 @@
      %7D%0A        %7D
+ else %7B%0A            log = Message.MSG_INFO;%0A        %7D%0A        options.setListener(new DownloadListener() %7B%0A            public void startArtifactDownload(RepositoryCacheManager cache, ResolvedResource rres,%0A                    Artifact artifact, ArtifactOrigin origin) %7B%0A                if (log %3C= Message.MSG_INFO) %7B%0A                    Message.info(%22%5Ctdownloading %22 + rres.getResource().getName());%0A                %7D%0A            %7D%0A            %0A            public void needArtifact(RepositoryCacheManager cache, Artifact artifact) %7B%0A                if (log %3C= Message.MSG_VERBOSE) %7B%0A                    Message.verbose(%22%5Cttrying to download %22 + artifact);%0A                %7D%0A            %7D%0A            %0A            public void endArtifactDownload(RepositoryCacheManager cache, Artifact artifact,%0A                    ArtifactDownloadReport adr, File archiveFile) %7B%0A                if (log %3C= Message.MSG_VERBOSE) %7B%0A                    if (adr.isDownloaded()) %7B%0A                        Message.verbose(%22%5Ctdownloaded to %22 + archiveFile.getAbsolutePath());%0A                    %7D else %7B%0A                        Message.verbose(%22%5Ctnothing to download%22);                        %0A                    %7D%0A                %7D                %0A            %7D%0A        %7D);
 %0A        UpdateS

Add logs about the downloaded repository metadata