This project has retired. For details please refer to its
Attic page.
ArchivaStartup xref
1 package org.apache.archiva.web.startup;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.common.ArchivaException;
23 import org.apache.archiva.components.scheduler.DefaultScheduler;
24 import org.apache.archiva.components.taskqueue.Task;
25 import org.apache.archiva.components.taskqueue.execution.ThreadedTaskQueueExecutor;
26 import org.apache.archiva.scheduler.repository.DefaultRepositoryArchivaTaskScheduler;
27 import org.quartz.SchedulerException;
28 import org.springframework.web.context.WebApplicationContext;
29 import org.springframework.web.context.support.WebApplicationContextUtils;
30
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletContextEvent;
33 import javax.servlet.ServletContextListener;
34 import java.lang.reflect.Field;
35 import java.util.Properties;
36 import java.util.concurrent.ExecutorService;
37
38
39
40
41 public class ArchivaStartup
42 implements ServletContextListener
43 {
44 private ThreadedTaskQueueExecutor tqeDbScanning;
45
46 private ThreadedTaskQueueExecutor tqeRepoScanning;
47
48 private ThreadedTaskQueueExecutor tqeIndexing;
49
50 private DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
51
52 @Override
53 public void contextInitialized( ServletContextEvent contextEvent )
54 {
55 WebApplicationContext wac =
56 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
57
58 SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
59
60 repositoryTaskScheduler =
61 wac.getBean( "archivaTaskScheduler#repository", DefaultRepositoryArchivaTaskScheduler.class );
62
63 Properties archivaRuntimeProperties = wac.getBean( "archivaRuntimeProperties", Properties.class );
64
65 tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
66
67 tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
68
69
70 try
71 {
72 securitySync.startup();
73 repositoryTaskScheduler.startup();
74 Banner.display( (String) archivaRuntimeProperties.get( "archiva.version" ) );
75 }
76 catch ( ArchivaException e )
77 {
78 throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
79 }
80 }
81
82 @Override
83 public void contextDestroyed( ServletContextEvent contextEvent )
84 {
85 WebApplicationContext applicationContext =
86 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
87
88
89
90 ServletContext servletContext = contextEvent.getServletContext();
91
92
93
94
95
96
97
98
99
100 if ( applicationContext != null )
101 {
102
103 stopTaskQueueExecutor( tqeDbScanning, servletContext );
104 stopTaskQueueExecutor( tqeRepoScanning, servletContext );
105 stopTaskQueueExecutor( tqeIndexing, servletContext );
106
107
108 if ( repositoryTaskScheduler != null )
109 {
110 try
111 {
112 repositoryTaskScheduler.stop();
113 }
114 catch ( SchedulerException e )
115 {
116 servletContext.log( e.getMessage(), e );
117 }
118
119 try
120 {
121
122 Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
123 schedulerField.setAccessible( true );
124
125 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
126 scheduler.stop();
127 }
128 catch ( Exception e )
129 {
130 servletContext.log( e.getMessage(), e );
131 }
132 }
133
134
135
136
137
138 }
139
140
141 }
142
143 private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor, ServletContext servletContext )
144 {
145 if ( taskQueueExecutor != null )
146 {
147 Task currentTask = taskQueueExecutor.getCurrentTask();
148 if ( currentTask != null )
149 {
150 taskQueueExecutor.cancelTask( currentTask );
151 }
152
153 try
154 {
155 taskQueueExecutor.stop();
156 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor, servletContext );
157 if ( service != null )
158 {
159 service.shutdown();
160 }
161 }
162 catch ( Exception e )
163 {
164 servletContext.log( e.getMessage(), e );
165 }
166 }
167 }
168
169 private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe, ServletContext servletContext )
170 {
171 ExecutorService service = null;
172 try
173 {
174 Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
175 executorServiceField.setAccessible( true );
176 service = (ExecutorService) executorServiceField.get( ttqe );
177 }
178 catch ( Exception e )
179 {
180 servletContext.log( e.getMessage(), e );
181 }
182 return service;
183 }
184 }