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();
130 cacheEntries.add( new CacheEntry( entry.getKey(), cacheStatistics.getSize(), cacheStatistics.getCacheHits(),
131 cacheStatistics.getCacheMiss(),
132 decimalFormat.format( cacheStatistics.getCacheHitRate() ).toString() ) );
138 public Boolean clearCache( String cacheKey )
139 throws ArchivaRestServiceException
141 Cache cache = caches.get( cacheKey );
144 throw new ArchivaRestServiceException( "no cache for key: " + cacheKey,
145 Response.Status.BAD_REQUEST.getStatusCode(), null );
152 public Boolean clearAllCaches()
153 throws ArchivaRestServiceException
155 for ( Cache cache : caches.values() )
162 public List<RepositoryScannerStatistics> getRepositoryScannerStatistics()
163 throws ArchivaRestServiceException
165 Set<RepositoryScannerInstance> repositoryScannerInstances = scanner.getInProgressScans();
166 if ( repositoryScannerInstances.isEmpty() )
168 return Collections.emptyList();
170 List<RepositoryScannerStatistics> repositoryScannerStatisticsList =
171 new ArrayList<RepositoryScannerStatistics>( repositoryScannerInstances.size() );
173 for ( RepositoryScannerInstance instance : repositoryScannerInstances )
175 RepositoryScannerStatistics repositoryScannerStatistics = new RepositoryScannerStatistics();
176 repositoryScannerStatisticsList.add( repositoryScannerStatistics );
177 repositoryScannerStatistics.setManagedRepository( instance.getRepository() );
178 repositoryScannerStatistics.setNewFileCount( instance.getStats().getNewFileCount() );
179 repositoryScannerStatistics.setTotalFileCount( instance.getStats().getTotalFileCount() );
180 repositoryScannerStatistics.setConsumerScanningStatistics( mapConsumerScanningStatistics( instance ) );
183 return repositoryScannerStatisticsList;
186 private List<ConsumerScanningStatistics> mapConsumerScanningStatistics( RepositoryScannerInstance instance )
188 DecimalFormat decimalFormat = new DecimalFormat( "###.##" );
189 // FIXME take care of NPE here !!!
190 List<ConsumerScanningStatistics> ret =
191 new ArrayList<ConsumerScanningStatistics>( instance.getConsumerCounts().size() );
192 for ( Map.Entry<String, Long> entry : instance.getConsumerCounts().entrySet() )
194 ConsumerScanningStatistics consumerScanningStatistics = new ConsumerScanningStatistics();
195 consumerScanningStatistics.setConsumerKey( entry.getKey() );
196 consumerScanningStatistics.setCount( entry.getValue() );
197 consumerScanningStatistics.setTime( instance.getConsumerTimings().get( entry.getKey() ) );
198 if ( consumerScanningStatistics.getCount() > 0 )
200 consumerScanningStatistics.setAverage( decimalFormat.format(
201 consumerScanningStatistics.getTime() / consumerScanningStatistics.getCount() ) );
203 ret.add( consumerScanningStatistics );
205 Collections.sort( ret, ConsumerScanningStatisticsComparator.INSTANCE );