Debian Codegraph @

source/org/jfree/report/modules/preferences/filesystem/FileConfigStoreModuleInitializer.java

raw
1 /**
2 * ========================================
3 * JFreeReport : a free Java report library
4 * ========================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23 * in the United States and other countries.]
24 *
25 * ------------
26 * $Id: FileConfigStoreModuleInitializer.java 10756 2009-12-02 15:58:24Z tmorgner $
27 * ------------
28 * (C) Copyright 2000-2005, by Object Refinery Limited.
29 * (C) Copyright 2005-2007, by Pentaho Corporation.
30 */
31
32 package org.jfree.report.modules.preferences.filesystem;
33
34 import java.io.File;
35
36 import org.jfree.report.JFreeReportBoot;
37 import org.jfree.report.modules.preferences.base.ConfigFactory;
38 import org.pentaho.reporting.libraries.base.boot.ModuleInitializer;
39 import org.pentaho.reporting.libraries.base.boot.ModuleInitializeException;
40
41 /**
42 * The initializer is used to setup the file system storage provider and to register the
43 * providers at the configfactory.
44 * <p/>
45 * The directories are specified in the report configuration at boot time. If an directory
46 * name starts with "~/", the users home directory is used as base directory for that
47 * string.
48 *
49 * @author Thomas Morgner
50 */
51 public class FileConfigStoreModuleInitializer implements ModuleInitializer
52 {
53 /**
54 * The configuration key that specifies the base directory for the user configuration
55 * storage.
56 */
57 public static final String USER_BASEDIR_CONFIG_KEY =
58 "org.jfree.report.modules.misc.configstore.filesystem.UserTargetDir";
59
60 /**
61 * The configuration key that specifies the base directory for the system configuration
62 * storage.
63 */
64 public static final String SYSTEM_BASEDIR_CONFIG_KEY =
65 "org.jfree.report.modules.misc.configstore.filesystem.SystemTargetDir";
66
67 /**
68 * DefaultConstructor.
69 */
70 public FileConfigStoreModuleInitializer ()
71 {
72 }
73
74 /**
75 * Performs the module initialization and registers the storage providers at the config
76 * factory.
77 *
78 * @throws ModuleInitializeException if an error occures
79 */
80 public void performInit ()
81 throws ModuleInitializeException
82 {
83 final String userBaseDirectory =
84 JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
85 (USER_BASEDIR_CONFIG_KEY, "~/.jfreereport/user");
86
87 final String systemBaseDirectory =
88 JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
89 (SYSTEM_BASEDIR_CONFIG_KEY, "~/.jfreereport/system");
90
91 final ConfigFactory factory = ConfigFactory.getInstance();
92 factory.defineUserStorage(new FileConfigStorage(getStoragePath(userBaseDirectory)));
93 factory.defineSystemStorage(new FileConfigStorage(getStoragePath(systemBaseDirectory)));
94 }
95
96 /**
97 * Tries to fint the specified directory and creates a new one if the directory does not
98 * yet exist. An occurence of "~/" at the beginning of the name will be replaced with
99 * 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 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 (baseDirectory.equals("~/"))
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 }