KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > SMFServiceHandler


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;
25
26 import com.sun.enterprise.util.i18n.StringManager;
27 import com.sun.enterprise.admin.util.LineTokenReplacer;
28 import com.sun.enterprise.admin.util.TokenValue;
29 import com.sun.enterprise.admin.util.TokenValueSet;
30 import com.sun.enterprise.util.OS;
31 import com.sun.enterprise.util.ProcessExecutor;
32 import java.io.File JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37
38 /** An implementation of {@link ServiceHandler} interface for the Solaris 10 SMF.
39  * The service is created as a standard S10 service. The class tries to validate
40  * and import the service in a disabled state. The users are expected to enable
41  * the service later.
42  * Here is a typical way for clients to use this class ensuring that they are on S10 and SMF is enabled:
43  * <pre>
44  * final ServiceHandler sh = new SMFServiceHandler();
45  * final SMFService service = new SMFService();
46  * //configure service
47  * service.setDate(new Date().toString());
48  * service.setName("salesdomain");
49  * service.setLocation("/homes/sales/domains");
50  * service.setType(AppserverServiceType.Domain);
51  * service.setOSUser("joe");
52  * //...
53  * <b><u>service.isConfigValid();</b></u> //this is important
54  * sh.createService(service.tokensAndValues());
55  * </pre>
56  * @since SJSAS 9.0
57  * @see SMFService
58  */

59 public class SMFServiceHandler implements ServiceHandler {
60     
61     private static final StringManager sm = StringManager.getManager(SMFServiceHandler.class);
62     private boolean trace = false;
63     /** The default constructor.
64      */

65     public SMFServiceHandler() {
66     }
67
68     public void createService(final Map JavaDoc<String JavaDoc, String JavaDoc> params) throws RuntimeException JavaDoc {
69         final SMFService smf = new SMFService(params);
70         boolean success = false;
71         try {
72             smf.isConfigValid(); //safe, throws exception if not valid
73
if (trace)
74                 printOut(smf.toString());
75             testPlatform();
76             validateManifest(smf.getManifestFilePath());
77             tokenReplaceTemplateAtDestination(smf);
78             validateService(smf);
79             success = importService(smf);
80         } catch(final Exception JavaDoc e) {
81             if (!success) {
82                 cleanupManifest(smf);
83             }
84             throw new RuntimeException JavaDoc(e);
85         }
86     }
87
88     private void testPlatform() throws Exception JavaDoc {
89         if (!OS.isSolaris10()) {
90             final String JavaDoc os = System.getProperty("os.name");
91             final String JavaDoc vr = System.getProperty("os.version");
92             final String JavaDoc msg = sm.getString("notSolaris10", os, vr);
93             throw new IllegalArgumentException JavaDoc(msg);
94         }
95     }
96     private void validateManifest(final String JavaDoc manifestPath) throws Exception JavaDoc {
97         final File JavaDoc manifest = new File JavaDoc(manifestPath);
98         if (manifest.exists()) {
99             final String JavaDoc msg = sm.getString("smfManifestExists", manifest.getAbsolutePath());
100             throw new IllegalArgumentException JavaDoc(msg);
101         }
102         if (manifest.getParentFile().exists()) {
103             final String JavaDoc msg = sm.getString("smfManifestFolderExists", manifest.getParentFile().getAbsolutePath());
104             throw new IllegalArgumentException JavaDoc(msg);
105         }
106         manifest.getParentFile().mkdirs();
107         if (trace)
108             printOut("Manifest validated: " + manifestPath);
109     }
110     private void tokenReplaceTemplateAtDestination(final SMFService smf) throws Exception JavaDoc {
111         final LineTokenReplacer tr = new LineTokenReplacer(map2Set(smf.tokensAndValues()));
112         tr.replace(smf.getManifestFileTemplatePath(), smf.getManifestFilePath());
113         if (trace)
114             printOut("Manifest configured: " + smf.getManifestFilePath());
115     }
116     
117     private TokenValueSet map2Set(final Map JavaDoc<String JavaDoc, String JavaDoc> map) throws Exception JavaDoc {
118         final Set<TokenValue> set = new HashSet JavaDoc<TokenValue> ();
119         final Set<String JavaDoc> keys = map.keySet();
120         for (final String JavaDoc key : keys) {
121             final String JavaDoc value = map.get(key);
122             final TokenValue tv = new TokenValue(key, value);
123             set.add(tv);
124         }
125         final TokenValueSet tvset = new TokenValueSet(set);
126         return ( tvset );
127     }
128     
129     private void validateService(final SMFService smf) throws Exception JavaDoc {
130         final String JavaDoc[] cmda = new String JavaDoc[]{SMFService.SVCCFG, "validate", smf.getManifestFilePath()};
131         final ProcessExecutor pe = new ProcessExecutor(cmda);
132         pe.execute();
133         if (trace)
134             printOut("Validated the SMF Service: " + smf.getFQSN() + " using: " + SMFService.SVCCFG);
135     }
136     private boolean importService(final SMFService smf) throws Exception JavaDoc {
137         final String JavaDoc[] cmda = new String JavaDoc[]{SMFService.SVCCFG, "import", smf.getManifestFilePath()};
138         final ProcessExecutor pe = new ProcessExecutor(cmda);
139         pe.execute(); //throws ExecException in case of an error
140
if (trace)
141             printOut("Imported the SMF Service: " + smf.getFQSN());
142         return ( true );
143     }
144     private void cleanupManifest(final SMFService smf) throws RuntimeException JavaDoc {
145         final File JavaDoc manifest = new File JavaDoc(smf.getManifestFilePath());
146         if (manifest.exists()) {
147             manifest.delete();
148             manifest.deleteOnExit();
149             if(trace)
150                 printOut("Attempted deleting failed service manifest: " + manifest.getAbsolutePath());
151         }
152         final File JavaDoc failedServiceNode = manifest.getParentFile();
153         if (failedServiceNode.exists()) {
154             failedServiceNode.delete();
155             failedServiceNode.deleteOnExit();
156             if(trace)
157                 printOut("Attempted deleting failed service folder: " + failedServiceNode.getAbsolutePath());
158         }
159     }
160     private void printOut(final String JavaDoc s) {
161         System.out.println(s);
162     }
163
164     public void setTrace(boolean trace) {
165         this.trace = trace;
166     }
167 }
168
Popular Tags