Dateiverwaltung für die WebBox
ulrich
2018-04-03 b12c95e1d53caed954314ed5e42f0e54730df5cd
commit | author | age
6e70be 1 /*
U 2     Dateiverwaltung - File management in your browser
3     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Affero General Public License as
7     published by the Free Software Foundation, either version 3 of the
8     License, or (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Affero General Public License for more details.
14
15     You should have received a copy of the GNU Affero General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package de.uhilger.filecms.web;
20
b12c95 21 import de.uhilger.wbx.WbxUtils;
6e70be 22 import java.io.File;
02f7b1 23 import java.io.IOException;
U 24 import java.util.logging.Level;
6e70be 25 import java.util.logging.Logger;
U 26 import javax.servlet.ServletContext;
21589e 27 import javax.servlet.ServletContextEvent;
U 28 import javax.servlet.ServletContextListener;
02f7b1 29 import org.apache.commons.io.FileUtils;
6e70be 30
U 31 /**
32  * Initialisieren der Dateiverwaltung
33  */
21589e 34 public class Initialiser implements ServletContextListener {
6e70be 35   
U 36   private static final Logger logger = Logger.getLogger(Initialiser.class.getName());
37   
38   public static final String FILE_BASE = "filebase";
39   public static final String DATENABLAGE = "datenAblage";
b12c95 40   public static final String WBX_FILE_BASE = "wbxFileBase";
6e70be 41   
U 42   /**
43    * Bei der WebBox ist das Datenverzeichnis relativ zum Verzeichnis 
44    * $CATALINA_BASE/webapps untergebracht. 
45    * Die Abfrage ServletContext.getRealPath 
46    * liefert das Verzeichnis des Context dieser Webanwendung, also 
47    * $CATALINA_BASE/webapps/file-cms
48    * oder
49    * $WBX/sys/base/webapps/file-cms
50    * 
51    * Unter Windows z.B.
52    * C:\Users\fred\Documents\srv\wbx\sys\base\webapps\file-cms
53    * Unter Linux oder Mac OS z.B.
54    * /home/fred/srv/wbx/sys/base/webapps/file-cms
55    * 
56    * Das Datenverzeichis liegt dann auf 
57    * $WBX/daten
58    * 
59    * Mit dem Verzeichnis des Context dieser Webanwendung ist das 
60    * Datenverzeichnis der WebBox hart kodierbar mit dieser Methode
61    * 
62    * @return Verzeichnis 'daten' der WebBox
63    */
64   protected File getWbxDataDir(ServletContext ctx) {
65     File file = getWbxDir(ctx);    
66     file = new File(file, "daten/");
67     logger.fine("WebBox Datenbasis: " + file.getAbsolutePath());
68     return file;
69   }
70   
71   protected File getWbxDir(ServletContext ctx) {
72     String path = ctx.getRealPath("/");
73     logger.fine("getRealPath: " + path); // file-cms in webapps
74     File file = new File(path);
75     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
76     logger.fine("WebBox: " + file.getAbsolutePath());
77     return file;
78   }
79   
80   /**
81    * Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, 
82    * der im Deployment Descriptor hinterlegt werden kann oder, wenn 
83    * dort nichts eingetragen ist, auf den hart kodierten Pfad 
84    * der WebBox.
85    * 
86    * @param ctx der ServletContext, in den die Angabe eingetragen wird. Dort 
87    * ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar
88    */
89   protected void ablageErmitteln(ServletContext ctx) {
b12c95 90     //Object o = ctx.getInitParameter(DATENABLAGE);
U 91     WbxUtils wu = new WbxUtils();
92     Object o = wu.getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING);
6e70be 93     try {
U 94       if(o instanceof String) {
95         String pfad = o.toString();
96         if(pfad.trim().length() > 0) {
97           ctx.setAttribute(FILE_BASE, pfad);
98           logger.fine("Basis: " + pfad);
99         } else {
100           ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());  
101         }
102       } else {
103         ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());      
104       }    
105     } catch(Exception ex) {
106       ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());
107     }
108   }  
02f7b1 109   
U 110   protected void ablageInitialisieren(ServletContext ctx) {
111     Object o = ctx.getAttribute(FILE_BASE);
112     if(o instanceof String) {
113       String targetDirName = (String) o;
114       File dataDir = new File(targetDirName, "www");
115       if(!dataDir.exists()) {
116         String srcPath = ctx.getRealPath("/"); // file-cms in webapps
117         File srcDir = new File(srcPath, "/META-INF/daten/www");
968b07 118         dataDir = new File(targetDirName);
02f7b1 119         try {
968b07 120           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
02f7b1 121           srcDir = new File(srcPath, "/META-INF/daten/home");
968b07 122           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
U 123           srcDir = new File(srcPath, "/META-INF/daten/dav");
124           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);          
02f7b1 125         } catch (IOException ex) {
U 126           logger.log(Level.SEVERE, null, ex);
127         }
128       }
129     }
130   }
21589e 131
U 132   @Override
133   public void contextInitialized(ServletContextEvent sce) {
134     ServletContext ctx = sce.getServletContext();
135     ablageErmitteln(ctx);
02f7b1 136     ablageInitialisieren(ctx);
21589e 137   }
U 138
139   @Override
140   public void contextDestroyed(ServletContextEvent sce) {
141     ServletContext ctx = sce.getServletContext();
142     ctx.removeAttribute(FILE_BASE);
143   }
6e70be 144     
U 145 }