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.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
23 import org.apache.archiva.components.cache.Cache;
24 import org.apache.archiva.components.cache.CacheStatistics;
25 import org.apache.archiva.redback.components.taskqueue.TaskQueue;
26 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
27 import org.apache.archiva.repository.scanner.RepositoryScanner;
28 import org.apache.archiva.repository.scanner.RepositoryScannerInstance;
29 import org.apache.archiva.rest.api.model.CacheEntry;
30 import org.apache.archiva.rest.api.model.ConsumerScanningStatistics;
31 import org.apache.archiva.rest.api.model.QueueEntry;
32 import org.apache.archiva.rest.api.model.RepositoryScannerStatistics;
33 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
34 import org.apache.archiva.rest.api.services.SystemStatusService;
35 import org.apache.archiva.rest.services.utils.ConsumerScanningStatisticsComparator;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Inject;
40 import javax.ws.rs.core.Response;
41 import java.text.DecimalFormat;
42 import java.text.SimpleDateFormat;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.Comparator;
46 import java.util.Date;
47 import java.util.List;
48 import java.util.Locale;
53 * @author Olivier Lamy
56 @Service( "systemStatusService#rest" )
57 public class DefaultSystemStatusService
58 extends AbstractRestService
59 implements SystemStatusService
63 private Map<String, TaskQueue> queues = null;
65 private Map<String, Cache> caches = null;
67 private RepositoryScanner scanner;
69 ManagedRepositoryAdmin managedRepositoryAdmin;
71 // display spring scheduled
72 //@Inject @Named (value="springScheduler");
76 public DefaultSystemStatusService( ApplicationContext applicationContext, RepositoryScanner scanner )
78 this.scanner = scanner;
80 queues = getBeansOfType( applicationContext, TaskQueue.class );
82 caches = getBeansOfType( applicationContext, Cache.class );
84 managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
88 public String getMemoryStatus()
89 throws ArchivaRestServiceException
91 Runtime runtime = Runtime.getRuntime();
93 long total = runtime.totalMemory();
94 long used = total - runtime.freeMemory();
95 long max = runtime.maxMemory();
96 return formatMemory( used ) + "/" + formatMemory( total ) + " (Max: " + formatMemory( max ) + ")";
99 private static String formatMemory( long l )
101 return l / ( 1024 * 1024 ) + "M";
105 public String getCurrentServerTime( String locale )
106 throws ArchivaRestServiceException
108 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( locale ) );
109 return sdf.format( new Date() );
113 public List<QueueEntry> getQueueEntries()
114 throws ArchivaRestServiceException
118 List<QueueEntry> queueEntries = new ArrayList<QueueEntry>( queues.size() );
119 for ( Map.Entry<String, TaskQueue> entry : queues.entrySet() )
121 queueEntries.add( new QueueEntry( entry.getKey(), entry.getValue().getQueueSnapshot().size() ) );
126 catch ( TaskQueueException e )
128 log.error( e.getMessage(), e );
129 throw new ArchivaRestServiceException( e.getMessage(),
130 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
135 private class CacheEntryComparator implements Comparator<CacheEntry>
139 public int compare( CacheEntry o1, CacheEntry o2 )
141 return o1.compareTo( o2 );
146 public List<CacheEntry> getCacheEntries()
147 throws ArchivaRestServiceException
149 List<CacheEntry> cacheEntries = new ArrayList<CacheEntry>( caches.size() );
150 DecimalFormat decimalFormat = new DecimalFormat( "#%" );
152 for ( Map.Entry<String, Cache> entry : caches.entrySet() )
154 CacheStatistics cacheStatistics = entry.getValue().getStatistics();
156 cacheEntries.add( new CacheEntry( entry.getKey(), cacheStatistics.getSize(), cacheStatistics.getCacheHits(),
157 cacheStatistics.getCacheMiss(),
158 decimalFormat.format( cacheStatistics.getCacheHitRate() ).toString(),
159 cacheStatistics.getInMemorySize() ) );
162 Collections.sort( cacheEntries, new CacheEntryComparator() );
168 public Boolean clearCache( String cacheKey )
169 throws ArchivaRestServiceException
171 Cache cache = caches.get( cacheKey );
174 throw new ArchivaRestServiceException( "no cache for key: " + cacheKey,
175 Response.Status.BAD_REQUEST.getStatusCode(), null );
183 public Boolean clearAllCaches()
184 throws ArchivaRestServiceException
186 for ( Cache cache : caches.values() )
194 public List<RepositoryScannerStatistics> getRepositoryScannerStatistics()
195 throws ArchivaRestServiceException
197 Set<RepositoryScannerInstance> repositoryScannerInstances = scanner.getInProgressScans();
198 if ( repositoryScannerInstances.isEmpty() )
200 return Collections.emptyList();
202 List<RepositoryScannerStatistics> repositoryScannerStatisticsList =
203 new ArrayList<RepositoryScannerStatistics>( repositoryScannerInstances.size() );
206 for ( RepositoryScannerInstance instance : repositoryScannerInstances )
208 RepositoryScannerStatistics repositoryScannerStatistics = new RepositoryScannerStatistics();
209 repositoryScannerStatisticsList.add( repositoryScannerStatistics );
212 repositoryScannerStatistics.setManagedRepository( managedRepositoryAdmin.getManagedRepository( instance.getRepository().getId()) );
214 catch ( RepositoryAdminException e )
216 log.error("Could not retrieve repository '{}'", instance.getRepository().getId());
218 repositoryScannerStatistics.setNewFileCount( instance.getStats().getNewFileCount() );
219 repositoryScannerStatistics.setTotalFileCount( instance.getStats().getTotalFileCount() );
220 repositoryScannerStatistics.setConsumerScanningStatistics( mapConsumerScanningStatistics( instance ) );
223 return repositoryScannerStatisticsList;
226 private List<ConsumerScanningStatistics> mapConsumerScanningStatistics( RepositoryScannerInstance instance )
228 DecimalFormat decimalFormat = new DecimalFormat( "###.##" );
229 if ( instance.getConsumerCounts() == null )
231 return Collections.emptyList();
233 List<ConsumerScanningStatistics> ret =
234 new ArrayList<ConsumerScanningStatistics>( instance.getConsumerCounts().size() );
235 for ( Map.Entry<String, Long> entry : instance.getConsumerCounts().entrySet() )
237 ConsumerScanningStatistics consumerScanningStatistics = new ConsumerScanningStatistics();
238 consumerScanningStatistics.setConsumerKey( entry.getKey() );
239 consumerScanningStatistics.setCount( entry.getValue() );
240 consumerScanningStatistics.setTime( instance.getConsumerTimings().get( entry.getKey() ) );
241 if ( consumerScanningStatistics.getCount() > 0 )
243 consumerScanningStatistics.setAverage( decimalFormat.format(
244 consumerScanningStatistics.getTime() / consumerScanningStatistics.getCount() ) );
246 ret.add( consumerScanningStatistics );
248 Collections.sort( ret, ConsumerScanningStatisticsComparator.INSTANCE );