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