1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
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 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.util; |
---|
31 | |
---|
32 | import java.net.InetAddress; |
---|
33 | import java.net.NetworkInterface; |
---|
34 | import java.net.SocketException; |
---|
35 | import java.net.UnknownHostException; |
---|
36 | import java.nio.file.Path; |
---|
37 | import java.nio.file.Paths; |
---|
38 | import java.util.Enumeration; |
---|
39 | import java.util.LinkedHashSet; |
---|
40 | import java.util.Set; |
---|
41 | |
---|
42 | import com.sun.jna.Native; |
---|
43 | import com.sun.jna.platform.win32.Shell32; |
---|
44 | import com.sun.jna.platform.win32.ShlObj; |
---|
45 | import com.sun.jna.platform.win32.W32Errors; |
---|
46 | import com.sun.jna.platform.win32.WinDef; |
---|
47 | import com.sun.jna.platform.win32.WinNT.HRESULT; |
---|
48 | |
---|
49 | public class Environment { |
---|
50 | |
---|
51 | private Environment() {} |
---|
52 | |
---|
53 | private static class EnvironmentHolder { |
---|
54 | public static final Environment INSTANCE = new Environment(); |
---|
55 | } |
---|
56 | |
---|
57 | public static Environment getInstance() { |
---|
58 | return EnvironmentHolder.INSTANCE; |
---|
59 | } |
---|
60 | |
---|
61 | private final long timestamp = System.currentTimeMillis(); |
---|
62 | |
---|
63 | public long getExecTimestamp() { |
---|
64 | return timestamp; |
---|
65 | } |
---|
66 | |
---|
67 | private String getKnownFolder(int folder) { |
---|
68 | char[] path = new char[WinDef.MAX_PATH]; |
---|
69 | HRESULT hr = Shell32.INSTANCE.SHGetFolderPath(null, folder, null, ShlObj.SHGFP_TYPE_CURRENT, path); |
---|
70 | if(hr.equals(W32Errors.S_OK)) { |
---|
71 | return Native.toString(path); |
---|
72 | } |
---|
73 | else { |
---|
74 | System.err.printf("getKnownFolderPath {csidl=%s} error: %d", folder, hr.intValue()); |
---|
75 | } |
---|
76 | return null; |
---|
77 | } |
---|
78 | |
---|
79 | //<editor-fold defaultstate="collapsed" desc=" OperatingSystem "> |
---|
80 | private class OperatingSystemCache { |
---|
81 | final OperatingSystem value; |
---|
82 | { |
---|
83 | String name = System.getProperty("os.name"); |
---|
84 | value = (name.equals("Linux")) ? OperatingSystem.Linux |
---|
85 | : (name.equals("Mac OS X")) ? OperatingSystem.MacOSX |
---|
86 | : (name.equals("Windows XP")) ? OperatingSystem.WindowsXP |
---|
87 | : (name.equals("Windows Vista")) ? OperatingSystem.WindowsVista |
---|
88 | : (name.equals("Windows 7")) ? OperatingSystem.Windows7 |
---|
89 | : null; |
---|
90 | } |
---|
91 | } |
---|
92 | private OperatingSystemCache operatingSystem; |
---|
93 | public OperatingSystem getOperatingSystem() { |
---|
94 | if(operatingSystem == null) { |
---|
95 | operatingSystem = new OperatingSystemCache(); |
---|
96 | } |
---|
97 | return operatingSystem.value; |
---|
98 | } |
---|
99 | //</editor-fold> |
---|
100 | |
---|
101 | |
---|
102 | //<editor-fold defaultstate="collapsed" desc=" HomePath "> |
---|
103 | private class HomePathCache { |
---|
104 | final Path value; |
---|
105 | { |
---|
106 | String home = null; |
---|
107 | |
---|
108 | switch(getOperatingSystem()) { |
---|
109 | case WindowsXP: |
---|
110 | case WindowsVista: |
---|
111 | case Windows7: |
---|
112 | |
---|
113 | home = getKnownFolder(ShlObj.CSIDL_PROFILE); |
---|
114 | break; |
---|
115 | |
---|
116 | case Linux: |
---|
117 | case MacOSX: |
---|
118 | |
---|
119 | home = System.getenv("HOME"); |
---|
120 | break; |
---|
121 | } |
---|
122 | |
---|
123 | if((home != null) && (!home.isEmpty())) { |
---|
124 | value = Paths.get(home); |
---|
125 | } |
---|
126 | else { |
---|
127 | value = null; |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | private HomePathCache homePath; |
---|
132 | public Path getHomePath() { |
---|
133 | if(homePath == null) { |
---|
134 | homePath = new HomePathCache(); |
---|
135 | System.out.println("homePath: " + homePath.value); |
---|
136 | } |
---|
137 | return homePath.value; |
---|
138 | } |
---|
139 | //</editor-fold> |
---|
140 | |
---|
141 | |
---|
142 | //<editor-fold defaultstate="collapsed" desc=" AppDataPath "> |
---|
143 | private class AppDataPathCache { |
---|
144 | final Path value; |
---|
145 | { |
---|
146 | String appData = null; |
---|
147 | |
---|
148 | switch(getOperatingSystem()) { |
---|
149 | case WindowsXP: |
---|
150 | case WindowsVista: |
---|
151 | case Windows7: |
---|
152 | |
---|
153 | appData = getKnownFolder(ShlObj.CSIDL_APPDATA); |
---|
154 | break; |
---|
155 | } |
---|
156 | |
---|
157 | if((appData != null) && (!appData.isEmpty())) { |
---|
158 | value = Paths.get(appData); |
---|
159 | } |
---|
160 | else { |
---|
161 | value = null; |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | private AppDataPathCache appDataPath; |
---|
166 | public Path getAppDataPath() { |
---|
167 | if(appDataPath == null) { |
---|
168 | appDataPath = new AppDataPathCache(); |
---|
169 | System.out.println("appDataPath: " + appDataPath.value); |
---|
170 | } |
---|
171 | return appDataPath.value; |
---|
172 | } |
---|
173 | //</editor-fold> |
---|
174 | |
---|
175 | |
---|
176 | //<editor-fold defaultstate="collapsed" desc=" LocalAppDataPath "> |
---|
177 | private class LocalAppDataPathCache { |
---|
178 | final Path value; |
---|
179 | { |
---|
180 | String localAppData = null; |
---|
181 | |
---|
182 | switch(getOperatingSystem()) { |
---|
183 | case WindowsXP: |
---|
184 | case WindowsVista: |
---|
185 | case Windows7: |
---|
186 | |
---|
187 | localAppData = getKnownFolder(ShlObj.CSIDL_LOCAL_APPDATA); |
---|
188 | break; |
---|
189 | } |
---|
190 | |
---|
191 | if((localAppData != null) && (!localAppData.isEmpty())) { |
---|
192 | value = Paths.get(localAppData); |
---|
193 | } |
---|
194 | else { |
---|
195 | value = null; |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | private LocalAppDataPathCache localAppDataPath; |
---|
200 | public Path getLocalAppDataPath() { |
---|
201 | if(localAppDataPath == null) { |
---|
202 | localAppDataPath = new LocalAppDataPathCache(); |
---|
203 | System.out.println("localAppDataPath: " + localAppDataPath.value); |
---|
204 | } |
---|
205 | return localAppDataPath.value; |
---|
206 | } |
---|
207 | //</editor-fold> |
---|
208 | |
---|
209 | |
---|
210 | //<editor-fold defaultstate="collapsed" desc=" InternetAddresses "> |
---|
211 | private class InternetAddressesCache { |
---|
212 | final InetAddress[] value; |
---|
213 | { |
---|
214 | Set<InetAddress> addresses = new LinkedHashSet<InetAddress>(); |
---|
215 | try { |
---|
216 | addresses.add(InetAddress.getLocalHost()); |
---|
217 | } |
---|
218 | catch(UnknownHostException ex) { |
---|
219 | ex.printStackTrace(); |
---|
220 | } |
---|
221 | try { |
---|
222 | Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces(); |
---|
223 | while(eni.hasMoreElements()) { |
---|
224 | NetworkInterface ni = eni.nextElement(); |
---|
225 | Enumeration<InetAddress> eia = ni.getInetAddresses(); |
---|
226 | while(eia.hasMoreElements()) { |
---|
227 | addresses.add(eia.nextElement()); |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | catch(SocketException ex) { |
---|
232 | ex.printStackTrace(); |
---|
233 | } |
---|
234 | value = addresses.toArray(new InetAddress[addresses.size()]); |
---|
235 | } |
---|
236 | } |
---|
237 | private InternetAddressesCache internetAddresses; |
---|
238 | public InetAddress[] getInternetAddresses() { |
---|
239 | if(internetAddresses == null) { |
---|
240 | internetAddresses = new InternetAddressesCache(); |
---|
241 | } |
---|
242 | return internetAddresses.value; |
---|
243 | } |
---|
244 | //</editor-fold> |
---|
245 | |
---|
246 | } |
---|