1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
37
38 public final class ApacheDsTanukiWrapper implements WrapperListener
39 {
40
41 private static final Logger LOG = LoggerFactory.getLogger( ApacheDsTanukiWrapper.class );
42
43
44 private ApacheDsService service;
45
46
47
48
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
63
64
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
72 service = new ApacheDsService();
73
74
75 try
76 {
77 System.out.println( "Starting the service." );
78
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
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
104 stop( 0 );
105 }
106
107
108
109
110
111
112
113
114
115
116
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
136 String action = "START";
137 String instanceDirectory = args[0];
138
139 switch ( args.length )
140 {
141 case 2 :
142 action = args[1];
143
144
145 case 1 :
146
147 service = new ApacheDsService();
148
149
150 InstanceLayoutanceLayout.html#InstanceLayout">InstanceLayout instanceLayout = new InstanceLayout( instanceDirectory );
151
152
153 switch ( Strings.toLowerCaseAscii( action ) )
154 {
155 case "stop" :
156
157 LOG.debug( "Stopping runtime" );
158 stop( 1 );
159
160 break;
161
162 case "repair" :
163
164 LOG.debug( "Fixing the database runtime" );
165 repair( instanceDirectory );
166
167 break;
168
169 default :
170
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
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
224
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 }