KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.ide.j2ee.ui;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.InetSocketAddress JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.util.Random JavaDoc;
27 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
28 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager;
29 import org.netbeans.modules.j2ee.sun.ide.dm.SunDeploymentManager;
30 import org.netbeans.modules.j2ee.sun.ide.j2ee.ui.AddDomainWizardIterator;
31 import org.openide.ErrorManager;
32 import org.openide.WizardDescriptor;
33 import org.openide.WizardDescriptor.Panel;
34 import org.openide.util.NbBundle;
35
36 /**
37  * Helper class which serves to create and register a new domain without
38  * any use of the ui.
39  *
40  * @author Michal Mocnak,
41  * @author vkraemer
42  */

43 public final class DomainCreator {
44     
45     // default values
46

47     private final static String JavaDoc DEFAULT_SERVER_NAME =
48             NbBundle.getMessage(SunDeploymentManager.class, "FACTORY_DISPLAYNAME"); // NOI18N
49
private final static String JavaDoc DEFAULT_USERNAME = "admin"; // NOI18N
50
private final static String JavaDoc DEFAULT_PASSWORD = "adminadmin"; // NOI18N
51
private final static String JavaDoc DEFAULT_HOST = "localhost"; // NOI18N
52
private final static String JavaDoc DEFAULT_DOMAIN_NAME = "personalDomain"; // NOI18N
53
private final static int DEFAULT_ADMIN_PORT = 4848;
54     private final static int DEFAULT_HTTP_PORT = 8080;
55     private final static int DEFAULT_JMX_PORT = 8686;
56     private final static int DEFAULT_JMS_PORT = 7676;
57     private final static int DEFAULT_ORB_PORT = 3700;
58     private final static int DEFAULT_HTTPS_PORT = 8181;
59     private final static int DEFAULT_ORB_SSL_PORT = 3820;
60     private final static int DEFAULT_ORB_MUTUALAUTH_PORT = 3920;
61     
62     // properties
63

64     private final static String JavaDoc PROP_USER_NAME = "username"; // NOI18N
65
private final static String JavaDoc PROP_PASSWORD = "password"; // NOI18N
66
private final static String JavaDoc PROP_HOST = "host"; // NOI18N
67
private final static String JavaDoc PROP_PORT = "port"; // NOI18N
68
private final static String JavaDoc PROP_PLATFORM_LOCATION = "platform_location"; // NOI18N
69
private final static String JavaDoc PROP_INSTALL_LOCATION = "install_location"; // NOI18N
70
private final static String JavaDoc PROP_DOMAIN = "domain"; // NOI18N
71
private final static String JavaDoc PROP_INSTANCE_PORT = "instance_port"; // NOI18N
72
private final static String JavaDoc PROP_JMS_PORT = "jms_port"; // NOI18N
73
private final static String JavaDoc PROP_ORB_LISTENER_PORT = "orb_listener_port"; // NOI18N
74
private final static String JavaDoc PROP_ORB_SSL_PORT = "orb_ssl_port"; // NOI18N
75
private final static String JavaDoc PROP_HTTP_SSL_PORT = "http_ssl_port"; // NOI18N
76
private final static String JavaDoc PROP_ORB_MUTUAL_AUTH_PORT = "orb_mutual_auth_port"; // NOI18N
77
private final static String JavaDoc PROP_ADMIN_JMX_PORT = "admin_jmx_port"; // NOI18N
78
private final static String JavaDoc PROP_TYPE = "type"; // NOI18N
79
private final static String JavaDoc PROP_DISPLAY_NAME = "ServInstWizard_displayName"; // NOI18N
80

81     private WizardDescriptor wizardDescriptor;
82     private AddDomainWizardIterator wizard;
83     
84     /**
85      * Do not allow to create instances of this class.
86      */

87     private DomainCreator() {
88         wizard = new AddDomainWizardIterator();
89         wizardDescriptor = new WizardDescriptor(new Panel[] {});
90     }
91     
92     /**
93      * Creates a default domain (.personalDomain) in user's home dir
94      *
95      * @param serverPath full path to the SJSAS
96      * @return the InstanceProperties object, null if domain is not created
97      * or if already registered.
98      */

99     public static InstanceProperties createPersonalDefaultDomain(String JavaDoc serverPath) {
100         String JavaDoc homeDir = System.getProperty("user.home"); // NOI18N
101
return createPersonalDomain(serverPath,
102                 homeDir + File.separator + "." +DEFAULT_DOMAIN_NAME); // NOI18N
103
}
104     
105     /**
106      * Creates a specific domain
107      *
108      * @param serverPath absolute path to the SJSAS root
109      * @param domainPath absolute path to the domain root
110      * @return the InstanceProperties object, null if domain is not created
111      * or if already registered.
112      */

113     public static InstanceProperties createPersonalDomain(String JavaDoc serverPath,
114             String JavaDoc domainPath) {
115         File JavaDoc serverRoot = new File JavaDoc(serverPath);
116         File JavaDoc domainRoot = new File JavaDoc(domainPath);
117         InstanceProperties retVal = null;
118         
119         // Check if serverRoot location is valid glassfish instance
120
if (ServerLocationManager.isGoodAppServerLocation(serverRoot)) {
121             
122             // If any instance of the server is registered return InstanceProperties
123
// of that glassfish server instance
124
String JavaDoc serverID = "[" + serverRoot.getAbsolutePath() + "]deployer:Sun:AppServer::" + DEFAULT_HOST + ":" + // NOI18N
125
DEFAULT_ADMIN_PORT;
126             retVal = InstanceProperties.getInstanceProperties(serverID);
127             
128             if(null == retVal) {
129                 // If domain already exists create another one
130
int counter = 1;
131                 
132                 while (domainRoot.exists()) {
133                     domainRoot = new File JavaDoc(domainPath + counter++);
134                 }
135                 
136                 // if domainRoot parent is not writable inform user and return null
137
if(domainRoot.getParentFile().canWrite()) {
138                     
139                     // Creates domain factory
140
DomainCreator factory = new DomainCreator();
141                     
142                     // try to find a good spot for the ports that won't clash
143
// with another user... this can never be completely correct.
144
int bucket = 0;
145                     String JavaDoc home = System.getProperty("user.home"); // NOI18N
146
if (null != home && home.trim().length() > 0) {
147                         bucket = home.hashCode() % 10000;
148                     }
149                     
150                     // Checks ports availability
151
int adminPort = DEFAULT_ADMIN_PORT + bucket;
152                     int httpPort = DEFAULT_HTTP_PORT + bucket;
153                     int jmsPort = DEFAULT_JMS_PORT + bucket;
154                     int orbPort = DEFAULT_ORB_PORT + bucket;
155                     int httpsPort = DEFAULT_HTTPS_PORT + bucket;
156                     int orbSSLPort = DEFAULT_ORB_SSL_PORT + bucket;
157                     int orbMAPort = DEFAULT_ORB_MUTUALAUTH_PORT + bucket;
158                     int jmxPort = DEFAULT_JMX_PORT + bucket;
159                     
160                     while (!isPortAvaibale(adminPort) || !isPortAvaibale(httpPort)
161                             || !isPortAvaibale(jmsPort) || !isPortAvaibale(orbPort)
162                             || !isPortAvaibale(httpsPort) || !isPortAvaibale(orbSSLPort)
163                             || !isPortAvaibale(orbMAPort) || !isPortAvaibale(jmxPort)) {
164                         int incr = (new Random JavaDoc()).nextInt(100)+1;
165                         adminPort += incr;
166                         httpPort += incr;
167                         jmsPort += incr;
168                         orbPort += incr;
169                         httpsPort += incr;
170                         orbSSLPort += incr;
171                         orbMAPort += incr;
172                         jmxPort += incr;
173                         if (jmxPort >= 0xffff) {
174                             // we ran out of ports
175
break;
176                         }
177                     }
178                     
179                     // Set all needed default properties
180
factory.setProperty(PROP_DISPLAY_NAME, DEFAULT_SERVER_NAME);
181                     factory.setProperty(PROP_USER_NAME, DEFAULT_USERNAME);
182                     factory.setProperty(PROP_PASSWORD, DEFAULT_PASSWORD);
183                     factory.setProperty(PROP_HOST, DEFAULT_HOST);
184                     factory.setProperty(PROP_INSTALL_LOCATION, domainRoot.getAbsolutePath());
185                     factory.setProperty(PROP_PLATFORM_LOCATION, serverRoot);
186                     factory.setProperty(PROP_PORT, String.valueOf(adminPort));
187                     factory.setProperty(PROP_DOMAIN, DEFAULT_DOMAIN_NAME);
188                     factory.setProperty(PROP_INSTANCE_PORT, String.valueOf(httpPort));
189                     factory.setProperty(PROP_JMS_PORT, String.valueOf(jmsPort));
190                     factory.setProperty(PROP_ORB_LISTENER_PORT, String.valueOf(orbPort));
191                     factory.setProperty(PROP_HTTP_SSL_PORT, String.valueOf(httpsPort));
192                     factory.setProperty(PROP_ORB_SSL_PORT, String.valueOf(orbSSLPort));
193                     factory.setProperty(PROP_ORB_MUTUAL_AUTH_PORT, String.valueOf(orbMAPort));
194                     factory.setProperty(PROP_ADMIN_JMX_PORT, String.valueOf(jmxPort));
195                     factory.setProperty(PROP_TYPE, AddDomainWizardIterator.PERSONAL);
196                     
197                     if (jmxPort < 0xffff) {
198                         // Creates and register domain
199
retVal = factory.createInstance();
200                     }
201                 } else {
202                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
203                             NbBundle.getMessage(DomainCreator.class, "MSG_DomainDirWriteProtect",
204                             domainRoot.getParentFile().getAbsolutePath()));
205                 }
206             }
207         }
208         return retVal;
209     }
210     
211     /**
212      * Sets property in WizardDescriptor
213      */

