This project has retired. For details please refer to its Attic page.
DefaultSystemStatusService xref
View Javadoc
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  
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;
36  
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;
46  import java.util.Map;
47  import java.util.Set;
48  
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  
59  
60      private Map<String, TaskQueue> queues = null;
61  
62      private Map<String, Cache> caches = null;
63  
64      private RepositoryScanner scanner;
65  
66      // display spring scheduled
67      //@Inject @Named (value="springScheduler");
68  
69  
70      @Inject
71      public DefaultSystemStatusService( ApplicationContext applicationContext, RepositoryScanner scanner )
72      {
73          this.scanner = scanner;
74  
75          queues = getBeansOfType( applicationContext, TaskQueue.class );
76  
77          caches = getBeansOfType( applicationContext, Cache.class );
78      }
79  
80      @Override
81      public String getMemoryStatus()
82          throws ArchivaRestServiceException
83      {
84          Runtime runtime = Runtime.getRuntime();
85  
86          long total = runtime.totalMemory();
87          long used = total - runtime.freeMemory();
88          long max = runtime.maxMemory();
89          return formatMemory( used ) + "/" + formatMemory( total ) + " (Max: " + formatMemory( max ) + ")";
90      }
91  
92      private static String formatMemory( long l )
93      {
94          return l / ( 1024 * 1024 ) + "M";
95      }
96  
97      @Override
98      public String getCurrentServerTime( String locale )
99          throws ArchivaRestServiceException
100     {
101         SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( locale ) );
102         return sdf.format( new Date() );
103     }
104 
105     @Override
106     public List<QueueEntry> getQueueEntries()
107         throws ArchivaRestServiceException
108     {
109         try
110         {
111             List<QueueEntry> queueEntries = new ArrayList<QueueEntry>( queues.size() );
112             for ( Map.Entry<String, TaskQueue> entry : queues.entrySet() )
113             {
114                 queueEntries.add( new QueueEntry( entry.getKey(), entry.getValue().getQueueSnapshot().size() ) );
115             }
116 
117             return queueEntries;
118         }
119         catch ( TaskQueueException e )
120         {
121             log.error( e.getMessage(), e );
122             throw new ArchivaRestServiceException( e.getMessage(),
123                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
124         }
125     }
126 
127     @Override
128     public List<CacheEntry> getCacheEntries()
129         throws ArchivaRestServiceException
130     {
131         List<CacheEntry> cacheEntries = new ArrayList<CacheEntry>( caches.size() );
132         DecimalFormat decimalFormat = new DecimalFormat( "#%" );
133 
134         for ( Map.Entry<String, Cache> entry : caches.entrySet() )
135         {
136             CacheStatistics cacheStatistics = entry.getValue().getStatistics();
137 
138             cacheEntries.add( new CacheEntry( entry.getKey(), cacheStatistics.getSize(), cacheStatistics.getCacheHits(),
139                                               cacheStatistics.getCacheMiss(),
140                                               decimalFormat.format( cacheStatistics.getCacheHitRate() ).toString(),
141                                               cacheStatistics.getInMemorySize() ) );
142         }
143 
144         Collections.sort( cacheEntries );
145 
146         return cacheEntries;
147     }
148 
149     @Override
150     public Boolean clearCache( String cacheKey )
151         throws ArchivaRestServiceException
152     {
153         Cache cache = caches.get( cacheKey );
154         if ( cache == null )
155         {
156             throw new ArchivaRestServiceException( "no cache for key: " + cacheKey,
157                                                    Response.Status.BAD_REQUEST.getStatusCode(), null );
158         }
159 
160         cache.clear();
161         return Boolean.TRUE;
162     }
163 
164     @Override
165     public Boolean clearAllCaches()
166         throws ArchivaRestServiceException
167     {
168         for ( Cache cache : caches.values() )
169         {
170             cache.clear();
171         }
172         return Boolean.TRUE;
173     }
174 
175     @Override
176     public List<RepositoryScannerStatistics> getRepositoryScannerStatistics()
177         throws ArchivaRestServiceException
178     {
179         Set<RepositoryScannerInstance> repositoryScannerInstances = scanner.getInProgressScans();
180         if ( repositoryScannerInstances.isEmpty() )
181         {
182             return Collections.emptyList();
183         }
184         List<RepositoryScannerStatistics> repositoryScannerStatisticsList =
185             new ArrayList<RepositoryScannerStatistics>( repositoryScannerInstances.size() );
186 
187         for ( RepositoryScannerInstance instance : repositoryScannerInstances )
188         {
189             RepositoryScannerStatistics repositoryScannerStatistics = new RepositoryScannerStatistics();
190             repositoryScannerStatisticsList.add( repositoryScannerStatistics );
191             repositoryScannerStatistics.setManagedRepository( instance.getRepository() );
192             repositoryScannerStatistics.setNewFileCount( instance.getStats().getNewFileCount() );
193             repositoryScannerStatistics.setTotalFileCount( instance.getStats().getTotalFileCount() );
194             repositoryScannerStatistics.setConsumerScanningStatistics( mapConsumerScanningStatistics( instance ) );
195         }
196 
197         return repositoryScannerStatisticsList;
198     }
199 
200     private List<ConsumerScanningStatistics> mapConsumerScanningStatistics( RepositoryScannerInstance instance )
201     {
202         DecimalFormat decimalFormat = new DecimalFormat( "###.##" );
203         if ( instance.getConsumerCounts() == null )
204         {
205             return Collections.emptyList();
206         }
207         List<ConsumerScanningStatistics> ret =
208             new ArrayList<ConsumerScanningStatistics>( instance.getConsumerCounts().size() );
209         for ( Map.Entry<String, Long> entry : instance.getConsumerCounts().entrySet() )
210         {
211             ConsumerScanningStatistics consumerScanningStatistics = new ConsumerScanningStatistics();
212             consumerScanningStatistics.setConsumerKey( entry.getKey() );
213             consumerScanningStatistics.setCount( entry.getValue() );
214             consumerScanningStatistics.setTime( instance.getConsumerTimings().get( entry.getKey() ) );
215             if ( consumerScanningStatistics.getCount() > 0 )
216             {
217                 consumerScanningStatistics.setAverage( decimalFormat.format(
218                     consumerScanningStatistics.getTime() / consumerScanningStatistics.getCount() ) );
219             }
220             ret.add( consumerScanningStatistics );
221         }
222         Collections.sort( ret, ConsumerScanningStatisticsComparator.INSTANCE );
223         return ret;
224     }
225 }