001 /*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2013
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas
009 *
010 * All Rights Reserved
011 *
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.
014 *
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 *
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023 * GNU Lesser Public License for more details.
024 *
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program. If not, see http://www.gnu.org/licenses.
027 */
028 package edu.wisc.ssec.mcidasv.startupmanager;
029
030 import java.util.Properties;
031
032 import edu.wisc.ssec.mcidasv.Constants;
033 import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster;
034
035 public enum Platform {
036 /** Instance of unix-specific platform information. */
037 UNIXLIKE("/", "runMcV.prefs", "\n"),
038
039 /** Instance of windows-specific platform information. */
040 WINDOWS("\\", "runMcV-Prefs.bat", "\r\n");
041
042 /** Path to the user's {@literal "userpath"} directory. */
043 private String userDirectory;
044
045 /** The path to the user's copy of the startup preferences. */
046 private String userPrefs;
047
048 /** Path to the preference file that ships with McIDAS-V. */
049 private final String defaultPrefs;
050
051 /** Holds the platform's representation of a new line. */
052 private final String newLine;
053
054 /** Directory delimiter for the current platform. */
055 private final String pathSeparator;
056
057 /** Total amount of memory avilable in megabytes */
058 private int availableMemory = 0;
059
060 /**
061 * Initializes the platform-specific paths to the different files
062 * required by the startup manager.
063 *
064 * @param pathSeparator Character that delimits directories. On Windows
065 * this will be {@literal \\}, while on Unix-style systems, it will be
066 * {@literal /}.
067 * @param defaultPrefs The path to the preferences file that ships with
068 * McIDAS-V.
069 * @param newLine Character(s!) that represent a new line for this
070 * platform.
071 *
072 * @throws NullPointerException if either {@code pathSeparator} or
073 * {@code defaultPrefs} are null.
074 *
075 * @throws IllegalArgumentException if either {@code pathSeparator} or
076 * {@code defaultPrefs} are an empty string.
077 */
078 Platform(final String pathSeparator, final String defaultPrefs,
079 final String newLine)
080 {
081 if (pathSeparator == null || defaultPrefs == null) {
082 throw new NullPointerException("");
083 }
084 if (pathSeparator.isEmpty() || defaultPrefs.isEmpty()) {
085 throw new IllegalArgumentException("");
086 }
087
088 String osName = System.getProperty("os.name");
089 if (osName.startsWith("Mac OS X")) {
090 this.userDirectory = System.getProperty("user.home") + pathSeparator + "Documents" + pathSeparator + Constants.USER_DIRECTORY_NAME;
091 } else {
092 this.userDirectory = System.getProperty("user.home") + pathSeparator + Constants.USER_DIRECTORY_NAME;
093 }
094 this.userPrefs = userDirectory + pathSeparator + defaultPrefs;
095 this.defaultPrefs = defaultPrefs;
096 this.newLine = newLine;
097 this.pathSeparator = pathSeparator;
098 }
099
100 /**
101 * Sets the path to the user's userpath directory explicitly.
102 *
103 * @param path New path. Cannot be {@code null}.
104 */
105 public void setUserDirectory(final String path) {
106 userDirectory = path;
107 userPrefs = userDirectory + pathSeparator + defaultPrefs;
108 }
109
110 /**
111 * Sets the amount of available memory. {@code megabytes} must be
112 * greater than or equal to zero.
113 *
114 * @param megabytes Memory in megabytes
115 *
116 * @throws NullPointerException if {@code megabytes} is {@code null}.
117 * @throws IllegalArgumentException if {@code megabytes} is less than
118 * zero or does not represent an integer.
119 *
120 * @see StartupManager#getArgs
121 */
122 public void setAvailableMemory(String megabytes) {
123 if (megabytes == null) {
124 throw new NullPointerException("Available memory cannot be null");
125 }
126 if (megabytes.isEmpty()) {
127 megabytes = "0";
128 }
129
130 try {
131 int test = Integer.parseInt(megabytes);
132 if (test < 0) {
133 throw new IllegalArgumentException("Available memory must be a non-negative integer, not \""+megabytes+"\"");
134 }
135 availableMemory = test;
136 } catch (NumberFormatException e) {
137 throw new IllegalArgumentException("Could not convert \""+megabytes+"\" to a non-negative integer");
138 }
139 }
140
141 /**
142 * Returns the path to the user's {@literal "userpath"} directory.
143 *
144 * @return Path to the user's directory.
145 */
146 public String getUserDirectory() {
147 return userDirectory;
148 }
149
150 /**
151 * Returns the path to a file in the user's {@literal "userpath"} directory.
152 *
153 * @param filename Filename within the {@code userpath}. Cannot be
154 * {@code null}, but does not need to be a filename that already exists
155 * within the {@code userpath}.
156 *
157 * @return Path to a file in the user's directory. <b>Note:</b> the file
158 * may not yet exist.
159 */
160 public String getUserFile(String filename) {
161 return getUserDirectory() + pathSeparator + filename;
162 }
163
164 public String getUserBundles() {
165 return getUserDirectory() + pathSeparator + "bundles";
166 }
167
168 /**
169 * Returns the amount of available memory in megabytes
170 *
171 * @return Available memory in megabytes
172 */
173 public int getAvailableMemory() {
174 return availableMemory;
175 }
176
177 /**
178 * Returns the path of user's copy of the startup preferences.
179 *
180 * @return Path to the user's startup preferences file.
181 */
182 public String getUserPrefs() {
183 return userPrefs;
184 }
185
186 /**
187 * Returns the path of the startup preferences included in the McIDAS-V
188 * distribution. Mostly useful for normalizing the user
189 * directory.
190 *
191 * @return Path to the default startup preferences.
192 *
193 * @see OptionMaster#normalizeUserDirectory()
194 */
195 public String getDefaultPrefs() {
196 return defaultPrefs;
197 }
198
199 /**
200 * Returns the platform's notion of a new line.
201 *
202 * @return Unix-like: {@literal \n}; Windows: {@literal \r\n}.
203 */
204 public String getNewLine() {
205 return newLine;
206 }
207
208 /**
209 * Returns a brief summary of the platform specific file locations.
210 * Please note that the format and contents are subject to change.
211 *
212 * @return String that looks like
213 * {@code [Platform@HASHCODE: defaultPrefs=..., userDirectory=...,
214 * userPrefs=...]}
215 */
216 @Override public String toString() {
217 return String.format(
218 "[Platform@%x: defaultPrefs=%s, userDirectory=%s, userPrefs=%s]",
219 hashCode(), defaultPrefs, userDirectory, userPrefs);
220 }
221 }