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.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.components.taskqueue.TaskQueue;
26  import org.apache.archiva.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;
38  
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;
49  import java.util.Map;
50  import java.util.Set;
51  
52  /**
53   * @author Olivier Lamy
54   * @since 1.4-M3
55   */
56  @Service( "systemStatusService#rest" )
57  public class DefaultSystemStatusService
58      extends AbstractRestService
59      implements SystemStatusService
60  {
61  
62  
63      private Map<String, TaskQueue> queues = null;
64  
65      private Map<String, Cache> caches = null;
66  
67      private RepositoryScanner scanner;
68  
69      ManagedRepositoryAdmin managedRepositoryAdmin;
70  
71      // display spring scheduled
72      //@Inject @Named (value="springScheduler");
73  
74  
75      @Inject
76      public DefaultSystemStatusService( ApplicationContext applicationContext, RepositoryScanner scanner )
77      {
78          this.scanner = scanner;
79  
80          queues = getBeansOfType( applicationContext, TaskQueue.class );
81  
82          caches = getBeansOfType( applicationContext, Cache.class );
83  
84          managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
85      }
86  
87      @Override
88      public String getMemoryStatus()
89          throws ArchivaRestServiceException
90      {
91          Runtime runtime = Runtime.getRuntime();
92  
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 ) + ")";
97      }
98  
99      private static String formatMemory( long l )
100     {
101         return l / ( 1024 * 1024 ) + "M";
102     }
103 
104     @Override
105     public String getCurrentServerTime( String locale )
106         throws ArchivaRestServiceException
107     {
108         SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( locale ) );
109         return sdf.format( new Date() );
110     }
111 
112     @Override
113     public List<QueueEntry> getQueueEntries()
114         throws ArchivaRestServiceException
115     {
116         try
117         {
118             List<QueueEntry> queueEntries = new ArrayList<QueueEntry>( queues.size() );
119             for ( Map.Entry<String, TaskQueue> entry : queues.entrySet() )
120             {
121                 queueEntries.add( new QueueEntry( entry.getKey(), entry.getValue().getQueueSnapshot().size() ) );
122             }
123 
124             return queueEntries;
125         }
126         catch ( TaskQueueException e )
127         {
128             log.error( e.getMessage(), e );
129             throw new ArchivaRestServiceException( e.getMessage(),
130                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
131         }
132     }
133 
134     // Used for generics
135     private class CacheEntryComparator implements Comparator<CacheEntry>
136     {
137 
138         @Override
139         public int compare( CacheEntry="../../../../../org/apache/archiva/rest/api/model/CacheEntry.html#CacheEntry">CacheEntry o1, CacheEntry o2 )
140         {
141             return o1.compareTo( o2 );
142         }
143     }
144 
145     @Override
146     public List<CacheEntry> getCacheEntries()
147         throws ArchivaRestServiceException
148     {
149         List<CacheEntry> cacheEntries = new ArrayList<CacheEntry>( caches.size() );
150         DecimalFormat decimalFormat = new DecimalFormat( "#%" );
151 
152         for ( Map.Entry<String, Cache> entry : caches.entrySet() )
153         {
154             CacheStatistics cacheStatistics = entry.getValue().getStatistics();
155 
156             cacheEntries.add( new CacheEntry( entry.getKey(), cacheStatistics.getSize(), cacheStatistics.getCacheHits(),
157                                               cacheStatistics.getCacheMiss(),
158                                               decimalFormat.format( cacheStatistics.getCacheHitRate() ).toString(),
159                                               cacheStatistics.getInMemorySize() ) );
160         }
161 
162         Collections.sort( cacheEntries, new CacheEntryComparator() );
163 
164         return cacheEntries;
165     }
166 
167     @Override
168     public Boolean clearCache( String cacheKey )
169         throws ArchivaRestServiceException
170     {
171         Cache cache = caches.get( cacheKey );
172         if ( cache == null )
173         {
174             throw new ArchivaRestServiceException( "no cache for key: " + cacheKey,
175                                                    Response.Status.BAD_REQUEST.getStatusCode(), null );
176         }
177 
178         cache.clear();
179         return Boolean.TRUE;
180     }
181 
182     @Override
183     public Boolean clearAllCaches()
184         throws ArchivaRestServiceException
185     {
186         for ( Cache cache : caches.values() )
187         {
188             cache.clear();
189         }
190         return Boolean.TRUE;
191     }
192 
193     @Override
194     public List<RepositoryScannerStatistics> getRepositoryScannerStatistics()
195         throws ArchivaRestServiceException
196     {
197         Set<RepositoryScannerInstance> repositoryScannerInstances = scanner.getInProgressScans();
198         if ( repositoryScannerInstances.isEmpty() )
199         {
200             return Collections.emptyList();
201         }
202         List<RepositoryScannerStatistics> repositoryScannerStatisticsList =
203             new ArrayList<RepositoryScannerStatistics>( repositoryScannerInstances.size() );
204 
205 
206         for ( RepositoryScannerInstance instance : repositoryScannerInstances )
207         {
208             RepositoryScannerStatisticsRepositoryScannerStatistics">RepositoryScannerStatistics repositoryScannerStatistics = new RepositoryScannerStatistics();
209             repositoryScannerStatisticsList.add( repositoryScannerStatistics );
210             try
211             {
212                 repositoryScannerStatistics.setManagedRepository( managedRepositoryAdmin.getManagedRepository( instance.getRepository().getId())  );
213             }
214             catch ( RepositoryAdminException e )
215             {
216                 log.error("Could not retrieve repository '{}'", instance.getRepository().getId());
217             }
218             repositoryScannerStatistics.setNewFileCount( instance.getStats().getNewFileCount() );
219             repositoryScannerStatistics.setTotalFileCount( instance.getStats().getTotalFileCount() );
220             repositoryScannerStatistics.setConsumerScanningStatistics( mapConsumerScanningStatistics( instance ) );
221         }
222 
223         return repositoryScannerStatisticsList;
224     }
225 
226     private List<ConsumerScanningStatistics> mapConsumerScanningStatistics( RepositoryScannerInstance instance )
227     {
228         DecimalFormat decimalFormat = new DecimalFormat( "###.##" );
229         if ( instance.getConsumerCounts() == null )
230         {
231             return Collections.emptyList();
232         }
233         List<ConsumerScanningStatistics> ret =
234             new ArrayList<ConsumerScanningStatistics>( instance.getConsumerCounts().size() );
235         for ( Map.Entry<String, Long> entry : instance.getConsumerCounts().entrySet() )
236         {
237             ConsumerScanningStatistics#ConsumerScanningStatistics">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 )
242             {
243                 consumerScanningStatistics.setAverage( decimalFormat.format(
244                     consumerScanningStatistics.getTime() / consumerScanningStatistics.getCount() ) );
245             }
246             ret.add( consumerScanningStatistics );
247         }
248         Collections.sort( ret, ConsumerScanningStatisticsComparator.INSTANCE );
249         return ret;
250     }
251 }