You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultSystemStatusService.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package org.apache.archiva.rest.services;
  2. /*
  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
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  22. import org.apache.archiva.redback.components.cache.Cache;
  23. import org.apache.archiva.redback.components.cache.CacheStatistics;
  24. import org.apache.archiva.redback.components.taskqueue.TaskQueue;
  25. import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
  26. import org.apache.archiva.repository.scanner.RepositoryScanner;
  27. import org.apache.archiva.repository.scanner.RepositoryScannerInstance;
  28. import org.apache.archiva.rest.api.model.CacheEntry;
  29. import org.apache.archiva.rest.api.model.ConsumerScanningStatistics;
  30. import org.apache.archiva.rest.api.model.QueueEntry;
  31. import org.apache.archiva.rest.api.model.RepositoryScannerStatistics;
  32. import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
  33. import org.apache.archiva.rest.api.services.SystemStatusService;
  34. import org.apache.archiva.rest.services.utils.ConsumerScanningStatisticsComparator;
  35. import org.springframework.context.ApplicationContext;
  36. 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.Comparator;
  44. import java.util.Date;
  45. import java.util.List;
  46. import java.util.Locale;
  47. import java.util.Map;
  48. import java.util.Set;
  49. /**
  50. * @author Olivier Lamy
  51. * @since 1.4-M3
  52. */
  53. @Service( "systemStatusService#rest" )
  54. public class DefaultSystemStatusService
  55. extends AbstractRestService
  56. implements SystemStatusService
  57. {
  58. private Map<String, TaskQueue> queues = null;
  59. private Map<String, Cache> caches = null;
  60. private RepositoryScanner scanner;
  61. ManagedRepositoryAdmin managedRepositoryAdmin;
  62. // display spring scheduled
  63. //@Inject @Named (value="springScheduler");
  64. @Inject
  65. public DefaultSystemStatusService( ApplicationContext applicationContext, RepositoryScanner scanner )
  66. {
  67. this.scanner = scanner;
  68. queues = getBeansOfType( applicationContext, TaskQueue.class );
  69. caches = getBeansOfType( applicationContext, Cache.class );
  70. managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
  71. }
  72. @Override
  73. public String getMemoryStatus()
  74. throws ArchivaRestServiceException
  75. {
  76. Runtime runtime = Runtime.getRuntime();
  77. long total = runtime.totalMemory();
  78. long used = total - runtime.freeMemory();
  79. long max = runtime.maxMemory();
  80. return formatMemory( used ) + "/" + formatMemory( total ) + " (Max: " + formatMemory( max ) + ")";
  81. }
  82. private static String formatMemory( long l )
  83. {
  84. return l / ( 1024 * 1024 ) + "M";
  85. }
  86. @Override
  87. public String getCurrentServerTime( String locale )
  88. throws ArchivaRestServiceException
  89. {
  90. SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( locale ) );
  91. return sdf.format( new Date() );
  92. }
  93. @Override
  94. public List<QueueEntry> getQueueEntries()
  95. throws ArchivaRestServiceException
  96. {
  97. try
  98. {
  99. List<QueueEntry> queueEntries = new ArrayList<QueueEntry>( queues.size() );
  100. for ( Map.Entry<String, TaskQueue> entry : queues.entrySet() )
  101. {
  102. queueEntries.add( new QueueEntry( entry.getKey(), entry.getValue().getQueueSnapshot().size() ) );
  103. }
  104. return queueEntries;
  105. }
  106. catch ( TaskQueueException e )
  107. {
  108. log.error( e.getMessage(), e );
  109. throw new ArchivaRestServiceException( e.getMessage(),
  110. Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
  111. }
  112. }
  113. // Used for generics
  114. private class CacheEntryComparator implements Comparator<CacheEntry>
  115. {
  116. @Override
  117. public int compare( CacheEntry o1, CacheEntry o2 )
  118. {
  119. return o1.compareTo( o2 );
  120. }
  121. }
  122. @Override
  123. public List<CacheEntry> getCacheEntries()
  124. throws ArchivaRestServiceException
  125. {
  126. List<CacheEntry> cacheEntries = new ArrayList<CacheEntry>( caches.size() );
  127. DecimalFormat decimalFormat = new DecimalFormat( "#%" );
  128. for ( Map.Entry<String, Cache> entry : caches.entrySet() )
  129. {
  130. 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() ) );
  135. }
  136. Collections.sort( cacheEntries, new CacheEntryComparator() );
  137. return cacheEntries;
  138. }
  139. @Override
  140. public Boolean clearCache( String cacheKey )
  141. throws ArchivaRestServiceException
  142. {
  143. Cache cache = caches.get( cacheKey );
  144. if ( cache == null )
  145. {
  146. throw new ArchivaRestServiceException( "no cache for key: " + cacheKey,
  147. Response.Status.BAD_REQUEST.getStatusCode(), null );
  148. }
  149. cache.clear();
  150. return Boolean.TRUE;
  151. }
  152. @Override
  153. public Boolean clearAllCaches()
  154. throws ArchivaRestServiceException
  155. {
  156. for ( Cache cache : caches.values() )
  157. {
  158. cache.clear();
  159. }
  160. return Boolean.TRUE;
  161. }
  162. @Override
  163. public List<RepositoryScannerStatistics> getRepositoryScannerStatistics()
  164. throws ArchivaRestServiceException
  165. {
  166. Set<RepositoryScannerInstance> repositoryScannerInstances = scanner.getInProgressScans();
  167. if ( repositoryScannerInstances.isEmpty() )
  168. {
  169. return Collections.emptyList();
  170. }
  171. List<RepositoryScannerStatistics> repositoryScannerStatisticsList =
  172. new ArrayList<RepositoryScannerStatistics>( repositoryScannerInstances.size() );
  173. for ( RepositoryScannerInstance instance : repositoryScannerInstances )
  174. {
  175. RepositoryScannerStatistics repositoryScannerStatistics = new RepositoryScannerStatistics();
  176. repositoryScannerStatisticsList.add( repositoryScannerStatistics );
  177. try
  178. {
  179. repositoryScannerStatistics.setManagedRepository( managedRepositoryAdmin.getManagedRepository( instance.getRepository().getId()) );
  180. }
  181. catch ( RepositoryAdminException e )
  182. {
  183. log.error("Could not retrieve repository '{}'", instance.getRepository().getId());
  184. }
  185. repositoryScannerStatistics.setNewFileCount( instance.getStats().getNewFileCount() );
  186. repositoryScannerStatistics.setTotalFileCount( instance.getStats().getTotalFileCount() );
  187. repositoryScannerStatistics.setConsumerScanningStatistics( mapConsumerScanningStatistics( instance ) );
  188. }
  189. return repositoryScannerStatisticsList;
  190. }
  191. private List<ConsumerScanningStatistics> mapConsumerScanningStatistics( RepositoryScannerInstance instance )
  192. {
  193. DecimalFormat decimalFormat = new DecimalFormat( "###.##" );
  194. if ( instance.getConsumerCounts() == null )
  195. {
  196. return Collections.emptyList();
  197. }
  198. List<ConsumerScanningStatistics> ret =
  199. new ArrayList<ConsumerScanningStatistics>( instance.getConsumerCounts().size() );
  200. for ( Map.Entry<String, Long> entry : instance.getConsumerCounts().entrySet() )
  201. {
  202. ConsumerScanningStatistics consumerScanningStatistics = new ConsumerScanningStatistics();
  203. consumerScanningStatistics.setConsumerKey( entry.getKey() );
  204. consumerScanningStatistics.setCount( entry.getValue() );
  205. consumerScanningStatistics.setTime( instance.getConsumerTimings().get( entry.getKey() ) );
  206. if ( consumerScanningStatistics.getCount() > 0 )
  207. {
  208. consumerScanningStatistics.setAverage( decimalFormat.format(
  209. consumerScanningStatistics.getTime() / consumerScanningStatistics.getCount() ) );
  210. }
  211. ret.add( consumerScanningStatistics );
  212. }
  213. Collections.sort( ret, ConsumerScanningStatisticsComparator.INSTANCE );
  214. return ret;
  215. }
  216. }