1 23 24 package com.sun.enterprise.admin.servermgmt; 25 import com.sun.enterprise.util.SystemPropertyConstants; 26 import com.sun.enterprise.util.ProcessExecutor; 27 import com.sun.enterprise.util.io.FileUtils; 28 import com.sun.enterprise.util.i18n.StringManager; 29 import java.io.File ; 30 import java.io.BufferedInputStream ; 31 import java.io.FileInputStream ; 32 import java.util.Date ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Map ; 36 import java.util.Arrays ; 37 import java.util.Set ; 38 import java.util.StringTokenizer ; 39 import java.util.Properties ; 40 41 51 public class SMFService { 52 53 public static final String DATE_CREATED_TN = "DATE_CREATED"; 54 public static final String SERVICE_NAME_TN = "NAME"; 55 public static final String SERVICE_TYPE_TN = "TYPE"; 56 public static final String CFG_LOCATION_TN = "LOCATION"; 57 public static final String ENTITY_NAME_TN = "ENTITY_NAME"; 58 public static final String FQSN_TN = "FQSN"; 59 public static final String AS_ADMIN_PATH_TN = "AS_ADMIN_PATH"; 60 public static final String AS_ADMIN_USER_TN = "AS_ADMIN_USER"; 61 public static final String AS_ADMIN_PASSWORD_TN = "AS_ADMIN_PASSWORD"; 62 public static final String AS_ADMIN_MASTERPASSWORD_TN = "AS_ADMIN_MASTERPASSWORD"; 63 public static final String PASSWORD_FILE_PATH_TN = "PASSWORD_FILE_PATH"; 64 public static final String TIMEOUT_SECONDS_TN = "TIMEOUT_SECONDS"; 65 public static final String OS_USER_TN = "OS_USER"; 66 public static final String PRIVILEGES_TN = "PRIVILEGES"; 67 68 public static final String TIMEOUT_SECONDS_DV = "0"; 69 public static final String AS_ADMIN_USER_DEF_VAL = "admin"; 70 public static final String SVCCFG = "svccfg"; 71 public static final String SP_DELIMITER = ":"; 72 public static final String PRIVILEGES_DEFAULT_VAL = "basic"; 73 public static final String NETADDR_PRIV_VAL = "net_privaddr"; 74 public static final String BASIC_NETADDR_PRIV_VAL = PRIVILEGES_DEFAULT_VAL + "," + NETADDR_PRIV_VAL; 75 public static final String START_INSTANCES_TN = "START_INSTANCES"; 76 public static final String START_INSTANCES_DEFAULT_VAL = Boolean.TRUE.toString(); 77 public static final String NO_START_INSTANCES_PROPERTY = "startinstances=false"; 78 79 public static final String MANIFEST_HOME = "/var/svc/manifest/application/SUNWappserver/"; 80 81 private static final String NULL_VALUE = "null"; 82 private static final String SERVICE_NAME_PREFIX = "application/SUNWappserver/"; 83 private static final StringManager sm = StringManager.getManager(SMFService.class); 84 private static final String nullArgMsg = sm.getString("null_arg"); 85 private static final String MANIFEST_FILE_SUFFIX = "-service-smf.xml"; 86 private static final String MANIFEST_FILE_TEMPL_SUFFIX = MANIFEST_FILE_SUFFIX + ".template"; 87 88 private final Map <String , String > pairs; 89 90 95 public SMFService() { 96 pairs = new HashMap <String , String > (); 97 init(); 98 } 99 100 107 public SMFService(final Map <String , String > tv) { 108 if (tv == null) 109 throw new IllegalArgumentException (nullArgMsg); 110 pairs = new HashMap <String , String > (tv); 111 } 112 113 115 public String getName() { 116 return ( pairs.get(SERVICE_NAME_TN) ); 117 } 118 119 122 public void setName(final String name) { 123 if (name == null) 124 throw new IllegalArgumentException (nullArgMsg); 125 final String fullName = SERVICE_NAME_PREFIX + name; 126 if (serviceNameExists(fullName)) { 127 final String msg = sm.getString("serviceNameExists", fullName); 128 throw new IllegalArgumentException (msg); 129 } 130 pairs.put(SERVICE_NAME_TN, fullName); 131 } 132 137 public AppserverServiceType getType() { 138 return ( AppserverServiceType.valueOf(pairs.get(SERVICE_TYPE_TN)) ); 139 } 140 143 public void setType(final AppserverServiceType type) { 144 pairs.put(SERVICE_TYPE_TN, type.toString()); 145 } 146 147 151 public String getDate() { 152 return ( pairs.get(DATE_CREATED_TN) ); 153 } 154 158 public void setDate(final String date) { 159 if (date == null) 160 throw new IllegalArgumentException (nullArgMsg); 161 pairs.put(DATE_CREATED_TN, date); 162 } 163 164 167 public String getLocation() { 168 return ( pairs.get(CFG_LOCATION_TN) ); 169 } 170 174 public void setLocation(final String cfgLocation) { 175 if (cfgLocation == null) 176 throw new IllegalArgumentException (nullArgMsg); 177 final File cf = FileUtils.safeGetCanonicalFile(new File (cfgLocation)); 178 pairs.put(CFG_LOCATION_TN, cf.getParent()); 179 pairs.put(ENTITY_NAME_TN, cf.getName()); 180 } 181 187 public String getFQSN() { 188 return ( pairs.get(FQSN_TN) ); 189 } 190 196 public void setFQSN() { 197 assert !NULL_VALUE.equals(pairs.get(SERVICE_NAME_TN)):"Internal: Caller tried to call this method before setName()"; 200 final String underscored = pairs.get(ENTITY_NAME_TN) + pairs.get(CFG_LOCATION_TN).replace('/', '_'); 201 pairs.put(FQSN_TN, underscored); 202 } 203 204 206 public String getAsadminPath() { 207 return (pairs.get(AS_ADMIN_PATH_TN) ); 208 } 209 211 public void setAsadminPath(final String path) { 212 if (path == null) 213 throw new IllegalArgumentException (nullArgMsg); 214 if (! new File (path).exists()) { 215 final String msg = sm.getString("doesNotExist", path); 216 throw new IllegalArgumentException (msg); 217 } 218 pairs.put(AS_ADMIN_PATH_TN, path); 219 } 220 223 public String getPasswordFilePath() { 224 return (pairs.get(PASSWORD_FILE_PATH_TN) ); 225 } 226 229 public void setPasswordFilePath(final String path) { 230 if (path == null) 231 throw new IllegalArgumentException (nullArgMsg); 232 String msg = null; 233 if (!new File (path).exists()) { 234 msg = sm.getString("doesNotExist", path); 235 throw new IllegalArgumentException (msg); 236 } 237 final String cp = FileUtils.safeGetCanonicalPath(new File (path)); 238 final Map <String , String > tv = new HashMap <String , String > (); 239 if (!fileContainsToken(cp, AS_ADMIN_USER_TN, tv)) { 240 msg = sm.getString("missingParamsInFile", cp, AS_ADMIN_USER_TN); 241 throw new IllegalArgumentException (msg); 242 } 243 if (!fileContainsToken(cp, AS_ADMIN_PASSWORD_TN, tv)) { 244 msg = sm.getString("missingParamsInFile", cp, AS_ADMIN_PASSWORD_TN); 245 throw new IllegalArgumentException (msg); 246 } 247 if (!fileContainsToken(path, AS_ADMIN_MASTERPASSWORD_TN, tv)) { 248 msg = sm.getString("missingParamsInFile", cp, AS_ADMIN_MASTERPASSWORD_TN); 249 throw new IllegalArgumentException (msg); 250 } 251 pairs.put(AS_ADMIN_USER_TN, tv.get(AS_ADMIN_USER_TN)); 252 pairs.put(PASSWORD_FILE_PATH_TN, cp); 253 } 254 257 public int getTimeoutSeconds() { 258 final int to = Integer.parseInt(pairs.get(TIMEOUT_SECONDS_TN)); 259 return ( to ); 260 } 261 265 public void setTimeoutSeconds(final int number) { 266 Integer to = new Integer (number); 267 if (to < 0) { 268 final String msg = sm.getString("invalidTO", number); 269 throw new IllegalArgumentException (msg); 270 } 271 pairs.put(TIMEOUT_SECONDS_TN, to.toString() ); 272 } 273 276 public String getOSUser() { 277 return (pairs.get(OS_USER_TN) ); 278 } 279 286 public void setOSUser() { 287 final String user = System.getProperty("user.name"); 288 String msg; 289 if (!canCreateManifest()) { 290 msg = sm.getString("noPermissionToCreateManifest", user, MANIFEST_HOME); 291 throw new IllegalArgumentException (msg); 292 } 293 final StringBuilder auths = new StringBuilder (); 294 if (!isUserSmfAuthorized(user, auths)) { 295 msg = sm.getString("noSmfAuth", user, auths); 296 throw new IllegalArgumentException (msg); 297 } 298 pairs.put(OS_USER_TN, user); 299 } 300 301 304 public String getServiceProperties() { 305 return ( pairs.get(PRIVILEGES_TN) ); 306 } 307 308 311 public void setServiceProperties(final String cds) { 312 316 if (cds != null) { 317 final Set <String > props = ps2Pairs(cds); 318 if (props.contains(NETADDR_PRIV_VAL)) { 319 pairs.put(PRIVILEGES_TN, BASIC_NETADDR_PRIV_VAL); } 321 if (props.contains(NO_START_INSTANCES_PROPERTY)) { 322 pairs.put(START_INSTANCES_TN, Boolean.FALSE.toString()); 323 } 324 } 325 } 326 327 337 public boolean isConfigValid() { 338 final Set <String > keys = pairs.keySet(); 339 for (final String k : keys) { 340 final boolean aNullValue = NULL_VALUE.equals(pairs.get(k)); 341 if (aNullValue) { 342 final String msg = sm.getString("smfTokenNeeded", k, pairs.get(k)); 343 throw new RuntimeException (msg); 344 } 345 } 346 final File mf = new File (getManifestFileTemplatePath()); 347 if (!mf.exists()) { 348 final String msg = sm.getString("serviceTemplateNotFound", getManifestFileTemplatePath()); 349 throw new RuntimeException (msg); 350 } 351 return ( true ); 352 } 353 354 358 public Map <String , String > tokensAndValues() { 359 return ( new HashMap <String , String > (pairs) ); } 361 366 public String getManifestFilePath() { 367 final String fqsn = getFQSN(); 368 if (NULL_VALUE.equals(fqsn)) { 369 final String msg = sm.getString("serviceNameInvalid", fqsn); 370 throw new RuntimeException (msg); 371 } 372 final String fn = new StringBuilder ().append(MANIFEST_HOME).append(fqsn).append("/").append(this.getType().toString()).append(MANIFEST_FILE_SUFFIX).toString(); 374 return ( fn ) ; 375 } 376 380 public String getManifestFileTemplatePath() { 381 385 if (NULL_VALUE.equals(getType().toString())) { 386 final String msg = sm.getString("serviceTypeNotSet"); 387 throw new RuntimeException (msg); 388 } 389 final StringBuilder sb = new StringBuilder (); 390 sb.append(System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY)); 391 if (sb.charAt(sb.length() - 1) == '/') 392 sb.setLength(sb.length() - 1); sb.append("/lib/install/templates/"); 394 sb.append(this.getType().toString()); sb.append(MANIFEST_FILE_TEMPL_SUFFIX); return ( sb.toString() ); 397 } 398 403 public String toString() { 404 405 final StringBuilder sb = new StringBuilder (); 406 final String [] ka = new String [pairs.size()]; 407 Arrays.sort(pairs.keySet().toArray(ka)); 408 for (final String n : ka) { 409 sb.append(n).append("=").append(pairs.get(n)).append(System.getProperty("line.separator")); 410 } 411 return ( sb.toString() ); 412 } 413 414 private void init() { 415 pairs.put(DATE_CREATED_TN, new Date ().toString()); 416 pairs.put(SERVICE_NAME_TN, NULL_VALUE); 417 pairs.put(SERVICE_TYPE_TN, NULL_VALUE); 418 pairs.put(CFG_LOCATION_TN, NULL_VALUE); 419 pairs.put(ENTITY_NAME_TN, NULL_VALUE); 420 pairs.put(FQSN_TN, NULL_VALUE); 421 pairs.put(START_INSTANCES_TN, START_INSTANCES_DEFAULT_VAL); 422 pairs.put(AS_ADMIN_PATH_TN, NULL_VALUE); 423 pairs.put(AS_ADMIN_USER_TN, AS_ADMIN_USER_DEF_VAL); 424 pairs.put(PASSWORD_FILE_PATH_TN, NULL_VALUE); 425 pairs.put(TIMEOUT_SECONDS_TN, TIMEOUT_SECONDS_DV); 426 pairs.put(OS_USER_TN, NULL_VALUE); 427 pairs.put(PRIVILEGES_TN, PRIVILEGES_DEFAULT_VAL); 428 } 429 private Set <String > ps2Pairs(final String cds) { 430 final StringTokenizer p = new StringTokenizer (cds, SP_DELIMITER); 431 final Set <String > tokens = new HashSet <String >(); 432 while (p.hasMoreTokens()) { 433 tokens.add(p.nextToken()); 434 } 435 return ( tokens ); 436 } 437 private boolean canCreateManifest() { 438 final File mh = new File (MANIFEST_HOME); 439 boolean ok = true; 440 if (!mh.exists()) { 441 ok = mh.mkdirs(); 442 } 443 if (ok) { 444 if (!mh.canWrite()) { 445 ok = false; 446 } 447 } 448 return ( ok ); 449 } 450 private boolean isUserSmfAuthorized(final String user, final StringBuilder auths) { 451 boolean authorized = false; 452 String path2Auths = "auths"; 453 String at = ","; 454 final String AUTH1 = "solaris.*"; 455 final String AUTH2 = "solaris.smf.*"; 456 final String AUTH3 = "solaris.smf.modify"; 457 if (System.getProperty("PATH_2_AUTHS") != null) 458 path2Auths = System.getProperty("PATH_2_AUTHS"); 459 if (System.getProperty("AUTH_TOKEN") != null) 460 at = System.getProperty("AUTH_TOKEN"); 461 try { 462 final String [] cmd = new String []{path2Auths, user}; 463 ProcessExecutor pe = new ProcessExecutor(cmd); 464 pe.setExecutionRetentionFlag(true); 465 pe.execute(); 466 auths.append(pe.getLastExecutionOutput()); 467 final StringTokenizer st = new StringTokenizer (pe.getLastExecutionOutput(), at); 468 while (st.hasMoreTokens()) { 469 String t = st.nextToken(); 470 if (t != null) 471 t = t.trim(); 472 if (AUTH1.equals(t) || AUTH2.equals(t) || AUTH3.equals(t)) { 473 authorized = true; 474 break; 475 } 476 } 477 return ( authorized ); 478 } catch(Exception e) { 479 throw new RuntimeException (e); 480 } 481 } 482 private boolean fileContainsToken(final String path, final String t, final Map <String , String > tv) throws RuntimeException { 483 BufferedInputStream bis = null; 484 try { 485 boolean present = false; 486 bis = new BufferedInputStream (new FileInputStream (path)); 487 final Properties p = new Properties (); 488 p.load(bis); 489 if (p.containsKey(t)) { 490 tv.put(t, (String )p.get(t)); 491 present = true; 492 } 493 return ( present ); 494 } 495 catch(final Exception e) { 496 throw new RuntimeException (e); 497 } 498 finally { 499 if (bis != null) 500 try { 501 bis.close(); 502 } catch(Exception ee) {} 503 } 504 } 505 private boolean serviceNameExists(final String sn) { 506 boolean exists = false; 507 try { 508 final String [] cmd = new String [] {"svcs", sn}; 509 ProcessExecutor pe = new ProcessExecutor(cmd); 510 pe.setExecutionRetentionFlag(true); 511 pe.execute(); 512 exists = true; 513 } catch(final Exception e) { 514 } 516 return ( exists ); 517 } 518 public enum AppserverServiceType { 519 Domain, 520 NodeAgent 521 } 522 } 523 | Popular Tags |