commit | author | age
|
a01889
|
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.wbx; |
|
20 |
|
|
21 |
import java.io.File; |
5ebac8
|
22 |
import java.util.logging.Level; |
a01889
|
23 |
import java.util.logging.Logger; |
5ebac8
|
24 |
import javax.naming.Context; |
U |
25 |
import javax.naming.InitialContext; |
|
26 |
import javax.naming.NamingException; |
a01889
|
27 |
import javax.servlet.ServletContext; |
U |
28 |
|
|
29 |
/** |
|
30 |
* |
|
31 |
*/ |
|
32 |
public class WbxUtils { |
|
33 |
|
|
34 |
private static final Logger logger = Logger.getLogger(WbxUtils.class.getName()); |
|
35 |
|
5ebac8
|
36 |
public static final String JNDI_CTX_NAME = "java:comp/env"; |
U |
37 |
|
|
38 |
public static final String NOT_FOUND = " nicht gefunden"; |
|
39 |
public static final String NO_STRING = " ist kein String"; |
|
40 |
public static final String EMPTY_STRING = ""; |
|
41 |
|
eab7bb
|
42 |
//public static final String FILE_BASE = "filebase"; |
U |
43 |
//public static final String DATENABLAGE = "datenAblage"; |
5ebac8
|
44 |
|
U |
45 |
public int getJNDIInt(String paramName, int defaultVal) { |
|
46 |
String jndiStr = getJNDIParameter(paramName, Integer.toString(defaultVal)); |
|
47 |
try { |
|
48 |
return Integer.parseInt(jndiStr); |
|
49 |
} catch(NumberFormatException ex) { |
|
50 |
logger.log(Level.FINE, ex.getMessage()); |
|
51 |
return defaultVal; |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
public String getJNDIParameter(String pname, String defaultVal) { |
|
56 |
try { |
|
57 |
// unseren environment naming context ermitteln |
|
58 |
Context initCtx = new InitialContext(); |
|
59 |
Context envCtx = (Context) initCtx.lookup(JNDI_CTX_NAME); |
|
60 |
|
|
61 |
// unseren Parameter lesen |
|
62 |
Object o = envCtx.lookup(pname); |
|
63 |
if(o instanceof String) { |
|
64 |
return o.toString(); |
|
65 |
} else { |
|
66 |
return defaultVal; |
|
67 |
} |
|
68 |
} catch (NamingException ex) { |
|
69 |
logger.log(Level.FINE, ex.getMessage()); |
|
70 |
return defaultVal; |
|
71 |
} |
|
72 |
} |
eab7bb
|
73 |
|
a01889
|
74 |
/** |
U |
75 |
* Bei der WebBox ist das Datenverzeichnis relativ zum Verzeichnis |
|
76 |
* $CATALINA_BASE/webapps untergebracht. |
|
77 |
* Die Abfrage ServletContext.getRealPath |
|
78 |
* liefert das Verzeichnis des Context dieser Webanwendung, also |
|
79 |
* $CATALINA_BASE/webapps/file-cms |
|
80 |
* oder |
|
81 |
* $WBX/sys/base/webapps/file-cms |
|
82 |
* |
|
83 |
* Unter Windows z.B. |
|
84 |
* C:\Users\fred\Documents\srv\wbx\sys\base\webapps\file-cms |
|
85 |
* Unter Linux oder Mac OS z.B. |
|
86 |
* /home/fred/srv/wbx/sys/base/webapps/file-cms |
|
87 |
* |
|
88 |
* Das Datenverzeichis liegt dann auf |
|
89 |
* $WBX/daten |
|
90 |
* |
|
91 |
* Mit dem Verzeichnis des Context dieser Webanwendung ist das |
|
92 |
* Datenverzeichnis der WebBox hart kodierbar mit dieser Methode |
|
93 |
* |
|
94 |
* @return Verzeichnis 'daten' der WebBox |
|
95 |
*/ |
eab7bb
|
96 |
/* |
a01889
|
97 |
public static File getWbxDataDir(ServletContext ctx) { |
6c2165
|
98 |
File file = getWbxDir(ctx); |
U |
99 |
file = new File(file, "daten/"); |
|
100 |
logger.fine("WebBox Datenbasis: " + file.getAbsolutePath()); |
|
101 |
return file; |
|
102 |
} |
|
103 |
|
|
104 |
public static File getWbxDir(ServletContext ctx) { |
a01889
|
105 |
String path = ctx.getRealPath("/"); |
U |
106 |
logger.fine("getRealPath: " + path); // file-cms in webapps |
|
107 |
File file = new File(path); |
|
108 |
file = file.getParentFile().getParentFile().getParentFile().getParentFile(); |
6c2165
|
109 |
logger.fine("WebBox: " + file.getAbsolutePath()); |
a01889
|
110 |
return file; |
U |
111 |
} |
eab7bb
|
112 |
*/ |
U |
113 |
|
|
114 |
/** |
|
115 |
* Die Dateiablage wird entweder auf einen absoluten Pfad gesetzt, |
|
116 |
* der im Deployment Descriptor hinterlegt werden kann oder, wenn |
|
117 |
* dort nichts eingetragen ist, auf den hart kodierten Pfad |
|
118 |
* der WebBox. |
|
119 |
* |
|
120 |
* @param ctx der ServletContext, in den die Angabe eingetragen wird. Dort |
|
121 |
* ist anschliessend die Angabe unter Initialiser.FILE_BASE abrufbar |
|
122 |
*/ |
|
123 |
/* |
|
124 |
public static void ablageErmitteln(ServletContext ctx) { |
|
125 |
Object o = ctx.getInitParameter(DATENABLAGE); |
|
126 |
try { |
|
127 |
if(o instanceof String) { |
|
128 |
String pfad = o.toString(); |
|
129 |
if(pfad.trim().length() > 0) { |
|
130 |
ctx.setAttribute(FILE_BASE, pfad); |
|
131 |
logger.fine("Basis: " + pfad); |
|
132 |
} else { |
|
133 |
ctx.setAttribute(FILE_BASE, WbxUtils.getWbxDataDir(ctx).getAbsolutePath()); |
|
134 |
} |
|
135 |
} else { |
|
136 |
ctx.setAttribute(FILE_BASE, WbxUtils.getWbxDataDir(ctx).getAbsolutePath()); |
|
137 |
} |
|
138 |
} catch(Exception ex) { |
|
139 |
ctx.setAttribute(FILE_BASE, WbxUtils.getWbxDataDir(ctx).getAbsolutePath()); |
|
140 |
} |
|
141 |
} |
|
142 |
*/ |
a01889
|
143 |
|
U |
144 |
} |