KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > files > JahiaFileTransferBaseService


1 //
2
// EV 30.11.2000
3
//
4

5 package org.jahia.services.files;
6
7 import java.io.File JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.net.URL JavaDoc;
13
14 import org.jahia.exceptions.JahiaException;
15 import org.jahia.exceptions.JahiaInitializationException;
16 import org.jahia.settings.SettingsBean;
17 import org.jahia.utils.FileUtils;
18
19 public class JahiaFileTransferBaseService extends JahiaFileTransferService {
20
21     private static org.apache.log4j.Logger logger =
22             org.apache.log4j.Logger.getLogger(JahiaFileTransferBaseService.class);
23
24     private static String JavaDoc serviceName = "JahiaFileTransferBaseService";
25     private static String JavaDoc componentsDiskPath = "";
26     private static String JavaDoc tmpDiskPath = "";
27
28     private static JahiaFileTransferBaseService theObject = null;
29
30    /**
31     * constructor
32      * EV 18.11.2000
33     */

34     protected JahiaFileTransferBaseService()
35     {
36         logger.debug( "Starting " + serviceName );
37     } // end constructor
38

39
40    /***
41     * getInstance
42      * EV 31.10.2000
43     *
44     */

45     public static synchronized JahiaFileTransferBaseService getInstance()
46     {
47         if (theObject == null) {
48             theObject = new JahiaFileTransferBaseService();
49         }
50         return theObject;
51     } // end getInstance
52

53
54    /***
55     * init
56      * EV 30.11.2000
57     *
58     */

59    public void init( SettingsBean jSettings ) throws JahiaInitializationException
60     {
61
62         logger.debug( "Initializing " + serviceName );
63
64         componentsDiskPath = jSettings.getComponentsDiskPath();
65         tmpDiskPath = jSettings.getJahiaTemplatesDiskPath();
66     } // end init
67

68
69
70    /***
71     * downloadApp
72      * EV 30.11.2000
73      * called by DownloadApp_Engine.processForm
74     *
75     */

76     public boolean downloadApp( String JavaDoc fileName, String JavaDoc urlPath, String JavaDoc diskPath )
77     {
78         logger.debug("ComponentsDiskPath is " + componentsDiskPath );
79         String JavaDoc completeDiskPath = componentsDiskPath + File.separator + diskPath + "\\";
80         // let's first check if the directory already exists
81
// checkFile will check :
82
// - if directory already exists, this means that the appliation is already there ! -> do nothin'
83
// - if directory does not exist, create it and return true
84
if (checkFile( completeDiskPath, fileName )) {
85             // now we can download the file, and return true
86
downloadFile( urlPath + fileName, completeDiskPath + fileName );
87             return true;
88         } else {
89             // couldn't replace the application -> problems !
90
return false;
91         }
92     } // end downloadApp
93

94
95
96    /***
97     * downloadTemplate
98      * EV 30.11.2000
99      * called by DownloadTemplate_Engine.processForm
100     *
101     */

102     public boolean downloadTemplate( String JavaDoc fileName, String JavaDoc urlPath, String JavaDoc diskPath )
103     {
104         String JavaDoc completeDiskPath = tmpDiskPath + diskPath + "\\";
105         logger.debug("Downloading template to : " + completeDiskPath );
106         // let's first check if the directory already exists
107
// checkFile will check :
108
// - if directory already exists, this means that the appliation is already there ! -> do nothin'
109
// - if directory does not exist, create it and return true
110
if (checkFile( completeDiskPath, fileName )) {
111             // now we can download the file
112
if (downloadFile( urlPath + fileName, completeDiskPath + fileName )) {
113                 // download ok, return true
114
return true;
115             } else {
116                 // download failed -> problems !
117
return false;
118             }
119         } else {
120             // couldn't replace the template -> problems !
121
return false;
122         }
123     } // end downloadTemplate
124

125
126
127    /***
128     * checkFile
129      * EV 30.11.2000
130      * called by downloadApp
131     *
132     */

133    protected boolean checkFile( String JavaDoc diskPath, String JavaDoc fileName )
134     {
135         File JavaDoc fAppPath = new File JavaDoc( diskPath );
136         if (!fAppPath.exists()) {
137             logger.debug( "Creating " + diskPath + " path " );
138             fAppPath.mkdirs();
139         }
140         File JavaDoc fApp = new File JavaDoc( diskPath + "\\" + fileName );
141         if (fApp.exists()) {
142             if (fApp.delete()) {
143                 return true;
144             } else {
145                 String JavaDoc msg = "Failed to replace existing file " + diskPath + "\\" + fileName + " for download";
146                 JahiaException je = new JahiaException( "Download error", msg,
147                                         JahiaException.TEMPLATE_ERROR, JahiaException.WARNING_SEVERITY );
148
149                 return false;
150             }
151         } else {
152             return true;
153         }
154     } // end checkFile
155

156
157
158    /***
159     * downloadFile
160      * EV 30.11.2000
161      * called by downloadApp
162     *
163     */

164     protected boolean downloadFile( String JavaDoc urlPath, String JavaDoc diskPath )
165     {
166         try {
167             URL JavaDoc url = new URL JavaDoc( urlPath );
168             InputStream JavaDoc input = url.openStream();
169             File JavaDoc myFile = new File JavaDoc( diskPath );
170             OutputStream JavaDoc output = (OutputStream JavaDoc) new FileOutputStream JavaDoc( myFile );
171             FileUtils.getInstance().copyStream( input, output );
172             logger.debug(diskPath + " successfully transfered from " + urlPath );
173             return true;
174         } catch (IOException JavaDoc ie) {
175             String JavaDoc msg = "IOException while trying to download " + diskPath + " from " + urlPath;
176             JahiaException je = new JahiaException( "Download error", msg,
177                                     JahiaException.TEMPLATE_ERROR, JahiaException.WARNING_SEVERITY );
178             return false;
179         }
180     } // end downloadFile
181

182
183
184    /**
185     * set the service name
186     */

187     public void setName(String JavaDoc name){
188
189       serviceName = name;
190
191     }
192
193 } // end JahiaFileTransferBaseService
194
Popular Tags