001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.server.core.api;
021
022
023import java.io.File;
024import java.io.IOException;
025
026import org.apache.directory.server.i18n.I18n;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030
031/**
032 * Convenience class to encapsulate paths to various directories and files within
033 * an layout.
034 * 
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public abstract class AbstractLayout
038{
039    /** The logger*/
040    private static final Logger LOG = LoggerFactory.getLogger( AbstractLayout.class );
041
042    /** The required directories */
043    private File[] requiredDirectories = new File[0];
044
045    /** The required files */
046    private File[] requiredFiles = new File[0];
047
048    /** The base directory */
049    private File directory;
050
051
052    /**
053     * Creates a new instance of AbstractLayout.
054     *
055     * @param directory the base directory
056     */
057    protected AbstractLayout( File directory )
058    {
059        this.directory = directory;
060    }
061
062
063    /**
064     * Creates a new instance of AbstractLayout.
065     *
066     * @param directoryPath the path to the base directory
067     */
068    protected AbstractLayout( String directoryPath )
069    {
070        this.directory = new File( directoryPath );
071    }
072
073
074    /**
075     * Gets the base directory.
076     *
077     * @return the base directory
078     */
079    protected File getDirectory()
080    {
081        return directory;
082    }
083
084
085    /**
086     * Gets the required directories.
087     *
088     * @return the required directories
089     */
090    public File[] getRequiredDirectories()
091    {
092        return requiredDirectories;
093    }
094
095
096    /**
097     * Gets the required files.
098     *
099     * @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}