[MRM-526] add services to configure the cache

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1426642 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-12-28 21:40:29 +00:00
parent aa482e9b9b
commit 46a1c65169
5 changed files with 373 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package org.apache.archiva.admin.model.beans;
/*
* 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 javax.xml.bind.annotation.XmlRootElement;
/**
* @author Olivier Lamy
* @since 1.4-M4
*/
@XmlRootElement( name = "archivaRuntimeConfiguration" )
public class ArchivaRuntimeConfiguration
{
private CacheConfiguration urlFailureCacheConfiguration;
public ArchivaRuntimeConfiguration()
{
// no op
}
public CacheConfiguration getUrlFailureCacheConfiguration()
{
return urlFailureCacheConfiguration;
}
public void setUrlFailureCacheConfiguration( CacheConfiguration urlFailureCacheConfiguration )
{
this.urlFailureCacheConfiguration = urlFailureCacheConfiguration;
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder();
sb.append( "ArchivaRuntimeConfiguration" );
sb.append( "{urlFailureCacheConfiguration=" ).append( urlFailureCacheConfiguration );
sb.append( '}' );
return sb.toString();
}
}

View File

@ -0,0 +1,35 @@
package org.apache.archiva.admin.model.runtime;
/*
* 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.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.beans.ArchivaRuntimeConfiguration;
/**
* @author Olivier Lamy
* @since 1.4-M4
*/
public interface ArchivaRuntimeConfigurationAdmin
{
ArchivaRuntimeConfiguration getArchivaRuntimeConfiguration()
throws RepositoryAdminException;
void updateArchivaRuntimeConfiguration( ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
throws RepositoryAdminException;
}

View File

@ -0,0 +1,146 @@
package org.apache.archiva.admin.repository.runtime;
/*
* 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 net.sf.beanlib.provider.replicator.BeanReplicator;
import org.apache.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.beans.ArchivaRuntimeConfiguration;
import org.apache.archiva.admin.model.beans.CacheConfiguration;
import org.apache.archiva.admin.model.runtime.ArchivaRuntimeConfigurationAdmin;
import org.apache.archiva.configuration.ArchivaConfiguration;
import org.apache.archiva.redback.components.cache.Cache;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
/**
* @author Olivier Lamy
* @since 1.4-M4
*/
@Service( "archivaRuntimeConfigurationAdmin#default" )
public class DefaultArchivaRuntimeConfigurationAdmin
implements ArchivaRuntimeConfigurationAdmin
{
@Inject
private ArchivaConfiguration archivaConfiguration;
@Inject
@Named( value = "cache#url-failures-cache" )
private Cache usersCache;
@PostConstruct
public void initialize()
throws RepositoryAdminException
{
ArchivaRuntimeConfiguration archivaRuntimeConfiguration = getArchivaRuntimeConfiguration();
boolean save = false;
// NPE free
if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration() == null )
{
archivaRuntimeConfiguration.setUrlFailureCacheConfiguration( new CacheConfiguration() );
}
// if -1 it means non initialized to take values from the spring bean
if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToIdleSeconds() < 0 )
{
archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setTimeToIdleSeconds(
usersCache.getTimeToIdleSeconds() );
save = true;
}
usersCache.setTimeToIdleSeconds(
archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToIdleSeconds() );
if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToLiveSeconds() < 0 )
{
archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setTimeToLiveSeconds(
usersCache.getTimeToLiveSeconds() );
save = true;
}
usersCache.setTimeToLiveSeconds(
archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToLiveSeconds() );
if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsInMemory() < 0 )
{
archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setMaxElementsInMemory(
usersCache.getMaxElementsInMemory() );
save = true;
}
usersCache.setMaxElementsInMemory(
archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsInMemory() );
if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsOnDisk() < 0 )
{
archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setMaxElementsOnDisk(
usersCache.getMaxElementsOnDisk() );
save = true;
}
usersCache.setMaxElementsOnDisk(
archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsOnDisk() );
if ( save )
{
updateArchivaRuntimeConfiguration( archivaRuntimeConfiguration );
}
}
public ArchivaRuntimeConfiguration getArchivaRuntimeConfiguration()
throws RepositoryAdminException
{
return build( archivaConfiguration.getConfiguration().getArchivaRuntimeConfiguration() );
}
public void updateArchivaRuntimeConfiguration( ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
throws RepositoryAdminException
{
//To change body of implemented methods use File | Settings | File Templates.
}
protected ArchivaRuntimeConfiguration build(
org.apache.archiva.configuration.ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
{
if ( archivaRuntimeConfiguration == null )
{
return new ArchivaRuntimeConfiguration();
}
ArchivaRuntimeConfiguration res =
new BeanReplicator().replicateBean( archivaRuntimeConfiguration, ArchivaRuntimeConfiguration.class );
if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration() != null )
{
res.setUrlFailureCacheConfiguration(
new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration(),
CacheConfiguration.class ) );
}
return res;
}
}

