Dateiverwaltung für die WebBox
ulrich
2017-02-20 7342b1f3850299264b571d1d63778173c9a30703
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
    Dateiverwaltung - File management in your browser
    Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
package de.uhilger.filecms.web;
 
import de.uhilger.filecms.api.FileMgr;
import de.uhilger.filesystem.FileRef;
import java.io.File;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
/**
 *
 * @author ulrich
 */
public class Initialiser implements ServletContextListener {
  
  private static final Logger logger = Logger.getLogger(Initialiser.class.getName());
  
  
  public static final String FILE_BASE = "filebase";
  
  // http://localhost:8097/file-cms/rpc/de.uhilger.filecms.api.FileMgr/getBase/JSONNICE
  private FileRef getBase() {
    FileRef ref = null;
    //String fileBase = getServletContext().getInitParameter(FILE_BASE);
    // -Dfilecms.base=/pfad/zu/daten
    //String fileBase = System.getProperty("filecms.base");
    //File file = new File(fileBase);
    //logger.info(file.getAbsolutePath());
    //logger.info(getWebappsDir().getAbsolutePath());
    
    /* 
        file = new File("."); liefert 
        /home/ulrich/dev/lib/java/tomcat/tomcat2-8.5.9/bin/.
    
        ..auf der WebBox aber
        /home/ulrich/srv/wbx_probe/.
        ..weil das Startskript dort liegt
    
        der Tomcat der WebBox ist unter
        sys/jrs/tomcat/bin
    
        also z.B.
        /home/ulrich/srv/wbx_probe/sys/jrs/tomcat/bin
 
        das Datenverzeichnis ist z.B. auf
        /home/ulrich/srv/wbx_probe/daten
    
        dann ist das Datenverzeichnis unter
        ../../../daten
    
        Der Ausdruck file = new File("."); liefert stets den 
        Ort von dem aus der Java-Prozess gestartet wurde.
    
        Die unten folgende Bestimmung des Datenverzeichnisses 
        ist beschraenkt auf das Datenverzeichnis der WebBox, 
        entweder relativ zum Startskript der WebBox oder 
        dem Startskript von Tomcat, wie es aus Netbeans heraus 
        waehrend der Entwicklung genutzt wird.
    
        Besser ware vielleicht eine Bestimmung ueber einen 
        Systemparameter -Dfilecms.base=... wie weiter oben 
        auskommentiert. Damit liesse sich das file-cms auch 
        ohne WebBox einsetzen. Allerdings muss dann das 
        Datenverzeichnis im Start-Skript gebildet werden, 
        also ausserhalb von Java, wenn es dynamisch aus 
        einem Pfad relativ zum Start-Skript erzeugt werden 
        soll.
    */
    
    File file = new File(".");
    logger.info(file.getAbsolutePath());
    String path = file.getAbsolutePath();
    path = path.substring(0, path.length() - 1);
    file = new File(path);
    if(path.endsWith("bin")) {
      file = file.getParentFile().getParentFile().getParentFile();
    } else {
      
    }
    file = new File(file, "daten/");
    ref = new FileRef(file.getAbsolutePath(), file.isDirectory());
    logger.info(ref.getAbsolutePath());
    return ref;
  }
  
  /* ----- ServletContextListener Implementation ----- */
 
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    // hier kann etwas initialisiert werden
    ServletContext ctx = sce.getServletContext();
    ctx.setAttribute(FILE_BASE, getBase());
  }
 
  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    // hier wird alles wieder aufgeraeumt
    ServletContext ctx = sce.getServletContext();
    ctx.removeAttribute(FILE_BASE);
  }
  
}