View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  
21  package org.apache.directory.server.wrapper;
22  
23  
24  import org.apache.directory.api.util.Strings;
25  import org.apache.directory.server.ApacheDsService;
26  import org.apache.directory.server.core.api.InstanceLayout;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.tanukisoftware.wrapper.WrapperListener;
30  import org.tanukisoftware.wrapper.WrapperManager;
31  
32  
33  /**
34   * A Tanuki Wrapper implementation for the ApacheDS service.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public final class ApacheDsTanukiWrapper implements WrapperListener
39  {
40      /** The logger */
41      private static final Logger LOG = LoggerFactory.getLogger( ApacheDsTanukiWrapper.class );
42  
43      /** The ApacheDS service*/
44      private ApacheDsService service;
45  
46  
47      /**
48       * Creates a new instance of ApacheDsTanukiWrapper.
49       */
50      private ApacheDsTanukiWrapper()
51      {
52      }
53  
54  
55      public static void main( String[] args )
56      {
57          WrapperManager.start( new ApacheDsTanukiWrapper(), args );
58      }
59  
60      
61      /**
62       * Try to repair the databases
63       *
64       * @param instanceDirectory The directory containing the server instance 
65       */
66      public void repair( String instanceDirectory )
67      {
68          System.out.println( "Trying to repair the following data :" + instanceDirectory );
69          InstanceLayoutapi/InstanceLayout.html#InstanceLayout">InstanceLayout layout = new InstanceLayout( instanceDirectory );
70          
71          // Creating ApacheDS service
72          service = new ApacheDsService();
73          
74          // Initializing the service
75          try
76          {
77              System.out.println( "Starting the service." );
78              // must start servers otherwise stop() won't work
79              service.start( layout, true );
80              System.out.println( "Service started." );
81          }
82          catch ( Exception e )
83          {
84              LOG.error( "Failed to start the service.", e );
85              stop( 1 );
86              System.exit( ExitCodes.START );
87          }
88  
89          // Repairing the database
90          try
91          {
92              System.out.println( "Repairing the database." );
93              service.repair( layout );
94              System.out.println( "Database repaired." );
95          }
96          catch ( Exception e )
97          {
98              LOG.error( "Failed to repair the database.", e );
99              stop( 1 );
100             System.exit( ExitCodes.START );
101         }
102         
103         // Stop the service
104         stop( 0 );
105     }
106 
107 
108     /**
109      * Implemented the start() method from the WrapperListener class.
110      * 
111      * The possible arguments are the instance layout directory and the command, one of :
112      * <ul>
113      *   <li>START (default) : starts the server</li>
114      *   <li>STOP : stops the server</li>
115      *   <li>REPAIR : repairs the index</li>
116      * </ul>
117      */
118     public Integer start( String[] args )
119     {
120         LOG.info( "Starting the service..." );
121 
122         if ( args != null )
123         {
124             int argNb = 0;
125             
126             for ( String arg : args )
127             {
128                 LOG.info( "Args[{}] : {}", argNb, arg );
129                 argNb++;
130             }
131         }
132 
133         if ( args != null )
134         {
135             // the default action
136             String action = "START";
137             String instanceDirectory = args[0];
138 
139             switch ( args.length )
140             {
141                 case 2 :
142                     action = args[1];
143                     /* Passthrough...*/
144                     
145                 case 1 :
146                     // Creating ApacheDS service
147                     service = new ApacheDsService();
148 
149                     // Creating instance layouts from the argument
150                     InstanceLayoutanceLayout.html#InstanceLayout">InstanceLayout instanceLayout = new InstanceLayout( instanceDirectory );
151                     
152                     // Process the action
153                     switch ( Strings.toLowerCaseAscii( action ) )
154                     {
155                         case "stop" :
156                             // Stops the server
157                             LOG.debug( "Stopping runtime" );
158                             stop( 1 );
159                             
160                             break;
161 
162                         case "repair" :
163                             // Try to fix the JDBM database
164                             LOG.debug( "Fixing the database runtime" );
165                             repair( instanceDirectory );
166                             
167                             break;
168                             
169                         default :
170                             // Starts the server
171                             LOG.debug( "Starting runtime" );
172 
173                             try
174                             {
175                                 service.start( instanceLayout );
176                             }
177                             catch ( Exception e )
178                             {
179                                 LOG.error( "Failed to start the service.", e );
180                                 System.exit( ExitCodes.START );
181                             }
182                             
183                             break;
184                     }
185                     
186                     break;
187                     
188                 default :
189                     throw new IllegalArgumentException(
190                         "Program must be launched with at least 1 argument, the path to the instance directory." );
191             }
192         }
193 
194         return null;
195     }
196 
197 
198     public int stop( int exitCode )
199     {
200         LOG.info( "Attempting graceful shutdown of the service..." );
201 
202         // Stopping the service
203         try
204         {
205             service.stop();
206         }
207         catch ( Exception e )
208         {
209             LOG.error( "Failed to stop the service.", e );
210             System.exit( ExitCodes.STOP );
211         }
212 
213         LOG.info( "Completed graceful shutdown of the service..." );
214 
215         return exitCode;
216     }
217 
218 
219     public void controlEvent( int event )
220     {
221         if ( !WrapperManager.isControlledByNativeWrapper() )
222         {
223             // We are not being controlled by the Wrapper, so
224             // handle the event ourselves.
225             if ( ( event == WrapperManager.WRAPPER_CTRL_C_EVENT )
226                 || ( event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT )
227                 || ( event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT ) )
228             {
229                 WrapperManager.stop( 0 );
230             }
231         }
232     }
233 }