KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > pe > PEDomainsManager


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.admin.servermgmt.pe;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import com.sun.enterprise.util.OS;
30 import com.sun.enterprise.util.io.FileUtils;
31 import com.sun.enterprise.util.i18n.StringManager;
32
33 import com.sun.enterprise.admin.servermgmt.DomainException;
34 import com.sun.enterprise.admin.servermgmt.DomainConfig;
35 import com.sun.enterprise.admin.servermgmt.RepositoryConfig;
36 import com.sun.enterprise.admin.servermgmt.RepositoryException;
37 import com.sun.enterprise.admin.servermgmt.DomainsManager;
38 import com.sun.enterprise.admin.servermgmt.InstancesManager;
39 import com.sun.enterprise.admin.servermgmt.DomainConfigValidator;
40 import com.sun.enterprise.admin.servermgmt.StringValidator;
41 import com.sun.enterprise.admin.servermgmt.FileValidator;
42 import com.sun.enterprise.admin.servermgmt.RepositoryNameValidator;
43 import com.sun.enterprise.admin.servermgmt.DomainXmlEventListener;
44 import com.sun.enterprise.admin.servermgmt.RepositoryManager;
45 import com.sun.enterprise.admin.servermgmt.util.DomainXmlSAXParser;
46
47 import com.sun.enterprise.admin.common.Status;
48
49 import com.sun.enterprise.admin.util.TokenValueSet;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.BitSet JavaDoc;
53
54 public class PEDomainsManager extends RepositoryManager
55     implements DomainsManager
56 {
57     /**
58      * i18n strings manager object
59      */

60     private static final StringManager strMgr =
61         StringManager.getManager(PEDomainsManager.class);
62   
63     public PEDomainsManager()
64     {
65         super();
66     }
67     
68     //PE does not require that an admin user / password is available at start-domain time.
69
//SE/SEE does require it.
70
public BitSet JavaDoc getDomainFlags()
71     {
72         BitSet JavaDoc bs = new BitSet JavaDoc();
73         bs.set(DomainConfig.K_FLAG_START_DOMAIN_NEEDS_ADMIN_USER, false);
74         return bs;
75     }
76     
77     public void validateDomain(DomainConfig domainConfig, boolean domainExists)
78         throws DomainException
79     {
80         try {
81             checkRepository(domainConfig, domainExists, domainExists);
82         } catch (RepositoryException ex) {
83             throw new DomainException(ex);
84         }
85     }
86
87     public void validateAdminUserAndPassword(DomainConfig domainConfig)
88         throws DomainException
89     {
90         try {
91             validateAdminUserAndPassword(domainConfig, getDomainUser(domainConfig),
92                 getDomainPasswordClear(domainConfig));
93         } catch (RepositoryException ex) {
94             throw new DomainException(ex);
95         }
96     }
97     
98     public void validateMasterPassword(DomainConfig domainConfig)
99         throws DomainException
100     {
101         try {
102             validateMasterPassword(domainConfig, getMasterPasswordClear(domainConfig));
103         } catch (RepositoryException ex) {
104             throw new DomainException(ex);
105         }
106     }
107     
108     public void createDomain(DomainConfig domainConfig)
109         throws DomainException
110     {
111         PEFileLayout layout = getFileLayout(domainConfig);
112         
113         try {
114             new RepositoryNameValidator(strMgr.getString("domainsRoot")).
115                     checkValidXmlToken(
116                         layout.getRepositoryRootDir().getAbsolutePath());
117             layout.createRepositoryRoot();
118             new PEDomainConfigValidator().validate(domainConfig);
119             checkRepository(domainConfig, false);
120         } catch (Exception JavaDoc ex) {
121             throw new DomainException(ex);
122         }
123         
124         try {
125             String JavaDoc masterPassword = getMasterPasswordClear(domainConfig);
126             layout.createRepositoryDirectories();
127             createDomainXml(domainConfig);
128             createDomainXmlEvents(domainConfig);
129             createScripts(domainConfig);
130             createServerPolicyFile(domainConfig);
131             createAdminKeyFile(domainConfig, getDomainUser(domainConfig),
132                 getDomainPasswordClear(domainConfig));
133             createKeyFile(domainConfig, getDomainUser(domainConfig),
134                 getDomainPasswordClear(domainConfig));
135             createAppClientContainerXml(domainConfig);
136             createIndexFile(domainConfig);
137             createDefaultWebXml(domainConfig);
138             createLoginConf(domainConfig);
139             createWssServerConfig(domainConfig);
140             createWssServerConfigOld(domainConfig);
141             createSSLCertificateDatabase(domainConfig, masterPassword);
142             changeMasterPasswordInMasterPasswordFile(domainConfig, masterPassword,
143                 saveMasterPassword(domainConfig));
144             createPasswordAliasKeystore(domainConfig, masterPassword);
145             createTimerWal(domainConfig);
146             createTimerDbn(domainConfig);
147             createMQInstance(domainConfig);
148             if (layout.getDerbyEjbTimerSqlFile().exists()) //will be cleaned up once derby integrates, 05/19/2005
149
handleDerby(domainConfig);
150             setPermissions(domainConfig);
151         } catch (DomainException de) {
152             //rollback
153
FileUtils.liquidate(getDomainDir(domainConfig));
154             throw de;
155         } catch (Exception JavaDoc ex) {
156             //rollback
157
FileUtils.liquidate(getDomainDir(domainConfig));
158             throw new DomainException(ex);
159         }
160     }
161     
162     /**
163      * Sets the permissions for the domain directory, its config directory,
164      * startserv/stopserv scripts etc.
165      */

166     protected void setPermissions(DomainConfig domainConfig) throws DomainException
167     {
168         final PEFileLayout layout = getFileLayout(domainConfig);
169         try {
170             //4958533
171
chmod("-R u+x ", layout.getBinDir());
172             chmod("-R g-rwx,o-rwx ", layout.getConfigRoot());
173             //4958533
174
} catch (Exception JavaDoc e) {
175             throw new DomainException(
176                 strMgr.getString("setPermissionError"), e);
177         }
178     }
179
180     public void deleteDomain(DomainConfig domainConfig)
181         throws DomainException
182     {
183         try {
184             deleteRepository(domainConfig);
185         } catch (Exception JavaDoc e) {
186             throw new DomainException(e);
187         }
188     }
189
190     public void startDomain(DomainConfig domainConfig)
191         throws DomainException
192     {
193         try {
194             checkRepository(domainConfig);
195             String JavaDoc[] options = getInteractiveOptions(
196                 (String JavaDoc)domainConfig.get(DomainConfig.K_USER),
197                 (String JavaDoc)domainConfig.get(DomainConfig.K_PASSWORD),
198                 (String JavaDoc)domainConfig.get(DomainConfig.K_MASTER_PASSWORD),
199                 (HashMap JavaDoc)domainConfig.get(DomainConfig.K_EXTRA_PASSWORDS));
200             getInstancesManager(domainConfig).startInstance(options);
201         } catch (Exception JavaDoc e) {
202             throw new DomainException(e);
203         }
204     }
205
206     public void stopDomain(DomainConfig domainConfig)
207         throws DomainException
208     {
209         try {
210             checkRepository(domainConfig);
211             getInstancesManager(domainConfig).stopInstance();
212         } catch (Exception JavaDoc e) {
213             throw new DomainException(e);
214         }
215     }
216
217     public String JavaDoc[] listDomainsAndStatus(DomainConfig domainConfig)
218         throws DomainException
219     {
220         try {
221             return listDomainsAndStatusAsString(domainConfig);
222         } catch (Exception JavaDoc e) {
223             throw new DomainException(e);
224         }
225     }
226
227     /**
228      * Lists all the domains.
229      */

230     public String JavaDoc[] listDomains(DomainConfig domainConfig)
231         throws DomainException
232     {
233         try {
234             return listRepository(domainConfig);
235         } catch (Exception JavaDoc e) {
236             throw new DomainException(e);
237         }
238     }
239
240
241     protected void createDomainXmlEvents(DomainConfig domainConfig)
242         throws DomainException {
243             try {
244                 final PEFileLayout layout = getFileLayout(domainConfig);
245                 final File JavaDoc domainXml = layout.getDomainConfigFile();
246                 DomainXmlSAXParser parser = new DomainXmlSAXParser();
247                 try {
248                     parser.parse(domainXml,layout.getDtdFile());
249                 }
250                 catch(Exception JavaDoc e) {
251                     throw new DomainException(
252                         strMgr.getString("domainXmlNotParsed"), e);
253                 }
254                 String JavaDoc className = parser.getDomainXmlEventListenerClass();
255                 if(className!=null) {
256                     DomainXmlEventListener listener = (DomainXmlEventListener) Class.forName(className).newInstance();
257                     listener.handleCreateEvent(domainConfig);
258                 }
259             }
260             catch(Exception JavaDoc e) {
261                 throw new DomainException(
262                     strMgr.getString("domainXmlEventsNotCreated"), e);
263             }
264     }
265
266     /**
267      * The EEDomains manager needs to have an augmented set of tokens
268      */

269     protected TokenValueSet getDomainXmlTokens(DomainConfig domainConfig) {
270         return PEDomainXmlTokens.getTokenValueSet(domainConfig);
271     }
272     
273     protected void createDomainXml(DomainConfig domainConfig)
274         throws DomainException
275     {
276         try
277         {
278             final PEFileLayout layout = getFileLayout(domainConfig);
279             final File JavaDoc domainXml = layout.getDomainConfigFile();
280             final File JavaDoc domainXmlTemplate;
281             String JavaDoc templateName = (String JavaDoc)domainConfig.get(DomainConfig.K_TEMPLATE_NAME);
282             if((templateName == null)||(templateName.equals("")))
283                 domainXmlTemplate = layout.getDomainXmlTemplate();
284             else
285                 domainXmlTemplate = layout.getDomainXmlTemplate(templateName);
286
287             TokenValueSet tokens = getDomainXmlTokens(domainConfig);
288             generateFromTemplate(tokens, domainXmlTemplate, domainXml);
289         }
290         catch(Exception JavaDoc e)
291         {
292             throw new DomainException(
293                 strMgr.getString("domainXmlNotCreated"), e);
294         }
295     }
296
297     protected void createScripts(DomainConfig domainConfig)
298         throws DomainException
299     {
300         final TokenValueSet tokens = PEScriptsTokens.getTokenValueSet(domainConfig);
301         createStartServ(domainConfig, tokens);
302         createStopServ(domainConfig, tokens);
303     }
304
305     void createStartServ(DomainConfig domainConfig,
306         TokenValueSet tokens) throws DomainException
307     {
308         try
309         {
310             final PEFileLayout layout = getFileLayout(domainConfig);
311             final File JavaDoc startServTemplate = layout.getStartServTemplate();
312             final File JavaDoc startServ = layout.getStartServ();
313             generateFromTemplate(tokens, startServTemplate, startServ);
314         }
315         catch (Exception JavaDoc e)
316         {
317             throw new DomainException(
318                 strMgr.getString("startServNotCreated"), e);
319         }
320     }
321
322     void createStopServ(DomainConfig domainConfig,
323         TokenValueSet tokens) throws DomainException
324     {
325         try
326         {
327             final PEFileLayout layout = getFileLayout(domainConfig);
328             final File JavaDoc stopServTemplate = layout.getStopServTemplate();
329             final File JavaDoc stopServ = layout.getStopServ();
330             generateFromTemplate(tokens, stopServTemplate, stopServ);
331         }
332         catch (Exception JavaDoc e)
333         {
334             throw new DomainException(
335                 strMgr.getString("stopServNotCreated"), e);
336         }
337     }
338
339     protected void createAppClientContainerXml(
340         DomainConfig domainConfig) throws DomainException
341     {
342         try
343         {
344             final PEFileLayout layout = getFileLayout(domainConfig);
345             final File JavaDoc accXmlTemplate =
346                 layout.getAppClientContainerXmlTemplate();
347             final File JavaDoc accXml = layout.getAppClientContainerXml();
348             TokenValueSet tokens = PEAccXmlTokens.getTokenValueSet(domainConfig);
349             generateFromTemplate(tokens, accXmlTemplate, accXml);
350         }
351         catch(Exception JavaDoc e)
352         {
353             throw new DomainException(strMgr.getString("accXmlNotCreated"), e);
354         }
355     }
356
357     protected void createIndexFile(
358         DomainConfig domainConfig) throws DomainException
359     {
360         final PEFileLayout layout = getFileLayout(domainConfig);
361         final File JavaDoc src = layout.getIndexFileTemplate();
362         final File JavaDoc dest = layout.getIndexFile();
363         try
364         {
365             FileUtils.copy(src, dest);
366         }
367         catch (IOException JavaDoc ioe)
368         {
369             throw new DomainException(
370                 strMgr.getString("indexFileNotCreated"), ioe);
371         }
372     }
373
374     protected void createDefaultWebXml(
375         DomainConfig domainConfig) throws DomainException
376     {
377         final PEFileLayout layout = getFileLayout(domainConfig);
378         final File JavaDoc src = layout.getDefaultWebXmlTemplate();
379         final File JavaDoc dest = layout.getDefaultWebXml();
380         try
381         {
382             FileUtils.copy(src, dest);
383         }
384         catch (IOException JavaDoc ioe)
385         {
386             throw new DomainException(
387                 strMgr.getString("defaultWebXmlNotCreated"), ioe);
388         }
389     }
390     
391     protected void createLoginConf(
392         RepositoryConfig config) throws DomainException
393     {
394         final PEFileLayout layout = getFileLayout(config);
395         final File JavaDoc src = layout.getLoginConfTemplate();
396         final File JavaDoc dest = layout.getLoginConf();
397         try
398         {
399             FileUtils.copy(src, dest);
400         }
401         catch (IOException JavaDoc ioe)
402         {
403             throw new DomainException(
404                 strMgr.getString("loginConfNotCreated"), ioe);
405         }
406     }
407
408     protected void createWssServerConfigOld(RepositoryConfig config)
409         throws DomainException
410     {
411         final PEFileLayout layout = getFileLayout(config);
412         final File JavaDoc src = layout.getWssServerConfigOldTemplate();
413         final File JavaDoc dest = layout.getWssServerConfigOld();
414         try
415         {
416             FileUtils.copy(src, dest);
417         }
418         catch (IOException JavaDoc ioe)
419         {
420             throw new DomainException(
421                 strMgr.getString("wssserverconfignotcreated"), ioe);
422         }
423
424     }
425
426     protected void createWssServerConfig(RepositoryConfig config)
427         throws DomainException
428     {
429         final PEFileLayout layout = getFileLayout(config);
430         final File JavaDoc src = layout.getWssServerConfigTemplate();
431         final File JavaDoc dest = layout.getWssServerConfig();
432         try
433         {
434             FileUtils.copy(src, dest);
435         }
436         catch (IOException JavaDoc ioe)
437         {
438             throw new DomainException(
439                 strMgr.getString("wssserverconfignotcreated"), ioe);
440         }
441
442     }
443
444     protected File JavaDoc getDomainDir(DomainConfig domainConfig)
445     {
446         return getRepositoryDir(domainConfig);
447     }
448
449     protected File JavaDoc getDomainRoot(DomainConfig domainConfig)
450     {
451         return getRepositoryRootDir(domainConfig);
452     }
453
454     String JavaDoc getDefaultInstance()
455     {
456         return PEFileLayout.DEFAULT_INSTANCE_NAME;
457     }
458      
459     /** Returns the domain user from the domainConfig.
460      * @param Map that represents the domain configuration
461      * @return String representing the domain user if the given map contains
462      * it, null otherwise
463     */

464
465     protected static String JavaDoc getDomainUser(DomainConfig domainConfig)
466     {
467         return ( (String JavaDoc) domainConfig.get(DomainConfig.K_USER) );
468     }
469     
470     /** Returns the domain user's password in cleartext from the domainConfig.
471      * @param Map that represents the domain configuration
472      * @return String representing the domain user password if the
473      * given map contains it, null otherwise
474     */

475
476     protected static String JavaDoc getDomainPasswordClear(DomainConfig domainConfig)
477     {
478         return ( (String JavaDoc) domainConfig.get(DomainConfig.K_PASSWORD) );
479     }
480     
481     protected static String JavaDoc getMasterPasswordClear(DomainConfig domainConfig)
482     {
483         return ((String JavaDoc)domainConfig.get(DomainConfig.K_MASTER_PASSWORD));
484     }
485     
486     protected static String JavaDoc getNewMasterPasswordClear(DomainConfig domainConfig)
487     {
488         return ((String JavaDoc)domainConfig.get(DomainConfig.K_NEW_MASTER_PASSWORD));
489     }
490     
491     protected static boolean saveMasterPassword(DomainConfig domainConfig) {
492         Boolean JavaDoc b = (Boolean JavaDoc)domainConfig.get(DomainConfig.K_SAVE_MASTER_PASSWORD);
493         return b.booleanValue();
494     }
495     
496     /**
497      * Changes the master password for the domain
498      */

499     public void changeMasterPassword(DomainConfig config) throws DomainException
500     {
501         try {
502             //Ensure that the entity is stopped
503
final int status = getInstancesManager(config).getInstanceStatus();
504             if (status != Status.kInstanceNotRunningCode) {
505                 throw new DomainException(
506                     strMgr.getString("cannotChangePassword_invalidState",
507                         config.getDisplayName(), Status.getStatusString(status)));
508             }
509         
510             String JavaDoc oldPass = getMasterPasswordClear(config);
511             String JavaDoc newPass = getNewMasterPasswordClear(config);
512             
513             //Change the password of the keystore alias file
514
changePasswordAliasKeystorePassword(config, oldPass, newPass);
515             
516             //Change the password of the keystore and truststore
517
changeSSLCertificateDatabasePassword(config, oldPass, newPass);
518
519             //Change the password in the masterpassword file or delete the file if it is
520
//not to be saved.
521
changeMasterPasswordInMasterPasswordFile(config, newPass, saveMasterPassword(config));
522         } catch (Exception JavaDoc ex) {
523             throw new DomainException(
524                 strMgr.getString("masterPasswordNotChanged"), ex);
525         }
526     }
527     
528     public String JavaDoc[] getExtraPasswordOptions(DomainConfig config)
529         throws DomainException
530     {
531         return null;
532     }
533 }
534
Popular Tags