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     */
020    package org.apache.directory.studio.apacheds;
021    
022    
023    import java.io.File;
024    import java.io.FileInputStream;
025    import java.io.FileOutputStream;
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.io.OutputStream;
029    import java.net.URL;
030    
031    import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
032    import org.apache.directory.studio.apacheds.configuration.model.ServerXmlIOException;
033    import org.apache.directory.studio.apacheds.configuration.model.v153.ServerXmlIOV153;
034    import org.apache.directory.studio.apacheds.configuration.model.v154.ServerXmlIOV154;
035    import org.apache.directory.studio.apacheds.configuration.model.v155.ServerXmlIOV155;
036    import org.apache.directory.studio.apacheds.model.Server;
037    import org.eclipse.core.runtime.FileLocator;
038    import org.eclipse.core.runtime.IPath;
039    import org.eclipse.core.runtime.Path;
040    import org.eclipse.jface.dialogs.IDialogConstants;
041    import org.eclipse.jface.dialogs.MessageDialog;
042    
043    
044    /**
045     * This class contains helpful methods.
046     *
047     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048     * @version $Rev$, $Date$
049     */
050    public class ApacheDsPluginUtils
051    {
052        private static final String RESOURCES = "resources";
053        private static final String LIBS = "libs";
054        private static final String SERVERS = "servers";
055        /** The name of the libraries folder */
056        private static final String LIBRARIES_FOLDER_NAME = "libs";
057        private static final String APACHEDS = "apacheds";
058    
059        /** The line separator */
060        public static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
061    
062    
063        /**
064         * Verifies that the libraries folder exists and contains the jar files 
065         * needed to launch the server.
066         * 
067         * @param server
068         *      the server
069         */
070        public static void verifyLibrariesFolder( Server server )
071        {
072            IPath stateLocationPath = ApacheDsPlugin.getDefault().getStateLocation();
073    
074            // Libraries folder
075            IPath librariesFolderPath = stateLocationPath.append( LIBRARIES_FOLDER_NAME );
076            File librariesFolder = new File( librariesFolderPath.toOSString() );
077            if ( !librariesFolder.exists() )
078            {
079                librariesFolder.mkdir();
080            }
081    
082            // Specific Apache DS folder
083            IPath apacheDsFolderPath = librariesFolderPath.append( APACHEDS + "-" + server.getVersion() );
084            File apacheDsFolder = new File( apacheDsFolderPath.toOSString() );
085            if ( !apacheDsFolder.exists() )
086            {
087                apacheDsFolder.mkdir();
088            }
089    
090            // Jar libraries
091            for ( String apachedsLibraryFilename : getApacheDsLibraries( server ) )
092            {
093                IPath apachedsLibraryPath = apacheDsFolderPath.append( apachedsLibraryFilename );
094                File apachedsLibrary = new File( apachedsLibraryPath.toOSString() );
095                if ( !apachedsLibrary.exists() )
096                {
097                    try
098                    {
099                        copyLibrary( apachedsLibraryFilename, apachedsLibrary, server );
100                    }
101                    catch ( IOException e )
102                    {
103                        ApacheDsPluginUtils.reportError( "An error occurred when copying the library '"
104                            + apachedsLibraryFilename + "' to the location '" + apachedsLibrary.getAbsolutePath()
105                            + "'.\n\n" + e.getMessage() );
106                    }
107                }
108            }
109        }
110    
111    
112        /**
113         * Gets an array containing the names of the Apache DS libraries, according
114         * to the given server.
115         *
116         * @param server
117         *      the server
118         * @return
119         *      an array containing the names of the Apache DS libraries, according
120         * to the given server
121         */
122        public static String[] getApacheDsLibraries( Server server )
123        {
124            if ( server != null )
125            {
126                switch ( server.getVersion() )
127                {
128                    case VERSION_1_5_5:
129                        return new String[]
130                            { "antlr-2.7.7.jar", "apacheds-bootstrap-extract-1.5.5.jar",
131                                "apacheds-bootstrap-partition-1.5.5.jar", "apacheds-core-1.5.5.jar",
132                                "apacheds-core-avl-1.5.5.jar", "apacheds-core-constants-1.5.5.jar",
133                                "apacheds-core-entry-1.5.5.jar", "apacheds-core-jndi-1.5.5.jar",
134                                "apacheds-core-shared-1.5.5.jar", "apacheds-interceptor-kerberos-1.5.5.jar",
135                                "apacheds-jdbm-1.5.5.jar", "apacheds-jdbm-store-1.5.5.jar",
136                                "apacheds-kerberos-shared-1.5.5.jar", "apacheds-launcher-1.5.0.jar",
137                                "apacheds-protocol-changepw-1.5.5.jar", "apacheds-protocol-dns-1.5.5.jar",
138                                "apacheds-protocol-kerberos-1.5.5.jar", "apacheds-protocol-ldap-1.5.5.jar",
139                                "apacheds-protocol-ntp-1.5.5.jar", "apacheds-protocol-shared-1.5.5.jar",
140                                "apacheds-schema-bootstrap-1.5.5.jar", "apacheds-schema-extras-1.5.5.jar",
141                                "apacheds-schema-registries-1.5.5.jar", "apacheds-server-jndi-1.5.5.jar",
142                                "apacheds-server-xml-1.5.5.jar", "apacheds-utils-1.5.5.jar",
143                                "apacheds-xbean-spring-1.5.5.jar", "apacheds-xdbm-base-1.5.5.jar",
144                                "apacheds-xdbm-search-1.5.5.jar", "apacheds-xdbm-tools-1.5.5.jar", "bcprov-jdk15-140.jar",
145                                "commons-cli-1.2.jar", "commons-collections-3.2.1.jar", "commons-daemon-1.0.1.jar",
146                                "commons-io-1.4.jar", "commons-lang-2.4.jar", "daemon-bootstrappers-1.1.6.jar",
147                                "jcl-over-slf4j-1.5.6.jar", "log4j-1.2.14.jar", "mina-core-2.0.0-M6.jar",
148                                "shared-asn1-0.9.15.jar", "shared-asn1-codec-0.9.15.jar", "shared-cursor-0.9.15.jar",
149                                "shared-ldap-0.9.15.jar", "shared-ldap-constants-0.9.15.jar", "slf4j-api-1.5.6.jar",
150                                "slf4j-log4j12-1.5.6.jar", "spring-beans-2.5.6.SEC01.jar",
151                                "spring-context-2.5.6.SEC01.jar", "spring-core-2.5.6.SEC01.jar", "xbean-spring-3.5.jar" };
152                    case VERSION_1_5_4:
153                        return new String[]
154                            { "antlr-2.7.7.jar", "xbean-spring-3.3.jar", "apacheds-bootstrap-extract-1.5.4.jar",
155                                "apacheds-bootstrap-partition-1.5.4.jar", "apacheds-core-1.5.4.jar",
156                                "apacheds-core-avl-1.5.4.jar", "apacheds-core-constants-1.5.4.jar",
157                                "apacheds-core-cursor-1.5.4.jar", "apacheds-core-entry-1.5.4.jar",
158                                "apacheds-core-shared-1.5.4.jar", "apacheds-jdbm-1.5.4.jar",
159                                "apacheds-jdbm-store-1.5.4.jar", "apacheds-kerberos-shared-1.5.4.jar",
160                                "apacheds-launcher-1.2.0.jar", "apacheds-protocol-changepw-1.5.4.jar",
161                                "apacheds-protocol-dns-1.5.4.jar", "apacheds-protocol-kerberos-1.5.4.jar",
162                                "apacheds-protocol-ldap-1.5.4.jar", "apacheds-protocol-ntp-1.5.4.jar",
163                                "apacheds-protocol-shared-1.5.4.jar", "apacheds-schema-bootstrap-1.5.4.jar",
164                                "apacheds-schema-extras-1.5.4.jar", "apacheds-schema-registries-1.5.4.jar",
165                                "apacheds-server-jndi-1.5.4.jar", "apacheds-server-xml-1.5.4.jar",
166                                "apacheds-utils-1.5.4.jar", "apacheds-xbean-spring-1.5.4.jar",
167                                "apacheds-xdbm-base-1.5.4.jar", "apacheds-xdbm-search-1.5.4.jar",
168                                "apacheds-xdbm-tools-1.5.4.jar", "commons-cli-1.1.jar", "commons-collections-3.2.jar",
169                                "commons-daemon-1.0.1.jar", "commons-lang-2.3.jar", "daemon-bootstrappers-1.1.4.jar",
170                                "jcl104-over-slf4j-1.4.3.jar", "log4j-1.2.14.jar", "mina-core-1.1.6.jar",
171                                "mina-filter-ssl-1.1.6.jar", "shared-asn1-0.9.12.jar", "shared-asn1-codec-0.9.12.jar",
172                                "shared-bouncycastle-reduced-0.9.12.jar", "shared-ldap-0.9.12.jar",
173                                "shared-ldap-constants-0.9.12.jar", "slf4j-api-1.4.3.jar", "slf4j-log4j12-1.4.3.jar",
174                                "spring-beans-2.0.6.jar", "spring-context-2.0.6.jar", "spring-core-2.0.6.jar" };
175                    case VERSION_1_5_3:
176                        return new String[]
177                            { "antlr-2.7.7.jar", "apacheds-bootstrap-extract-1.5.3.jar",
178                                "apacheds-bootstrap-partition-1.5.3.jar", "apacheds-btree-base-1.5.3.jar",
179                                "apacheds-core-1.5.3.jar", "apacheds-core-constants-1.5.3.jar",
180                                "apacheds-core-entry-1.5.3.jar", "apacheds-core-shared-1.5.3.jar",
181                                "apacheds-jdbm-1.5.3.jar", "apacheds-jdbm-store-1.5.3.jar",
182                                "apacheds-kerberos-shared-1.5.3.jar", "apacheds-launcher-1.2.0.jar",
183                                "apacheds-protocol-changepw-1.5.3.jar", "apacheds-protocol-dns-1.5.3.jar",
184                                "apacheds-protocol-kerberos-1.5.3.jar", "apacheds-protocol-ldap-1.5.3.jar",
185                                "apacheds-protocol-ntp-1.5.3.jar", "apacheds-protocol-shared-1.5.3.jar",
186                                "apacheds-schema-bootstrap-1.5.3.jar", "apacheds-schema-extras-1.5.3.jar",
187                                "apacheds-schema-registries-1.5.3.jar", "apacheds-server-jndi-1.5.3.jar",
188                                "apacheds-server-xml-1.5.3.jar", "apacheds-utils-1.5.3.jar",
189                                "apacheds-xbean-spring-1.5.3.jar", "commons-cli-1.1.jar", "commons-collections-3.2.jar",
190                                "commons-daemon-1.0.1.jar", "commons-lang-2.3.jar", "daemon-bootstrappers-1.1.3.jar",
191                                "jcl104-over-slf4j-1.4.3.jar", "log4j-1.2.14.jar", "mina-core-1.1.6.jar",
192                                "mina-filter-ssl-1.1.6.jar", "shared-asn1-0.9.11.jar", "shared-asn1-codec-0.9.11.jar",
193                                "shared-bouncycastle-reduced-0.9.11.jar", "shared-ldap-0.9.11.jar",
194                                "shared-ldap-constants-0.9.11.jar", "slf4j-api-1.4.3.jar", "slf4j-log4j12-1.4.3.jar",
195                                "spring-beans-2.0.6.jar", "spring-context-2.0.6.jar", "spring-core-2.0.6.jar",
196                                "xbean-spring-3.3.jar" };
197                };
198            }
199    
200            return new String[0];
201        }
202    
203    
204        /**
205         * Copy the given library.
206         *
207         * @param library
208         *      the name of the library
209         * @param destination
210         *      the destination
211         * @param server
212         *      the server
213         * @throws IOException
214         *      if an error occurrs when copying the jar file
215         */
216        private static void copyLibrary( String library, File destination, Server server ) throws IOException
217        {
218            // Getting he URL of the library within the bundle
219            URL libraryUrl = FileLocator.find( ApacheDsPlugin.getDefault().getBundle(), new Path( RESOURCES
220                + IPath.SEPARATOR + LIBS + IPath.SEPARATOR + APACHEDS + "-" + server.getVersion().toString()
221                + IPath.SEPARATOR + library ), null );
222    
223            // Creating the input and output streams
224            InputStream libraryInputStream = libraryUrl.openStream();
225            FileOutputStream libraryOutputStream = new FileOutputStream( destination );
226    
227            // Copying the library
228            copyFile( libraryInputStream, libraryOutputStream );
229    
230            // Closing the streams
231            libraryInputStream.close();
232            libraryOutputStream.close();
233        }
234    
235    
236        /**
237         * Copies a file from the given streams.
238         *
239         * @param inputStream
240         *      the input stream
241         * @param outputStream
242         *      the output stream
243         * @throws IOException
244         *      if an error occurs when copying the file
245         */
246        private static void copyFile( InputStream inputStream, OutputStream outputStream ) throws IOException
247        {
248            byte[] buf = new byte[1024];
249            int i = 0;
250            while ( ( i = inputStream.read( buf ) ) != -1 )
251            {
252                outputStream.write( buf, 0, i );
253            }
254        }
255    
256    
257        /**
258         * Get the path to the Apache DS libraries folder.
259         *
260         * @param server
261         *      the server
262         * @return
263         *      the path to the Apache DS libraries folder
264         */
265        public static IPath getApacheDsLibrariesFolder( Server server )
266        {
267            return ApacheDsPlugin.getDefault().getStateLocation().append( LIBRARIES_FOLDER_NAME ).append(
268                APACHEDS + "-" + server.getVersion() );
269        }
270    
271    
272        /**
273         * Get the path to the Apache DS servers folder.
274         *
275         * @return
276         *      the path to the Apache DS server folder
277         */
278        public static IPath getApacheDsServersFolder()
279        {
280            return ApacheDsPlugin.getDefault().getStateLocation().append( SERVERS );
281        }
282    
283    
284        /**
285         * Creates a new server folder for the given id.
286         *
287         * @param id
288         *      the id of the server
289         */
290        public static void createNewServerFolder( String id )
291        {
292            // Checking if the Apache DS servers folder exists
293            checkApacheDsServersFolder();
294    
295            // Creating the server folder
296            IPath serverFolderPath = getApacheDsServersFolder().append( id );
297            File serverFolder = new File( serverFolderPath.toOSString() );
298            serverFolder.mkdir();
299    
300            // Creating the server sub folders
301            File confFolder = new File( serverFolder, "conf" );
302            confFolder.mkdir();
303            File serverSocketFolder = new File( serverFolder, "serverSocket" );
304            serverSocketFolder.mkdir();
305            new File( serverFolder, "ldif" ).mkdir();
306            new File( serverFolder, "log" ).mkdir();
307            new File( serverFolder, "partitions" ).mkdir();
308    
309            // Copying default configuration files
310            try
311            {
312                // Creating log4j.properties file
313                File log4jPropertiesFile = new File( confFolder, "log4j.properties" );
314                createServersLog4jPropertiesFile( new FileOutputStream( log4jPropertiesFile ), 1024, getServerLogsLevel(), // Setting 1024 as default port
315                    getServerLogsPattern() );
316    
317                // Copying server.xml file
318                File serverXmlFile = new File( confFolder, "server.xml" );
319                copyConfigurationFile( "server-1.5.5.xml", serverXmlFile );
320    
321                // Creating log4j.properties file to the serverSocket folder
322                File log4jPropertiesServerSocketFile = new File( serverSocketFolder, "log4j.properties" );
323                createServerSocketLog4jPropertiesFile( new FileOutputStream( log4jPropertiesServerSocketFile ), id );
324            }
325            catch ( IOException e )
326            {
327                ApacheDsPluginUtils
328                    .reportError( "An error occurred when copying the default configuration files to the server's folder '"
329                        + serverFolder.getAbsolutePath() + "'.\n\n" + e.getMessage() );
330            }
331        }
332    
333    
334        /**
335         * Create the log4j.properties file for the server.
336         *
337         * @param os
338         *      the {@link OutputStream} to write to
339         * @param port
340         *      the port
341         * @param logsLevel
342         *      the logs level
343         * @param logsLevel
344         *      the logs pattern
345         * @throws IOException
346         *      if an error occurs when writing to the file
347         */
348        public static void createServersLog4jPropertiesFile( OutputStream os, int port, String logsLevel, String logsPattern )
349            throws IOException
350        {
351            // Creating the file content in a StringBuffer
352            StringBuffer sb = new StringBuffer();
353            sb.append( "#############################################################################" ).append( "\n" );
354            sb.append( "#    Licensed to the Apache Software Foundation (ASF) under one or more" ).append( "\n" );
355            sb.append( "#    contributor license agreements.  See the NOTICE file distributed with" ).append( "\n" );
356            sb.append( "#    this work for additional information regarding copyright ownership." ).append( "\n" );
357            sb.append( "#    The ASF licenses this file to You under the Apache License, Version 2.0" ).append( "\n" );
358            sb.append( "#    (the \"License\"); you may not use this file except in compliance with" ).append( "\n" );
359            sb.append( "#    the License.  You may obtain a copy of the License at" ).append( "\n" );
360            sb.append( "#" ).append( "\n" );
361            sb.append( "#       http://www.apache.org/licenses/LICENSE-2.0" ).append( "\n" );
362            sb.append( "#" ).append( "\n" );
363            sb.append( "#    Unless required by applicable law or agreed to in writing, software" ).append( "\n" );
364            sb.append( "#    distributed under the License is distributed on an \"AS IS\" BASIS," ).append( "\n" );
365            sb.append( "#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." ).append( "\n" );
366            sb.append( "#    See the License for the specific language governing permissions and" ).append( "\n" );
367            sb.append( "#    limitations under the License." ).append( "\n" );
368            sb.append( "#############################################################################" ).append( "\n" );
369            sb.append( "log4j.rootCategory=" + logsLevel + ", socketAppender, rollingFileAppender" ).append( "\n" );
370            sb.append( "" ).append( "\n" );
371            sb.append( "# The Socket Appender (used to send the logs to Apache Directory Studio)" ).append( "\n" );
372            sb.append( "log4j.appender.socketAppender=org.apache.log4j.net.SocketAppender" ).append( "\n" );
373            sb.append( "log4j.appender.socketAppender.RemoteHost=localhost" ).append( "\n" );
374            sb.append( "log4j.appender.socketAppender.Port=" ).append( port ).append( "\n" );
375            sb.append( "" ).append( "\n" );
376            sb.append( "# The Rolling File Appender" ).append( "\n" );
377            sb.append( "log4j.appender.rollingFileAppender=org.apache.log4j.RollingFileAppender" ).append( "\n" );
378            sb.append( "log4j.appender.rollingFileAppender.File=${apacheds.log.dir}/apacheds-rolling.log" ).append( "\n" );
379            sb.append( "log4j.appender.rollingFileAppender.MaxFileSize=1024KB" ).append( "\n" );
380            sb.append( "log4j.appender.rollingFileAppender.MaxBackupIndex=5" ).append( "\n" );
381            sb.append( "log4j.appender.rollingFileAppender.layout=org.apache.log4j.PatternLayout" ).append( "\n" );
382            sb.append( "log4j.appender.rollingFileAppender.layout.ConversionPattern=[%d{HH:mm:ss}] %p [%c] - %m%n" )
383                .append( "\n" );
384            sb.append( "" ).append( "\n" );
385            sb.append( "# with these we'll not get innundated when switching to DEBUG" ).append( "\n" );
386            sb.append( "log4j.logger.org.apache.directory.shared.ldap.name=WARN" ).append( "\n" );
387            sb.append( "log4j.logger.org.springframework=WARN" ).append( "\n" );
388            sb.append( "log4j.logger.org.apache.directory.shared.codec=WARN" ).append( "\n" );
389            sb.append( "log4j.logger.org.apache.directory.shared.asn1=WARN" ).append( "\n" );
390    
391            // Writing the content to the file
392            os.write( sb.toString().getBytes() );
393        }
394    
395    
396        /**
397         * Create the log4j.properties file for the server socket logger.
398         *
399         * @param os
400         *      the {@link OutputStream} to write to
401         * @param id
402         *      the id of the server
403         * @throws IOException
404         *      if an error occurs when writing to the file
405         */
406        private static void createServerSocketLog4jPropertiesFile( OutputStream os, String id ) throws IOException
407        {
408            // Creating the file content in a StringBuffer
409            StringBuffer sb = new StringBuffer();
410            sb.append( "#############################################################################" ).append( "\n" );
411            sb.append( "#    Licensed to the Apache Software Foundation (ASF) under one or more" ).append( "\n" );
412            sb.append( "#    contributor license agreements.  See the NOTICE file distributed with" ).append( "\n" );
413            sb.append( "#    this work for additional information regarding copyright ownership." ).append( "\n" );
414            sb.append( "#    The ASF licenses this file to You under the Apache License, Version 2.0" ).append( "\n" );
415            sb.append( "#    (the \"License\"); you may not use this file except in compliance with" ).append( "\n" );
416            sb.append( "#    the License.  You may obtain a copy of the License at" ).append( "\n" );
417            sb.append( "#" ).append( "\n" );
418            sb.append( "#       http://www.apache.org/licenses/LICENSE-2.0" ).append( "\n" );
419            sb.append( "#" ).append( "\n" );
420            sb.append( "#    Unless required by applicable law or agreed to in writing, software" ).append( "\n" );
421            sb.append( "#    distributed under the License is distributed on an \"AS IS\" BASIS," ).append( "\n" );
422            sb.append( "#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." ).append( "\n" );
423            sb.append( "#    See the License for the specific language governing permissions and" ).append( "\n" );
424            sb.append( "#    limitations under the License." ).append( "\n" );
425            sb.append( "#############################################################################" ).append( "\n" );
426            sb.append( "log4j.rootCategory=ALL, studioConsoleAppender" ).append( "\n" );
427            sb.append( "" ).append( "\n" );
428            sb.append( "# Studio Console Appender (identified with the server id)" ).append( "\n" );
429            sb.append( "log4j.appender.studioConsoleAppender=org.apache.directory.studio.apacheds.StudioConsoleAppender" )
430                .append( "\n" );
431            sb.append( "log4j.appender.studioConsoleAppender.ServerId=" ).append( id ).append( "\n" );
432            sb.append( "log4j.appender.studioConsoleAppender.layout=org.apache.log4j.PatternLayout" ).append( "\n" );
433            sb.append( "" ).append( "\n" );
434            sb.append( "# Hiding logs from log4j" ).append( "\n" );
435            sb.append( "log4j.logger.org.apache.log4j.net.SocketServer=OFF" ).append( "\n" );
436            sb.append( "log4j.logger.org.apache.log4j.net.SocketNode=OFF" ).append( "\n" );
437    
438            // Writing the content to the file
439            os.write( sb.toString().getBytes() );
440        }
441    
442    
443        /**
444         * Copies a configuration file.
445         *
446         * @param inputFilename
447         *      the filename
448         * @param ouputFile
449         *      the output file
450         * @throws IOException
451         *      if an error occurs when copying
452         */
453        private static void copyConfigurationFile( String inputFilename, File ouputFile ) throws IOException
454        {
455            // Getting he URL of the file within the bundle
456            URL inputFilenameUrl = FileLocator.find( ApacheDsPlugin.getDefault().getBundle(), new Path( RESOURCES
457                + IPath.SEPARATOR + "conf" + IPath.SEPARATOR + inputFilename ), null );
458    
459            // Creating the input and output streams
460            InputStream inputStream = inputFilenameUrl.openStream();
461            OutputStream outputStream = new FileOutputStream( ouputFile );
462    
463            // Copying the file
464            copyFile( inputStream, outputStream );
465    
466            // Closing the streams
467            inputStream.close();
468            outputStream.close();
469        }
470    
471    
472        /**
473         * Verifies that the Apache DS server folder exists.
474         * If it does not exists, it creates it.
475         */
476        private static void checkApacheDsServersFolder()
477        {
478            File apacheDsserversFolder = new File( getApacheDsServersFolder().toOSString() );
479            if ( !apacheDsserversFolder.exists() )
480            {
481                apacheDsserversFolder.mkdir();
482            }
483        }
484    
485    
486        /**
487         * Gets the server logs level.
488         *
489         * @return
490         *      the server logs level
491         */
492        public static String getServerLogsLevel()
493        {
494            String level = ApacheDsPlugin.getDefault().getPreferenceStore().getString(
495                ApacheDsPluginConstants.PREFS_SERVER_LOGS_LEVEL );
496            if ( ApacheDsPluginConstants.PREFS_SERVER_LOGS_LEVEL_DEBUG.equalsIgnoreCase( level ) )
497            {
498                return "DEBUG";
499            }
500            else if ( ApacheDsPluginConstants.PREFS_SERVER_LOGS_LEVEL_INFO.equalsIgnoreCase( level ) )
501            {
502                return "INFO";
503            }
504            else if ( ApacheDsPluginConstants.PREFS_SERVER_LOGS_LEVEL_WARN.equalsIgnoreCase( level ) )
505            {
506                return "WARN";
507            }
508            else if ( ApacheDsPluginConstants.PREFS_SERVER_LOGS_LEVEL_ERROR.equalsIgnoreCase( level ) )
509            {
510                return "ERROR";
511            }
512            else if ( ApacheDsPluginConstants.PREFS_SERVER_LOGS_LEVEL_FATAL.equalsIgnoreCase( level ) )
513            {
514                return "FATAL";
515            }
516    
517            return "";
518        }
519    
520    
521        /**
522         * Gets the server logs pattern.
523         *
524         * @return
525         *      the server logs pattern
526         */
527        public static String getServerLogsPattern()
528        {
529            return ApacheDsPlugin.getDefault().getPreferenceStore().getString(
530                ApacheDsPluginConstants.PREFS_SERVER_LOGS_PATTERN );
531        }
532    
533    
534        /**
535         * Reports an error.
536         *
537         * @param message
538         *      the message
539         */
540        public static void reportError( String message )
541        {
542    
543            MessageDialog dialog = new MessageDialog( ApacheDsPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow()
544                .getShell(), "Error!", null, message, MessageDialog.ERROR, new String[]
545                { IDialogConstants.OK_LABEL }, MessageDialog.OK );
546            dialog.open();
547        }
548    
549    
550        /**
551         * Gets the server configuration.
552         *
553         * @param server
554         *      the server
555         * @return
556         *      the associated server configuration
557         * @throws ServerXmlIOException 
558         * @throws ServerXmlIOException
559         * @throws IOException 
560         */
561        public static ServerConfiguration getServerConfiguration( Server server ) throws ServerXmlIOException, IOException
562        {
563            InputStream fis = new FileInputStream( new File( ApacheDsPluginUtils.getApacheDsServersFolder().append(
564                server.getId() ).append( "conf" ).append( "server.xml" ).toOSString() ) );
565    
566            // Parsing and returning the server configuration
567            switch ( server.getVersion() )
568            {
569                case VERSION_1_5_5:
570                    ServerXmlIOV155 serverXmlIOV155 = new ServerXmlIOV155();
571                    return serverXmlIOV155.parse( fis );
572                case VERSION_1_5_4:
573                    ServerXmlIOV154 serverXmlIOV154 = new ServerXmlIOV154();
574                    return serverXmlIOV154.parse( fis );
575                case VERSION_1_5_3:
576                    ServerXmlIOV153 serverXmlIOV153 = new ServerXmlIOV153();
577                    return serverXmlIOV153.parse( fis );
578            }
579    
580            // No corresponding reader has been found, we return null
581            return null;
582        }
583    }