Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ArchivaStartup.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package org.apache.archiva.web.startup;
  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.common.ArchivaException;
  21. import org.apache.archiva.redback.components.scheduler.DefaultScheduler;
  22. import org.apache.archiva.redback.components.taskqueue.Task;
  23. import org.apache.archiva.redback.components.taskqueue.execution.ThreadedTaskQueueExecutor;
  24. import org.apache.archiva.scheduler.repository.DefaultRepositoryArchivaTaskScheduler;
  25. import org.apache.maven.index.context.IndexingContext;
  26. import org.quartz.SchedulerException;
  27. import org.springframework.web.context.WebApplicationContext;
  28. import org.springframework.web.context.support.WebApplicationContextUtils;
  29. import javax.servlet.ServletContext;
  30. import javax.servlet.ServletContextEvent;
  31. import javax.servlet.ServletContextListener;
  32. import java.lang.reflect.Field;
  33. import java.util.Properties;
  34. import java.util.concurrent.ExecutorService;
  35. /**
  36. * ArchivaStartup - the startup of all archiva features in a deterministic order.
  37. */
  38. public class ArchivaStartup
  39. implements ServletContextListener
  40. {
  41. private ThreadedTaskQueueExecutor tqeDbScanning;
  42. private ThreadedTaskQueueExecutor tqeRepoScanning;
  43. private ThreadedTaskQueueExecutor tqeIndexing;
  44. private DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
  45. @Override
  46. public void contextInitialized( ServletContextEvent contextEvent )
  47. {
  48. WebApplicationContext wac =
  49. WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
  50. SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
  51. repositoryTaskScheduler =
  52. wac.getBean( "archivaTaskScheduler#repository", DefaultRepositoryArchivaTaskScheduler.class );
  53. Properties archivaRuntimeProperties = wac.getBean( "archivaRuntimeProperties", Properties.class );
  54. tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
  55. tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
  56. try
  57. {
  58. securitySync.startup();
  59. repositoryTaskScheduler.startup();
  60. Banner.display( (String) archivaRuntimeProperties.get( "archiva.version" ) );
  61. }
  62. catch ( ArchivaException e )
  63. {
  64. throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
  65. }
  66. }
  67. @Override
  68. public void contextDestroyed( ServletContextEvent contextEvent )
  69. {
  70. WebApplicationContext applicationContext =
  71. WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
  72. // we log using servlet mechanism as due to some possible problem with slf4j when container shutdown
  73. // so servletContext.log
  74. ServletContext servletContext = contextEvent.getServletContext();
  75. // TODO check this stop
  76. /*
  77. if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
  78. {
  79. ( (ClassPathXmlApplicationContext) applicationContext ).close();
  80. } */
  81. if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
  82. {
  83. // stop task queue executors
  84. stopTaskQueueExecutor( tqeDbScanning, servletContext );
  85. stopTaskQueueExecutor( tqeRepoScanning, servletContext );
  86. stopTaskQueueExecutor( tqeIndexing, servletContext );
  87. // stop the DefaultArchivaTaskScheduler and its scheduler
  88. if ( repositoryTaskScheduler != null )
  89. {
  90. try
  91. {
  92. repositoryTaskScheduler.stop();
  93. }
  94. catch ( SchedulerException e )
  95. {
  96. servletContext.log( e.getMessage(), e );
  97. }
  98. try
  99. {
  100. // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
  101. Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
  102. schedulerField.setAccessible( true );
  103. DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
  104. scheduler.stop();
  105. }
  106. catch ( Exception e )
  107. {
  108. servletContext.log( e.getMessage(), e );
  109. }
  110. }
  111. // close the application context
  112. //applicationContext.close();
  113. // TODO fix close call
  114. //applicationContext.
  115. }
  116. }
  117. private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor, ServletContext servletContext )
  118. {
  119. if ( taskQueueExecutor != null )
  120. {
  121. Task currentTask = taskQueueExecutor.getCurrentTask();
  122. if ( currentTask != null )
  123. {
  124. taskQueueExecutor.cancelTask( currentTask );
  125. }
  126. try
  127. {
  128. taskQueueExecutor.stop();
  129. ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor, servletContext );
  130. if ( service != null )
  131. {
  132. service.shutdown();
  133. }
  134. }
  135. catch ( Exception e )
  136. {
  137. servletContext.log( e.getMessage(), e );
  138. }
  139. }
  140. }
  141. private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe, ServletContext servletContext )
  142. {
  143. ExecutorService service = null;
  144. try
  145. {
  146. Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
  147. executorServiceField.setAccessible( true );
  148. service = (ExecutorService) executorServiceField.get( ttqe );
  149. }
  150. catch ( Exception e )
  151. {
  152. servletContext.log( e.getMessage(), e );
  153. }
  154. return service;
  155. }
  156. }