Dateiverwaltung für die WebBox
ulrich
2021-01-03 719f73ea3ea9204585de5487fb83f6d5be97d1ac
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
    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.wbx.WbxUtils;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.io.FileUtils;
 
/**
 * Initialisieren der Dateiverwaltung
 */
 
/*
    der folgende Eintrag kann z.B. in $CATALINA_BASE/conf/context.xml 
    eingetragen werden
 
    <Environment  
      name="wbxFileBase"
      type="java.lang.String"
      value="/media/wbx-data/cms"
      override="false"
    />
 
*/
public class Initialiser implements ServletContextListener {
  
  private static final Logger logger = Logger.getLogger(Initialiser.class.getName());
  
  public static final String FILE_BASE = "filebase";
  public static final String DATENABLAGE = "datenAblage";
  public static final String WBX_FILE_BASE = "wbxFileBase";
  public static final String WBX_DATA_DIR = "wbx.data";
  public static final String WBX_DEPTH = "tiefe";
  public static final String WBX_DATA_FOLDER = "datenOrdner";
    
  /**
   * Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, 
   * der im Deployment Descriptor hinterlegt werden kann oder, wenn 
   * dort nichts eingetragen ist, auf den hart kodierten Pfad 
   * der WebBox.
   * 
   * @param ctx der ServletContext, in den die Angabe eingetragen wird. Dort 
   * ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar
   */
  protected void ablageErmitteln(ServletContext ctx) {
    //Object o = ctx.getInitParameter(DATENABLAGE);
    
    //File wbxDaten = getDataDir(ctx, 2, "data/");
    
    //WbxUtils wu = new WbxUtils();
    //Object o = wu.getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING);
    String dir = System.getProperty(WBX_DATA_DIR);
    try {
      if(dir != null) {
        //String pfad = o.toString();
        if(dir.trim().length() > 0) {
          ctx.setAttribute(FILE_BASE, dir);
          logger.log(Level.INFO, "Daten-Ordner aus wbx.data: {0}", dir);
        } else {
          ctx.setAttribute(FILE_BASE, getDefaultDataDir(ctx).getAbsolutePath());  
        }
      } else {
        ctx.setAttribute(FILE_BASE, getDefaultDataDir(ctx).getAbsolutePath());      
      }    
    } catch(Exception ex) {
      ctx.setAttribute(FILE_BASE, getDefaultDataDir(ctx).getAbsolutePath());
    }
  }  
  
  private File getDefaultDataDir(ServletContext ctx) {
    String tiefe = ctx.getInitParameter(WBX_DEPTH);
    String ordnerName = ctx.getInitParameter(WBX_DATA_FOLDER);
    return getDataDir(ctx, Integer.parseInt(tiefe), ordnerName);
  }
  
  /**
   * Daten-Ordner relativ zum Ablageort des file-cms finden.
   * 
   * Die Standard-Struktur einer WebBox sieht folgende Ordner vor
   * 
   * wbx/apps
   * wbx/data
   * 
   * Liegt das file-cms z.B. im Ordner /home/ulrich/dev/srv/wbx-3/apps/file-cms 
   * kann von dort aus der Daten-Ordner mit 
   * getDataDir(ctx, 2, "data/")
   * gefunden werden. 
   * 
   * @param ctx  der ServletContext, in dem das file-cms laeuft
   * @param tiefe Anzahl Ordner, die der Ablageort des file-cms vom 
   * Eltern-Ordner entfernt ist
   * @param pfad der Pfad zum Daten-Ordner ausgehend vom Eltern-Ordner
   * @return Daten-Ordner
   */
  private File getDataDir(ServletContext ctx, int tiefe, String pfad) {
    File appsOrdner = new File(ctx.getRealPath("/"));
    logger.log(Level.INFO, "Apps-Ordner: {0}", appsOrdner.getAbsolutePath());
    File wbx = new File(appsOrdner, "");
    for(int i = 0; i < tiefe; i++) {
      wbx = wbx.getParentFile();
    }
    logger.log(Level.INFO, "Wbx-Ordner: {0}", wbx.getAbsolutePath());
    File daten = new File(wbx, pfad);
    logger.log(Level.INFO, "Daten-Ordner: {0}", daten.getAbsolutePath());
    return daten;
  }
  
  protected void ablageInitialisieren(ServletContext ctx) {
    Object o = ctx.getAttribute(FILE_BASE);
    if(o instanceof String) {
      String targetDirName = (String) o;
      File dataDir = new File(targetDirName, "www");
      if(!dataDir.exists()) {
        dataDir.mkdirs();
        String srcPath = ctx.getRealPath("/"); // file-cms in webapps
        File srcDir = new File(srcPath, "/META-INF/daten/www");
        dataDir = new File(targetDirName);
        try {
          FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
          srcDir = new File(srcPath, "/META-INF/daten/home");
          FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
          srcDir = new File(srcPath, "/META-INF/daten/dav");
          FileUtils.copyDirectoryToDirectory(srcDir, dataDir);   
        } catch (IOException ex) {
          logger.log(Level.SEVERE, null, ex);
        }
      }
    }
  }
 
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    ServletContext ctx = sce.getServletContext();
    ablageErmitteln(ctx);
    ablageInitialisieren(ctx);
  }
 
  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    ServletContext ctx = sce.getServletContext();
    ctx.removeAttribute(FILE_BASE);
  }
    
}