KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > instance > InstanceDefinition


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.instance;
25
26 //JDK imports
27
import java.net.InetAddress JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29
30 //iAS imports
31
import com.sun.enterprise.util.OS;
32 import com.sun.enterprise.util.StringUtils;
33 import com.sun.enterprise.config.*;
34 import com.sun.enterprise.config.serverbeans.ServerTags;
35 import com.sun.enterprise.config.serverbeans.JmsService;
36 import com.sun.enterprise.config.serverbeans.ElementProperty;
37 import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
38 import com.sun.enterprise.server.Constants;
39
40 /**
41     @author kedar
42     @version 1.0
43 */

44
45 /**
46     A class that defines the Server Instance so that it can be created.
47     Identifies all the required attributes of an (iAS) Instance.
48     Depends upon the value of Install Root ie. "com.sun.aas.instanceRoot"
49 */

50
51 public class InstanceDefinition
52 {
53     public static final int DEFAULT_PORT = 80;
54     public static final int DEFAULT_JMS_PORT = 7676;
55
56     public static final String JavaDoc DEFAULT_JMS_USER = "admin";
57     public static final String JavaDoc DEFAULT_JMS_PW = "admin";
58
59     public static final String JavaDoc SPACE = " ";
60     public static final String JavaDoc WINDOWS_BIN_EXT = ".exe";
61     public static final String JavaDoc BIN_DIR_NAME = "bin";
62     public static final String JavaDoc LIB_DIR_NAME = "lib";
63
64     private String JavaDoc mJavaHome = System.getProperty("java.home");
65     private String JavaDoc mImqHome = null;
66     private String JavaDoc mServerName = null;
67     private int mHttpPort = DEFAULT_PORT;
68     private String JavaDoc mIdentifier = null;
69     private String JavaDoc mMailHost = null;
70     private String JavaDoc mUser = null;
71     private String JavaDoc mDocRoot = null;
72     private String JavaDoc mPortString = null;
73
74     private int mJMSPort = DEFAULT_JMS_PORT;
75     private String JavaDoc mJMSPortString = null;
76     private String JavaDoc mJMSUser = null;
77     private String JavaDoc mJMSPasswd = null;
78
79     /*
80         The cases are different for Windows and non-Windows platforms.
81         For Non-Windows platforms, starting an instance in secure and
82         non-secure mode is different than that on windows platform.
83         For Non-windows platforms, the startserv/stopserv scripts in
84         addition to the gettokens executable is required.
85         Also the restart of a server instance is possible only on
86         Non-windows platforms.
87     */

88     
89     public static final String JavaDoc UNIX_START_COMMAND_NAME = "startserv";
90     public static final String JavaDoc UNIX_STOP_COMMAND_NAME = "stopserv";
91     public static final String JavaDoc UNIX_GETTOKENS_COMMAND_NAME = "gettokens";
92     public static final String JavaDoc UNIX_RESTART_COMMAND_NAME = "restartserv";
93
94     
95     /*
96         On Windows platform, unlike the non-windows platforms, there is different
97         executable that is used. The name of that executable is startsec.exe. This
98         executable calls into iws source code for pcontrol.cpp which uses shared
99         memory for passing the key-pair (db) password and other passwords to the
100         server process.
101         Note that this code runs in admin-server's memory and it forks startsec.exe
102         which gets its own environment. The most important variable that is necessary
103         for startsec.exe to run is ADMSERV_ROOT which points to the parent directory
104         of the folder where the entire directory structure of admin-server (and hence
105         all the other instances in same domain) is stored.
106     */

107     public static final String JavaDoc WIN_START_COMMAND_NAME = "startsec.exe";
108     public static final String JavaDoc WIN_STOP_COMMAND_NAME = "stopserv.bat";
109     public static final String JavaDoc WIN_GETTOKENS_COMMAND_NAME = "gettokens.exe";
110
111     public final String JavaDoc JMS_NODE_PATH = ServerXPathHelper.XPATH_JMS_SERVICE;
112
113     /**
114         Creates new InstanceDefinition.
115         @param serverName String representing the fully qualified hostName e.g. www.sun.com
116         @param httpPort is integer specifying port at which this server should start listening to requests.
117         @param identifier String that is the server-id e.g. prod_server.
118         @param mailHost is the name of the mail host.
119         @param user is the name of user with which we can start this instance. (Unix/Linux)
120         @param docRoot is the absolute location for the webserver docroot.
121         @param jmsPort integer represeting the port of jms-service.
122         @param jmsUser user name to connect to the jms-service.
123         @param jmsPasswd password to connect to the jms-service.
124     */

125     
126     public InstanceDefinition(String JavaDoc serverName, int httpPort,
127         String JavaDoc identifier, String JavaDoc mailHost, String JavaDoc user, String JavaDoc docRoot,
128         int jmsPort, String JavaDoc jmsUser, String JavaDoc jmsPasswd) {
129         initialize(serverName, httpPort, identifier, mailHost, user, docRoot,
130                    jmsPort, jmsUser, jmsPasswd);
131     }
132
133     /**
134         Creates new InstanceDefinition with the given parameters.
135         @param id String representing the unique id of the server. (e.g. prod_server)
136         @param port is the HTTP port.
137         Note that the servername would default to the local machine's IP address,
138         mailhost will default to the "localhost", server user will default to
139         current user and docroot will default to <ias-install>/docs
140     */

141     public InstanceDefinition (String JavaDoc id, int port) {
142         if (id == null || port <= 0)
143             throw new IllegalArgumentException JavaDoc(Localizer.getValue(ExceptionType.ILLEGAL_PORT));
144         String JavaDoc serverName = createLocalHostName();
145         String JavaDoc mailHost = createLocalHostName();
146         String JavaDoc user = System.getProperty("user.name");
147         String JavaDoc docRoot = ServerManager.INSTANCE_CFG_ROOT
148                 + "/" + ServerManager.DOC_DIR_NAME;
149         initialize(serverName, port, id, mailHost, user, docRoot,
150                    DEFAULT_JMS_PORT, DEFAULT_JMS_USER, DEFAULT_JMS_PW);
151     }
152     
153     private void initialize(String JavaDoc serverName, int httpPort, String JavaDoc identifier,
154         String JavaDoc mailHost, String JavaDoc user, String JavaDoc docRoot, int jmsPort,
155         String JavaDoc jmsUser, String JavaDoc jmsPasswd) {
156         if (serverName == null|| identifier == null||
157             mailHost == null|| user == null||
158             user == null|| docRoot == null||
159             jmsUser == null|| jmsPasswd == null ) {
160             throw new IllegalArgumentException JavaDoc();
161         }
162         if (httpPort <= 0 || jmsPort <= 0) {
163             throw new IllegalArgumentException JavaDoc();
164         }
165         mServerName = serverName;
166         mHttpPort = httpPort;
167         mIdentifier = identifier;
168         mMailHost = mailHost;
169         mUser = user;
170         mDocRoot = docRoot;
171         mPortString = "" + mHttpPort;
172         mJMSPort = jmsPort;
173         mJMSPortString = jmsPort +"";
174         mJMSUser = jmsUser;
175         mJMSPasswd = jmsPasswd;
176     }
177
178     public String JavaDoc getID() {
179         return mIdentifier;
180     }
181     
182     public int getPort() {
183         return mHttpPort;
184     }
185
186     public String JavaDoc getServerName() {
187         return mServerName;
188     }
189
190     public String JavaDoc getAdminJavaHome() throws ConfigException {
191         ConfigContext configContext;
192         InstanceEnvironment instanceEnvironment =
193                new InstanceEnvironment(ServerManager.ADMINSERVER_ID);
194         String JavaDoc fileUrl = instanceEnvironment.getConfigFilePath();
195         configContext = ConfigFactory.createConfigContext(fileUrl);
196         ConfigBean configbean = ConfigBeansFactory.getConfigBeanByXPath(
197                 configContext, ServerXPathHelper.XPATH_JAVACONFIG);
198         mJavaHome = configbean.getAttributeValue(ServerTags.JAVA_HOME);
199         return mJavaHome;
200     }
201
202     public String JavaDoc getDocRoot()
203     {
204         return ( mDocRoot );
205     }
206
207       // The following is copied in:
208
// tools.CreateInstanceCommand, tools.CreateDomainCommand,
209
// instance.InstanceDefinition.
210
// We need to refactor this method into one common class. - It
211
// has not state, so could be refactored as a static method.
212

213     private String JavaDoc createLocalHostName() {
214         try {
215             return InetAddress.getLocalHost().getHostName();
216         }
217         catch(UnknownHostException JavaDoc ue) {
218           return "localhost";
219         }
220     }
221    
222     
223     /**
224         Returns the complete path of the <code> start </code>
225         command for starting this instance. Note that it returns the
226         platform-specific string that could be exec'ed.
227      
228         @return String representing path of start command for this instance.
229     */

230
231     public String JavaDoc[] getStartCommand() {
232         String JavaDoc[] startCommand = null;
233         /* Note that for windows, the startup executable is stored
234            at the install-root - one per installation. startsec.exe
235            just takes the instance name as the only parameter.
236         */

237         if (OS.isWindows()) {
238             startCommand = getCompleteWindowsStartCommand();
239         }
240         else {
241         /* Note that for non-windows platforms, the startup executable is stored
242            at the instance directory - one per instance. startserv
243            script does not take any parameter.
244         */

245             startCommand = getCompleteNonWindowsStartCommand();
246         }
247         return ( startCommand );
248     }
249     
250     /**
251      * Returns the windows start command. Note that it is
252      * [install-root]/bin/startsec.exe.
253     */

254     private String JavaDoc[] getCompleteWindowsStartCommand()
255     {
256
257         String JavaDoc[] names = new String JavaDoc[] {
258             System.getProperty(Constants.INSTALL_ROOT), /* install-root */
259             BIN_DIR_NAME, /* "bin" */
260             WIN_START_COMMAND_NAME /* "startsec.exe" */
261         };
262         String JavaDoc programName = StringUtils.makeFilePath(names, false);
263
264         /* startsec requires server-id as the first(only) parameter */
265
266         return ( new String JavaDoc[] {
267             programName, /* path to strtsec */
268             mIdentifier, /* "server-id" */
269             System.getProperty(Constants.IAS_ROOT), /* path to domain root */
270         } );
271     }
272     
273     
274     /* Returns the start command for non windows platforms.
275        This is <instance-root>/bin/startserv
276     */

277     private String JavaDoc[] getCompleteNonWindowsStartCommand()
278     {
279         String JavaDoc[] names = new String JavaDoc[] {
280             System.getProperty(Constants.IAS_ROOT), /* upto domain */
281             BIN_DIR_NAME, /* "bin" */
282             UNIX_START_COMMAND_NAME /* "startserv" */
283         };
284         String JavaDoc programName = StringUtils.makeFilePath(names, false);
285         
286         return ( new String JavaDoc[]{programName} );
287     }
288     /**
289         Returns the complete path of the <code> gettokens </code>
290         command for getting security tokens of this instance.
291         It returns the array of Strings which form the complete
292         command line.
293      
294         @return String[] representing getSecurityTokensCommand
295     */

296
297     public String JavaDoc[] getGetSecurityTokensCommand() {
298         String JavaDoc[] command = null;
299         String JavaDoc onlyCommand = null;
300         if (OS.isWindows()) {
301             onlyCommand = getWindowsSecTokensCommand();
302         }
303         else {
304             onlyCommand = getNonWindowsSecTokensCommand();
305         }
306         
307         command = new String JavaDoc[] {
308             onlyCommand, /* path of gettokens */
309             mIdentifier, /* "instance-id": param1 */
310             System.getProperty(Constants.IAS_ROOT), /* path to domain root */
311         };
312         return ( command );
313     }
314     
315     /* Returns the fully qualified name of the gettokens command for
316        windows, where it is in install-root/bin.*/

317     private String JavaDoc getWindowsSecTokensCommand()
318     {
319         String JavaDoc[] names = new String JavaDoc[] {
320             System.getProperty(Constants.INSTALL_ROOT), /* install-root */
321             BIN_DIR_NAME, /* "bin" */
322             WIN_GETTOKENS_COMMAND_NAME /* "gettokens.exe" */
323         };
324
325         return ( StringUtils.makeFilePath(names, false) );
326     }
327     /* Returns the fully qualified name of the gettokens command for
328        non-windows, where it is in install-root/lib.*/

329
330     private String JavaDoc getNonWindowsSecTokensCommand()
331     {
332         String JavaDoc[] names = new String JavaDoc[] {
333             System.getProperty(Constants.INSTALL_ROOT), /* install-root */
334             LIB_DIR_NAME, /* "lib" */
335             UNIX_GETTOKENS_COMMAND_NAME /* "gettokens" */
336         };
337
338         return ( StringUtils.makeFilePath(names, false) );
339     }
340
341     /**
342         Returns the complete path of the <code> stop </code>
343         command for stopping this instance. Note that it returns the
344         platform-specific string that could be exec'ed.
345      
346         @return String representing path of stop command for this instance.
347     */

348
349     public String JavaDoc[] getStopCommand() {
350         String JavaDoc[] stopCommand = new String JavaDoc[1];
351
352         String JavaDoc command = null;
353         if (OS.isWindows()) {
354             command = WIN_STOP_COMMAND_NAME;
355         }
356         else {
357             command = UNIX_STOP_COMMAND_NAME;
358         }
359         String JavaDoc[] names = new String JavaDoc[] {
360             System.getProperty(Constants.IAS_ROOT), /*upto a domain */
361             BIN_DIR_NAME, /* "bin" */
362             command /* "stopserv" or "stopserv.bat"*/
363         };
364
365         stopCommand[0] = StringUtils.makeFilePath(names, false);
366         
367         return ( stopCommand );
368     }
369
370     /**
371         Returns the complete path of the <code> restart </code>
372         script for restarting this instance. Note that it returns the
373         platform-specific string that could be exec'ed.
374      
375         @return String representing path of restart command for this
376         instance.
377     */

378
379     public String JavaDoc[] getRestartCommand() {
380         if (OS.isWindows()) {
381             throw new UnsupportedOperationException JavaDoc(Localizer.getValue(ExceptionType.ILLEGAL_RESTART));
382         }
383         String JavaDoc[] restartCommand = new String JavaDoc[1];
384
385         String JavaDoc[] names = new String JavaDoc[] {
386             System.getProperty(Constants.IAS_ROOT), /* upto a domain */
387             BIN_DIR_NAME, /* "bin" */
388             UNIX_RESTART_COMMAND_NAME /* "restartserv" */
389         };
390         restartCommand[0] = StringUtils.makeFilePath(names, false);
391
392         return ( restartCommand );
393     }
394
395     /**
396         Overridden definition of toString for this Instance Definition.
397         Shows the ServerName, Port, Identifier, MailHostName, User and
398         Doc Root for the instance.
399     */

400     
401     public String JavaDoc toString() {
402         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
403         sb.append(mServerName);
404         sb.append(SPACE + mPortString);
405         sb.append(SPACE + mIdentifier);
406         sb.append(SPACE + mMailHost);
407         sb.append(SPACE + mUser);
408         sb.append(SPACE + mDocRoot);
409         
410         return ( sb.toString() );
411     }
412
413     /**
414      * Sets the user (owner) of the instance.
415      */

416     public void setUser(String JavaDoc user)
417     {
418         if ((user != null) && (user.length() > 0))
419         {
420             mUser = user;
421         }
422     }
423
424     /**
425      * Getter for the instance-user instance variable.
426      */

427     public String JavaDoc getUser()
428     {
429         return mUser;
430     }
431 }
432
Popular Tags