Dateiverwaltung für die WebBox
Ulrich
2020-06-17 c95ff55dc9780a20b8ff979f5c1888a69223337d
commit | author | age
4b5c8e 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.wbx.WbxUtils;
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletContextEvent;
28 import javax.servlet.ServletContextListener;
29 import org.apache.commons.io.FileUtils;
30
31 /**
32  * Initialisieren der Dateiverwaltung
33  */
34 public class InitialiserDep implements ServletContextListener {
35   
36   private static final Logger logger = Logger.getLogger(InitialiserDep.class.getName());
37   
38   public static final String FILE_BASE = "filebase";
39   public static final String DATENABLAGE = "datenAblage";
40   public static final String WBX_FILE_BASE = "wbxFileBase";
41   
42   /**
43    * Bei der WebBox ist das Datenverzeichnis relativ zum Verzeichnis 
44    * $CATALINA_BASE/webapps untergebracht. 
45    * Die Abfrage ServletContext.getRealPath 
46    * liefert das Verzeichnis des Context dieser Webanwendung, also 
47    * $CATALINA_BASE/webapps/file-cms
48    * oder
49    * $WBX/sys/base/webapps/file-cms
50    * 
51    * Unter Windows z.B.
52    * C:\Users\fred\Documents\srv\wbx\sys\base\webapps\file-cms
53    * Unter Linux oder Mac OS z.B.
54    * /home/fred/srv/wbx/sys/base/webapps/file-cms
55    * 
56    * Das Datenverzeichis liegt dann auf 
57    * $WBX/daten
58    * 
59    * Mit dem Verzeichnis des Context dieser Webanwendung ist das 
60    * Datenverzeichnis der WebBox hart kodierbar mit dieser Methode
61    * 
62    * @return Verzeichnis 'daten' der WebBox
63    */
64   /*
65   protected File getWbxDataDir(ServletContext ctx) {
66     File file = getWbxDir(ctx);    
67     file = new File(file, "daten/");
68     logger.fine("WebBox Datenbasis: " + file.getAbsolutePath());
69     return file;
70   }
71   */
72   
73   /*
74   protected File getWbxDir(ServletContext ctx) {
75     logger.fine("Catalina Base: " + System.getProperty("catalina.base"));
76     File catalinaBase = new File(System.getProperty("catalina.base"));
77     File wbxDir = catalinaBase.getParentFile().getParentFile();
78     return wbxDir;
79     /*
80     String path = ctx.getRealPath("/");
81     logger.fine("getRealPath: " + path); // file-cms in webapps
82     File file = new File(path);
83     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
84     logger.fine("WebBox: " + file.getAbsolutePath());
85     return file;
86     */
87   //}
88   
89   /**
90    * Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, 
91    * der im Deployment Descriptor hinterlegt werden kann oder, wenn 
92    * dort nichts eingetragen ist, auf den hart kodierten Pfad 
93    * der WebBox.
94    * 
95    * @param ctx der ServletContext, in den die Angabe eingetragen wird. Dort 
96    * ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar
97    */
98   protected void ablageErmitteln(ServletContext ctx) {
99     //Object o = ctx.getInitParameter(DATENABLAGE);
100     WbxUtils wu = new WbxUtils();
101     Object o = wu.getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING);
102     try {
103       if(o instanceof String) {
104         String pfad = o.toString();
105         if(pfad.trim().length() > 0) {
106           ctx.setAttribute(FILE_BASE, pfad);
107           logger.fine("Basis: " + pfad);
108         } else {
109           ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());  
110         }
111       } else {
112         ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());      
113       }    
114     } catch(Exception ex) {
115       ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath());
116     }
117   }  
118   
119   protected void ablageInitialisieren(ServletContext ctx) {
120     Object o = ctx.getAttribute(FILE_BASE);
121     if(o instanceof String) {
122       String targetDirName = (String) o;
123       File dataDir = new File(targetDirName, "www");
124       if(!dataDir.exists()) {
125         dataDir.mkdirs();
126         String srcPath = ctx.getRealPath("/"); // file-cms in webapps
127         File srcDir = new File(srcPath, "/META-INF/daten/www");
128         dataDir = new File(targetDirName);
129         try {
130           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
131           srcDir = new File(srcPath, "/META-INF/daten/home");
132           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);
133           srcDir = new File(srcPath, "/META-INF/daten/dav");
134           FileUtils.copyDirectoryToDirectory(srcDir, dataDir);   
135           
136           /*
137           
138           an dieser Stelle koennten noch die Kontexte fuer www und home 
139           angelegt werden. Sie muessten aber dynamisch erzeugt werden,
140           mit der jeweiligen Einstellung laut FILE_BASE, nicht, wie 
141           unten durch Kopieren einer statischen Datei
142           
143           // hier noch den context anlegen
144           String path = ctx.getRealPath("/");
145           //File appDir = new File(path);
146           logger.fine("Catalina Base: " + System.getProperty("catalina.base"));
147           //File catalinaBase = appDir.getParentFile().getParentFile();
148           File catalinaBase = new File(System.getProperty("catalina.base"));
149           File confLocalhost = new File(catalinaBase, "conf/Catalina/localhost");    
150           File dataContext = new File(confLocalhost, "data.xml");
151           srcDir = new File(path, "/META-INF/conf");
152           File dataCtxSrc = new File(srcDir, "data.xml");
153           logger.fine("dataCtxSrc: " + dataCtxSrc.getAbsolutePath());
154           logger.fine("dataContext: " + dataContext.getAbsolutePath());
155           FileUtils.copyFile(dataCtxSrc, dataContext);
156           */
157         } catch (IOException ex) {
158           logger.log(Level.SEVERE, null, ex);
159         }
160       }
161     }
162   }
163
164   @Override
165   public void contextInitialized(ServletContextEvent sce) {
166     ServletContext ctx = sce.getServletContext();
167     //ablageErmitteln(ctx);
168     //ablageInitialisieren(ctx);
169   }
170
171   @Override
172   public void contextDestroyed(ServletContextEvent sce) {
173     ServletContext ctx = sce.getServletContext();
174     ctx.removeAttribute(FILE_BASE);
175   }
176     
177 }