KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > ide > ui > JBPluginProperties


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 package org.netbeans.modules.j2ee.jboss4.ide.ui;
20
21 import java.io.File JavaDoc;
22 import java.util.Properties JavaDoc;
23 import org.openide.filesystems.FileLock;
24 import org.openide.filesystems.FileObject;
25 import org.openide.filesystems.FileSystem;
26 import org.openide.filesystems.Repository;
27 import org.openide.ErrorManager;
28
29 /**
30  * Plugin Properties Singleton class
31  * @author Ivan Sidorkin
32  */

33 public class JBPluginProperties {
34     
35     public static final String JavaDoc PROPERTY_DISPLAY_NAME ="displayName";//NOI18N
36
public static final String JavaDoc PROPERTY_SERVER = "server";//NOI18N
37
public static final String JavaDoc PROPERTY_DEPLOY_DIR = "deploy-dir";//NOI18N
38
public static final String JavaDoc PROPERTY_SERVER_DIR = "server-dir";//NOI18N
39
public static final String JavaDoc PROPERTY_ROOT_DIR = "root-dir";//NOI18N
40
public static final String JavaDoc PROPERTY_HOST = "host";//NOI18N
41
public static final String JavaDoc PROPERTY_PORT = "port";//NOI18N
42

43     
44     private static JBPluginProperties pluginProperties = null;
45     private String JavaDoc installLocation;
46     private String JavaDoc domainLocation;
47     
48     
49     public static JBPluginProperties getInstance(){
50         if(pluginProperties==null){
51             pluginProperties = new JBPluginProperties();
52         }
53         return pluginProperties;
54     }
55     
56     
57     
58     /** Creates a new instance of */
59     private JBPluginProperties() {
60         java.io.InputStream JavaDoc inStream = null;
61         try {
62             try {
63                 propertiesFile = getPropertiesFile();
64                 if (null != propertiesFile)
65                     inStream = propertiesFile.getInputStream();
66             } catch (java.io.FileNotFoundException JavaDoc e) {
67                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
68             } catch (java.io.IOException JavaDoc e) {
69                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
70             } finally {
71                 loadPluginProperties(inStream);
72                 if (null != inStream)
73                     inStream.close();
74             }
75         } catch (java.io.IOException JavaDoc e) {
76             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
77         }
78         
79     }
80     
81     void loadPluginProperties(java.io.InputStream JavaDoc inStream) {
82         Properties JavaDoc inProps = new Properties JavaDoc();
83         if (null != inStream)
84             try {
85                 inProps.load(inStream);
86             } catch (java.io.IOException JavaDoc e) {
87                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
88             }
89         String JavaDoc loc = inProps.getProperty(INSTALL_ROOT_KEY);
90         if (loc!=null){// try to get the default value
91
setInstallLocation(loc);
92         }
93     }
94     
95     private static final String JavaDoc INSTALL_ROOT_KEY = "installRoot"; // NOI18N
96
public static final String JavaDoc INSTALL_ROOT_PROP_NAME = "com.sun.aas.installRoot"; //NOI18N
97

98     
99     private FileObject propertiesFile = null;
100     
101     private FileObject getPropertiesFile() throws java.io.IOException JavaDoc {
102         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
103         FileObject dir = fs.findResource("J2EE");
104         FileObject retVal = null;
105         if (null != dir) {
106             retVal = dir.getFileObject("jb","properties"); // NOI18N
107
if (null == retVal) {
108                 retVal = dir.createData("jb","properties"); //NOI18N
109
}
110         }
111         return retVal;
112     }
113     
114     
115     public void saveProperties(){
116         Properties JavaDoc outProp = new Properties JavaDoc();
117         String JavaDoc installRoot = getInstallLocation();
118         if (installRoot != null)
119             outProp.setProperty(INSTALL_ROOT_KEY, installRoot);
120         
121         FileLock l = null;
122         java.io.OutputStream JavaDoc outStream = null;
123         try {
124             if (null != propertiesFile) {
125                 try {
126                     l = propertiesFile.lock();
127                     outStream = propertiesFile.getOutputStream(l);
128                     if (null != outStream)
129                         outProp.store(outStream, "");
130                 } catch (java.io.IOException JavaDoc e) {
131                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
132                 } finally {
133                     if (null != outStream)
134                         outStream.close();
135                     if (null != l)
136                         l.releaseLock();
137                 }
138             }
139         } catch (java.io.IOException JavaDoc e) {
140             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
141         }
142     }
143     
144     public boolean isCurrentServerLocationValid(){
145         if (getInstallLocation()!=null)
146             return
147                 JBPluginUtils.isGoodJBServerLocation4x(new File JavaDoc(getInstallLocation())) ||
148                 JBPluginUtils.isGoodJBServerLocation5x(new File JavaDoc(getInstallLocation()));
149         else
150             return false;
151     }
152     
153     
154     public void setInstallLocation(String JavaDoc installLocation){
155         if ( installLocation.endsWith("/") || installLocation.endsWith("\\") ){
156             installLocation = installLocation.substring(0, installLocation.length() - 1 );
157         }
158         
159         this.installLocation = installLocation;
160     }
161     
162     public String JavaDoc getInstallLocation(){
163         return this.installLocation;
164     }
165     
166     public void setDomainLocation(String JavaDoc domainLocation) {
167         if ( domainLocation.endsWith("/") || domainLocation.endsWith("\\") ){
168             domainLocation = domainLocation.substring(0, domainLocation.length() - 1 );
169         }
170         
171         this.domainLocation = domainLocation;
172     }
173     
174     public String JavaDoc getDomainLocation() {
175         return domainLocation;
176     }
177 }
Popular Tags