View File

@ -0,0 +1,53 @@
package org.apache.archiva.rest.api.services;
/*
* 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.archiva.admin.model.beans.ArchivaRuntimeConfiguration;
import org.apache.archiva.redback.authorization.RedbackAuthorization;
import org.apache.archiva.security.common.ArchivaRoleConstants;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* @author Olivier Lamy
* @since 1.4-M4
*/
@Path( "/archivaRuntimeConfigurationService/" )
public interface ArchivaRuntimeConfigurationService
{
@Path( "archivaRuntimeConfiguration" )
@GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
ArchivaRuntimeConfiguration getArchivaRuntimeConfiguration()
throws ArchivaRestServiceException;
@Path( "archivaRuntimeConfiguration" )
@PUT
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
Boolean updateArchivaRuntimeConfiguration( ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
throws ArchivaRestServiceException;
}

View File

@ -0,0 +1,83 @@
package org.apache.archiva.rest.services;
/*
* 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.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.beans.ArchivaRuntimeConfiguration;
import org.apache.archiva.admin.model.beans.CacheConfiguration;
import org.apache.archiva.admin.model.runtime.ArchivaRuntimeConfigurationAdmin;
import org.apache.archiva.redback.components.cache.Cache;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.rest.api.services.ArchivaRuntimeConfigurationService;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
import javax.inject.Named;
/**
* @author Olivier Lamy
* @since 1.4-M4
*/
@Service( "archivaRuntimeConfigurationService#rest" )
public class DefaultArchivaRuntimeConfigurationService
extends AbstractRestService
implements ArchivaRuntimeConfigurationService
{
@Inject
private ArchivaRuntimeConfigurationAdmin archivaRuntimeConfigurationAdmin;
@Inject
@Named( value = "cache#url-failures-cache" )
private Cache usersCache;
public ArchivaRuntimeConfiguration getArchivaRuntimeConfiguration()
throws ArchivaRestServiceException
{
try
{
return archivaRuntimeConfigurationAdmin.getArchivaRuntimeConfiguration();
}
catch ( RepositoryAdminException e )
{
throw new ArchivaRestServiceException( e.getMessage(), e );
}
}
public Boolean updateArchivaRuntimeConfiguration( ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
throws ArchivaRestServiceException
{
try
{
archivaRuntimeConfigurationAdmin.updateArchivaRuntimeConfiguration( archivaRuntimeConfiguration );
CacheConfiguration cacheConfiguration = archivaRuntimeConfiguration.getUrlFailureCacheConfiguration();
if ( cacheConfiguration != null )
{
usersCache.setTimeToLiveSeconds( cacheConfiguration.getTimeToLiveSeconds() );
usersCache.setTimeToIdleSeconds( cacheConfiguration.getTimeToIdleSeconds() );
usersCache.setMaxElementsOnDisk( cacheConfiguration.getMaxElementsOnDisk() );
usersCache.setMaxElementsInMemory( cacheConfiguration.getMaxElementsInMemory() );
}
}
catch ( RepositoryAdminException e )
{
throw new ArchivaRestServiceException( e.getMessage(), e );
}
return Boolean.TRUE;
}
}