/** * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /******************************************************************************\ * * This product was developed by * * SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), * * a government company established under Brazilian law (5.615/70), * at Department of Development of Porto Alegre. * \******************************************************************************/ package serpro.mailarchiver.util; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.Set; import com.sun.jna.Native; import com.sun.jna.platform.win32.Shell32; import com.sun.jna.platform.win32.ShlObj; import com.sun.jna.platform.win32.W32Errors; import com.sun.jna.platform.win32.WinDef; import com.sun.jna.platform.win32.WinNT.HRESULT; public class Environment { private Environment() {} private static class EnvironmentHolder { public static final Environment INSTANCE = new Environment(); } public static Environment getInstance() { return EnvironmentHolder.INSTANCE; } private final long timestamp = System.currentTimeMillis(); public long getExecTimestamp() { return timestamp; } private String getKnownFolder(int folder) { char[] path = new char[WinDef.MAX_PATH]; HRESULT hr = Shell32.INSTANCE.SHGetFolderPath(null, folder, null, ShlObj.SHGFP_TYPE_CURRENT, path); if(hr.equals(W32Errors.S_OK)) { return Native.toString(path); } else { System.err.printf("getKnownFolderPath {csidl=%s} error: %d", folder, hr.intValue()); } return null; } // private class OperatingSystemCache { final OperatingSystem value; { String name = System.getProperty("os.name"); value = (name.equals("Linux")) ? OperatingSystem.Linux : (name.equals("Mac OS X")) ? OperatingSystem.MacOSX : (name.equals("Windows XP")) ? OperatingSystem.WindowsXP : (name.equals("Windows Vista")) ? OperatingSystem.WindowsVista : (name.equals("Windows 7")) ? OperatingSystem.Windows7 : null; } } private OperatingSystemCache operatingSystem; public OperatingSystem getOperatingSystem() { if(operatingSystem == null) { operatingSystem = new OperatingSystemCache(); } return operatingSystem.value; } // // private class HomePathCache { final Path value; { String home = null; switch(getOperatingSystem()) { case WindowsXP: case WindowsVista: case Windows7: home = getKnownFolder(ShlObj.CSIDL_PROFILE); break; case Linux: case MacOSX: home = System.getenv("HOME"); break; } if((home != null) && (!home.isEmpty())) { value = Paths.get(home); } else { value = null; } } } private HomePathCache homePath; public Path getHomePath() { if(homePath == null) { homePath = new HomePathCache(); System.out.println("homePath: " + homePath.value); } return homePath.value; } // // private class AppDataPathCache { final Path value; { String appData = null; switch(getOperatingSystem()) { case WindowsXP: case WindowsVista: case Windows7: appData = getKnownFolder(ShlObj.CSIDL_APPDATA); break; } if((appData != null) && (!appData.isEmpty())) { value = Paths.get(appData); } else { value = null; } } } private AppDataPathCache appDataPath; public Path getAppDataPath() { if(appDataPath == null) { appDataPath = new AppDataPathCache(); System.out.println("appDataPath: " + appDataPath.value); } return appDataPath.value; } // // private class LocalAppDataPathCache { final Path value; { String localAppData = null; switch(getOperatingSystem()) { case WindowsXP: case WindowsVista: case Windows7: localAppData = getKnownFolder(ShlObj.CSIDL_LOCAL_APPDATA); break; } if((localAppData != null) && (!localAppData.isEmpty())) { value = Paths.get(localAppData); } else { value = null; } } } private LocalAppDataPathCache localAppDataPath; public Path getLocalAppDataPath() { if(localAppDataPath == null) { localAppDataPath = new LocalAppDataPathCache(); System.out.println("localAppDataPath: " + localAppDataPath.value); } return localAppDataPath.value; } // // private class InternetAddressesCache { final InetAddress[] value; { Set addresses = new LinkedHashSet(); try { addresses.add(InetAddress.getLocalHost()); } catch(UnknownHostException ex) { ex.printStackTrace(); } try { Enumeration eni = NetworkInterface.getNetworkInterfaces(); while(eni.hasMoreElements()) { NetworkInterface ni = eni.nextElement(); Enumeration eia = ni.getInetAddresses(); while(eia.hasMoreElements()) { addresses.add(eia.nextElement()); } } } catch(SocketException ex) { ex.printStackTrace(); } value = addresses.toArray(new InetAddress[addresses.size()]); } } private InternetAddressesCache internetAddresses; public InetAddress[] getInternetAddresses() { if(internetAddresses == null) { internetAddresses = new InternetAddressesCache(); } return internetAddresses.value; } // }