001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: FileConfigStoreModuleInitializer.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    
032    package org.jfree.report.modules.preferences.filesystem;
033    
034    import java.io.File;
035    
036    import org.jfree.base.modules.ModuleInitializeException;
037    import org.jfree.base.modules.ModuleInitializer;
038    import org.jfree.report.JFreeReportBoot;
039    import org.jfree.report.modules.preferences.base.ConfigFactory;
040    
041    /**
042     * The initializer is used to setup the file system storage provider and to register the
043     * providers at the configfactory.
044     * <p/>
045     * The directories are specified in the report configuration at boot time. If an directory
046     * name starts with "~/", the users home directory is used as base directory for that
047     * string.
048     *
049     * @author Thomas Morgner
050     */
051    public class FileConfigStoreModuleInitializer implements ModuleInitializer
052    {
053      /**
054       * The configuration key that specifies the base directory for the user configuration
055       * storage.
056       */
057      public static final String USER_BASEDIR_CONFIG_KEY =
058              "org.jfree.report.modules.misc.configstore.filesystem.UserTargetDir";
059    
060      /**
061       * The configuration key that specifies the base directory for the system configuration
062       * storage.
063       */
064      public static final String SYSTEM_BASEDIR_CONFIG_KEY =
065              "org.jfree.report.modules.misc.configstore.filesystem.SystemTargetDir";
066    
067      /**
068       * DefaultConstructor.
069       */
070      public FileConfigStoreModuleInitializer ()
071      {
072      }
073    
074      /**
075       * Performs the module initialization and registers the storage providers at the config
076       * factory.
077       *
078       * @throws ModuleInitializeException if an error occures
079       */
080      public void performInit ()
081              throws ModuleInitializeException
082      {
083        final String userBaseDirectory =
084                JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
085                (USER_BASEDIR_CONFIG_KEY, "~/.jfreereport/user");
086    
087        final String systemBaseDirectory =
088                JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
089                (SYSTEM_BASEDIR_CONFIG_KEY, "~/.jfreereport/system");
090    
091        final ConfigFactory factory = ConfigFactory.getInstance();
092        factory.defineUserStorage(new FileConfigStorage(getStoragePath(userBaseDirectory)));
093        factory.defineSystemStorage(new FileConfigStorage(getStoragePath(systemBaseDirectory)));
094      }
095    
096      /**
097       * Tries to fint the specified directory and creates a new one if the directory does not
098       * yet exist. An occurence of "~/" at the beginning of the name will be replaced with
099       * the users home directory.
100       *
101       * @param baseDirectory the base directory as specified in the configuration.
102       * @return the file object pointing to that directory.
103       *
104       * @throws org.jfree.base.modules.ModuleInitializeException
105       *          if an error occured or the directory could not be created.
106       */
107      private File getStoragePath (String baseDirectory)
108              throws ModuleInitializeException
109      {
110        final File baseDirectoryFile;
111    
112        if (baseDirectory.startsWith("~/") == false)
113        {
114          baseDirectoryFile = new File(baseDirectory);
115        }
116        else
117        {
118          try
119          {
120            final String homeDirectory = System.getProperty("user.home");
121            if ("~/".equals(baseDirectory))
122            {
123              baseDirectoryFile = new File(homeDirectory);
124            }
125            else
126            {
127              baseDirectory = "." + baseDirectory.substring(1);
128              baseDirectoryFile = new File(homeDirectory, baseDirectory);
129            }
130          }
131          catch (Exception e)
132          {
133            throw new ModuleInitializeException
134                    ("Failed to create the file config storage.", e);
135          }
136        }
137    
138        if (baseDirectoryFile.exists() == false)
139        {
140          if (baseDirectoryFile.mkdirs() == false)
141          {
142            throw new ModuleInitializeException
143                    ("Unable to create the specified directory.");
144          }
145        }
146        else
147        {
148          if (baseDirectoryFile.canRead() == false ||
149                  baseDirectoryFile.canWrite() == false)
150          {
151            throw new ModuleInitializeException
152                    ("Unable to access the specified directory.");
153          }
154        }
155        return baseDirectoryFile;
156      }
157    }