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