From 74fab1c53fd3850bf0fd075c93622f14ad8dbe30 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Wed, 06 Jan 2021 23:03:51 +0000
Subject: [PATCH] null pruefung hinzugefuegt

---
 src/java/de/uhilger/filecms/web/Initialiser.java |  149 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 141 insertions(+), 8 deletions(-)

diff --git a/src/java/de/uhilger/filecms/web/Initialiser.java b/src/java/de/uhilger/filecms/web/Initialiser.java
index 90cf53a..486704b 100644
--- a/src/java/de/uhilger/filecms/web/Initialiser.java
+++ b/src/java/de/uhilger/filecms/web/Initialiser.java
@@ -1,27 +1,160 @@
+/*
+    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 {
-    
-  public static final String FILE_BASE = "filebase";
   
   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();
-    StringBuilder sb = new StringBuilder();
-    sb.append(System.getProperty("user.home"));
-    sb.append("/file-cms");
-    System.out.println("  FILE_BASE: " + sb.toString());
-    ctx.setAttribute(FILE_BASE, sb.toString());
+    ablageErmitteln(ctx);
+    ablageInitialisieren(ctx);
   }
 
   @Override

--
Gitblit v1.9.3