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.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
25 import org.apache.archiva.redback.components.scheduler.DefaultScheduler;
26 import org.apache.archiva.scheduler.repository.DefaultRepositoryArchivaTaskScheduler;
27 import org.apache.maven.index.NexusIndexer;
28 import org.apache.maven.index.context.IndexingContext;
29 import org.apache.archiva.redback.components.taskqueue.Task;
30 import org.apache.archiva.redback.components.taskqueue.execution.ThreadedTaskQueueExecutor;
31 import org.quartz.SchedulerException;
32 import org.springframework.web.context.WebApplicationContext;
33 import org.springframework.web.context.support.WebApplicationContextUtils;
34
35 import javax.servlet.ServletContext;
36 import javax.servlet.ServletContextEvent;
37 import javax.servlet.ServletContextListener;
38 import java.lang.reflect.Field;
39 import java.util.Properties;
40 import java.util.concurrent.ExecutorService;
41
42
43
44
45 public class ArchivaStartup
46 implements ServletContextListener
47 {
48 private ThreadedTaskQueueExecutor tqeDbScanning;
49
50 private ThreadedTaskQueueExecutor tqeRepoScanning;
51
52 private ThreadedTaskQueueExecutor tqeIndexing;
53
54 private DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
55
56 private PlexusSisuBridge plexusSisuBridge;
57
58 private NexusIndexer nexusIndexer;
59
60 @Override
61 public void contextInitialized( ServletContextEvent contextEvent )
62 {
63 WebApplicationContext wac =
64 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
65
66 SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
67
68 repositoryTaskScheduler =
69 wac.getBean( "archivaTaskScheduler#repository", DefaultRepositoryArchivaTaskScheduler.class );
70
71 Properties archivaRuntimeProperties = wac.getBean( "archivaRuntimeProperties", Properties.class );
72
73 tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
74
75 tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
76
77 plexusSisuBridge = wac.getBean( PlexusSisuBridge.class );
78
79 try
80 {
81 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
82 }
83 catch ( PlexusSisuBridgeException e )
84 {
85 throw new RuntimeException( "Unable to get NexusIndexer: " + e.getMessage(), e );
86 }
87 try
88 {
89 securitySync.startup();
90 repositoryTaskScheduler.startup();
91 Banner.display( (String) archivaRuntimeProperties.get( "archiva.version" ) );
92 }
93 catch ( ArchivaException e )
94 {
95 throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
96 }
97 }
98
99 @Override
100 public void contextDestroyed( ServletContextEvent contextEvent )
101 {
102 WebApplicationContext applicationContext =
103 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
104
105
106
107 ServletContext servletContext = contextEvent.getServletContext();
108
109
110
111
112
113
114
115
116
117 if ( applicationContext != null )
118 {
119
120 stopTaskQueueExecutor( tqeDbScanning, servletContext );
121 stopTaskQueueExecutor( tqeRepoScanning, servletContext );
122 stopTaskQueueExecutor( tqeIndexing, servletContext );
123
124
125 if ( repositoryTaskScheduler != null )
126 {
127 try
128 {
129 repositoryTaskScheduler.stop();
130 }
131 catch ( SchedulerException e )
132 {
133 servletContext.log( e.getMessage(), e );
134 }
135
136 try
137 {
138
139 Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
140 schedulerField.setAccessible( true );
141
142 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
143 scheduler.stop();
144 }
145 catch ( Exception e )
146 {
147 servletContext.log( e.getMessage(), e );
148 }
149 }
150
151
152
153
154
155 }
156
157
158 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
159 {
160 try
161 {
162 indexingContext.close( false );
163 }
164 catch ( Exception e )
165 {
166 servletContext.log( "skip error closing indexingContext " + e.getMessage(), e );
167 }
168 }
169
170 }
171
172 private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor, ServletContext servletContext )
173 {
174 if ( taskQueueExecutor != null )
175 {
176 Task currentTask = taskQueueExecutor.getCurrentTask();
177 if ( currentTask != null )
178 {
179 taskQueueExecutor.cancelTask( currentTask );
180 }
181
182 try
183 {
184 taskQueueExecutor.stop();
185 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor, servletContext );
186 if ( service != null )
187 {
188 service.shutdown();
189 }
190 }
191 catch ( Exception e )
192 {
193 servletContext.log( e.getMessage(), e );
194 }
195 }
196 }
197
198 private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe, ServletContext servletContext )
199 {
200 ExecutorService service = null;
201 try
202 {
203 Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
204 executorServiceField.setAccessible( true );
205 service = (ExecutorService) executorServiceField.get( ttqe );
206 }
207 catch ( Exception e )
208 {
209 servletContext.log( e.getMessage(), e );
210 }
211 return service;
212 }
213 }