1 package org.apache.maven.archiva.configuration;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.math.NumberUtils;
26 import org.apache.maven.archiva.xml.XMLException;
27 import org.apache.maven.archiva.xml.XMLReader;
28 import org.codehaus.plexus.logging.Logger;
29 import org.codehaus.plexus.logging.console.ConsoleLogger;
32 import java.io.FileOutputStream;
33 import java.io.FileWriter;
34 import java.io.IOException;
35 import java.io.InputStream;
39 * A component that is first in the plexus startup that ensure that the configuration
40 * file format has been upgraded properly.
42 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
45 public class ConfigurationUpgrade
47 public static final int CURRENT_CONFIG_VERSION = 1;
49 private Logger logger;
52 * Perform the upgrade (if needed).
54 * NOTE: This component should *NOT USE* the configuration api to do it's upgrade
56 * @return true if the upgrade modified the archiva.xml file. false otherwise.
58 public boolean perform()
60 File userConfigFile = new File( System.getProperty( "user.home" ), ".m2/archiva.xml" );
62 if ( !userConfigFile.exists() )
64 writeDefaultConfigFile( userConfigFile );
68 boolean configOk = false;
71 XMLReader xml = new XMLReader( "configuration", userConfigFile );
72 String configVersion = xml.getElementText( "//configuration/version" );
73 if ( StringUtils.isNotBlank( configVersion ) )
77 // Found an embedded configuration version.
78 int version = NumberUtils.toInt( configVersion, 0 );
79 if ( version < CURRENT_CONFIG_VERSION )
81 upgradeVersion( userConfigFile, xml );
85 catch ( XMLException e )
87 getLogger().warn( "Unable to read user configuration XML: " + e.getMessage(), e );
95 FileUtils.copyFile( userConfigFile, new File( userConfigFile.getAbsolutePath() + ".bak" ) );
96 writeDefaultConfigFile( userConfigFile );
99 catch ( IOException e )
101 getLogger().warn( "Unable to create backup of your configuration file: " + e.getMessage(), e );
108 private void upgradeVersion( File userConfigFile, XMLReader xml )
110 // TODO: write implementation when we have a current version greater than 1.
113 private void writeDefaultConfigFile( File userConfigFile )
115 URL defaultConfigURL = this.getClass()
116 .getResource( "/org/apache/maven/archiva/configuration/default-archiva.xml" );
118 if ( defaultConfigURL == null )
122 FileWriter writer = new FileWriter( userConfigFile );
123 writer.write( "<?xml version=\"1.0\"?>\n" );
124 writer.write( "<configuration />" );
129 catch ( IOException e )
131 getLogger().warn( "Unable to write default (generic) configuration file: " + e.getMessage(), e );
135 // Write default to user config file location.
138 FileOutputStream output = new FileOutputStream( userConfigFile );
139 InputStream input = defaultConfigURL.openStream();
140 IOUtils.copy( input, output );
145 catch ( IOException e )
147 getLogger().warn( "Unable to write default configuration file: " + e.getMessage(), e );
151 public Logger getLogger()
153 if ( logger == null )
155 logger = new ConsoleLogger( ConsoleLogger.LEVEL_INFO, this.getClass().getName() );
160 public void setLogger( Logger logger )
162 this.logger = logger;