| | |
| | | /** |
| | | * 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"; |
| | | |
| | | /** |
| | | * Bei der WebBox ist das Datenverzeichnis relativ zum Verzeichnis |
| | | * $CATALINA_BASE/webapps untergebracht. |
| | | * Die Abfrage ServletContext.getRealPath |
| | | * liefert das Verzeichnis des Context dieser Webanwendung, also |
| | | * $CATALINA_BASE/webapps/file-cms |
| | | * oder |
| | | * $WBX/sys/base/webapps/file-cms |
| | | * |
| | | * Unter Windows z.B. |
| | | * C:\Users\fred\Documents\srv\wbx\sys\base\webapps\file-cms |
| | | * Unter Linux oder Mac OS z.B. |
| | | * /home/fred/srv/wbx/sys/base/webapps/file-cms |
| | | * |
| | | * Das Datenverzeichis liegt dann auf |
| | | * $WBX/daten |
| | | * |
| | | * Mit dem Verzeichnis des Context dieser Webanwendung ist das |
| | | * Datenverzeichnis der WebBox hart kodierbar mit dieser Methode |
| | | * |
| | | * @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; |
| | | */ |
| | | //} |
| | | |
| | | 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 |
| | |
| | | */ |
| | | protected void ablageErmitteln(ServletContext ctx) { |
| | | //Object o = ctx.getInitParameter(DATENABLAGE); |
| | | WbxUtils wu = new WbxUtils(); |
| | | Object o = wu.getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING); |
| | | |
| | | //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(o instanceof String) { |
| | | String pfad = o.toString(); |
| | | if(pfad.trim().length() > 0) { |
| | | ctx.setAttribute(FILE_BASE, pfad); |
| | | logger.fine("Basis: " + pfad); |
| | | 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, wu.getWbxDataDir().getAbsolutePath()); |
| | | ctx.setAttribute(FILE_BASE, getDefaultDataDir(ctx).getAbsolutePath()); |
| | | } |
| | | } else { |
| | | ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath()); |
| | | ctx.setAttribute(FILE_BASE, getDefaultDataDir(ctx).getAbsolutePath()); |
| | | } |
| | | } catch(Exception ex) { |
| | | ctx.setAttribute(FILE_BASE, wu.getWbxDataDir().getAbsolutePath()); |
| | | 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); |
| | |
| | | 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); |
| | |
| | | srcDir = new File(srcPath, "/META-INF/daten/home"); |
| | | FileUtils.copyDirectoryToDirectory(srcDir, dataDir); |
| | | srcDir = new File(srcPath, "/META-INF/daten/dav"); |
| | | FileUtils.copyDirectoryToDirectory(srcDir, dataDir); |
| | | FileUtils.copyDirectoryToDirectory(srcDir, dataDir); |
| | | } catch (IOException ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | } |
| | | } |
| | | } |
| | | // 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"); |
| | | File srcDir = new File(path, "/META-INF/conf"); |
| | | File dataCtxSrc = new File(srcDir, "data.xml"); |
| | | logger.fine("dataCtxSrc: " + dataCtxSrc.getAbsolutePath()); |
| | | logger.fine("dataContext: " + dataContext.getAbsolutePath()); |
| | | try { |
| | | FileUtils.copyFile(dataCtxSrc, dataContext); |
| | | } catch (IOException ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | } |
| | | } |
| | | |