KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genbase > utils > DirStorer


1 /*
2  * JOnAS : Java(TM) OpenSource Application Server
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 2.1 of the License, or any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Initial Developer : Guillaume Sauthier
18  * --------------------------------------------------------------------------
19  * $Id: DirStorer.java,v 1.1 2004/10/11 13:16:14 benoitf Exp $
20  * --------------------------------------------------------------------------
21  */

22
23 package org.objectweb.jonas_lib.genbase.utils;
24
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30
31 import org.objectweb.jonas_lib.genbase.GenBaseException;
32 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
33
34 /**
35  * Store a J2EEArchive in an unpacked form.
36  *
37  * @author Guillaume Sauthier
38  */

39 public class DirStorer extends ArchiveStorer {
40
41     /**
42      * directory name containing unpacked archive.
43      */

44     private File JavaDoc base;
45
46     /**
47      * Creates a new DirStorer object.
48      *
49      * @param archive archive to be stored
50      * @param dir output directory
51      *
52      * @throws GenBaseException if cannot create output directory
53      */

54     public DirStorer(J2EEArchive archive, File JavaDoc dir) throws GenBaseException {
55         super(archive);
56
57         setOut(dir.getAbsolutePath());
58
59         // assure base exists
60
if (!dir.exists()) {
61             if (!dir.mkdirs()) {
62                 String JavaDoc err = getI18n().getMessage("DirStorer.constr.create", dir);
63                 throw new GenBaseException(err);
64             }
65         }
66
67         base = dir;
68     }
69
70     /**
71      * Convert a filename to a local filesystem filename.
72      *
73      * @param name name to be converted
74      *
75      * @return converted filename
76      */

77     protected String JavaDoc convertName(String JavaDoc name) {
78         return name.replace('/', File.separatorChar);
79     }
80
81     /**
82      * add a given file in the filesystem.
83      *
84      * @param name filename
85      *
86      * @throws IOException When fill fails.
87      */

88     protected void addFile(String JavaDoc name) throws IOException JavaDoc {
89         OutputStream JavaDoc fos = getOutputStream(name);
90         InputStream JavaDoc is = getArchive().getInputStream(name);
91         fill(is, fos);
92         is.close();
93     }
94
95     /**
96      * returns the OuputStream corresponding to the given filename.
97      *
98      * @param name filename to create
99      *
100      * @return An OutputStream
101      *
102      * @throws IOException When file creation fails.
103      */

104     protected OutputStream JavaDoc getOutputStream(String JavaDoc name) throws IOException JavaDoc {
105         File JavaDoc out = new File JavaDoc(base, convertName(name));
106         File JavaDoc parent = out.getParentFile();
107
108         if (!parent.exists()) {
109             if (!parent.mkdirs()) {
110                 String JavaDoc err = getI18n().getMessage("DirStorer.getOutputStream.create", out);
111                 throw new IOException JavaDoc(err);
112             }
113         }
114
115         OutputStream JavaDoc os = new FileOutputStream JavaDoc(out);
116
117         return os;
118     }
119 }
Popular Tags