Added J2EE Portal support.
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web / J2EEUtils.cs
1 //\r
2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)\r
3 //\r
4 \r
5 //\r
6 // Permission is hereby granted, free of charge, to any person obtaining\r
7 // a copy of this software and associated documentation files (the\r
8 // "Software"), to deal in the Software without restriction, including\r
9 // without limitation the rights to use, copy, modify, merge, publish,\r
10 // distribute, sublicense, and/or sell copies of the Software, and to\r
11 // permit persons to whom the Software is furnished to do so, subject to\r
12 // the following conditions:\r
13 //\r
14 // The above copyright notice and this permission notice shall be\r
15 // included in all copies or substantial portions of the Software.\r
16 //\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
24 //\r
25 \r
26 using System;\r
27 using System.Web.Util;\r
28 using System.IO;\r
29 using vmw.@internal.io;\r
30 using vmw.common;\r
31 \r
32 using javax.servlet;\r
33 \r
34 namespace Mainsoft.Web\r
35 {\r
36         internal static class J2EEUtils\r
37         {\r
38                 public static string GetInitParameterByHierarchy(ServletConfig config, string name)\r
39                 {\r
40                         if (config == null)\r
41                                 throw new ArgumentNullException("config");\r
42 \r
43                         string value = config.getInitParameter(name);\r
44                         if (value != null)\r
45                                 return value;\r
46 \r
47                         return config.getServletContext().getInitParameter(name);\r
48                 }\r
49 \r
50                 public static string GetApplicationRealPath (ServletConfig config)\r
51                 {\r
52                         string realFs = GetInitParameterByHierarchy (config, J2EEConsts.FILESYSTEM_ACCESS);\r
53                         if (realFs == null || realFs == J2EEConsts.ACCESS_FULL) {\r
54                                 try {\r
55                                         if (Path.IsPathRooted (config.getServletContext ().getRealPath ("")))\r
56                                                 return config.getServletContext ().getRealPath ("").Replace ("\\", "/").TrimEnd ('/');\r
57                                 }\r
58                                 catch (ArgumentException e) {\r
59                                         Console.WriteLine (e.Message);\r
60                                 }\r
61                                 catch (Exception e) {\r
62                                         Console.WriteLine (e.Message);\r
63                                 }\r
64                         }\r
65                         return IAppDomainConfig.WAR_ROOT_SYMBOL;\r
66                 }\r
67 \r
68                 public static string GetApplicationPhysicalPath (ServletConfig config) {\r
69                         string path = "";\r
70                         ServletContext context = config.getServletContext ();\r
71                         string appDir = GetInitParameterByHierarchy (config, IAppDomainConfig.APP_DIR_NAME);\r
72                         //                      Console.WriteLine("appdir = {0}", appDir);\r
73                         if (appDir != null) {\r
74                                 try {\r
75                                         java.io.File f = new java.io.File (appDir);\r
76                                         if (f.exists ()) {\r
77                                                 //                                              Console.WriteLine("Physical path= {0}", appDir);\r
78                                                 path = appDir;\r
79                                         }\r
80                                 }\r
81                                 catch (Exception e) {\r
82                                         Console.WriteLine (e.Message + appDir + "is invalid or unaccessible." +\r
83                                                 " If " + appDir + " really exists, check your security permissions");\r
84                                 };\r
85                         }\r
86                         if (path == "") {\r
87                                 path = GetApplicationRealPath (config);\r
88                         }\r
89 \r
90                         if (!path.EndsWith ("/") && !path.EndsWith ("\\"))\r
91                                 path += "/";\r
92 \r
93                         //                      Console.WriteLine("Physical path= {0}", path); \r
94                         return path;\r
95                 }\r
96         }\r
97 }\r