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