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;
44 * @author Olivier Lamy
47 @Service( "systemStatusService#rest" )
48 public class DefaultSystemStatusService
49 extends AbstractRestService
50 implements SystemStatusService
54 private Map<String, TaskQueue> queues = null;
56 private Map<String, Cache> caches = null;
58 private RepositoryScanner scanner;
60 // display spring scheduled
61 //@Inject @Named (value="springScheduler");
65 public DefaultSystemStatusService( ApplicationContext applicationContext, RepositoryScanner scanner )
67 this.scanner = scanner;
69 queues = getBeansOfType( applicationContext, TaskQueue.class );
71 caches = getBeansOfType( applicationContext, Cache.class );
75 public String getMemoryStatus()
76 throws ArchivaRestServiceException
78 Runtime runtime = Runtime.getRuntime();
80 long total = runtime.totalMemory();
81 long used = total - runtime.freeMemory();
82 long max = runtime.maxMemory();
83 return formatMemory( used ) + "/" + formatMemory( total ) + " (Max: " + formatMemory( max ) + ")";
86 private static String formatMemory( long l )
88 return l / ( 1024 * 1024 ) + "M";
92 public String getCurrentServerTime( String locale )
93 throws ArchivaRestServiceException
95 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( locale ) );
96 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 );
122 public List<CacheEntry> getCacheEntries()
123 throws ArchivaRestServiceException
125 List<CacheEntry> cacheEntries = new ArrayList<CacheEntry>( caches.size() );
126 DecimalFormat decimalFormat = new DecimalFormat( "#%" );
128 for ( Map.Entry<String, Cache> entry : caches.entrySet() )
130 CacheStatistics cacheStatistics = entry.getValue().getStatistics();
132 cacheEntries.add( new CacheEntry( entry.getKey(), cacheStatistics.getSize(), cacheStatistics.getCacheHits(),
133 cacheStatistics.getCacheMiss(),
134 decimalFormat.format( cacheStatistics.getCacheHitRate() ).toString(),
135 cacheStatistics.getInMemorySize() ) );
138 Collections.sort( cacheEntries );
144 public Boolean clearCache( String cacheKey )
145 throws ArchivaRestServiceException
147 Cache cache = caches.get( cacheKey );
150 throw new ArchivaRestServiceException( "no cache for key: " + cacheKey,
151 Response.Status.BAD_REQUEST.getStatusCode(), null );
159 public Boolean clearAllCaches()
160 throws ArchivaRestServiceException
162 for ( Cache cache : caches.values() )
170 public List<RepositoryScannerStatistics> getRepositoryScannerStatistics()
171 throws ArchivaRestServiceException
173 Set<RepositoryScannerInstance> repositoryScannerInstances = scanner.getInProgressScans();
174 if ( repositoryScannerInstances.isEmpty() )
176 return Collections.emptyList();
178 List<RepositoryScannerStatistics> repositoryScannerStatisticsList =
179 new ArrayList<RepositoryScannerStatistics>( repositoryScannerInstances.size() );
181 for ( RepositoryScannerInstance instance : repositoryScannerInstances )
183 RepositoryScannerStatistics repositoryScannerStatistics = new RepositoryScannerStatistics();
184 repositoryScannerStatisticsList.add( repositoryScannerStatistics );
185 repositoryScannerStatistics.setManagedRepository( instance.getRepository() );
186 repositoryScannerStatistics.setNewFileCount( instance.getStats().getNewFileCount() );
187 repositoryScannerStatistics.setTotalFileCount( instance.getStats().getTotalFileCount() );
188 repositoryScannerStatistics.setConsumerScanningStatistics( mapConsumerScanningStatistics( instance ) );
191 return repositoryScannerStatisticsList;
194 private List<ConsumerScanningStatistics> mapConsumerScanningStatistics( RepositoryScannerInstance instance )
196 DecimalFormat decimalFormat = new DecimalFormat( "###.##" );
197 if ( instance.getConsumerCounts() == null )
199 return Collections.emptyList();
201 List<ConsumerScanningStatistics> ret =
202 new ArrayList<ConsumerScanningStatistics>( instance.getConsumerCounts().size() );
203 for ( Map.Entry<String, Long> entry : instance.getConsumerCounts().entrySet() )
205 ConsumerScanningStatistics consumerScanningStatistics = new ConsumerScanningStatistics();
206 consumerScanningStatistics.setConsumerKey( entry.getKey() );
207 consumerScanningStatistics.setCount( entry.getValue() );
208 consumerScanningStatistics.setTime( instance.getConsumerTimings().get( entry.getKey() ) );
209 if ( consumerScanningStatistics.getCount() > 0 )
211 consumerScanningStatistics.setAverage( decimalFormat.format(
212 consumerScanningStatistics.getTime() / consumerScanningStatistics.getCount() ) );
214 ret.add( consumerScanningStatistics );
216 Collections.sort( ret, ConsumerScanningStatisticsComparator.INSTANCE );