1 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 ; 33 import java.util.HashSet ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 38 59 public class SMFServiceHandler implements ServiceHandler { 60 61 private static final StringManager sm = StringManager.getManager(SMFServiceHandler.class); 62 private boolean trace = false; 63 65 public SMFServiceHandler() { 66 } 67 68 public void createService(final Map <String , String > params) throws RuntimeException { 69 final SMFService smf = new SMFService(params); 70 boolean success = false; 71 try { 72 smf.isConfigValid(); 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 e) { 81 if (!success) { 82 cleanupManifest(smf); 83 } 84 throw new RuntimeException (e); 85 } 86 } 87 88 private void testPlatform() throws Exception { 89 if (!OS.isSolaris10()) { 90 final String os = System.getProperty("os.name"); 91 final String vr = System.getProperty("os.version"); 92 final String msg = sm.getString("notSolaris10", os, vr); 93 throw new IllegalArgumentException (msg); 94 } 95 } 96 private void validateManifest(final String manifestPath) throws Exception { 97 final File manifest = new File (manifestPath); 98 if (manifest.exists()) { 99 final String msg = sm.getString("smfManifestExists", manifest.getAbsolutePath()); 100 throw new IllegalArgumentException (msg); 101 } 102 if (manifest.getParentFile().exists()) { 103 final String msg = sm.getString("smfManifestFolderExists", manifest.getParentFile().getAbsolutePath()); 104 throw new IllegalArgumentException (msg); 105 } 106 manifest.getParentFile().mkdirs(); 107 if (trace) 108 printOut("Manifest validated: " + manifestPath); 109 } 110 private void tokenReplaceTemplateAtDestination(final SMFService smf) throws Exception { 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 <String , String > map) throws Exception { 118 final Set<TokenValue> set = new HashSet <TokenValue> (); 119 final Set<String > keys = map.keySet(); 120 for (final String key : keys) { 121 final String 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 { 130 final String [] cmda = new String []{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 { 137 final String [] cmda = new String []{SMFService.SVCCFG, "import", smf.getManifestFilePath()}; 138 final ProcessExecutor pe = new ProcessExecutor(cmda); 139 pe.execute(); if (trace) 141 printOut("Imported the SMF Service: " + smf.getFQSN()); 142 return ( true ); 143 } 144 private void cleanupManifest(final SMFService smf) throws RuntimeException { 145 final File manifest = new File (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 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 s) { 161 System.out.println(s); 162 } 163 164 public void setTrace(boolean trace) { 165 this.trace = trace; 166 } 167 } 168 | Popular Tags |