KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > ui > AddDomainPlatformPanel


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.sun.ide.j2ee.ui;
20
21 import java.awt.Component JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager;
33 import org.openide.ErrorManager;
34 import org.openide.WizardDescriptor;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.NbBundle;
37
38 /** Queries the user for the platform directory associated with the
39  * instance they are registering.
40  */

41 class AddDomainPlatformPanel implements WizardDescriptor.FinishablePanel,
42         ChangeListener JavaDoc {
43     
44     /**
45      * The visual component that displays this panel. If you need to access the
46      * component from this class, just use getComponent().
47      */

48     private AddInstanceVisualPlatformPanel component;
49     private WizardDescriptor wiz;
50     
51    /* Get the visual component for the panel. In this template, the component
52      is kept separate. This can be more efficient: if the wizard is created
53      but never displayed, or not all panels are displayed, it is better to
54      create only those which really need to be visible.
55     */

56     public Component JavaDoc getComponent() {
57         if (component == null) {
58             File JavaDoc f = ServerLocationManager.getLatestPlatformLocation();
59             File JavaDoc defaultLoc = null;
60             if (null == f || !f.exists()) {
61                 String JavaDoc prop = System.getProperty(ServerLocationManager.INSTALL_ROOT_PROP_NAME);
62                 if (null != prop && prop.length() > 0) {
63                     // there is a possible root directory for the AS
64
File JavaDoc installRoot = new File JavaDoc(prop);
65                     if (ServerLocationManager.isGoodAppServerLocation(installRoot)) {
66                         defaultLoc = installRoot;
67                     }
68                 }
69                 if (null == defaultLoc) {
70                     defaultLoc = new File JavaDoc(System.getProperty("user.home"));//NOI18N
71
}
72             } else {
73                 defaultLoc = f;
74             }
75             component = new AddInstanceVisualPlatformPanel(defaultLoc);
76             component.addChangeListener(this);
77         }
78         return component;
79     }
80     
81     public HelpCtx getHelp() {
82         return new HelpCtx("AS_RegServ_EnterPlatformDir"); //NOI18N
83
}
84     
85     /** Determine if the input is valid.
86      *
87      * Is the user entered directory an app server install directory?
88      *
89      * If the install directory is a GlassFish install, is the IDE running in
90      * a J2SE 5.0 VM?
91      *
92      * If the user attempts to register a default instance, is there a usable one?
93      * See Util.getRegisterableDefaultDomains(File)
94      *
95      * Is the user asking for an unsupported instance type?
96      */

97     public boolean isValid() {
98         boolean retVal = true;
99         wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE, null);
100         String JavaDoc instLoc = component.getInstallLocation();
101         if (instLoc.startsWith("\\\\")) {
102             wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
103                     NbBundle.getMessage(AddDomainPlatformPanel.class,
104                     "Msg_NoAuthorityComponent")); // NOI18N
105
retVal = false;
106         }
107         File JavaDoc location = new File JavaDoc(component.getInstallLocation());
108         if (retVal && !ServerLocationManager.isGoodAppServerLocation(location)) {
109             Object JavaDoc selectedType = component.getSelectedType();
110             if (selectedType == AddDomainWizardIterator.REMOTE){
111                 wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
112                         NbBundle.getMessage(AddDomainPlatformPanel.class,
113                         "Msg_NeedValidInstallEvenForRemote"));
114             }else{
115                 // not valid install directory
116
wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
117                         NbBundle.getMessage(AddDomainPlatformPanel.class,
118                         "Msg_InValidInstall")); // NOI18N
119
}
120             component.setDomainsList(new Object JavaDoc[0]);
121             retVal = false;
122         } else if (retVal) {
123             Object JavaDoc oldPlatformLoc = wiz.getProperty(AddDomainWizardIterator.PLATFORM_LOCATION);
124             if (!location.equals(oldPlatformLoc) || component.getDomainsListModel().getSize() < 1) {
125                 Object JavaDoc[] domainsList = getDomainList(Util.getRegisterableDefaultDomains(location));
126                 component.setDomainsList(domainsList);
127             }
128             //component.setDomainsList();
129
if (ServerLocationManager.isGlassFish(location)) {
130                 String JavaDoc javaClassVersion =
131                         System.getProperty("java.class.version"); // NOI18N
132
double jcv = Double.parseDouble(javaClassVersion);
133                 if (jcv < 49.0) {
134                     // prevent ClassVersionUnsupportedError....
135
wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
136                             NbBundle.getMessage(AddDomainPlatformPanel.class,
137                             "Msg_RequireJ2SE5")); // NOI18N
138
retVal = false;
139                 }
140             }
141         }
142         if (retVal) {
143             wiz.putProperty(AddDomainWizardIterator.PLATFORM_LOCATION,location);
144             wiz.putProperty(AddDomainWizardIterator.USER_NAME,
145                     AddDomainWizardIterator.BLANK);
146             wiz.putProperty(AddDomainWizardIterator.PASSWORD,
147                     AddDomainWizardIterator.BLANK);
148             Object JavaDoc selectedType = component.getSelectedType();
149             if (selectedType == AddDomainWizardIterator.DEFAULT) {
150                 File JavaDoc[] usableDomains = Util.getRegisterableDefaultDomains(location);
151                 if (usableDomains.length == 0) {
152                     wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
153                             NbBundle.getMessage(AddDomainPlatformPanel.class,
154                             "Msg_NoDefaultDomainsAvailable")); //NOI18N
155
retVal = false;
156                 }
157                 wiz.putProperty(AddDomainWizardIterator.TYPE, selectedType);
158                 String JavaDoc dirCandidate = component.getDomainDir();
159                 if (null != dirCandidate) {
160                     File JavaDoc domainDir = new File JavaDoc(dirCandidate);
161                     // this should not happen. The previous page of the wizard should
162
// prevent this panel from appearing.
163
if (!Util.rootOfUsableDomain(domainDir)) {
164                         wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
165                                 NbBundle.getMessage(AddDomainPlatformPanel.class,
166                                 "Msg_InValidDomainDir", //NOI18N
167
component.getDomainDir()));
168                         retVal = false;
169                     } else {
170                         //File platformDir = (File) wiz.getProperty(AddDomainWizardIterator.PLATFORM_LOCATION);
171
Util.fillDescriptorFromDomainXml(wiz, domainDir);
172                         // fill in the admin name and password from the asadminprefs file
173
String JavaDoc username = "admin";
174                         String JavaDoc password = null;
175                         File JavaDoc f = new File JavaDoc(System.getProperty("user.home")+"/.asadminprefs"); //NOI18N
176
if (f.exists()){
177                             FileInputStream JavaDoc fis = null;
178                             try{
179                                 
180                                 fis = new FileInputStream JavaDoc(f);
181                                 Properties JavaDoc p = new Properties JavaDoc();
182                                 p.load(fis);
183                                 fis.close();
184                                 
185                                 Enumeration JavaDoc e = p.propertyNames() ;
186                                 for ( ; e.hasMoreElements() ;) {
187                                     String JavaDoc v = (String JavaDoc)e.nextElement();
188                                     if (v.equals("AS_ADMIN_USER"))//admin user//NOI18N
189
username = p.getProperty(v );
190                                     else if (v.equals("AS_ADMIN_PASSWORD")){ // admin password//NOI18N
191
password = p.getProperty(v );
192                                     }
193                                 }
194                                 
195                             } catch (Exception JavaDoc e){
196                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
197                                         e);
198                             } finally {
199                                 if (null != fis) {
200                                     try {
201                                         fis.close();
202                                     } catch (IOException JavaDoc ex) {
203                                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
204                                                 ex);
205                                     }
206                                 }
207                             }
208                         }
209                         wiz.putProperty(AddDomainWizardIterator.PASSWORD, password);
210                         wiz.putProperty(AddDomainWizardIterator.USER_NAME,username);
211                     }
212                 } else {
213                     wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
214                             NbBundle.getMessage(AddDomainPlatformPanel.class,
215                             "Msg_NoDefaultDomainsAvailable")); //NOI18N
216
retVal = false;
217                 }
218             } else if (selectedType == AddDomainWizardIterator.REMOTE) {
219                 wiz.putProperty(AddDomainWizardIterator.TYPE, selectedType);
220                 wiz.putProperty(AddDomainWizardIterator.INSTALL_LOCATION,"");
221                 wiz.putProperty(AddDomainWizardIterator.DOMAIN,"");
222             } else if (selectedType == AddDomainWizardIterator.LOCAL) {
223                 wiz.putProperty(AddDomainWizardIterator.TYPE, selectedType);
224             } else if (selectedType == AddDomainWizardIterator.PERSONAL) {
225                 wiz.putProperty(AddDomainWizardIterator.TYPE, selectedType);
226             } else {
227                 wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
228                         NbBundle.getMessage(AddDomainPlatformPanel.class,
229                         "Msg_UnsupportedType")); //NOI18N
230
retVal = false;
231             }
232         }
233         return retVal;
234     }
235     
236     private Object JavaDoc[] getDomainList(File JavaDoc[] dirs){
237         return getServerList(dirs);
238     }
239     
240     private Object JavaDoc[] getServerList(File JavaDoc[] dirs){
241         java.util.List JavaDoc xmlList = new java.util.ArrayList JavaDoc();
242         Object JavaDoc retVal[] = null;
243         File JavaDoc platformDir = (File JavaDoc) wiz.getProperty(AddDomainWizardIterator.PLATFORM_LOCATION);
244         for(int i=0; platformDir != null && i<dirs.length; i++){
245             String JavaDoc hostPort = Util.getHostPort(dirs[i],platformDir);
246             if(hostPort != null) {
247                 xmlList.add(
248                         NbBundle.getMessage(AddDomainPlatformPanel.class,
249                         "LBL_domainListEntry", new Object JavaDoc[] {hostPort,dirs[i].toString()}));
250             }
251         }//for
252
if(xmlList != null) {
253             retVal = xmlList.toArray();
254         }
255         return retVal;
256     }
257     
258     // Event Handling
259
private final Set JavaDoc/*<ChangeListener>*/ listeners = new HashSet JavaDoc/*<ChangeListener>*/(1);
260     public final void addChangeListener(ChangeListener JavaDoc l) {
261         synchronized (listeners) {
262             listeners.add(l);
263         }
264     }
265     public final void removeChangeListener(ChangeListener JavaDoc l) {
266         synchronized (listeners) {
267             listeners.remove(l);
268         }
269     }
270     protected final void fireChangeEvent() {
271         Iterator JavaDoc/*<ChangeListener>*/ it;
272         synchronized (listeners) {
273             it = new HashSet JavaDoc/*<ChangeListener>*/(listeners).iterator();
274         }
275         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
276 // System.out.println("PP fireChangeEvent on");
277
while (it.hasNext()) {
278             ChangeListener JavaDoc l = (ChangeListener JavaDoc) it.next();
279 // System.out.println(" "+l);
280
l.stateChanged(ev);
281         }
282     }
283     
284     
285     // You can use a settings object to keep track of state. Normally the
286
// settings object will be the WizardDescriptor, so you can use
287
// WizardDescriptor.getProperty & putProperty to store information entered
288
// by the user.
289
public void readSettings(Object JavaDoc settings) {
290         wiz = (WizardDescriptor) settings;
291     }
292     public void storeSettings(Object JavaDoc settings) {
293         // TODO implement?
294
}
295     
296     public void stateChanged(ChangeEvent JavaDoc e) {
297 // System.out.println("PP stateChanged");
298
wiz.putProperty(AddDomainWizardIterator.TYPE, component.getSelectedType());
299         fireChangeEvent(); //e);
300
}
301     
302     public boolean isFinishPanel() {
303         Object JavaDoc selectedType = component.getSelectedType();
304         return selectedType == AddDomainWizardIterator.DEFAULT;
305     }
306 }
307
308
Popular Tags