Dateiverwaltung für die WebBox
ulrich
2018-03-04 c30d2d7b5f87d6a3aaeffbb6b0298a1ac3647afe
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
21 import java.io.File;
02f7b1 22 import java.io.IOException;
U 23 import java.util.logging.Level;
6e70be 24 import java.util.logging.Logger;
U 25 import javax.servlet.ServletContext;
21589e 26 import javax.servlet.ServletContextEvent;
U 27 import javax.servlet.ServletContextListener;
02f7b1 28 import org.apache.commons.io.FileUtils;
6e70be 29
U 30 /**
31  * Initialisieren der Dateiverwaltung
32  */
21589e 33 public class Initialiser implements ServletContextListener {
6e70be 34   
U 35   private static final Logger logger = Logger.getLogger(Initialiser.class.getName());
36   
37   public static final String FILE_BASE = "filebase";
38   public static final String DATENABLAGE = "datenAblage";
39   
40   /**
41    * Bei der WebBox ist das Datenverzeichnis relativ zum Verzeichnis 
42    * $CATALINA_BASE/webapps untergebracht. 
43    * Die Abfrage ServletContext.getRealPath 
44    * liefert das Verzeichnis des Context dieser Webanwendung, also 
45    * $CATALINA_BASE/webapps/file-cms
46    * oder
47    * $WBX/sys/base/webapps/file-cms
48    * 
49    * Unter Windows z.B.
50    * C:\Users\fred\Documents\srv\wbx\sys\base\webapps\file-cms
51    * Unter Linux oder Mac OS z.B.
52    * /home/fred/srv/wbx/sys/base/webapps/file-cms
53    * 
54    * Das Datenverzeichis liegt dann auf 
55    * $WBX/daten
56    * 
57    * Mit dem Verzeichnis des Context dieser Webanwendung ist das 
58    * Datenverzeichnis der WebBox hart kodierbar mit dieser Methode
59    * 
60    * @return Verzeichnis 'daten' der WebBox
61    */
62   protected File getWbxDataDir(ServletContext ctx) {
63     File file = getWbxDir(ctx);    
64     file = new File(file, "daten/");
65     logger.fine("WebBox Datenbasis: " + file.getAbsolutePath());
66     return file;
67   }
68   
69   protected File getWbxDir(ServletContext ctx) {
70     String path = ctx.getRealPath("/");
71     logger.fine("getRealPath: " + path); // file-cms in webapps
72     File file = new File(path);
73     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
74     logger.fine("WebBox: " + file.getAbsolutePath());
75     return file;
76   }
77   
78   /**
79    * Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, 
80    * der im Deployment Descriptor hinterlegt werden kann oder, wenn 
81    * dort nichts eingetragen ist, auf den hart kodierten Pfad 
82    * der WebBox.
83    * 
84    * @param ctx der ServletContext, in den die Angabe eingetragen wird. Dort 
85    * ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar
86    */
87   protected void ablageErmitteln(ServletContext ctx) {
88     Object o = ctx.getInitParameter(DATENABLAGE);
89     try {
90       if(o instanceof String) {
91         String pfad = o.toString();
92         if(pfad.trim().length() > 0) {
93           ctx.setAttribute(FILE_BASE, pfad);
94           logger.fine("Basis: " + pfad);
95         } else {
96           ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());  
97         }
98       } else {
99         ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());      
100       }    
101     } catch(Exception ex) {
102       ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());
103     }
104   }  
02f7b1 105   
U 106   protected void ablageInitialisieren(ServletContext ctx) {
107     Object o = ctx.getAttribute(FILE_BASE);
108     if(o instanceof String) {
109       String targetDirName = (String) o;
110       File dataDir = new File(targetDirName, "www");
111       if(!dataDir.exists()) {
112         String srcPath = ctx.getRealPath("/"); // file-cms in webapps
113         File srcDir = new File(srcPath, "/META-INF/daten/www");
968b07 114         dataDir = new File(targetDirName);
02f7b1 115         try {
968b07 116           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
02f7b1 117           srcDir = new File(srcPath, "/META-INF/daten/home");
968b07 118           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
U 119           srcDir = new File(srcPath, "/META-INF/daten/dav");
120           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);          
02f7b1 121         } catch (IOException ex) {
U 122           logger.log(Level.SEVERE, null, ex);
123         }
124       }
125     }
126   }
21589e 127
U 128   @Override
129   public void contextInitialized(ServletContextEvent sce) {
130     ServletContext ctx = sce.getServletContext();
131     ablageErmitteln(ctx);
02f7b1 132     ablageInitialisieren(ctx);
21589e 133   }
U 134
135   @Override
136   public void contextDestroyed(ServletContextEvent sce) {
137     ServletContext ctx = sce.getServletContext();
138     ctx.removeAttribute(FILE_BASE);
139   }
6e70be 140     
U 141 }