package org.apache.maven.archiva.web.startup;

/*
 * 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.
 */

import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * ArchivaVersion 
 *
 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
 * @version $Id$
 */
public class ArchivaVersion
{
    private static String version = null;

    private ArchivaVersion()
    {
    }

    public static String determineVersion( ClassLoader cloader )
    {
        if ( version != null )
        {
            return version;
        }
        
        /* This is the search order of modules to find the version.
          @todo is this really necessary? Just use webapp?
         */
        String[] modules = new String[] {
            "archiva-common",
            "archiva-configuration",
            "archiva-database",
            "archiva-consumer-api",
            "archiva-core-consumers",
            "archiva-signature-consumers",
            "archiva-database-consumers",
            "archiva-lucene-consumers",
            "archiva-indexer",
            "archiva-model",
            "archiva-policies",
            "archiva-proxy",
            "archiva-report-manager",
            "archiva-artifact-reports",
            "archiva-project-reports",
            "archiva-metadata-reports",
            "archiva-repository-layer",
            "archiva-scheduled",
            "archiva-webapp",
            "archiva-security",
            "archiva-applet",
            "archiva-xml-tools" };

        for ( int i = 0; i < modules.length; i++ )
        {
            String module = modules[i];
            URL pomurl = findModulePom( cloader, module );
            if ( pomurl != null )
            {
                try
                {
                    Properties props = new Properties();
                    InputStream is = pomurl.openStream();
                    props.load( is );
                    String version = props.getProperty( "version" );
                    if ( StringUtils.isNotBlank( version ) )
                    {
                        ArchivaVersion.version = version;
                        return version;
                    }
                }
                catch ( IOException e )
                {
                    /* do nothing */
                }
            }
        }

        version = "<Unknown Version>";
        return version;
    }

    private static URL findModulePom( ClassLoader cloader, String module )
    {
        return cloader.getResource( "/META-INF/maven/org.apache.maven.archiva/" + module + "/pom.properties" );
    }

    public static String getVersion()
    {
        return version;
    }
}

@@ -891,16 +891,54 @@
 ngUtils;
+%0Aimport org.apache.commons.io.IOUtils;
 %0A%0Aimport
@@ -1437,1141 +1437,146 @@
     
-/* This is the search order of modules to find the version.%0A          @todo is this really necessary? Just use webapp?%0A         */%0A        String%5B%5D modules = new String%5B%5D %7B%0A            %22archiva-common%22,%0A            %22archiva-configuration%22,%0A            %22archiva-database%22,%0A            %22archiva-consumer-api%22,%0A            %22archiva-core-consumers%22,%0A            %22archiva-signature-consumers%22,%0A            %22archiva-database-consumers%22,%0A            %22archiva-lucene-consumers%22,%0A            %22archiva-indexer%22,%0A            %22archiva-model%22,%0A            %22archiva-policies%22,%0A            %22archiva-proxy%22,%0A            %22archiva-report-manager%22,%0A            %22archiva-artifact-reports%22,%0A            %22archiva-project-reports%22,%0A            %22archiva-metadata-reports%22,%0A            %22archiva-repository-layer%22,%0A            %22archiva-scheduled%22,%0A            %22archiva-webapp%22,%0A            %22archiva-security%22,%0A            %22archiva-applet%22,%0A            %22archiva-xml-tools%22 %7D;%0A%0A        for ( int i = 0; i %3C modules.length; i++ )%0A        %7B%0A            String module = modules%5Bi%5D;%0A            URL pomurl = findModulePom( cloader, module );%0A            if ( pomurl
+InputStream is = cloader.getResourceAsStream( %22/META-INF/maven/org.apache.archiva/archiva-configuration/pom.properties%22 );%0A        if ( is
  != 
@@ -1586,34 +1586,26 @@
 l )%0A        
-  
-  %7B%0A    
+%7B%0A
             
@@ -1612,36 +1612,32 @@
 try%0A            
-    
 %7B%0A              
@@ -1638,20 +1638,16 @@
         
- 
-   
 Properti
@@ -1679,70 +1679,8 @@
 ();%0A
-                    InputStream is = pomurl.openStream();%0A    
     
@@ -1705,28 +1705,24 @@
 load( is );%0A
-    
             
@@ -1786,28 +1786,24 @@
             
-    
 if ( StringU
@@ -1843,33 +1843,26 @@
             
-    %7B%0A   
+%7B%0A
             
@@ -1869,17 +1869,16 @@
         
- 
 ArchivaV
@@ -1915,36 +1915,32 @@
                 
-    
 return version;%0A
@@ -1951,32 +1951,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -1965,32 +1965,26 @@
             
-  %7D%0A    
+%7D%0A
             
@@ -2015,32 +2015,26 @@
             
-    %7B%0A  
+%7B%0A
             
@@ -2037,18 +2037,16 @@
         
-  
 /* do no
@@ -2058,36 +2058,32 @@
  */%0A            
-    
 %7D%0A            %7D%0A
@@ -2072,33 +2072,39 @@
   %7D%0A            
-%7D
+finally
 %0A        %7D%0A%0A    
@@ -2100,161 +2100,82 @@
     
-%7D%0A%0A
     
+%7B%0A
     
-version = %22%3CUnknown Version%3E%22;%0A        return version
+            IOUtils.closeQuietly( is )
 ;%0A    
-%7D%0A%0A
     
-private static URL findModulePom( ClassLoader cloader, String module )%0A
+    %7D%0A    
     
-%7B
+%7D%0A
 %0A   
@@ -2183,110 +2183,59 @@
     
-return cloader.getResource( %22/META-INF/maven/org.apache.maven.archiva/%22 + module + %22/pom.properties%22 )
+ArchivaVersion.version = %22%22;%0A        return version
 ;%0A  

fix the version handling since the group ID change, and simplify in the process