1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.redback.components.cache.Cache;
22 import org.apache.archiva.redback.components.cache.CacheStatistics;
23 import org.apache.archiva.redback.components.taskqueue.TaskQueue;
24 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
25 import org.apache.archiva.repository.scanner.RepositoryScanner;
26 import org.apache.archiva.repository.scanner.RepositoryScannerInstance;
27 import org.apache.archiva.rest.api.model.CacheEntry;
28 import org.apache.archiva.rest.api.model.ConsumerScanningStatistics;
29 import org.apache.archiva.rest.api.model.QueueEntry;
30 import org.apache.archiva.rest.api.model.RepositoryScannerStatistics;
31 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
32 import org.apache.archiva.rest.api.services.SystemStatusService;
33 import org.apache.archiva.rest.services.utils.ConsumerScanningStatisticsComparator;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
38 import javax.ws.rs.core.Response;
39 import java.text.DecimalFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.Date;
44 import java.util.List;
45 import java.util.Locale;
50 * @author Olivier Lamy
53 @Service( "systemStatusService#rest" )
54 public class DefaultSystemStatusService
55 extends AbstractRestService
56 implements SystemStatusService
60 private Map<String, TaskQueue> queues = null;
62 private Map<String, Cache> caches = null;
64 private RepositoryScanner scanner;
68 public DefaultSystemStatusService( ApplicationContext applicationContext, RepositoryScanner scanner )
70 this.scanner = scanner;
72 queues = getBeansOfType( applicationContext, TaskQueue.class );
74 caches = getBeansOfType( applicationContext, Cache.class );
77 public String getMemoryStatus()
78 throws ArchivaRestServiceException
80 Runtime runtime = Runtime.getRuntime();
82 long total = runtime.totalMemory();
83 long used = total - runtime.freeMemory();
84 long max = runtime.maxMemory();
85 return formatMemory( used ) + "/" + formatMemory( total ) + " (Max: " + formatMemory( max ) + ")";
88 private static String formatMemory( long l )
90 return l / ( 1024 * 1024 ) + "M";
93 public String getCurrentServerTime( String locale )
94 throws ArchivaRestServiceException
96 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( locale ) );
97 return sdf.format( new Date() );
100 public List<QueueEntry> getQueueEntries()
101 throws ArchivaRestServiceException
105 List<QueueEntry> queueEntries = new ArrayList<QueueEntry>( queues.size() );
106 for ( Map.Entry<String, TaskQueue> entry : queues.entrySet() )
108 queueEntries.add( new QueueEntry( entry.getKey(), entry.getValue().getQueueSnapshot().size() ) );
113 catch ( TaskQueueException e )
115 log.error( e.getMessage(), e );
116 throw new ArchivaRestServiceException( e.getMessage(),
117 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
121 public List<CacheEntry> getCacheEntries()
122 throws ArchivaRestServiceException
124 List<CacheEntry> cacheEntries = new ArrayList<CacheEntry>( caches.size() );
125 DecimalFormat decimalFormat = new DecimalFormat( "#%" );
127 for ( Map.Entry<String, Cache> entry : caches.entrySet() )
129 CacheStatistics cacheStatistics = entry.getValue().getStatistics();
131 cacheEntries.add( new CacheEntry( entry.getKey(), cacheStatistics.getSize(), cacheStatistics.getCacheHits(),
132 cacheStatistics.getCacheMiss(),
133 decimalFormat.format( cacheStatistics.getCacheHitRate() ).toString(),
134 cacheStatistics.getInMemorySize() ) );
137 Collections.sort( cacheEntries );
142 public Boolean clearCache( String cacheKey )
143 throws ArchivaRestServiceException
145 Cache cache = caches.get( cacheKey );
148 throw new ArchivaRestServiceException( "no cache for key: " + cacheKey,
149 Response.Status.BAD_REQUEST.getStatusCode(), null );
156 public Boolean clearAllCaches()
157 throws ArchivaRestServiceException
159 for ( Cache cache : caches.values() )
166 public List<RepositoryScannerStatistics> getRepositoryScannerStatistics()
167 throws ArchivaRestServiceException
169 Set<RepositoryScannerInstance> repositoryScannerInstances = scanner.getInProgressScans();
170 if ( repositoryScannerInstances.isEmpty() )
172 return Collections.emptyList();
174 List<RepositoryScannerStatistics> repositoryScannerStatisticsList =
175 new ArrayList<RepositoryScannerStatistics>( repositoryScannerInstances.size() );
177 for ( RepositoryScannerInstance instance : repositoryScannerInstances )
179 RepositoryScannerStatistics repositoryScannerStatistics = new RepositoryScannerStatistics();
180 repositoryScannerStatisticsList.add( repositoryScannerStatistics );
181 repositoryScannerStatistics.setManagedRepository( instance.getRepository() );
182 repositoryScannerStatistics.setNewFileCount( instance.getStats().getNewFileCount() );
183 repositoryScannerStatistics.setTotalFileCount( instance.getStats().getTotalFileCount() );
184 repositoryScannerStatistics.setConsumerScanningStatistics( mapConsumerScanningStatistics( instance ) );
187 return repositoryScannerStatisticsList;
190 private List<ConsumerScanningStatistics> mapConsumerScanningStatistics( RepositoryScannerInstance instance )
192 DecimalFormat decimalFormat = new DecimalFormat( "###.##" );
193 if ( instance.getConsumerCounts() == null )
195 return Collections.emptyList();
197 List<ConsumerScanningStatistics> ret =
198 new ArrayList<ConsumerScanningStatistics>( instance.getConsumerCounts().size() );
199 for ( Map.Entry<String, Long> entry : instance.getConsumerCounts().entrySet() )
201 ConsumerScanningStatistics consumerScanningStatistics = new ConsumerScanningStatistics();
202 consumerScanningStatistics.setConsumerKey( entry.getKey() );
203 consumerScanningStatistics.setCount( entry.getValue() );
204 consumerScanningStatistics.setTime( instance.getConsumerTimings().get( entry.getKey() ) );
205 if ( consumerScanningStatistics.getCount() > 0 )
207 consumerScanningStatistics.setAverage( decimalFormat.format(
208 consumerScanningStatistics.getTime() / consumerScanningStatistics.getCount() ) );
210 ret.add( consumerScanningStatistics );
212 Collections.sort( ret, ConsumerScanningStatisticsComparator.INSTANCE );