Dateiverwaltung für die WebBox
ulrich
2020-01-17 332c058bd407c4b6fb30e6bf32b634a72bc68e05
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    */
ef09bf 64   /*
6e70be 65   protected File getWbxDataDir(ServletContext ctx) {
U 66     File file = getWbxDir(ctx);    
67     file = new File(file, "daten/");
68     logger.fine("WebBox Datenbasis: " + file.getAbsolutePath());
69     return file;
70   }
ef09bf 71   */
6e70be 72   
ef09bf 73   /*
6e70be 74   protected File getWbxDir(ServletContext ctx) {
ef09bf 75     logger.fine("Catalina Base: " + System.getProperty("catalina.base"));
U 76     File catalinaBase = new File(System.getProperty("catalina.base"));
77     File wbxDir = catalinaBase.getParentFile().getParentFile();
78     return wbxDir;
79     /*
6e70be 80     String path = ctx.getRealPath("/");
U 81     logger.fine("getRealPath: " + path); // file-cms in webapps
82     File file = new File(path);
83     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
84     logger.fine("WebBox: " + file.getAbsolutePath());
85     return file;
ef09bf 86     */
U 87   //}
6e70be 88   
U 89   /**
90    * Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, 
91    * der im Deployment Descriptor hinterlegt werden kann oder, wenn 
92    * dort nichts eingetragen ist, auf den hart kodierten Pfad 
93    * der WebBox.
94    * 
95    * @param ctx der ServletContext, in den die Angabe eingetragen wird. Dort 
96    * ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar
97    */
98   protected void ablageErmitteln(ServletContext ctx) {
b12c95 99     //Object o = ctx.getInitParameter(DATENABLAGE);
U 100     WbxUtils wu = new WbxUtils();
101     Object o = wu.getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING);
6e70be 102     try {
U 103       if(o instanceof String) {
104         String pfad = o.toString();
105         if(pfad.trim().length() > 0) {
106           ctx.setAttribute(FILE_BASE, pfad);
107           logger.fine("Basis: " + pfad);
108         } else {
ef09bf 109           ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());  
6e70be 110         }
U 111       } else {
ef09bf 112         ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());      
6e70be 113       }    
U 114     } catch(Exception ex) {
ef09bf 115       ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());
6e70be 116     }
U 117   }  
02f7b1 118   
U 119   protected void ablageInitialisieren(ServletContext ctx) {
120     Object o = ctx.getAttribute(FILE_BASE);
121     if(o instanceof String) {
122       String targetDirName = (String) o;
123       File dataDir = new File(targetDirName, "www");
124       if(!dataDir.exists()) {
1a8136 125         dataDir.mkdirs();
02f7b1 126         String srcPath = ctx.getRealPath("/"); // file-cms in webapps
U 127         File srcDir = new File(srcPath, "/META-INF/daten/www");
968b07 128         dataDir = new File(targetDirName);
02f7b1 129         try {
968b07 130           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
02f7b1 131           srcDir = new File(srcPath, "/META-INF/daten/home");
968b07 132           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
U 133           srcDir = new File(srcPath, "/META-INF/daten/dav");
1a8136 134           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);   
U 135           
136           /*
137           
138           an dieser Stelle koennten noch die Kontexte fuer www und home 
139           angelegt werden. Sie muessten aber dynamisch erzeugt werden,
140           mit der jeweiligen Einstellung laut FILE_BASE, nicht, wie 
141           unten durch Kopieren einer statischen Datei
142           
e0a4be 143           // hier noch den context anlegen
U 144           String path = ctx.getRealPath("/");
145           //File appDir = new File(path);
146           logger.fine("Catalina Base: " + System.getProperty("catalina.base"));
147           //File catalinaBase = appDir.getParentFile().getParentFile();
148           File catalinaBase = new File(System.getProperty("catalina.base"));
149           File confLocalhost = new File(catalinaBase, "conf/Catalina/localhost");    
150           File dataContext = new File(confLocalhost, "data.xml");
151           srcDir = new File(path, "/META-INF/conf");
152           File dataCtxSrc = new File(srcDir, "data.xml");
153           logger.fine("dataCtxSrc: " + dataCtxSrc.getAbsolutePath());
154           logger.fine("dataContext: " + dataContext.getAbsolutePath());
155           FileUtils.copyFile(dataCtxSrc, dataContext);
1a8136 156           */
02f7b1 157         } catch (IOException ex) {
U 158           logger.log(Level.SEVERE, null, ex);
159         }
160       }
ef09bf 161     }
02f7b1 162   }
21589e 163
U 164   @Override
165   public void contextInitialized(ServletContextEvent sce) {
166     ServletContext ctx = sce.getServletContext();
167     ablageErmitteln(ctx);
02f7b1 168     ablageInitialisieren(ctx);
21589e 169   }
U 170
171   @Override
172   public void contextDestroyed(ServletContextEvent sce) {
173     ServletContext ctx = sce.getServletContext();
174     ctx.removeAttribute(FILE_BASE);
175   }
6e70be 176     
U 177 }