KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > providers > DirectoryProvider


1 /*
2  * Copyright (C) 2003 Erik Swenson - erik@oreports.com
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.providers;
21
22 import com.opensymphony.webwork.ServletActionContext;
23 import com.opensymphony.webwork.config.Configuration;
24 import com.opensymphony.xwork.ActionContext;
25 import com.opensymphony.xwork.interceptor.component.ComponentManager;
26
27 import org.apache.log4j.Logger;
28 import org.efs.openreports.objects.ORProperty;
29
30 public class DirectoryProvider implements PropertiesProviderAware
31 {
32     protected static Logger log = Logger.getLogger(DirectoryProvider.class.getName());
33
34     private String JavaDoc reportDirectory;
35     private String JavaDoc reportImageDirectory;
36     private String JavaDoc tempDirectory;
37     private String JavaDoc separator = System.getProperty("file.separator");
38
39     private PropertiesProvider propertiesProvider;
40     
41     // constructor for Spring IOC
42
public DirectoryProvider(PropertiesProvider propertiesProvider) throws ProviderException
43     {
44         this.propertiesProvider = propertiesProvider;
45         init();
46     }
47
48     // constructor for WebWork IOC
49
public DirectoryProvider() throws ProviderException
50     {
51         // try getting baseDirectory from properties
52
ComponentManager container = (ComponentManager) ActionContext.getContext().get(
53                 "com.opensymphony.xwork.interceptor.component.ComponentManager");
54
55         container.initializeObject(this);
56         
57         init();
58     }
59     
60     protected void init() throws ProviderException
61     {
62         log.info("Loading BaseDirectory from OR_PROPERTIES table.");
63
64         String JavaDoc baseDirectory = null;
65
66         ORProperty property = propertiesProvider.getProperty(ORProperty.BASE_DIRECTORY);
67         if (property != null) baseDirectory = property.getValue();
68         
69         if (baseDirectory == null || baseDirectory.trim().length() < 1)
70         {
71             log.info("BaseDirectory not set in OR_PROPERTIES table. Trying to get path from ServletContext.");
72
73             try
74             {
75                 baseDirectory = ServletActionContext.getServletContext().getRealPath("");
76                 baseDirectory = baseDirectory + separator + "reports";
77             }
78             catch (NullPointerException JavaDoc npe)
79             {
80                 log.info("ServletActionContext not available.");
81                 baseDirectory = ".";
82             }
83         }
84
85         log.info("BaseDirectory: " + baseDirectory);
86         
87         reportDirectory = baseDirectory + separator;
88         reportImageDirectory = reportDirectory + "images" + separator;
89         
90         //set path for uploaded reports
91
Configuration.set("webwork.multipart.saveDir", baseDirectory);
92         
93         //set temp directory path for report virtualization
94
property = propertiesProvider.getProperty(ORProperty.TEMP_DIRECTORY);
95         if (property != null && property.getValue() != null && property.getValue().trim().length() > 0)
96         {
97             tempDirectory = property.getValue();
98             log.info("TempDirectory: " + tempDirectory);
99         }
100         
101         log.info("Created");
102     }
103
104     public String JavaDoc getReportDirectory()
105     {
106         return reportDirectory;
107     }
108
109     public void setReportDirectory(String JavaDoc reportDirectory)
110     {
111         this.reportDirectory = reportDirectory;
112         reportImageDirectory = reportDirectory + "images" + separator;
113     }
114     
115     public String JavaDoc getReportImageDirectory()
116     {
117         return reportImageDirectory;
118     }
119     
120     public String JavaDoc getReportImageTempDirectory()
121     {
122         return reportImageDirectory + "temp" + separator;
123     }
124     
125     public String JavaDoc getTempDirectory()
126     {
127         return tempDirectory;
128     }
129     
130     public void setTempDirectory(String JavaDoc tempDirectory)
131     {
132         this.tempDirectory = tempDirectory;
133     }
134
135     public void setPropertiesProvider(PropertiesProvider propertiesProvider)
136     {
137         this.propertiesProvider = propertiesProvider;
138     }
139
140 }
Popular Tags