Dateiverwaltung für die WebBox
ulrich
2021-01-03 719f73ea3ea9204585de5487fb83f6d5be97d1ac
commit | author | age
17727f 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
6e70be 19 package de.uhilger.filecms.web;
U 20
17727f 21 import de.uhilger.wbx.WbxUtils;
U 22 import java.io.File;
23 import java.io.IOException;
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;
17727f 29 import org.apache.commons.io.FileUtils;
6e70be 30
U 31 /**
17727f 32  * Initialisieren der Dateiverwaltung
6e70be 33  */
17727f 34
U 35 /*
5794c8 36     der folgende Eintrag kann z.B. in $CATALINA_BASE/conf/context.xml 
U 37     eingetragen werden
38
17727f 39     <Environment  
U 40       name="wbxFileBase"
41       type="java.lang.String"
42       value="/media/wbx-data/cms"
43       override="false"
44     />
45
46 */
21589e 47 public class Initialiser implements ServletContextListener {
6e70be 48   
U 49   private static final Logger logger = Logger.getLogger(Initialiser.class.getName());
17727f 50   
U 51   public static final String FILE_BASE = "filebase";
52   public static final String DATENABLAGE = "datenAblage";
53   public static final String WBX_FILE_BASE = "wbxFileBase";
719f73 54   public static final String WBX_DATA_DIR = "wbx.data";
U 55   public static final String WBX_DEPTH = "tiefe";
56   public static final String WBX_DATA_FOLDER = "datenOrdner";
5794c8 57     
17727f 58   /**
U 59    * Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, 
60    * der im Deployment Descriptor hinterlegt werden kann oder, wenn 
61    * dort nichts eingetragen ist, auf den hart kodierten Pfad 
62    * der WebBox.
63    * 
64    * @param ctx der ServletContext, in den die Angabe eingetragen wird. Dort 
65    * ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar
66    */
67   protected void ablageErmitteln(ServletContext ctx) {
68     //Object o = ctx.getInitParameter(DATENABLAGE);
5794c8 69     
U 70     //File wbxDaten = getDataDir(ctx, 2, "data/");
71     
719f73 72     //WbxUtils wu = new WbxUtils();
U 73     //Object o = wu.getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING);
74     String dir = System.getProperty(WBX_DATA_DIR);
17727f 75     try {
719f73 76       if(dir != null) {
U 77         //String pfad = o.toString();
78         if(dir.trim().length() > 0) {
79           ctx.setAttribute(FILE_BASE, dir);
80           logger.log(Level.INFO, "Daten-Ordner aus wbx.data: {0}", dir);
17727f 81         } else {
719f73 82           ctx.setAttribute(FILE_BASE, getDefaultDataDir(ctx).getAbsolutePath());  
17727f 83         }
U 84       } else {
719f73 85         ctx.setAttribute(FILE_BASE, getDefaultDataDir(ctx).getAbsolutePath());      
17727f 86       }    
U 87     } catch(Exception ex) {
719f73 88       ctx.setAttribute(FILE_BASE, getDefaultDataDir(ctx).getAbsolutePath());
17727f 89     }
U 90   }  
5794c8 91   
719f73 92   private File getDefaultDataDir(ServletContext ctx) {
U 93     String tiefe = ctx.getInitParameter(WBX_DEPTH);
94     String ordnerName = ctx.getInitParameter(WBX_DATA_FOLDER);
95     return getDataDir(ctx, Integer.parseInt(tiefe), ordnerName);
96   }
97   
5794c8 98   /**
U 99    * Daten-Ordner relativ zum Ablageort des file-cms finden.
100    * 
101    * Die Standard-Struktur einer WebBox sieht folgende Ordner vor
102    * 
103    * wbx/apps
104    * wbx/data
105    * 
106    * Liegt das file-cms z.B. im Ordner /home/ulrich/dev/srv/wbx-3/apps/file-cms 
107    * kann von dort aus der Daten-Ordner mit 
108    * getDataDir(ctx, 2, "data/")
109    * gefunden werden. 
110    * 
111    * @param ctx  der ServletContext, in dem das file-cms laeuft
112    * @param tiefe Anzahl Ordner, die der Ablageort des file-cms vom 
113    * Eltern-Ordner entfernt ist
114    * @param pfad der Pfad zum Daten-Ordner ausgehend vom Eltern-Ordner
115    * @return Daten-Ordner
116    */
117   private File getDataDir(ServletContext ctx, int tiefe, String pfad) {
118     File appsOrdner = new File(ctx.getRealPath("/"));
119     logger.log(Level.INFO, "Apps-Ordner: {0}", appsOrdner.getAbsolutePath());
120     File wbx = new File(appsOrdner, "");
121     for(int i = 0; i < tiefe; i++) {
122       wbx = wbx.getParentFile();
123     }
124     logger.log(Level.INFO, "Wbx-Ordner: {0}", wbx.getAbsolutePath());
125     File daten = new File(wbx, pfad);
126     logger.log(Level.INFO, "Daten-Ordner: {0}", daten.getAbsolutePath());
127     return daten;
128   }
17727f 129   
U 130   protected void ablageInitialisieren(ServletContext ctx) {
131     Object o = ctx.getAttribute(FILE_BASE);
132     if(o instanceof String) {
133       String targetDirName = (String) o;
134       File dataDir = new File(targetDirName, "www");
135       if(!dataDir.exists()) {
136         dataDir.mkdirs();
137         String srcPath = ctx.getRealPath("/"); // file-cms in webapps
138         File srcDir = new File(srcPath, "/META-INF/daten/www");
139         dataDir = new File(targetDirName);
140         try {
141           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
142           srcDir = new File(srcPath, "/META-INF/daten/home");
143           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
144           srcDir = new File(srcPath, "/META-INF/daten/dav");
145           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);   
146         } catch (IOException ex) {
147           logger.log(Level.SEVERE, null, ex);
148         }
149       }
150     }
151   }
152
21589e 153   @Override
U 154   public void contextInitialized(ServletContextEvent sce) {
155     ServletContext ctx = sce.getServletContext();
17727f 156     ablageErmitteln(ctx);
U 157     ablageInitialisieren(ctx);
21589e 158   }
U 159
160   @Override
161   public void contextDestroyed(ServletContextEvent sce) {
162     ServletContext ctx = sce.getServletContext();
163     ctx.removeAttribute(FILE_BASE);
164   }
6e70be 165     
U 166 }