View Javadoc
1   package org.apache.directory.server.core.api;
2   
3   
4   /*
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *  
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *  
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License. 
21   *  
22   */
23  
24  import java.io.File;
25  
26  
27  /**
28   * Convenience class to encapsulate paths to various directories and files within
29   * an instance.
30   * <br>
31   * The default layout is :
32   * <pre>
33   *  &lt;instance directory&gt;
34   *    |
35   *    +-- conf/
36   *    |    |
37   *    |    +-- config.ldif
38   *    |    |
39   *    |    +-- wrapper.conf
40   *    |    |
41   *    |    +-- log4j.properties
42   *    |    |
43   *    |    +-- &lt;keystore file&gt;
44   *    |
45   *    +-- partitions/
46   *    |    |
47   *    |    +-- system/
48   *    |    |    |
49   *    |    |    +-- master.db
50   *    |    |    |
51   *    |    |    +-- objectclass.db
52   *    |    |    |
53   *    |    |    +-- objectclass.lg
54   *    |    |    |
55   *    |    |    +-- &lt;index XXX lg and db files&gt;
56   *    |    |
57   *    |    +-- schema/
58   *    |    |    |
59   *    |    |    :
60   *    |    |
61   *    |    +-- &lt;partition XXX&gt;/
62   *    |    |    |
63   *    |    :    :
64   *    |
65   *    +-- log/
66   *    |    |
67   *    |   [+-- journal.ldif]
68   *    |    |
69   *    |    +-- &lt;log file&gt;
70   *    |
71   *    +-- run/
72   *    |
73   *    +-- cache/
74   *    |
75   *    +-- syncrepl-data/
76   * </pre>
77   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
78   */
79  public class InstanceLayout extends AbstractLayout
80  {
81      // Static final fields for system property names
82      private static final String LOG_DIR = "apacheds.log.dir";
83      private static final String RUN_DIR = "apacheds.run.dir";
84  
85      /** Static directory names */
86      public static final String LOG_NAME = "log";
87      public static final String RUN_NAME = "run";
88      public static final String CONF_NAME = "conf";
89      public static final String PARTITIONS_NAME = "partitions";
90      private static final String REPL_NAME = "syncrepl-data";
91      private static final String CACHE_NAME = "cache";
92  
93      /** Static file names */
94      private static final String LOG4J_PROPERTIES = "log4j.properties";
95      private static final String WRAPPER_CONF = "wrapper.conf";
96      private static final String CONFIG_LDIF = "config.ldif";
97      private static final String KEYSTORE_FILE = "apacheds.ks";
98  
99      /** The Log directory */
100     private File logDir;
101 
102     /** The Partitions directory */
103     private File partitionsDir;
104 
105     /** The Run directory */
106     private File runDir;
107 
108     /** The Conf directory */
109     private File confDir;
110 
111     /** The replication data directory */
112     private File replDir;
113 
114     /** The cache directory */
115     private File cacheDir;
116 
117 
118     /**
119      * Creates a new instance of InstanceLayout.
120      *
121      * @param instanceDirectory the instance directory
122      */
123     public InstanceLayout( File instanceDirectory )
124     {
125         super( instanceDirectory );
126         init();
127     }
128 
129 
130     /**
131      * Creates a new instance of InstanceLayout.
132      *
133      * @param instanceDirectoryPath the path to the instance directory
134      */
135     public InstanceLayout( String instanceDirectoryPath )
136     {
137         super( instanceDirectoryPath );
138         init();
139     }
140 
141 
142     /**
143      * Initializes the InstanceLayout.
144      */
145     private void init()
146     {
147         // The required directories
148         File[] requiredDirectories = new File[]
149             {
150                 getInstanceDirectory(),
151                 getConfDirectory(),
152                 getLogDirectory(),
153                 getPartitionsDirectory(),
154                 getRunDirectory(),
155                 getCacheDirectory()
156         };
157 
158         setRequiredDirectories( requiredDirectories );
159 
160         // The required files
161         File[] requiredFiles = new File[]
162             {
163                 getWrapperConfigurationFile(),
164                 getLogConfigurationFile() /*,
165                                           getApacheDsConfigurationLdifFile() */// TODO re-activate this when possible.
166         };
167 
168         setRequiredFiles( requiredFiles );
169     }
170 
171 
172     /**
173      * Gets the 'conf' directory ('&lt;instance&gt;/conf').
174      *
175      * @return the 'conf' directory
176      */
177     public File getConfDirectory()
178     {
179         if ( confDir == null )
180         {
181             confDir = new File( getInstanceDirectory(), CONF_NAME );
182         }
183 
184         return confDir;
185     }
186 
187 
188     /**
189      * @param confDir the confDir to set
190      */
191     public void setConfDir( File confDir )
192     {
193         this.confDir = confDir;
194     }
195 
196 
197     /**
198      * Gets the 'cache' directory ('&lt;instance&gt;/cache').
199      *
200      * @return the 'cache' directory
201      */
202     public File getCacheDirectory()
203     {
204         if ( cacheDir == null )
205         {
206             cacheDir = new File( getInstanceDirectory(), CACHE_NAME );
207         }
208 
209         return cacheDir;
210     }
211 
212 
213     /**
214      * @param cacheDir the confDir to set
215      */
216     public void setCacheDir( File cacheDir )
217     {
218         this.cacheDir = cacheDir;
219     }
220 
221 
222     /**
223      * Gets the 'log' directory ('&lt;instance&gt;/log').
224      *
225      * @return the 'log' directory
226      */
227     public File getLogDirectory()
228     {
229         if ( logDir == null )
230         {
231             String systemLogDir = System.getProperty( LOG_DIR );
232 
233             if ( systemLogDir != null )
234             {
235                 logDir = new File( systemLogDir );
236             }
237             else
238             {
239                 logDir = new File( getInstanceDirectory(), LOG_NAME );
240             }
241         }
242 
243         return logDir;
244     }
245 
246 
247     /**
248      * @param logDir the logDir to set
249      */
250     public void setLogDir( File logDir )
251     {
252         this.logDir = logDir;
253     }
254 
255 
256     /**
257      * Gets the 'partitions' directory ('&lt;instance&gt;/partitions')
258      *
259      * @return the 'partitions' directory
260      */
261     public File getPartitionsDirectory()
262     {
263         if ( partitionsDir == null )
264         {
265             partitionsDir = new File( getInstanceDirectory(), PARTITIONS_NAME );
266         }
267 
268         return partitionsDir;
269     }
270 
271 
272     /**
273      * @param partitionsDir the partitionsDir to set
274      */
275     public void setPartitionsDir( File partitionsDir )
276     {
277         this.partitionsDir = partitionsDir;
278     }
279 
280 
281     /**
282      * Gets the 'run' directory in the installation directory ('&lt;instance&gt;/run').
283      *
284      * @return the 'run' directory
285      */
286     public File getRunDirectory()
287     {
288         if ( runDir == null )
289         {
290             String systemRunDir = System.getProperty( RUN_DIR );
291 
292             if ( systemRunDir != null )
293             {
294                 runDir = new File( systemRunDir );
295             }
296             else
297             {
298                 runDir = new File( getInstanceDirectory(), RUN_NAME );
299             }
300         }
301 
302         return runDir;
303     }
304 
305 
306     /**
307      * @param runDir the runDir to set
308      */
309     public void setRunDir( File runDir )
310     {
311         this.runDir = runDir;
312     }
313 
314 
315     /**
316      * Gets the instance directory.
317      *
318      * @return the instance directory
319      */
320     public File getInstanceDirectory()
321     {
322         return getDirectory();
323     }
324 
325 
326     /**
327      * Gets the log configuration file (<em>'&lt;instance&gt;/conf/log4j.properties'</em>).
328      *
329      * @return the log configuration file
330      */
331     public File getLogConfigurationFile()
332     {
333         return new File( getConfDirectory(), LOG4J_PROPERTIES );
334     }
335 
336 
337     /**
338      * Gets the wrapper configuration file (<em>'&lt;instance&gt;/conf/wrapper.conf'</em>).
339      *
340      * @return the wrapper configuration file
341      */
342     public File getWrapperConfigurationFile()
343     {
344         return new File( getConfDirectory(), WRAPPER_CONF );
345     }
346 
347 
348     /**
349      * Gets the apacheds configuration ldif file (<em>'&lt;instance&gt;/conf/config.ldif'</em>).
350      *
351      * @return the apacheds configuration ldif file
352      */
353     public File getApacheDsConfigurationLdifFile()
354     {
355         return new File( getConfDirectory(), CONFIG_LDIF );
356     }
357 
358 
359     /**
360      * Gets the apacheds KeyStore file (<em>'&lt;instance&gt;/conf/apacheds.ks'</em>).
361      *
362      * @return the apacheds KeyWStore file
363      */
364     public File getKeyStoreFile()
365     {
366         return new File( getConfDirectory(), KEYSTORE_FILE );
367     }
368 
369 
370     /**
371      * Gets the 'replication' directory where replication journals are stored
372      * (<em>'&lt;instance&gt;/syncrepl-data'</em>).
373      *
374      * @return the 'replication' directory
375      */
376     public File getReplDirectory()
377     {
378         if ( replDir == null )
379         {
380             replDir = new File( getInstanceDirectory(), REPL_NAME );
381         }
382 
383         return replDir;
384     }
385 
386 
387     /**
388      * Sets the directory where the replication data are stored
389      * 
390      * @param replDir the replication journal data directory
391      */
392     public void setReplDirectory( File replDir )
393     {
394         this.replDir = replDir;
395     }
396 
397 
398     /**
399      * @see String#toString()
400      */
401     public String toString()
402     {
403         return "Instance Layout: \n"
404             + "  Instance dir                  : " + getInstanceDirectory() + "\n"
405             + "  Instance conf dir             : " + getConfDirectory() + "\n"
406             + "  Instance log dir              : " + getLogDirectory() + "\n"
407             + "  Instance run dir              : " + getRunDirectory() + "\n"
408             + "  Instance partitions dir       : " + getPartitionsDirectory() + "\n"
409             + "  Instance replication data dir : " + getReplDirectory() + "\n"
410             + "  Instance cache dir            : " + getCacheDirectory() + "\n";
411     }
412 }