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.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   * ArchivaStartup - the startup of all archiva features in a deterministic order.
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          // we log using servlet mechanism as due to some possible problem with slf4j when container shutdown
89          // so servletContext.log
90          ServletContext servletContext = contextEvent.getServletContext();
91  
92          // TODO check this stop
93  
94          /*
95          if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
96          {
97              ( (ClassPathXmlApplicationContext) applicationContext ).close();
98          } */
99  
100         if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
101         {
102             // stop task queue executors
103             stopTaskQueueExecutor( tqeDbScanning, servletContext );
104             stopTaskQueueExecutor( tqeRepoScanning, servletContext );
105             stopTaskQueueExecutor( tqeIndexing, servletContext );
106 
107             // stop the DefaultArchivaTaskScheduler and its scheduler
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                     // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
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             // close the application context
135             //applicationContext.close();
136             // TODO fix close call
137             //applicationContext.
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 }