DirStorer.java

00001 /*
00002  * JOnAS : Java(TM) OpenSource Application Server
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License as published by the Free
00006  * Software Foundation; either version 2.1 of the License, or any later version.
00007  *
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
00011  * details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00016  *
00017  * Initial Developer : Guillaume Sauthier
00018  * --------------------------------------------------------------------------
00019  * $Id: DirStorer.java,v 1.1 2004/10/11 13:16:14 benoitf Exp $
00020  * --------------------------------------------------------------------------
00021  */
00022 
00023 package org.objectweb.jonas_lib.genbase.utils;
00024 
00025 import java.io.File;
00026 import java.io.FileOutputStream;
00027 import java.io.IOException;
00028 import java.io.InputStream;
00029 import java.io.OutputStream;
00030 
00031 import org.objectweb.jonas_lib.genbase.GenBaseException;
00032 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
00033 
00039 public class DirStorer extends ArchiveStorer {
00040 
00044     private File base;
00045 
00054     public DirStorer(J2EEArchive archive, File dir) throws GenBaseException {
00055         super(archive);
00056 
00057         setOut(dir.getAbsolutePath());
00058 
00059         // assure base exists
00060         if (!dir.exists()) {
00061             if (!dir.mkdirs()) {
00062                 String err = getI18n().getMessage("DirStorer.constr.create", dir);
00063                 throw new GenBaseException(err);
00064             }
00065         }
00066 
00067         base = dir;
00068     }
00069 
00077     protected String convertName(String name) {
00078         return name.replace('/', File.separatorChar);
00079     }
00080 
00088     protected void addFile(String name) throws IOException {
00089         OutputStream fos = getOutputStream(name);
00090         InputStream is = getArchive().getInputStream(name);
00091         fill(is, fos);
00092         is.close();
00093     }
00094 
00104     protected OutputStream getOutputStream(String name) throws IOException {
00105         File out = new File(base, convertName(name));
00106         File parent = out.getParentFile();
00107 
00108         if (!parent.exists()) {
00109             if (!parent.mkdirs()) {
00110                 String err = getI18n().getMessage("DirStorer.getOutputStream.create", out);
00111                 throw new IOException(err);
00112             }
00113         }
00114 
00115         OutputStream os = new FileOutputStream(out);
00116 
00117         return os;
00118     }
00119 }

Generated on Tue Feb 15 15:05:54 2005 for JOnAS by  doxygen 1.3.9.1