Dateiverwaltung für die WebBox
ulrich
2017-03-08 438b168cad54853a8b419e6c19e639b6d2f70bc0
commit | author | age
7342b1 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
fab80c 21 import de.uhilger.wbx.WbxUtils;
7342b1 22 import java.io.File;
U 23 import java.util.logging.Logger;
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27
28 /**
2b3e7e 29  * Initialisieren der Dateiverwaltung
7342b1 30  */
U 31 public class Initialiser implements ServletContextListener {
32   
33   private static final Logger logger = Logger.getLogger(Initialiser.class.getName());
34   
35   
36   public static final String FILE_BASE = "filebase";
2b3e7e 37   public static final String DATENABLAGE = "datenAblage";
7342b1 38     
U 39   /* ----- ServletContextListener Implementation ----- */
40
41   @Override
42   public void contextInitialized(ServletContextEvent sce) {
43     // hier kann etwas initialisiert werden
44     ServletContext ctx = sce.getServletContext();
2b3e7e 45     ablageErmitteln(ctx);
U 46   }
47   
48   /**
49    * Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, 
50    * der im Deployment Descriptor hinterlegt werden kann oder, wenn 
51    * dort nichts eingetragen ist, auf den hart kodierten Pfad 
52    * der WebBox.
53    * 
54    * @param ctx der ServletContext, in den die Angabe eingetragen wird. Dort 
55    * ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar
56    */
57   private void ablageErmitteln(ServletContext ctx) {
58     Object o = ctx.getInitParameter(DATENABLAGE);
59     try {
60       if(o instanceof String) {
61         String pfad = o.toString();
62         if(pfad.trim().length() > 0) {
63           ctx.setAttribute(FILE_BASE, pfad);
64           logger.fine("Basis: " + pfad);
65         } else {
fab80c 66           ctx.setAttribute(FILE_BASE, WbxUtils.getWbxDataDir(ctx).getAbsolutePath());  
2b3e7e 67         }
U 68       } else {
fab80c 69         ctx.setAttribute(FILE_BASE, WbxUtils.getWbxDataDir(ctx).getAbsolutePath());      
2b3e7e 70       }    
U 71     } catch(Exception ex) {
fab80c 72       ctx.setAttribute(FILE_BASE, WbxUtils.getWbxDataDir(ctx).getAbsolutePath());
2b3e7e 73     }
U 74   }
75   
7342b1 76   @Override
U 77   public void contextDestroyed(ServletContextEvent sce) {
78     // hier wird alles wieder aufgeraeumt
79     ServletContext ctx = sce.getServletContext();
80     ctx.removeAttribute(FILE_BASE);
81   }
82   
83 }