This project has retired. For details please refer to its Attic page.
ArchivaStartup xref
View Javadoc
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.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   * ArchivaStartup - the startup of all archiva features in a deterministic order.
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         // we log using servlet mechanism as due to some possible problem with slf4j when container shutdown
106         // so servletContext.log
107         ServletContext servletContext = contextEvent.getServletContext();
108 
109         // TODO check this stop
110 
111         /*
112         if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
113         {
114             ( (ClassPathXmlApplicationContext) applicationContext ).close();
115         } */
116 
117         if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
118         {
119             // stop task queue executors
120             stopTaskQueueExecutor( tqeDbScanning, servletContext );
121             stopTaskQueueExecutor( tqeRepoScanning, servletContext );
122             stopTaskQueueExecutor( tqeIndexing, servletContext );
123 
124             // stop the DefaultArchivaTaskScheduler and its scheduler
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                     // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
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             // close the application context
152             //applicationContext.close();
153             // TODO fix close call
154             //applicationContext.
155         }
156 
157         // closing correctly indexer to close correctly lock and file
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 }