]> source.dussan.org Git - archiva.git/blob
2863daa05dd4f497fb6fc911388bd34c22e1923d
[archiva.git] /
1 package org.apache.archiva.web.startup;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.common.ArchivaException;
23 import org.apache.archiva.redback.components.scheduler.DefaultScheduler;
24 import org.apache.archiva.redback.components.taskqueue.Task;
25 import org.apache.archiva.redback.components.taskqueue.execution.ThreadedTaskQueueExecutor;
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.quartz.SchedulerException;
30 import org.springframework.web.context.WebApplicationContext;
31 import org.springframework.web.context.support.WebApplicationContextUtils;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.ServletContextEvent;
35 import javax.servlet.ServletContextListener;
36 import java.lang.reflect.Field;
37 import java.util.Properties;
38 import java.util.concurrent.ExecutorService;
39
40 /**
41  * ArchivaStartup - the startup of all archiva features in a deterministic order.
42  */
43 public class ArchivaStartup
44     implements ServletContextListener
45 {
46     private ThreadedTaskQueueExecutor tqeDbScanning;
47
48     private ThreadedTaskQueueExecutor tqeRepoScanning;
49
50     private ThreadedTaskQueueExecutor tqeIndexing;
51
52     private DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
53
54     private NexusIndexer nexusIndexer;
55
56     @Override
57     public void contextInitialized( ServletContextEvent contextEvent )
58     {
59         WebApplicationContext wac =
60             WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
61
62         SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
63
64         repositoryTaskScheduler =
65             wac.getBean( "archivaTaskScheduler#repository", DefaultRepositoryArchivaTaskScheduler.class );
66
67         Properties archivaRuntimeProperties = wac.getBean( "archivaRuntimeProperties", Properties.class );
68
69         tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
70
71         tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
72
73         nexusIndexer = wac.getBean( NexusIndexer.class );
74
75         try
76         {
77             securitySync.startup();
78             repositoryTaskScheduler.startup();
79             Banner.display( (String) archivaRuntimeProperties.get( "archiva.version" ) );
80         }
81         catch ( ArchivaException e )
82         {
83             throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
84         }
85     }
86
87     @Override
88     public void contextDestroyed( ServletContextEvent contextEvent )
89     {
90         WebApplicationContext applicationContext =
91             WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
92
93         // we log using servlet mechanism as due to some possible problem with slf4j when container shutdown
94         // so servletContext.log
95         ServletContext servletContext = contextEvent.getServletContext();
96
97         // TODO check this stop
98
99         /*
100         if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
101         {
102             ( (ClassPathXmlApplicationContext) applicationContext ).close();
103         } */
104
105         if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
106         {
107             // stop task queue executors
108             stopTaskQueueExecutor( tqeDbScanning, servletContext );
109             stopTaskQueueExecutor( tqeRepoScanning, servletContext );
110             stopTaskQueueExecutor( tqeIndexing, servletContext );
111
112             // stop the DefaultArchivaTaskScheduler and its scheduler
113             if ( repositoryTaskScheduler != null )
114             {
115                 try
116                 {
117                     repositoryTaskScheduler.stop();
118                 }
119                 catch ( SchedulerException e )
120                 {
121                     servletContext.log( e.getMessage(), e );
122                 }
123
124                 try
125                 {
126                     // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
127                     Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
128                     schedulerField.setAccessible( true );
129
130                     DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
131                     scheduler.stop();
132                 }
133                 catch ( Exception e )
134                 {
135                     servletContext.log( e.getMessage(), e );
136                 }
137             }
138
139             // close the application context
140             //applicationContext.close();
141             // TODO fix close call
142             //applicationContext.
143         }
144
145         // closing correctly indexer to close correctly lock and file
146         for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
147         {
148             try
149             {
150                 indexingContext.close( false );
151             }
152             catch ( Exception e )
153             {
154                 servletContext.log( "skip error closing indexingContext " + e.getMessage(), e );
155             }
156         }
157
158     }
159
160     private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor, ServletContext servletContext )
161     {
162         if ( taskQueueExecutor != null )
163         {
164             Task currentTask = taskQueueExecutor.getCurrentTask();
165             if ( currentTask != null )
166             {
167                 taskQueueExecutor.cancelTask( currentTask );
168             }
169
170             try
171             {
172                 taskQueueExecutor.stop();
173                 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor, servletContext );
174                 if ( service != null )
175                 {
176                     service.shutdown();
177                 }
178             }
179             catch ( Exception e )
180             {
181                 servletContext.log( e.getMessage(), e );
182             }
183         }
184     }
185
186     private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe, ServletContext servletContext )
187     {
188         ExecutorService service = null;
189         try
190         {
191             Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
192             executorServiceField.setAccessible( true );
193             service = (ExecutorService) executorServiceField.get( ttqe );
194         }
195         catch ( Exception e )
196         {
197             servletContext.log( e.getMessage(), e );
198         }
199         return service;
200     }
201 }