214     private void setProperty(String JavaDoc property, Object JavaDoc value) {
215         wizardDescriptor.putProperty(property, value);
216     }
217     
218     /**
219      * Initializes WizardDescriptor into WizardIterator and calls
220      * createInstance() method on WizardIterator which creates and register
221      * personal domain.
222      */

223     private InstanceProperties createInstance() {
224         wizard.initialize(wizardDescriptor);
225         return wizard.createInstance();
226     }
227     
228     
229     /**
230      * Checks if the port is available
231      *
232      * @return true if the port is free or false when is being used
233      */

234     private static boolean isPortAvaibale(int port) {
235         String JavaDoc host = DEFAULT_HOST;
236         boolean retVal = true;
237         Socket JavaDoc socket = null;
238         try {
239             InetSocketAddress JavaDoc isa = new InetSocketAddress JavaDoc(host, port);
240             socket = new Socket JavaDoc();
241             socket.connect(isa, 1);
242             retVal = false;
243         } catch (IOException JavaDoc e) {
244             // TODO avoid the exception as flow of control
245
retVal = true;
246             //return true;
247
} finally {
248             if (socket != null && !retVal) {
249                 try {
250                     socket.close();
251                 } catch (IOException JavaDoc ioe) {
252                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
253                             ioe);
254                 }
255             }
256         }
257         return retVal;
258     }
259 }
260
Popular Tags