Dateiverwaltung für die WebBox
ulrich
2019-11-29 4e8bf303df852bd3ad1b557644c28ddcf5f957c3
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()) {
125         String srcPath = ctx.getRealPath("/"); // file-cms in webapps
126         File srcDir = new File(srcPath, "/META-INF/daten/www");
968b07 127         dataDir = new File(targetDirName);
02f7b1 128         try {
968b07 129           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
02f7b1 130           srcDir = new File(srcPath, "/META-INF/daten/home");
968b07 131           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
U 132           srcDir = new File(srcPath, "/META-INF/daten/dav");
133           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);          
e0a4be 134           // hier noch den context anlegen
U 135           String path = ctx.getRealPath("/");
136           //File appDir = new File(path);
137           logger.fine("Catalina Base: " + System.getProperty("catalina.base"));
138           //File catalinaBase = appDir.getParentFile().getParentFile();
139           File catalinaBase = new File(System.getProperty("catalina.base"));
140           File confLocalhost = new File(catalinaBase, "conf/Catalina/localhost");    
141           File dataContext = new File(confLocalhost, "data.xml");
142           srcDir = new File(path, "/META-INF/conf");
143           File dataCtxSrc = new File(srcDir, "data.xml");
144           logger.fine("dataCtxSrc: " + dataCtxSrc.getAbsolutePath());
145           logger.fine("dataContext: " + dataContext.getAbsolutePath());
146           FileUtils.copyFile(dataCtxSrc, dataContext);
02f7b1 147         } catch (IOException ex) {
U 148           logger.log(Level.SEVERE, null, ex);
149         }
150       }
ef09bf 151     }
02f7b1 152   }
21589e 153
U 154   @Override
155   public void contextInitialized(ServletContextEvent sce) {
156     ServletContext ctx = sce.getServletContext();
157     ablageErmitteln(ctx);
02f7b1 158     ablageInitialisieren(ctx);
21589e 159   }
U 160
161   @Override
162   public void contextDestroyed(ServletContextEvent sce) {
163     ServletContext ctx = sce.getServletContext();
164     ctx.removeAttribute(FILE_BASE);
165   }
6e70be 166     
U 167 }