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  package org.apache.directory.server.core.api;
21  
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.directory.server.i18n.I18n;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * Convenience class to encapsulate paths to various directories and files within
33   * an layout.
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public abstract class AbstractLayout
38  {
39      /** The logger*/
40      private static final Logger LOG = LoggerFactory.getLogger( AbstractLayout.class );
41  
42      /** The required directories */
43      private File[] requiredDirectories = new File[0];
44  
45      /** The required files */
46      private File[] requiredFiles = new File[0];
47  
48      /** The base directory */
49      private File directory;
50  
51  
52      /**
53       * Creates a new instance of AbstractLayout.
54       *
55       * @param directory the base directory
56       */
57      protected AbstractLayout( File directory )
58      {
59          this.directory = directory;
60      }
61  
62  
63      /**
64       * Creates a new instance of AbstractLayout.
65       *
66       * @param directoryPath the path to the base directory
67       */
68      protected AbstractLayout( String directoryPath )
69      {
70          this.directory = new File( directoryPath );
71      }
72  
73  
74      /**
75       * Gets the base directory.
76       *
77       * @return the base directory
78       */
79      protected File getDirectory()
80      {
81          return directory;
82      }
83  
84  
85      /**
86       * Gets the required directories.
87       *
88       * @return the required directories
89       */
90      public File[] getRequiredDirectories()
91      {
92          return requiredDirectories;
93      }
94  
95  
96      /**
97       * Gets the required files.
98       *
99       * @return the required files
100      */
101     public File[] getRequiredFiles()
102     {
103         return requiredFiles;
104     }
105 
106 
107     /**
108      * Creates the required directories (if they don't already exist).
109      * 
110      * @throws IOException If the directory cannot be created 
111      */
112     public void mkdirs() throws IOException
113     {
114         for ( File requiredDirectory : requiredDirectories )
115         {
116             if ( !requiredDirectory.exists() && !requiredDirectory.mkdirs() )
117             {
118                 throw new IOException( I18n.err( I18n.ERR_112_COULD_NOT_CREATE_DIRECTORY, requiredDirectory ) );
119             }
120         }
121     }
122 
123 
124     /**
125      * Sets the required directories.
126      *
127      * @param requiredDirectories an array of required directories
128      */
129     protected void setRequiredDirectories( File[] requiredDirectories )
130     {
131         this.requiredDirectories = requiredDirectories;
132     }
133 
134 
135     /**
136      * Sets the required files.
137      *
138      * @param requiredFiles an array of required files
139      */
140     protected void setRequiredFiles( File[] requiredFiles )
141     {
142         this.requiredFiles = requiredFiles;
143     }
144 
145 
146     /**
147      * Verifies the installation by checking required directories and files.
148      */
149     public void verifyInstallation()
150     {
151         LOG.debug( "Verifying required directories" );
152 
153         // Verifying required directories
154         for ( File requiredDirectory : requiredDirectories )
155         {
156             // Exists?
157             if ( !requiredDirectory.exists() )
158             {
159                 String message = "The required '" + requiredDirectory + " directory does not exist!";
160                 LOG.error( message );
161                 throw new IllegalStateException( message );
162             }
163 
164             // Directory?
165             if ( requiredDirectory.isFile() )
166             {
167                 String message = "'" + requiredDirectory + "' is a file when it should be a directory.";
168                 LOG.error( message );
169                 throw new IllegalStateException( message );
170             }
171 
172             // Writable?
173             if ( !requiredDirectory.canWrite() )
174             {
175                 String message = "'" + requiredDirectory
176                     + "' is write protected from the current user '"
177                     + System.getProperty( "user.name" ) + "'";
178                 LOG.error( message );
179                 throw new IllegalStateException( message );
180             }
181         }
182 
183         LOG.debug( "Required directories verification finished successfully." );
184 
185         LOG.debug( "Verifying required files" );
186 
187         // Verifying required files
188         for ( File requiredFile : requiredFiles )
189         {
190             // Exists?
191             if ( !requiredFile.exists() )
192             {
193                 String message = "The required'" + requiredFile + "' file does not exist!";
194                 LOG.error( message );
195                 throw new IllegalStateException( message );
196             }
197 
198             // File?
199             if ( requiredFile.isDirectory() )
200             {
201                 String message = "'" + requiredFile + "' is a directory when it should be a file.";
202                 LOG.error( message );
203                 throw new IllegalStateException( message );
204             }
205 
206             // Writable?
207             if ( !requiredFile.canRead() )
208             {
209                 String message = "'" + requiredFile + "' is not readable by the current user '"
210                     + System.getProperty( "user.name" ) + "'.";
211                 LOG.error( message );
212                 throw new IllegalStateException( message );
213             }
214         }
215 
216         LOG.debug( "Required files verification finished successfully." );
217     }
218 }