KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > api > Asenv


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.api;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import org.openide.ErrorManager;
29
30 /**
31  * Parser for asenv.conf and asenv.bat
32  */

33 public class Asenv {
34     
35     transient private final java.util.Properties JavaDoc props = new java.util.Properties JavaDoc();
36     
37     /**
38      * key for path to jdk stored in asenv file
39      */

40     public static final String JavaDoc AS_JAVA = "AS_JAVA";
41     /**
42      * key to path of default domains in asenv file
43      */

44     public static final String JavaDoc AS_DEF_DOMAINS_PATH = "AS_DEF_DOMAINS_PATH";
45     
46     /**
47      * Creates a new instance of Asenv
48      * @param asenv asenv.conf or asenv.bat file
49      */

50     public Asenv(File JavaDoc asenv) {
51         FileReader JavaDoc fReader = null;
52         BufferedReader JavaDoc bReader = null;
53         try {
54             fReader = new FileReader JavaDoc(asenv);
55             bReader = new BufferedReader JavaDoc(fReader);
56             
57             String JavaDoc line = bReader.readLine();
58             while (line != null) {
59                 StringTokenizer JavaDoc strtok = new StringTokenizer JavaDoc(line,"=");
60                 if (strtok.countTokens() == 2) {
61                     String JavaDoc key = strtok.nextToken();
62                     String JavaDoc val = strtok.nextToken();
63                     if (key.startsWith("set ")) {
64                         key = key.substring(3).trim();
65                     }
66                     if (val.startsWith("\"")) {
67                         val = val.substring(1,val.length()-1);
68                     }
69                     props.put(key,val);
70                 }
71                 line = bReader.readLine();
72             }
73         } catch (FileNotFoundException JavaDoc ex) {
74             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
75         } catch (IOException JavaDoc ex) {
76             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
77         } finally {
78             if (null != bReader) {
79                 try {
80                     bReader.close();
81                 } catch (IOException JavaDoc ioe) {
82                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ioe);
83                 }
84             }
85             if (null != fReader) {
86                 try {
87                     fReader.close();
88                 } catch (IOException JavaDoc ioe) {
89                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ioe);
90                 }
91             }
92         }
93     }
94     
95     /**
96      * Get values from asenv file
97      * @param key variable defined in asenv
98      * @return associated value
99      */

100     public String JavaDoc get(final String JavaDoc key) {
101         return (String JavaDoc) props.getProperty(key);
102     }
103     
104 }
105
106
Popular Tags