       1 /*
       2  * $Id$
       3  *
       4  * Copyright (c) 2002, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
      10  * particular file as subject to the "Classpath" exception as provided
      11  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      24  * or visit www.oracle.com if you need additional information or have any
      25  * questions.
      26  */
      27 package com.sun.javatest.cof;
      28
      29 import java.io.InputStreamReader;
      30 import java.io.RandomAccessFile;
      31 import java.net.InetAddress;
      32 import java.net.UnknownHostException;
      33 import java.util.ArrayList;
      34 import java.util.LinkedHashMap;
      35 import java.util.List;
      36 import java.util.StringTokenizer;
      37 import java.util.TimeZone;
      38
      39 import com.sun.javatest.util.I18NResourceBundle;
      40
      41 /* temp */public/* temp */class COFEnvironment extends COFItem {
      42         static String[] propOrder = { "machine", "os", "jdk", "systemLocale",
      43                         "userLocale", "encoding", "timezone", "bits", "displaydepth",
      44                         "description", "sw" };
      45
      46         static String[] propTags = { "machine", "os", "jdk", "system-locale",
      47                         "user-locale", "encoding", "timezone", "bits", "displaydepth",
      48                         "description", "sw" };
      49
      50         static LinkedHashMap xmlAttributes;
      51
      52         static LinkedHashMap xmlElements;
      53
      54         static String xmlTagName;
      55         private static I18NResourceBundle i18n = I18NResourceBundle
      56         .getBundleForClass(Main.class);
      57
      58         static {
      59                 xmlElements = new LinkedHashMap();
      60                 for (int i = 0; i < propOrder.length; i++) {
      61                         xmlElements.put(propOrder[i], propTags[i]);
      62                 }
      63                 xmlAttributes = new LinkedHashMap();
      64                 xmlAttributes.put("id", "id");
      65                 xmlTagName = "environment";
      66         }
      67
      68         //    @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
      69         protected Integer bits;
      70
      71         //    @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
      72         protected String description;
      73
      74         //    @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
      75         protected Integer displaydepth;
      76
      77         private String domainName;
      78
      79         //    @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
      80         protected String encoding;
      81
      82         private String hostName;
      83
      84         //    @XmlAttribute(required = true)
      85         protected String id = "env:0";
      86
      87         //    @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
      88         protected String jdk;
      89
      90         //  @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema", required = true)
      91         protected String machine;
      92
      93         //    @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema", required = true)
      94         protected COFOS os;
      95
      96         //    @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema", required = true)
      97         protected List/*<SWEntity>*/sw;
      98
      99         //    @XmlElement(name = "system-locale", namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
     100         protected String systemLocale;
     101
     102         //    @XmlElement(namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
     103         protected String timezone;
     104
     105         //    @XmlElement(name = "system-locale", namespace = "http://qare.sfbay.sun.com/projects/COF/2003/2_0_2/Schema")
     106         protected String userLocale;
     107     protected COFData data;
     108
     109         COFEnvironment(COFData data) {
     110         this.data = data;
     111
     112         initDefaultHostInfo();
     113                 initDefaultOSInfo();
     114
     115                 hostName = data.get("environment.host", hostName);
     116                 domainName = data.get("environment.domain", domainName);
     117                 setMachine(data.get("environment.machine", hostName + "." + domainName));
     118                 os.setName(data.get("environment.os.name", os.getName()));
     119                 os.setVersion(data.get("environment.os.version", os.getVersion()));
     120                 os.setArch(data.get("environment.os.arch", os.getArch()));
     121                 setJdk(data.get("environment.jdk", jdk));
     122                 setUserLocale(data.get("environment.user-locale", java.util.Locale.getDefault().toString()));
     123                 setSystemLocale(data.get("environment.system-locale", data
     124                                 .get("LOCALE")));
     125                 setEncoding(data.get("environment.encoding", (new InputStreamReader(
     126                                 System.in)).getEncoding()));
     127                 setTimezone(data.get("environment.timezone", TimeZone.getDefault()
     128                                 .getID()));
     129                 setBits(data.get("environment.bits", null) == null ? null
     130                                 : new Integer(data.get("environment.bits")));
     131                 setDisplaydepth(data.get("environment.displaydepth", null) == null ? null
     132                                 : new Integer(data.get("environment.displaydepth")));
     133                 setDescription(data.get("environment.description"));
     134         }
     135
     136     public COFEnvironment(COFData data, String id) {
     137         this(data);
     138         this.id = id;
     139     }
     140
     141         /**
     142          * Gets the value of the bits property.
     143          *
     144          * @return
     145          *     possible object is
     146          *     {@link Integer }
     147          *
     148          */
     149         public Integer getBits() {
     150                 return bits;
     151         }
     152
     153         private String getCalderaLinuxVersionInfo() {
     154                 String res = "N/A";
     155
     156                 RandomAccessFile raf = null;
     157                 try {
     158                         raf = new RandomAccessFile("/etc/issue", "r");
     159                         String line;
     160                         while ((line = raf.readLine()) != null) {
     161                                 StringTokenizer st = new StringTokenizer(line, " ");
     162                                 if (st.countTokens() >= 2) {
     163                                         if (st.nextToken().toUpperCase().equals("VERSION")) {
     164                                                 res = st.nextToken();
     165                                                 break;
     166                                         }
     167                                 }
     168                         }
     169                 } catch (Exception e) {
     170                         // System.err.println("Something might be wrong with Caldera");
     171                         // cat.warn("The version number of Caldera cannot be retrieved");
     172                 } finally {
     173                         if (raf != null) {
     174                                 try {
     175                                         raf.close();
     176                                 } catch (Exception e) {
     177                                 }
     178                         }
     179                 }
     180
     181                 return res;
     182         }
     183
     184         /**
     185          * Gets the value of the description property.
     186          *
     187          * @return
     188          *     possible object is
     189          *     {@link String }
     190          *
     191          */
     192         public String getDescription() {
     193                 return description;
     194         }
     195
     196         /**
     197          * Gets the value of the displaydepth property.
     198          *
     199          * @return
     200          *     possible object is
     201          *     {@link Integer }
     202          *
     203          */
     204         public Integer getDisplaydepth() {
     205                 return displaydepth;
     206         }
     207
     208         /**
     209          * Gets the value of the encoding property.
     210          *
     211          * @return
     212          *     possible object is
     213          *     {@link String }
     214          *
     215          */
     216         public String getEncoding() {
     217                 return encoding;
     218         }
     219
     220         private String getGenericLinuxVersionInfo(String fileName) {
     221                 String res = "N/A";
     222
     223                 RandomAccessFile raf = null;
     224                 try {
     225                         raf = new RandomAccessFile(fileName, "r");
     226                         StringTokenizer st = new StringTokenizer(raf.readLine(), " ");
     227                         while (st.hasMoreElements()) {
     228                                 String ele = st.nextToken();
     229                                 if (Character.isDigit(ele.charAt(0))) {
     230                                         res = ele;
     231                                         break;
     232                                 }
     233                         }
     234                 } catch (Exception e) {
     235                         // System.err.println("Something might be wrong with Linux");
     236                 } finally {
     237                         if (raf != null) {
     238                                 try {
     239                                         raf.close();
     240                                 } catch (Exception e) {
     241                                 }
     242                         }
     243                 }
     244
     245                 return res;
     246         }
     247
     248         /**
     249          * Gets the value of the id property.
     250          *
     251          * @return
     252          *     possible object is
     253          *     {@link String }
     254          *
     255          */
     256         public String getId() {
     257                 return id;
     258         }
     259
     260         LinkedHashMap getItemAttributes() {
     261                 return xmlAttributes;
     262         }
     263
     264         LinkedHashMap getItemElements() {
     265                 return xmlElements;
     266         }
     267
     268         String getItemTagName() {
     269                 return xmlTagName;
     270         }
     271
     272         /**
     273          * Gets the value of the jdk property.
     274          *
     275          * @return
     276          *     possible object is
     277          *     {@link String }
     278          *
     279          */
     280         public String getJdk() {
     281                 return jdk;
     282         }
     283
     284         /**
     285          * Gets the value of the machine property.
     286          *
     287          * @return
     288          *     possible object is
     289          *     {@link String }
     290          *
     291          */
     292         public String getMachine() {
     293                 return machine;
     294         }
     295
     296         /**
     297          * Gets the value of the os property.
     298          *
     299          * @return
     300          *     possible object is
     301          *     {@link OS }
     302          *
     303          */
     304         public COFOS getOs() {
     305                 return os;
     306         }
     307
     308         String[] getPropOrder() {
     309                 return propOrder;
     310         }
     311
     312         /**
     313          * Gets the value of the sw property.
     314          *
     315          * <p>
     316          * This accessor method returns a reference to the live list,
     317          * not a snapshot. Therefore any modification you make to the
     318          * returned list will be present inside the JAXB object.
     319          * This is why there is not a <CODE>set</CODE> method for the sw property.
     320          *
     321          * <p>
     322          * For example, to add a new item, do as follows:
     323          * <pre>
     324          *    getSw().add(newItem);
     325          * </pre>
     326          *
     327          *
     328          * <p>
     329          * Objects of the following type(s) are allowed in the list
     330          * {@link SWEntity }
     331          *
     332          *
     333          */
     334         public List/*<SWEntity>*/getSw() {
     335                 if (sw == null) {
     336                         sw = new ArrayList/*<SWEntity>*/();
     337                 }
     338                 return this.sw;
     339         }
     340
     341         /**
     342          * Gets the value of the systemLocale property.
     343          *
     344          * @return
     345          *     possible object is
     346          *     {@link String }
     347          *
     348          */
     349         public String getSystemLocale() {
     350                 return systemLocale;
     351         }
     352
     353         String getTagName() {
     354                 return itemTagName;
     355         }
     356
     357         String[] getTags() {
     358                 return propTags;
     359         }
     360
     361         /**
     362          * Gets the value of the timezone property.
     363          *
     364          * @return
     365          *     possible object is
     366          *     {@link String }
     367          *
     368          */
     369         public String getTimezone() {
     370                 return timezone;
     371         }
     372
     373         /**
     374          * Gets the value of the userLocale property.
     375          *
     376          * @return
     377          *     possible object is
     378          *     {@link String }
     379          *
     380          */
     381         public String getUserLocale() {
     382                 return userLocale;
     383         }
     384
     385         private void initDefaultHostInfo() {
     386                 domainName = "unknown";
     387
     388                 try {
     389                         hostName = InetAddress.getLocalHost().getHostName();
     390                 } catch (UnknownHostException e) {
     391                         hostName = "unknown";
     392                 }
     393         }
     394
     395         private void initDefaultOSInfo() {
     396                 os = new COFOS();
     397                 os.setName(System.getProperty("os.name"));
     398                 os.setVersion(System.getProperty("os.version"));
     399                 os.setArch(System.getProperty("os.arch"));
     400
     401                 // standardize results
     402                 /*        if (osName.equals("sunos") || osName.equals("solaris")) {
     403                  osName = "solaris";
     404                  if (osVersion.equals("5.6"))
     405                  osVersion = "2.6";
     406                  else if (osVersion.equals("5.7"))
     407                  osVersion = "2.7";
     408                  else if (osVersion.equals("5.8"))
     409                  osVersion = "8";
     410                  else if (osVersion.equals("5.9"))
     411                  osVersion = "9";
     412                  } else if (osName.startsWith("windows")) {
     413                  osName = "windows";
     414                  if (osName.indexOf("95") != -1)
     415                  osVersion = "95";
     416                  else if (osName.indexOf("98") != -1)
     417                  osVersion = "98";
     418                  else if (osName.indexOf("2000") != -1)
     419                  osVersion = "2000";
     420                  else if (osName.indexOf("me") != -1)
     421                  osVersion = "me";
     422                  else if (osName.indexOf("nt") != -1)
     423                  osVersion = "nt_4.0";
     424                  else if (osName.indexOf("xp") != -1)
     425                  osVersion = "xp";
     426                  } else if (osName.equals("linux")) {
     427                  if ((new File("/etc/turbolinux-release")).exists()) {
     428                  osName = "turbo_linux";
     429                  osVersion = getGenericLinuxVersionInfo("/etc/turbolinux-release");
     430                  } else if ((new File("/etc/SuSE-release")).exists()) {
     431                  osName = "suse_linux";
     432                  osVersion = getGenericLinuxVersionInfo("/etc/SuSE-release");
     433                  } else if ((new File("/etc/mandrake-release")).exists()) {
     434                  osName = "mandrake_linux";
     435                  osVersion = getGenericLinuxVersionInfo("/etc/mandrake-release");
     436                  } else if ((new File("/etc/redhat-release")).exists()) {
     437                  osName = "redhat_linux";
     438                  osVersion = getGenericLinuxVersionInfo("/etc/redhat-release");
     439                  } else if ((new File("/etc/lst.cnf")).exists()) {
     440                  osName = "caldera_linux";
     441                  osVersion = getCalderaLinuxVersionInfo();
     442                  } else {
     443                  osName = "unknown_linux";
     444                  osVersion = "N/A";
     445                  }
     446                  }
     447                  */}
     448
     449         /**
     450          * Sets the value of the bits property.
     451          *
     452          * @param value
     453          *     allowed object is
     454          *     {@link Integer }
     455          *
     456          */
     457         public void setBits(Integer value) {
     458                 this.bits = value;
     459         }
     460
     461         /**
     462          * Sets the value of the description property.
     463          *
     464          * @param value
     465          *     allowed object is
     466          *     {@link String }
     467          *
     468          */
     469         public void setDescription(String value) {
     470                 this.description = value;
     471         }
     472
     473         /**
     474          * Sets the value of the displaydepth property.
     475          *
     476          * @param value
     477          *     allowed object is
     478          *     {@link Integer }
     479          *
     480          */
     481         public void setDisplaydepth(Integer value) {
     482                 this.displaydepth = value;
     483         }
     484
     485         /**
     486          * Sets the value of the encoding property.
     487          *
     488          * @param value
     489          *     allowed object is
     490          *     {@link String }
     491          *
     492          */
     493         public void setEncoding(String value) {
     494                 this.encoding = value;
     495         }
     496
     497         /**
     498          * Sets the value of the id property.
     499          *
     500          * @param value
     501          *     allowed object is
     502          *     {@link String }
     503          *
     504          */
     505         public void setId(String value) {
     506                 this.id = value;
     507         }
     508
     509         /**
     510          * Sets the value of the jdk property.
     511          *
     512          * @param value
     513          *     allowed object is
     514          *     {@link String }
     515          *
     516          */
     517         public void setJdk(String value) {
     518                 this.jdk = value;
     519         }
     520
     521         /**
     522          * Sets the value of the machine property.
     523          *
     524          * @param value
     525          *     allowed object is
     526          *     {@link String }
     527          *
     528          */
     529         public void setMachine(String value) {
     530                 if (value.endsWith(".unknown"))
     531                 try {
     532                                 machine = InetAddress.getLocalHost().getCanonicalHostName();
     533                                 if (machine.indexOf(".") == -1) {
     534                     if ("false".equals(data.get("showWarnings"))) {
     535                         System.err.println(i18n.getString("environment.badMachineName", machine));
     536                     } else {
     537                         String warningMessage = data.get("warning", "");
     538                         data.put("warning", warningMessage + i18n.getString("environment.badMachineName") + "\n");
     539                     }
     540                 }
     541                                 return;
     542                 } catch (UnknownHostException e) {
     543             if ("false".equals(data.get("showWarnings"))) {
     544                 System.err.println(i18n.getString("environment.cantGetLocalhostName", e.getMessage()));
     545                 value = value.substring(0,  value.indexOf(".unknown")-1);
     546                 System.err.println(i18n.getString("environment.badMachineName", value));
     547             } else {
     548                 String warningMessage = data.get("warning", "");
     549                 value = value.substring(0,  value.indexOf(".unknown")-1);
     550                 data.put("warning", warningMessage + i18n.getString("environment.cantGetLocalhostName", e.getMessage()) + "\n" + i18n.getString("environment.badMachineName", value) + "\n");
     551             }
     552                 }
     553                 this.machine = value;
     554         }
     555
     556         /**
     557          * Sets the value of the os property.
     558          *
     559          * @param value
     560          *     allowed object is
     561          *     {@link OS }
     562          *
     563          */
     564         public void setOs(COFOS value) {
     565                 this.os = value;
     566         }
     567
     568         /**
     569          * Sets the value of the systemLocale property.
     570          *
     571          * @param value
     572          *     allowed object is
     573          *     {@link String }
     574          *
     575          */
     576         public void setSystemLocale(String value) {
     577                 this.systemLocale = value;
     578         }
     579
     580         /**
     581          * Sets the value of the timezone property.
     582          *
     583          * @param value
     584          *     allowed object is
     585          *     {@link String }
     586          *
     587          */
     588         public void setTimezone(String value) {
     589                 this.timezone = value;
     590         }
     591
     592         /**
     593          * Sets the value of the userLocale property.
     594          *
     595          * @param value
     596          *     allowed object is
     597          *     {@link String }
     598          *
     599          */
     600         public void setUserLocale(String value) {
     601                 this.userLocale = value;
     602         }
     603
     604 }