summaryrefslogtreecommitdiffstats
path: root/archiva-web
diff options
context:
space:
mode:
authorJoakim Erdfelt <joakime@apache.org>2007-04-25 18:07:06 +0000
committerJoakim Erdfelt <joakime@apache.org>2007-04-25 18:07:06 +0000
commit72a1d94d244d073d6554b535e2c3adf8393f9935 (patch)
treeeeaa747a4bffd6b7dc39fc9c9ac5352f2cace963 /archiva-web
parentf34cf2e6c06f52008d7b7c0ec1788563854a22dd (diff)
downloadarchiva-72a1d94d244d073d6554b535e2c3adf8393f9935.tar.gz
archiva-72a1d94d244d073d6554b535e2c3adf8393f9935.zip
* Ensure configuration file exists.
* Ensure configuration is synched with database. * Fix application.xml for new Configuration load-on-starts. git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@532431 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-web')
-rw-r--r--archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/check/RoleExistanceEnvironmentCheck.java2
-rw-r--r--archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ArchivaVersion.java98
-rw-r--r--archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/Banner.java225
-rw-r--r--archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ConfigurationSynchronization.java121
-rw-r--r--archiva-web/archiva-webapp/src/main/resources/META-INF/plexus/application.xml19
-rw-r--r--archiva-web/archiva-webapp/src/main/resources/log4j.xml2
-rw-r--r--archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/startup/BannerTest.java90
-rwxr-xr-xarchiva-web/archiva-webapp/src/test/resources/banner.gzbin0 -> 282 bytes
8 files changed, 553 insertions, 4 deletions
diff --git a/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/check/RoleExistanceEnvironmentCheck.java b/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/check/RoleExistanceEnvironmentCheck.java
index 1d93d8b05..3a87e0130 100644
--- a/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/check/RoleExistanceEnvironmentCheck.java
+++ b/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/check/RoleExistanceEnvironmentCheck.java
@@ -73,7 +73,7 @@ public class RoleExistanceEnvironmentCheck
Iterator it = repos.iterator();
while ( it.hasNext() )
{
- RepositoryConfiguration repository = (RepositoryConfiguration) it.next();
+ ArchivaRepository repository = (ArchivaRepository) it.next();
roleProfileManager.getDynamicRole( "archiva-repository-manager", repository.getId() );
diff --git a/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ArchivaVersion.java b/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ArchivaVersion.java
new file mode 100644
index 000000000..7a463192d
--- /dev/null
+++ b/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ArchivaVersion.java
@@ -0,0 +1,98 @@
+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:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class ArchivaVersion
+{
+ public static String determineVersion( ClassLoader cloader )
+ {
+ /* This is the search order of modules to find the version.
+ */
+ 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-cli",
+ "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 ) )
+ {
+ return version;
+ }
+ }
+ catch ( IOException e )
+ {
+ /* do nothing */
+ }
+ }
+ }
+
+ return "Unknown";
+ }
+
+ private static URL findModulePom( ClassLoader cloader, String module )
+ {
+ URL ret = cloader.getResource( "/META-INF/maven/org.apache.maven.archiva/" + module + "/pom.properties" );
+ return ret;
+ }
+}
diff --git a/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/Banner.java b/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/Banner.java
new file mode 100644
index 000000000..84a40eb9b
--- /dev/null
+++ b/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/Banner.java
@@ -0,0 +1,225 @@
+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 org.codehaus.plexus.logging.Logger;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Banner
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class Banner
+{
+ public static String encode( String raw )
+ {
+ StringBuffer encoded = new StringBuffer();
+ int rawlen = raw.length();
+
+ for ( int i = 0; i < rawlen; i++ )
+ {
+ char c = raw.charAt( i );
+ if ( c == '\\' )
+ {
+ encoded.append( "$." );
+ }
+ else if ( c == '$' )
+ {
+ encoded.append( "$$" );
+ }
+ else if ( c == '\n' )
+ {
+ encoded.append( "$n" );
+ }
+ else if ( Character.isDigit( c ) )
+ {
+ encoded.append( c );
+ }
+ else if ( Character.isLetter( c ) )
+ {
+ encoded.append( rot13( c ) );
+ }
+ else if ( i < raw.length() - 1 )
+ {
+ char nc;
+ boolean done = false;
+ int count = 0;
+ for ( int n = i; !done; n++ )
+ {
+ if ( n >= rawlen )
+ {
+ break;
+ }
+
+ nc = raw.charAt( n );
+
+ if ( nc != c )
+ {
+ done = true;
+ }
+ else
+ {
+ count++;
+ }
+ }
+ if ( count < 3 )
+ {
+ encoded.append( c );
+ }
+ else
+ {
+ encoded.append( "$" ).append( String.valueOf( count ) ).append( c );
+ i += count - 1;
+ }
+ }
+ else
+ {
+ encoded.append( c );
+ }
+ }
+
+ return encoded.toString();
+ }
+
+ public static String decode( String encoded )
+ {
+ StringBuffer decoded = new StringBuffer();
+ int enlen = encoded.length();
+ for ( int i = 0; i < enlen; i++ )
+ {
+ char c = encoded.charAt( i );
+ if ( c == '$' )
+ {
+ char nc = encoded.charAt( i + 1 );
+ if ( nc == '$' )
+ {
+ decoded.append( '$' );
+ i++;
+ }
+ else if ( nc == '.' )
+ {
+ decoded.append( '\\' );
+ i++;
+ }
+ else if ( nc == 'n' )
+ {
+ decoded.append( '\n' );
+ i++;
+ }
+ else if ( Character.isDigit( nc ) )
+ {
+ int count = 0;
+ int nn = i + 1;
+ while ( Character.isDigit( nc ) )
+ {
+ count = ( count * 10 );
+ count += ( nc - '0' );
+ nc = encoded.charAt( ++nn );
+ }
+ for ( int d = 0; d < count; d++ )
+ {
+ decoded.append( nc );
+ }
+ i = nn;
+ }
+ }
+ else if ( Character.isLetter( c ) )
+ {
+ decoded.append( rot13( c ) );
+ }
+ else
+ {
+ decoded.append( c );
+ }
+ }
+
+ return decoded.toString();
+ }
+
+ private static char rot13( char c )
+ {
+ if ( ( c >= 'a' ) && ( c <= 'z' ) )
+ {
+ char dc = c += 13;
+ if ( dc > 'z' )
+ {
+ dc -= 26;
+ }
+ return dc;
+ }
+ else if ( ( c >= 'A' ) && ( c <= 'Z' ) )
+ {
+ char dc = c += 13;
+ if ( dc > 'Z' )
+ {
+ dc -= 26;
+ }
+ return dc;
+ }
+ else
+ {
+ return c;
+ }
+ }
+
+ public static String injectVersion( String text, String version )
+ {
+ Pattern pat = Pattern.compile( "#{2,}" );
+ Matcher mat = pat.matcher( text );
+ StringBuffer ret = new StringBuffer();
+ int off = 0;
+
+ while ( mat.find( off ) )
+ {
+ ret.append( text.substring( off, mat.start() ) );
+ String repl = mat.group();
+ ret.append( StringUtils.center( version, repl.length() ) );
+ off = mat.end();
+ }
+
+ ret.append( text.substring( off ) );
+
+ return ret.toString();
+ }
+
+ public static String getBanner( String version )
+ {
+ String encodedBanner = "$26 $34_$n$15 /$._$7 /$34 $.$n$14 /`/@),$4 | Ba" +
+ " orunys bs nyy bs gur nycnpn'f |$n$14 | (~' __| gbvyvat njnl ba " +
+ "gur Ncnpur Znira |$n$6 _,--.$3_/ |$4 $.$5 cebwrpg grnzf, V jbhyq y" +
+ "vxr gb$3 |$n$4 ,' ,$5 ($3 |$5 $.$5 jrypbzr lbh gb Znira Nepuvin$4 |$" +
+ "n$4 | ($6 $. /$6 | $32# |$n$5 $. )$._/ ,_/$7 |$36 |$n$5 / /$3 " +
+ "( |/$9 | uggc://znira.ncnpur.bet/nepuvin/ |$n$4 ( |$4 ( |$10 | ne" +
+ "puvin-hfref@znira.ncnpur.bet$4 |$n$5 $.|$5 $.|$11 $.$34_/$n$n";
+
+ return injectVersion( decode( encodedBanner ), version );
+ }
+
+ public static void display( Logger logger, String version )
+ {
+ String banner = getBanner( version );
+ logger.info( StringUtils.repeat( "_", 25 ) + "\n" + banner );
+ }
+}
diff --git a/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ConfigurationSynchronization.java b/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ConfigurationSynchronization.java
new file mode 100644
index 000000000..a2b66876d
--- /dev/null
+++ b/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ConfigurationSynchronization.java
@@ -0,0 +1,121 @@
+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.maven.archiva.configuration.ArchivaConfiguration;
+import org.apache.maven.archiva.configuration.ConfigurationNames;
+import org.apache.maven.archiva.configuration.RepositoryConfiguration;
+import org.apache.maven.archiva.database.ArchivaDAO;
+import org.apache.maven.archiva.database.ArchivaDatabaseException;
+import org.apache.maven.archiva.database.ObjectNotFoundException;
+import org.apache.maven.archiva.model.ArchivaRepository;
+import org.apache.maven.archiva.repository.ArchivaConfigurationAdaptor;
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
+import org.codehaus.plexus.registry.Registry;
+import org.codehaus.plexus.registry.RegistryListener;
+
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * ConfigurationSynchronization
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ *
+ * @plexus.component role="org.apache.maven.archiva.web.startup.ConfigurationSynchronization"
+ * role-hint="default"
+ */
+public class ConfigurationSynchronization
+ extends AbstractLogEnabled
+ implements RegistryListener, Initializable
+{
+ /**
+ * @plexus.requirement
+ */
+ private ArchivaDAO dao;
+
+ /**
+ * @plexus.requirement
+ */
+ private ArchivaConfiguration archivaConfiguration;
+
+ public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+ {
+ if ( ConfigurationNames.isRepositories( propertyName ) )
+ {
+ synchConfiguration();
+ }
+ }
+
+ public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+ {
+ /* do nothing */
+ }
+
+ private void synchConfiguration()
+ {
+ List repos = archivaConfiguration.getConfiguration().getRepositories();
+ Iterator it = repos.iterator();
+ while ( it.hasNext() )
+ {
+ RepositoryConfiguration repoConfig = (RepositoryConfiguration) it.next();
+ try
+ {
+ try
+ {
+ ArchivaRepository repository = dao.getRepositoryDAO().getRepository( repoConfig.getId() );
+ // Found repository. Update it.
+
+ repository.getModel().setName( repoConfig.getName() );
+ repository.getModel().setUrl( repoConfig.getUrl() );
+ repository.getModel().setLayoutName( repoConfig.getLayout() );
+ repository.getModel().setCreationSource( "configuration" );
+ repository.getModel().setReleasePolicy( repoConfig.isReleases() );
+ repository.getModel().setSnapshotPolicy( repoConfig.isSnapshots() );
+
+ dao.getRepositoryDAO().saveRepository( repository );
+ }
+ catch ( ObjectNotFoundException e )
+ {
+ // Add the repository to the database.
+ getLogger().info( "Adding repository configuration to DB: " + repoConfig );
+ ArchivaRepository drepo = ArchivaConfigurationAdaptor.toArchivaRepository( repoConfig );
+ drepo.getModel().setCreationSource( "configuration" );
+ dao.getRepositoryDAO().saveRepository( drepo );
+ }
+ }
+ catch ( ArchivaDatabaseException e )
+ {
+ // Log error.
+ getLogger().error( "Unable to add configured repositories to the database: " + e.getMessage(), e );
+ }
+ }
+ }
+
+ public void initialize()
+ throws InitializationException
+ {
+ Banner.display( getLogger(), ArchivaVersion.determineVersion( this.getClass().getClassLoader() ) );
+ synchConfiguration();
+ }
+}
diff --git a/archiva-web/archiva-webapp/src/main/resources/META-INF/plexus/application.xml b/archiva-web/archiva-webapp/src/main/resources/META-INF/plexus/application.xml
index a470eae16..802f0c744 100644
--- a/archiva-web/archiva-webapp/src/main/resources/META-INF/plexus/application.xml
+++ b/archiva-web/archiva-webapp/src/main/resources/META-INF/plexus/application.xml
@@ -196,12 +196,27 @@
<load-on-start>
<component>
- <role>org.apache.maven.archiva.scheduler.RepositoryTaskScheduler</role>
+ <role>org.apache.maven.archiva.configuration.ConfigurationUpgrade</role>
+ <role-hint>default</role-hint>
+ </component>
+ <component>
+ <role>org.apache.maven.archiva.web.startup.ConfigurationSynchronization</role>
+ <role-hint>default</role-hint>
+ </component>
+ <component>
+ <role>org.apache.maven.archiva.web.startup.ConfigurationSynchronization</role>
+ <role-hint>default</role-hint>
+ </component>
+ <component>
+ <role>org.apache.maven.archiva.scheduled.ArchivaTaskScheduler</role>
+ <role-hint>default</role-hint>
</component>
+ <!--
<component>
<role>org.codehaus.plexus.taskqueue.execution.TaskQueueExecutor</role>
- <role-hint>data-refresh</role-hint>
+ <role-hint>archiva-task-executor</role-hint>
</component>
+ -->
</load-on-start>
<lifecycle-handler-manager implementation="org.codehaus.plexus.lifecycle.DefaultLifecycleHandlerManager">
diff --git a/archiva-web/archiva-webapp/src/main/resources/log4j.xml b/archiva-web/archiva-webapp/src/main/resources/log4j.xml
index 9520f8923..89ee23fb5 100644
--- a/archiva-web/archiva-webapp/src/main/resources/log4j.xml
+++ b/archiva-web/archiva-webapp/src/main/resources/log4j.xml
@@ -15,7 +15,7 @@
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%d [%t] %-5p %-30c{1} - %m%n"/>
+ <param name="ConversionPattern" value="%d [%t] %-5p %c{1} - %m%n"/>
</layout>
</appender>
diff --git a/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/startup/BannerTest.java b/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/startup/BannerTest.java
new file mode 100644
index 000000000..486d051d9
--- /dev/null
+++ b/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/startup/BannerTest.java
@@ -0,0 +1,90 @@
+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.io.IOUtils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.zip.GZIPInputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * BannerTest
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class BannerTest
+ extends TestCase
+{
+ private void assertEncodeDecode( String encoded, String decoded )
+ {
+ assertEquals( "Encoding: ", encoded, Banner.encode( decoded ) );
+ assertEquals( "Decoding: ", decoded, Banner.decode( encoded ) );
+ }
+
+ public void testEncodeDecode()
+ {
+ assertEncodeDecode( "[$10 ]", "[ ]" );
+ assertEncodeDecode( "$$$5_$n$5_", "$_____\n_____" );
+ assertEncodeDecode( "$${Refgjuvyr}", "${Erstwhile}" );
+ }
+
+ public void testInjectVersion()
+ {
+ assertEquals( "[ 1.0 ]", Banner.injectVersion( "[#####]", "1.0" ) );
+ assertEquals( ".\\ 1.0-SNAPSHOT \\._____", Banner.injectVersion( ".\\################\\._____",
+ "1.0-SNAPSHOT" ) );
+ assertEquals( "Archiva:\n ( 1.0-alpha-1 )", Banner
+ .injectVersion( "Archiva:\n (##############)", "1.0-alpha-1" ) );
+ }
+
+ public void testGetBanner()
+ throws IOException
+ {
+ String version = "1.0-alpha-1-SNAPSHOT";
+ String banner = Banner.getBanner( version );
+ assertNotNull( "Banner should not be null.", banner );
+ assertTrue( "Banner contains version.", banner.indexOf( version ) > 0 );
+
+ /* Want to make a new banner?
+ * Steps to do it.
+ * 1) Edit the src/test/resources/banner.gz file.
+ * 2) Save it compressed.
+ * 3) Add (to this test method) ...
+ * System.out.println( "\"" + Banner.encode( getRawBanner() ) + "\"" );
+ * 4) Run the test
+ * 5) Copy / Paste the encoded form into the Banner.getBanner() method.
+ */
+ }
+
+ public String getRawBanner()
+ throws IOException
+ {
+ File gzBanner = new File( "src/test/resources/banner.gz" );
+ assertTrue( "File [" + gzBanner.getPath() + "] not found.", gzBanner.exists() );
+ FileInputStream fis = new FileInputStream( gzBanner );
+ GZIPInputStream gzis = new GZIPInputStream( fis );
+ return IOUtils.toString( gzis );
+ }
+}
diff --git a/archiva-web/archiva-webapp/src/test/resources/banner.gz b/archiva-web/archiva-webapp/src/test/resources/banner.gz
new file mode 100755
index 000000000..9715c46e7
--- /dev/null
+++ b/archiva-web/archiva-webapp/src/test/resources/banner.gz
Binary files differ