1 21 22 package com.izforge.izpack.installer; 23 24 import java.io.File ; 25 import java.io.InputStream ; 26 import java.io.ObjectInputStream ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Properties ; 33 import java.net.InetAddress ; 34 35 import com.izforge.izpack.CustomData; 36 import com.izforge.izpack.Info; 37 import com.izforge.izpack.Pack; 38 import com.izforge.izpack.util.Debug; 39 import com.izforge.izpack.util.IoHelper; 40 import com.izforge.izpack.util.OsConstraint; 41 import com.izforge.izpack.util.OsVersion; 42 import com.izforge.izpack.util.VariableSubstitutor; 43 44 51 public class InstallerBase 52 { 53 54 58 protected static final String LANG_FILE_NAME = "CustomLangpack.xml"; 59 60 70 public void loadInstallData(AutomatedInstallData installdata) throws Exception 71 { 72 InputStream in; 74 ObjectInputStream objIn; 75 int size; 76 int i; 77 78 Properties variables = null; 80 in = InstallerBase.class.getResourceAsStream("/vars"); 81 if (null != in) 82 { 83 objIn = new ObjectInputStream (in); 84 variables = (Properties ) objIn.readObject(); 85 objIn.close(); 86 } 87 88 in = InstallerBase.class.getResourceAsStream("/info"); 90 objIn = new ObjectInputStream (in); 91 Info inf = (Info) objIn.readObject(); 92 objIn.close(); 93 94 installdata.setVariable(ScriptParser.APP_NAME, inf.getAppName()); 96 if (inf.getAppURL() != null) 97 installdata.setVariable(ScriptParser.APP_URL, inf.getAppURL()); 98 installdata.setVariable(ScriptParser.APP_VER, inf.getAppVersion()); 99 100 in = InstallerBase.class.getResourceAsStream("/panelsOrder"); 102 objIn = new ObjectInputStream (in); 103 List panelsOrder = (List ) objIn.readObject(); 104 objIn.close(); 105 106 in = InstallerBase.class.getResourceAsStream("/packs.info"); 108 objIn = new ObjectInputStream (in); 109 size = objIn.readInt(); 110 ArrayList availablePacks = new ArrayList (); 111 ArrayList allPacks = new ArrayList (); 112 for (i = 0; i < size; i++) 113 { 114 Pack pk = (Pack) objIn.readObject(); 115 allPacks.add(pk); 116 if (OsConstraint.oneMatchesCurrentSystem(pk.osConstraints)) availablePacks.add(pk); 117 } 118 objIn.close(); 119 120 String dir; 122 String installPath; 123 if (OsVersion.IS_WINDOWS) 124 { 125 dir = buildWindowsDefaultPath(); 126 } 127 else if (OsVersion.IS_OSX) 128 { 129 dir = "/Applications"; 130 } 131 else 132 { 133 if (new File ("/usr/local/").canWrite()) 134 { 135 dir = "/usr/local"; 136 } 137 else 138 { 139 dir = System.getProperty("user.home"); 140 } 141 } 142 143 String hostname; 145 String IPAddress; 146 147 try { 148 InetAddress addr = InetAddress.getLocalHost(); 149 150 IPAddress = addr.getHostAddress(); 152 153 hostname = addr.getHostName(); 155 } catch (Exception e) { 156 hostname = ""; 157 IPAddress = ""; 158 } 159 160 161 162 installdata.setVariable("APPLICATIONS_DEFAULT_ROOT", dir); 163 dir += File.separator; 164 installdata.setVariable(ScriptParser.JAVA_HOME, System.getProperty("java.home")); 165 installdata.setVariable(ScriptParser.CLASS_PATH, System.getProperty("java.class.path")); 166 installdata.setVariable(ScriptParser.USER_HOME, System.getProperty("user.home")); 167 installdata.setVariable(ScriptParser.USER_NAME, System.getProperty("user.name")); 168 installdata.setVariable(ScriptParser.IP_ADDRESS, IPAddress); 169 installdata.setVariable(ScriptParser.HOST_NAME, hostname); 170 installdata.setVariable(ScriptParser.FILE_SEPARATOR, File.separator); 171 172 Enumeration e = System.getProperties().keys(); 173 while (e.hasMoreElements()) 174 { 175 String varName = (String ) e.nextElement(); 176 String varValue = System.getProperty(varName); 177 if (varValue != null) 178 { 179 varName = varName.replace('.', '_'); 180 installdata.setVariable("SYSTEM_" + varName, varValue); 181 } 182 } 183 184 if (null != variables) 185 { 186 Enumeration enumeration = variables.keys(); 187 String varName; 188 String varValue; 189 while (enumeration.hasMoreElements()) 190 { 191 varName = (String ) enumeration.nextElement(); 192 varValue = variables.getProperty(varName); 193 installdata.setVariable(varName, varValue); 194 } 195 } 196 197 installdata.info = inf; 198 installdata.panelsOrder = panelsOrder; 199 installdata.availablePacks = availablePacks; 200 installdata.allPacks = allPacks; 201 202 Iterator pack_it = availablePacks.iterator(); 204 while (pack_it.hasNext()) 205 { 206 Pack pack = (Pack) pack_it.next(); 207 if (pack.preselected) installdata.selectedPacks.add(pack); 208 } 209 installPath = dir + inf.getAppName(); 211 if (inf.getInstallationSubPath() != null) 212 { installPath = IoHelper.translatePath(dir + inf.getInstallationSubPath(), 214 new VariableSubstitutor(installdata.getVariables())); 215 } 216 installdata.setInstallPath(installPath); 217 loadCustomData(installdata); 219 220 } 221 222 228 protected void addCustomLangpack(AutomatedInstallData idata) 229 { 230 try 232 { 233 idata.langpack.add(ResourceManager.getInstance().getInputStream(LANG_FILE_NAME)); 234 } 235 catch (Throwable exception) 236 { 237 Debug.trace("No custom langpack available."); 238 return; 239 } 240 Debug.trace("Custom langpack for " + idata.localeISO3 + " available."); 241 } 242 243 249 private String buildWindowsDefaultPath() 250 { 251 try{ 252 String prgFilesPath = IoHelper.getenv("ProgramFiles"); 254 if (prgFilesPath!=null && prgFilesPath.length()>0){ 255 return prgFilesPath; 256 }else{ 257 return buildWindowsDefaultPathFromProps(); 258 } 259 }catch(Exception x){ 260 x.printStackTrace(); 261 return buildWindowsDefaultPathFromProps(); 262 } 263 } 264 269 private String buildWindowsDefaultPathFromProps() 270 { 271 StringBuffer dpath = new StringBuffer (""); 272 try 273 { 274 Properties props = new Properties (); 276 props 277 .load(InstallerBase.class 278 .getResourceAsStream("/com/izforge/izpack/installer/win32-defaultpaths.properties")); 279 280 String drive = System.getProperty("user.home"); 282 if (drive.length() > 3) drive = drive.substring(0, 3); 283 284 dpath.append(drive); 286 287 if (drive.length() == 2) dpath.append("\\"); 291 292 String language = Locale.getDefault().getLanguage(); 293 String country = Locale.getDefault().getCountry(); 294 String language_country = language + "_" + country; 295 296 if (null != props.getProperty(language_country)) 298 { 299 dpath.append(props.getProperty(language_country)); 300 } 301 else if (null != props.getProperty(language)) 302 { 303 dpath.append(props.getProperty(language)); 304 } 305 else 306 { 307 dpath.append(props.getProperty(Locale.ENGLISH.getLanguage())); 308 } 309 } 310 catch (Exception err) 311 { 312 dpath = new StringBuffer ("C:\\Program Files"); 313 } 314 315 return dpath.toString(); 316 } 317 318 324 private void loadCustomData(AutomatedInstallData installdata) throws Exception 325 { 326 InputStream in; 328 ObjectInputStream objIn; 329 int i; 330 String [] streamNames = AutomatedInstallData.CUSTOM_ACTION_TYPES; 332 List [] out = new List [streamNames.length]; 333 for (i = 0; i < streamNames.length; ++i) 334 out[i] = new ArrayList (); 335 in = InstallerBase.class.getResourceAsStream("/customData"); 336 if (in != null) 337 { 338 objIn = new ObjectInputStream (in); 339 Object listeners = objIn.readObject(); 340 objIn.close(); 341 Iterator keys = ((List ) listeners).iterator(); 342 while (keys != null && keys.hasNext()) 343 { 344 CustomData ca = (CustomData) keys.next(); 345 346 if (ca.osConstraints != null 347 && !OsConstraint.oneMatchesCurrentSystem(ca.osConstraints)) 348 { continue; 351 } 352 switch (ca.type) 353 { 354 case CustomData.INSTALLER_LISTENER: 355 Class clazz = Class.forName(ca.listenerName); 356 if (clazz == null) 357 throw new InstallerException("Custom action " + ca.listenerName 358 + " not bound!"); 359 out[ca.type].add(clazz.newInstance()); 360 break; 361 case CustomData.UNINSTALLER_LISTENER: 362 case CustomData.UNINSTALLER_JAR: 363 out[ca.type].add(ca); 364 break; 365 case CustomData.UNINSTALLER_LIB: 366 out[ca.type].add(ca.contents); 367 break; 368 } 369 370 } 371 for (i = 0; i < streamNames.length; ++i) 373 installdata.customData.put(streamNames[i], out[i]); 374 } 375 377 } 378 } 379 | Popular Tags |