From e0a4bed472576a2ac5b7a4bfdaae97668a5a45c0 Mon Sep 17 00:00:00 2001
From: ulrich <undisclosed>
Date: Fri, 06 Apr 2018 16:24:33 +0000
Subject: [PATCH] Kontext data nur erzeugen, wenn der Ordner www noch nicht existiert

---
 src/java/de/uhilger/filecms/web/Initialiser.java |   78 ++++++++++++++++++++++++++++++++++++---
 1 files changed, 72 insertions(+), 6 deletions(-)

diff --git a/src/java/de/uhilger/filecms/web/Initialiser.java b/src/java/de/uhilger/filecms/web/Initialiser.java
index ea52033..9473f68 100644
--- a/src/java/de/uhilger/filecms/web/Initialiser.java
+++ b/src/java/de/uhilger/filecms/web/Initialiser.java
@@ -18,19 +18,26 @@
 
 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
  */
-public class Initialiser {
+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";
   
   /**
    * Bei der WebBox ist das Datenverzeichnis relativ zum Verzeichnis 
@@ -54,21 +61,30 @@
    * 
    * @return Verzeichnis 'daten' der WebBox
    */
+  /*
   protected File getWbxDataDir(ServletContext ctx) {
     File file = getWbxDir(ctx);    
     file = new File(file, "daten/");
     logger.fine("WebBox Datenbasis: " + file.getAbsolutePath());
     return file;
   }
+  */
   
+  /*
   protected File getWbxDir(ServletContext ctx) {
+    logger.fine("Catalina Base: " + System.getProperty("catalina.base"));
+    File catalinaBase = new File(System.getProperty("catalina.base"));
+    File wbxDir = catalinaBase.getParentFile().getParentFile();
+    return wbxDir;
+    /*
     String path = ctx.getRealPath("/");
     logger.fine("getRealPath: " + path); // file-cms in webapps
     File file = new File(path);
     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
     logger.fine("WebBox: " + file.getAbsolutePath());
     return file;
-  }
+    */
+  //}
   
   /**
    * Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, 
@@ -80,7 +96,9 @@
    * ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar
    */
   protected void ablageErmitteln(ServletContext ctx) {
-    Object o = ctx.getInitParameter(DATENABLAGE);
+    //Object o = ctx.getInitParameter(DATENABLAGE);
+    WbxUtils wu = new WbxUtils();
+    Object o = wu.getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING);
     try {
       if(o instanceof String) {
         String pfad = o.toString();
@@ -88,14 +106,62 @@
           ctx.setAttribute(FILE_BASE, pfad);
           logger.fine("Basis: " + pfad);
         } else {
-          ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());  
+          ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());  
         }
       } else {
-        ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());      
+        ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());      
       }    
     } catch(Exception ex) {
-      ctx.setAttribute(FILE_BASE, getWbxDataDir(ctx).getAbsolutePath());
+      ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());
     }
   }  
+  
+  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()) {
+        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);          
+          // hier noch den context anlegen
+          String path = ctx.getRealPath("/");
+          //File appDir = new File(path);
+          logger.fine("Catalina Base: " + System.getProperty("catalina.base"));
+          //File catalinaBase = appDir.getParentFile().getParentFile();
+          File catalinaBase = new File(System.getProperty("catalina.base"));
+          File confLocalhost = new File(catalinaBase, "conf/Catalina/localhost");    
+          File dataContext = new File(confLocalhost, "data.xml");
+          srcDir = new File(path, "/META-INF/conf");
+          File dataCtxSrc = new File(srcDir, "data.xml");
+          logger.fine("dataCtxSrc: " + dataCtxSrc.getAbsolutePath());
+          logger.fine("dataContext: " + dataContext.getAbsolutePath());
+          FileUtils.copyFile(dataCtxSrc, dataContext);
+        } 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);
+  }
     
 }

--
Gitblit v1.9.3