Dateiverwaltung für die WebBox
ulrich
2017-02-28 3c4b104eb3d9bf4e651dffbbd956985764bcacce
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
21 import de.uhilger.filesystem.FileRef;
22 import java.io.File;
3c4b10 23 import java.net.URL;
7342b1 24 import java.util.logging.Logger;
U 25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28
29 /**
30  *
31  * @author ulrich
32  */
33 public class Initialiser implements ServletContextListener {
34   
35   private static final Logger logger = Logger.getLogger(Initialiser.class.getName());
36   
37   
38   public static final String FILE_BASE = "filebase";
39   
40   // http://localhost:8097/file-cms/rpc/de.uhilger.filecms.api.FileMgr/getBase/JSONNICE
41   private FileRef getBase() {
42     FileRef ref = null;
43     //String fileBase = getServletContext().getInitParameter(FILE_BASE);
44     // -Dfilecms.base=/pfad/zu/daten
45     //String fileBase = System.getProperty("filecms.base");
46     //File file = new File(fileBase);
47     //logger.info(file.getAbsolutePath());
48     //logger.info(getWebappsDir().getAbsolutePath());
49     
50     /* 
51         file = new File("."); liefert 
52         /home/ulrich/dev/lib/java/tomcat/tomcat2-8.5.9/bin/.
53     
54         ..auf der WebBox aber
55         /home/ulrich/srv/wbx_probe/.
56         ..weil das Startskript dort liegt
57     
58         der Tomcat der WebBox ist unter
59         sys/jrs/tomcat/bin
60     
61         also z.B.
62         /home/ulrich/srv/wbx_probe/sys/jrs/tomcat/bin
63
64         das Datenverzeichnis ist z.B. auf
65         /home/ulrich/srv/wbx_probe/daten
66     
67         dann ist das Datenverzeichnis unter
68         ../../../daten
69     
70         Der Ausdruck file = new File("."); liefert stets den 
71         Ort von dem aus der Java-Prozess gestartet wurde.
72     
73         Die unten folgende Bestimmung des Datenverzeichnisses 
74         ist beschraenkt auf das Datenverzeichnis der WebBox, 
75         entweder relativ zum Startskript der WebBox oder 
76         dem Startskript von Tomcat, wie es aus Netbeans heraus 
77         waehrend der Entwicklung genutzt wird.
78     
79         Besser ware vielleicht eine Bestimmung ueber einen 
80         Systemparameter -Dfilecms.base=... wie weiter oben 
81         auskommentiert. Damit liesse sich das file-cms auch 
82         ohne WebBox einsetzen. Allerdings muss dann das 
83         Datenverzeichnis im Start-Skript gebildet werden, 
84         also ausserhalb von Java, wenn es dynamisch aus 
85         einem Pfad relativ zum Start-Skript erzeugt werden 
86         soll.
87     */
88     
89     File file = new File(".");
3c4b10 90     logger.fine(file.getAbsolutePath());
7342b1 91     String path = file.getAbsolutePath();
U 92     path = path.substring(0, path.length() - 1);
93     file = new File(path);
94     if(path.endsWith("bin")) {
95       file = file.getParentFile().getParentFile().getParentFile();
96     } else {
97       
98     }
99     file = new File(file, "daten/");
100     ref = new FileRef(file.getAbsolutePath(), file.isDirectory());
3c4b10 101     logger.fine(ref.getAbsolutePath());
7342b1 102     return ref;
U 103   }
104   
105   /* ----- ServletContextListener Implementation ----- */
106
107   @Override
108   public void contextInitialized(ServletContextEvent sce) {
109     // hier kann etwas initialisiert werden
110     ServletContext ctx = sce.getServletContext();
3c4b10 111
U 112     String path = ctx.getRealPath("/");
113     logger.fine("getRealPath: " + path); // file-cms in webapps
114     File file = new File(path);
115     file = file.getParentFile().getParentFile().getParentFile().getParentFile();
116     file = new File(file, "daten/");
117     
118     logger.fine("Basis: " + file.getAbsolutePath());
119     ctx.setAttribute(FILE_BASE, file.getAbsolutePath());
7342b1 120   }
U 121
122   @Override
123   public void contextDestroyed(ServletContextEvent sce) {
124     // hier wird alles wieder aufgeraeumt
125     ServletContext ctx = sce.getServletContext();
126     ctx.removeAttribute(FILE_BASE);
127   }
128   
129 }