PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/test/jtreg/com/sun/javatest/cof/COFEnvironment.java

https://bitbucket.org/xiaoqiangnk/icedtea6
Java | 584 lines | 235 code | 64 blank | 285 comment | 18 complexity | 8d992565775e51e8db8facbac1000078 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * $Id$
  3. *
  4. * Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved.
  5. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6. *
  7. * This code is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 only, as
  9. * published by the Free Software Foundation. Sun designates this
  10. * particular file as subject to the "Classpath" exception as provided
  11. * by Sun in the LICENSE file that accompanied this code.
  12. *
  13. * This code is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * version 2 for more details (a copy is included in the LICENSE file that
  17. * accompanied this code).
  18. *
  19. * You should have received a copy of the GNU General Public License version
  20. * 2 along with this work; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  24. * CA 95054 USA or visit www.sun.com if you need additional information or
  25. * have any questions.
  26. */
  27. package com.sun.javatest.cof;
  28. import java.io.InputStreamReader;
  29. import java.io.RandomAccessFile;
  30. import java.net.InetAddress;
  31. import java.net.UnknownHostException;
  32. import java.util.ArrayList;
  33. import java.util.LinkedHashMap;
  34. import java.util.List;
  35. import java.util.StringTokenizer;
  36. import java.util.TimeZone;
  37. import com.sun.javatest.util.I18NResourceBundle;
  38. /* temp */public/* temp */class COFEnvironment extends COFItem {
  39. static String[] propOrder = { "machine", "os", "jdk", "systemLocale",
  40. "userLocale", "encoding", "timezone", "bits", "displaydepth",
  41. "description", "sw" };
  42. static String[] propTags = { "machine", "os", "jdk", "system-locale",
  43. "user-locale", "encoding", "timezone", "bits", "displaydepth",
  44. "description", "sw" };
  45. static LinkedHashMap xmlAttributes;
  46. static LinkedHashMap xmlElements;
  47. static String xmlTagName;
  48. private static I18NResourceBundle i18n = I18NResourceBundle
  49. .getBundleForClass(Main.class);
  50. static {
  51. xmlElements = new LinkedHashMap();
  52. for (int i = 0; i < propOrder.length; i++) {
  53. xmlElements.put(propOrder[i], propTags[i]);
  54. }
  55. xmlAttributes = new LinkedHashMap();
  56. xmlAttributes.put("id", "id");
  57. xmlTagName = "environment";
  58. }
  59. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
  60. protected Integer bits;
  61. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
  62. protected String description;
  63. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
  64. protected Integer displaydepth;
  65. private String domainName;
  66. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
  67. protected String encoding;
  68. private String hostName;
  69. // @XmlAttribute(required = true)
  70. protected String id = "env:0";
  71. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
  72. protected String jdk;
  73. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema", required = true)
  74. protected String machine;
  75. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema", required = true)
  76. protected COFOS os;
  77. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema", required = true)
  78. protected List/*<SWEntity>*/sw;
  79. // @XmlElement(name = "system-locale", namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
  80. protected String systemLocale;
  81. // @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
  82. protected String timezone;
  83. // @XmlElement(name = "system-locale", namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
  84. protected String userLocale;
  85. COFEnvironment(COFData data) {
  86. initDefaultHostInfo();
  87. initDefaultOSInfo();
  88. hostName = data.get("environment.host", hostName);
  89. domainName = data.get("environment.domain", domainName);
  90. setMachine(data.get("environment.machine", hostName + "." + domainName));
  91. os.setName(data.get("environment.os.name", os.getName()));
  92. os.setVersion(data.get("environment.os.version", os.getVersion()));
  93. os.setArch(data.get("environment.os.arch", os.getArch()));
  94. setJdk(data.get("environment.jdk", jdk));
  95. setUserLocale(data.get("environment.user-locale", java.util.Locale.getDefault().toString()));
  96. setSystemLocale(data.get("environment.system-locale", data
  97. .get("LOCALE")));
  98. setEncoding(data.get("environment.encoding", (new InputStreamReader(
  99. System.in)).getEncoding()));
  100. setTimezone(data.get("environment.timezone", TimeZone.getDefault()
  101. .getID()));
  102. setBits(data.get("environment.bits", null) == null ? null
  103. : new Integer(data.get("environment.bits")));
  104. setDisplaydepth(data.get("environment.displaydepth", null) == null ? null
  105. : new Integer(data.get("environment.displaydepth")));
  106. setDescription(data.get("environment.description"));
  107. }
  108. /**
  109. * Gets the value of the bits property.
  110. *
  111. * @return
  112. * possible object is
  113. * {@link Integer }
  114. *
  115. */
  116. public Integer getBits() {
  117. return bits;
  118. }
  119. private String getCalderaLinuxVersionInfo() {
  120. String res = "N/A";
  121. RandomAccessFile raf = null;
  122. try {
  123. raf = new RandomAccessFile("/etc/issue", "r");
  124. String line;
  125. while ((line = raf.readLine()) != null) {
  126. StringTokenizer st = new StringTokenizer(line, " ");
  127. if (st.countTokens() >= 2) {
  128. if (st.nextToken().toUpperCase().equals("VERSION")) {
  129. res = st.nextToken();
  130. break;
  131. }
  132. }
  133. }
  134. } catch (Exception e) {
  135. // System.err.println("Something might be wrong with Caldera");
  136. // cat.warn("The version number of Caldera cannot be retrieved");
  137. } finally {
  138. if (raf != null) {
  139. try {
  140. raf.close();
  141. } catch (Exception e) {
  142. }
  143. }
  144. }
  145. return res;
  146. }
  147. /**
  148. * Gets the value of the description property.
  149. *
  150. * @return
  151. * possible object is
  152. * {@link String }
  153. *
  154. */
  155. public String getDescription() {
  156. return description;
  157. }
  158. /**
  159. * Gets the value of the displaydepth property.
  160. *
  161. * @return
  162. * possible object is
  163. * {@link Integer }
  164. *
  165. */
  166. public Integer getDisplaydepth() {
  167. return displaydepth;
  168. }
  169. /**
  170. * Gets the value of the encoding property.
  171. *
  172. * @return
  173. * possible object is
  174. * {@link String }
  175. *
  176. */
  177. public String getEncoding() {
  178. return encoding;
  179. }
  180. private String getGenericLinuxVersionInfo(String fileName) {
  181. String res = "N/A";
  182. RandomAccessFile raf = null;
  183. try {
  184. raf = new RandomAccessFile(fileName, "r");
  185. StringTokenizer st = new StringTokenizer(raf.readLine(), " ");
  186. while (st.hasMoreElements()) {
  187. String ele = st.nextToken();
  188. if (Character.isDigit(ele.charAt(0))) {
  189. res = ele;
  190. break;
  191. }
  192. }
  193. } catch (Exception e) {
  194. // System.err.println("Something might be wrong with Linux");
  195. } finally {
  196. if (raf != null) {
  197. try {
  198. raf.close();
  199. } catch (Exception e) {
  200. }
  201. }
  202. }
  203. return res;
  204. }
  205. /**
  206. * Gets the value of the id property.
  207. *
  208. * @return
  209. * possible object is
  210. * {@link String }
  211. *
  212. */
  213. public String getId() {
  214. return id;
  215. }
  216. LinkedHashMap getItemAttributes() {
  217. return xmlAttributes;
  218. }
  219. LinkedHashMap getItemElements() {
  220. return xmlElements;
  221. }
  222. String getItemTagName() {
  223. return xmlTagName;
  224. }
  225. /**
  226. * Gets the value of the jdk property.
  227. *
  228. * @return
  229. * possible object is
  230. * {@link String }
  231. *
  232. */
  233. public String getJdk() {
  234. return jdk;
  235. }
  236. /**
  237. * Gets the value of the machine property.
  238. *
  239. * @return
  240. * possible object is
  241. * {@link String }
  242. *
  243. */
  244. public String getMachine() {
  245. return machine;
  246. }
  247. /**
  248. * Gets the value of the os property.
  249. *
  250. * @return
  251. * possible object is
  252. * {@link OS }
  253. *
  254. */
  255. public COFOS getOs() {
  256. return os;
  257. }
  258. String[] getPropOrder() {
  259. return propOrder;
  260. }
  261. /**
  262. * Gets the value of the sw property.
  263. *
  264. * <p>
  265. * This accessor method returns a reference to the live list,
  266. * not a snapshot. Therefore any modification you make to the
  267. * returned list will be present inside the JAXB object.
  268. * This is why there is not a <CODE>set</CODE> method for the sw property.
  269. *
  270. * <p>
  271. * For example, to add a new item, do as follows:
  272. * <pre>
  273. * getSw().add(newItem);
  274. * </pre>
  275. *
  276. *
  277. * <p>
  278. * Objects of the following type(s) are allowed in the list
  279. * {@link SWEntity }
  280. *
  281. *
  282. */
  283. public List/*<SWEntity>*/getSw() {
  284. if (sw == null) {
  285. sw = new ArrayList/*<SWEntity>*/();
  286. }
  287. return this.sw;
  288. }
  289. /**
  290. * Gets the value of the systemLocale property.
  291. *
  292. * @return
  293. * possible object is
  294. * {@link String }
  295. *
  296. */
  297. public String getSystemLocale() {
  298. return systemLocale;
  299. }
  300. String getTagName() {
  301. return itemTagName;
  302. }
  303. String[] getTags() {
  304. return propTags;
  305. }
  306. /**
  307. * Gets the value of the timezone property.
  308. *
  309. * @return
  310. * possible object is
  311. * {@link String }
  312. *
  313. */
  314. public String getTimezone() {
  315. return timezone;
  316. }
  317. /**
  318. * Gets the value of the userLocale property.
  319. *
  320. * @return
  321. * possible object is
  322. * {@link String }
  323. *
  324. */
  325. public String getUserLocale() {
  326. return userLocale;
  327. }
  328. private void initDefaultHostInfo() {
  329. domainName = "unknown";
  330. try {
  331. hostName = InetAddress.getLocalHost().getHostName();
  332. } catch (UnknownHostException e) {
  333. hostName = "unknown";
  334. }
  335. }
  336. private void initDefaultOSInfo() {
  337. os = new COFOS();
  338. os.setName(System.getProperty("os.name"));
  339. os.setVersion(System.getProperty("os.version"));
  340. os.setArch(System.getProperty("os.arch"));
  341. // standardize results
  342. /* if (osName.equals("sunos") || osName.equals("solaris")) {
  343. osName = "solaris";
  344. if (osVersion.equals("5.6"))
  345. osVersion = "2.6";
  346. else if (osVersion.equals("5.7"))
  347. osVersion = "2.7";
  348. else if (osVersion.equals("5.8"))
  349. osVersion = "8";
  350. else if (osVersion.equals("5.9"))
  351. osVersion = "9";
  352. } else if (osName.startsWith("windows")) {
  353. osName = "windows";
  354. if (osName.indexOf("95") != -1)
  355. osVersion = "95";
  356. else if (osName.indexOf("98") != -1)
  357. osVersion = "98";
  358. else if (osName.indexOf("2000") != -1)
  359. osVersion = "2000";
  360. else if (osName.indexOf("me") != -1)
  361. osVersion = "me";
  362. else if (osName.indexOf("nt") != -1)
  363. osVersion = "nt_4.0";
  364. else if (osName.indexOf("xp") != -1)
  365. osVersion = "xp";
  366. } else if (osName.equals("linux")) {
  367. if ((new File("/etc/turbolinux-release")).exists()) {
  368. osName = "turbo_linux";
  369. osVersion = getGenericLinuxVersionInfo("/etc/turbolinux-release");
  370. } else if ((new File("/etc/SuSE-release")).exists()) {
  371. osName = "suse_linux";
  372. osVersion = getGenericLinuxVersionInfo("/etc/SuSE-release");
  373. } else if ((new File("/etc/mandrake-release")).exists()) {
  374. osName = "mandrake_linux";
  375. osVersion = getGenericLinuxVersionInfo("/etc/mandrake-release");
  376. } else if ((new File("/etc/redhat-release")).exists()) {
  377. osName = "redhat_linux";
  378. osVersion = getGenericLinuxVersionInfo("/etc/redhat-release");
  379. } else if ((new File("/etc/lst.cnf")).exists()) {
  380. osName = "caldera_linux";
  381. osVersion = getCalderaLinuxVersionInfo();
  382. } else {
  383. osName = "unknown_linux";
  384. osVersion = "N/A";
  385. }
  386. }
  387. */}
  388. /**
  389. * Sets the value of the bits property.
  390. *
  391. * @param value
  392. * allowed object is
  393. * {@link Integer }
  394. *
  395. */
  396. public void setBits(Integer value) {
  397. this.bits = value;
  398. }
  399. /**
  400. * Sets the value of the description property.
  401. *
  402. * @param value
  403. * allowed object is
  404. * {@link String }
  405. *
  406. */
  407. public void setDescription(String value) {
  408. this.description = value;
  409. }
  410. /**
  411. * Sets the value of the displaydepth property.
  412. *
  413. * @param value
  414. * allowed object is
  415. * {@link Integer }
  416. *
  417. */
  418. public void setDisplaydepth(Integer value) {
  419. this.displaydepth = value;
  420. }
  421. /**
  422. * Sets the value of the encoding property.
  423. *
  424. * @param value
  425. * allowed object is
  426. * {@link String }
  427. *
  428. */
  429. public void setEncoding(String value) {
  430. this.encoding = value;
  431. }
  432. /**
  433. * Sets the value of the id property.
  434. *
  435. * @param value
  436. * allowed object is
  437. * {@link String }
  438. *
  439. */
  440. public void setId(String value) {
  441. this.id = value;
  442. }
  443. /**
  444. * Sets the value of the jdk property.
  445. *
  446. * @param value
  447. * allowed object is
  448. * {@link String }
  449. *
  450. */
  451. public void setJdk(String value) {
  452. this.jdk = value;
  453. }
  454. /**
  455. * Sets the value of the machine property.
  456. *
  457. * @param value
  458. * allowed object is
  459. * {@link String }
  460. *
  461. */
  462. public void setMachine(String value) {
  463. if (value.endsWith(".unknown"))
  464. try {
  465. machine = InetAddress.getLocalHost().getCanonicalHostName();
  466. if (machine.indexOf(".") == -1)
  467. System.err.println(i18n.getString("environment.badMachineName", machine));
  468. return;
  469. } catch (UnknownHostException e) {
  470. System.err.println(i18n.getString("environment.cantGetLocalhostName", e.getMessage()));
  471. value = value.substring(0, value.indexOf(".unknown")-1);
  472. System.err.println(i18n.getString("environment.badMachineName", value));
  473. }
  474. this.machine = value;
  475. }
  476. /**
  477. * Sets the value of the os property.
  478. *
  479. * @param value
  480. * allowed object is
  481. * {@link OS }
  482. *
  483. */
  484. public void setOs(COFOS value) {
  485. this.os = value;
  486. }
  487. /**
  488. * Sets the value of the systemLocale property.
  489. *
  490. * @param value
  491. * allowed object is
  492. * {@link String }
  493. *
  494. */
  495. public void setSystemLocale(String value) {
  496. this.systemLocale = value;
  497. }
  498. /**
  499. * Sets the value of the timezone property.
  500. *
  501. * @param value
  502. * allowed object is
  503. * {@link String }
  504. *
  505. */
  506. public void setTimezone(String value) {
  507. this.timezone = value;
  508. }
  509. /**
  510. * Sets the value of the userLocale property.
  511. *
  512. * @param value
  513. * allowed object is
  514. * {@link String }
  515. *
  516. */
  517. public void setUserLocale(String value) {
  518. this.userLocale = value;
  519. }
  520. }