1 23 24 29 30 package com.sun.enterprise.admin.servermgmt; 31 32 import com.sun.enterprise.admin.util.TokenValueSet; 33 import com.sun.enterprise.admin.util.LineTokenReplacer; 34 import com.sun.enterprise.util.io.FileUtils; 35 import com.sun.enterprise.util.SystemPropertyConstants; 36 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout; 37 import com.sun.enterprise.admin.servermgmt.pe.PEInstancesManager; 38 import com.sun.enterprise.admin.servermgmt.pe.PEDomainsManager; 39 import com.sun.enterprise.util.i18n.StringManager; 40 41 import com.sun.enterprise.admin.common.constant.AdminConstants; 42 import com.sun.enterprise.security.auth.realm.file.FileRealm; 43 import com.sun.enterprise.security.auth.realm.BadRealmException; 44 import com.sun.enterprise.security.auth.realm.NoSuchRealmException; 45 import com.sun.enterprise.security.util.IASSecurityException; 46 import com.sun.enterprise.security.store.PasswordAdapter; 47 import com.sun.enterprise.security.RealmConfig; 48 49 import com.sun.enterprise.admin.common.Status; 50 import java.io.BufferedReader ; 51 52 import java.io.File ; 53 import java.io.FileReader ; 54 import java.io.FilenameFilter ; 55 import java.io.IOException ; 56 import java.sql.Connection ; 57 import java.sql.DriverManager ; 58 import java.sql.Statement ; 59 60 import java.util.HashMap ; 61 import java.util.Iterator ; 62 import java.util.ArrayList ; 63 import java.util.List ; 64 65 import com.sun.enterprise.config.ConfigFactory; 67 import com.sun.enterprise.config.ConfigContext; 68 import com.sun.enterprise.config.ConfigException; 69 import com.sun.enterprise.config.serverbeans.Server; 70 import com.sun.enterprise.config.serverbeans.Config; 71 import com.sun.enterprise.config.serverbeans.ServerHelper; 72 import com.sun.enterprise.config.serverbeans.JavaConfig; 73 import com.sun.enterprise.config.serverbeans.JmsService; 74 import com.sun.enterprise.config.serverbeans.AuthRealm; 75 import com.sun.enterprise.config.serverbeans.ServerHelper; 76 import com.sun.enterprise.config.serverbeans.JmxConnector; 77 import com.sun.enterprise.config.serverbeans.SecurityService; 78 79 80 import javax.management.remote.JMXAuthenticator ; 81 import com.sun.enterprise.admin.server.core.jmx.auth.ASJMXAuthenticator; 82 import com.sun.enterprise.admin.server.core.jmx.auth.ASLoginDriverImpl; 83 84 import com.sun.enterprise.jms.IASJmsUtil; 86 import com.sun.messaging.jmq.jmsspi.JMSAdmin; 87 import javax.jms.JMSException ; 88 89 import com.sun.enterprise.util.SystemPropertyConstants; 90 import com.sun.enterprise.util.ProcessExecutor; 91 import com.sun.enterprise.util.ExecException; 92 93 102 public class RepositoryManager extends MasterPasswordFileManager { 103 104 109 protected class RepositoryManagerMessages 110 { 111 private StringManager _strMgr; 112 private String _badNameMessage; 113 private String _repositoryNameMessage; 114 private String _repositoryRootMessage; 115 private String _existsMessage; 116 private String _noExistsMessage; 117 private String _repositoryNotValidMessage; 118 private String _cannotDeleteMessage; 119 private String _invalidPathMessage; 120 private String _listRepositoryElementMessage; 121 private String _cannotDeleteInstance_invalidState; 122 private String _instanceStartupExceptionMessage; 123 private String _cannotStartInstance_invalidStateMessage; 124 private String _startInstanceTimeOutMessage; 125 private String _portConflictMessage; 126 private String _startupFailedMessage; 127 private String _cannotStopInstance_invalidStateMessage; 128 private String _cannotStopInstanceMessage; 129 private String _timeoutStartingMessage; 130 private String _cannotDeleteJmsProviderInstance; 131 132 public RepositoryManagerMessages( 133 StringManager strMgr, 134 String badNameMessage, 135 String repositoryNameMessage, 136 String repositoryRootMessage, 137 String existsMessage, 138 String noExistsMessage, 139 String repositoryNotValidMessage, 140 String cannotDeleteMessage, 141 String invalidPathMessage, 142 String listRepositoryElementMessage, 143 String cannotDeleteInstance_invalidState, 144 String instanceStartupExceptionMessage, 145 String cannotStartInstance_invalidStateMessage, 146 String startInstanceTimeOutMessage, 147 String portConflictMessage, 148 String startupFailedMessage, 149 String cannotStopInstance_invalidStateMessage, 150 String cannotStopInstanceMessage, 151 String timeoutStartingMessage) 152 { 153 _strMgr = strMgr; 154 _badNameMessage = badNameMessage; 155 _repositoryNameMessage = repositoryNameMessage; 156 _repositoryRootMessage = repositoryRootMessage; 157 _existsMessage = existsMessage; 158 _noExistsMessage = noExistsMessage; 159 _repositoryNotValidMessage = repositoryNotValidMessage; 160 _cannotDeleteMessage = cannotDeleteMessage; 161 _invalidPathMessage = invalidPathMessage; 162 _listRepositoryElementMessage = listRepositoryElementMessage; 163 _cannotDeleteInstance_invalidState = cannotDeleteInstance_invalidState; 164 _instanceStartupExceptionMessage = instanceStartupExceptionMessage; 165 _cannotStartInstance_invalidStateMessage = 166 cannotStartInstance_invalidStateMessage; 167 _startInstanceTimeOutMessage = startInstanceTimeOutMessage; 168 _portConflictMessage = portConflictMessage; 169 _startupFailedMessage = startupFailedMessage; 170 _cannotStopInstance_invalidStateMessage = 171 cannotStopInstance_invalidStateMessage; 172 _cannotStopInstanceMessage = cannotStopInstanceMessage; 173 _timeoutStartingMessage = timeoutStartingMessage; 174 } 175 176 public String getRepositoryNameMessage() { 177 return _strMgr.getString(_repositoryNameMessage); 178 } 179 180 public String getBadNameMessage(String repositoryName) { 181 return _strMgr.getString(_badNameMessage, repositoryName); 182 } 183 184 public String getRepositoryRootMessage() { 185 return _strMgr.getString(_repositoryRootMessage); 186 } 187 188 public String getNoExistsMessage(String repositoryName, String repositoryLocation) { 189 return _strMgr.getString(_noExistsMessage, repositoryName, repositoryLocation); 190 } 191 192 public String getExistsMessage(String repositoryName, String repositoryLocation) { 193 return _strMgr.getString(_existsMessage, repositoryName, repositoryLocation); 194 } 195 196 public String getRepositoryNotValidMessage(String path) { 197 return _strMgr.getString(_repositoryNotValidMessage, path); 198 } 199 200 public String getCannotDeleteMessage(String repositoryName) { 201 return _strMgr.getString(_cannotDeleteMessage, repositoryName); 202 } 203 204 public String getInvalidPathMessage(String path) { 205 return _strMgr.getString(_invalidPathMessage, path); 206 } 207 208 public String getListRepositoryElementMessage(String repositoryName, 209 String repositoryStatus) { 210 return _strMgr.getString(_listRepositoryElementMessage, repositoryName, 211 repositoryStatus); 212 } 213 214 public String getCannotDeleteInstanceInvalidState(String name, 215 String state) { 216 return _strMgr.getString(_cannotDeleteInstance_invalidState, 217 name, state); 218 } 219 220 public String getInstanceStartupExceptionMessage(String name) { 221 return _strMgr.getString(_instanceStartupExceptionMessage, name); 222 } 223 224 public String getCannotStartInstanceInvalidStateMessage(String name, 225 String state) { 226 return _strMgr.getString(_cannotStartInstance_invalidStateMessage, 227 name, state); 228 } 229 230 public String getStartInstanceTimeOutMessage(String name) { 231 return _strMgr.getString(_startInstanceTimeOutMessage, name); 232 } 233 234 public String getStartupFailedMessage(String name) { 235 return _strMgr.getString(_startupFailedMessage, name); 236 } 237 238 public String getStartupFailedMessage(String name, int port) { 239 if (port != 0) { 240 return _strMgr.getString(_portConflictMessage, new Object []{name, String.valueOf(port)}); 241 } else { 242 return _strMgr.getString(_startupFailedMessage, name); 243 } 244 } 245 246 public String getCannotStopInstanceInvalidStateMessage(String name, 247 String state) { 248 return _strMgr.getString(_cannotStopInstance_invalidStateMessage, 249 name, state); 250 } 251 252 public String getCannotStopInstanceMessage(String name) { 253 return _strMgr.getString(_cannotStopInstanceMessage, name); 254 } 255 256 public String getTimeoutStartingMessage(String name) { 257 return _strMgr.getString(_timeoutStartingMessage, name); 258 } 259 } 260 261 protected static final String NEW_LINE = 262 System.getProperty("line.separator"); 263 264 private static final StringManager _strMgr = 265 StringManager.getManager(RepositoryManager.class); 266 267 private ConfigContext _configContext = null; 268 269 protected RepositoryManagerMessages _messages = null; 270 271 public static final String DEBUG = "Debug"; 272 273 274 public RepositoryManager() { 275 super(); 276 setMessages(new RepositoryManagerMessages( 277 StringManager.getManager(PEDomainsManager.class), 278 "illegalDomainName", 279 "domainName", "domainsRoot", "domainExists", 280 "domainDoesntExist", "domainDirNotValid", 281 "cannotDeleteDomainDir", "invalidDomainDir", 282 "listDomainElement", "cannotDeleteInstance_invalidState", 283 "instanceStartupException", "cannotStartInstance_invalidState", 284 "startInstanceTimeOut", "portConflict", "startupFailed", 285 "cannotStopInstance_invalidState", 286 "cannotStopInstance", "timeoutStarting")); 287 } 288 289 protected void setMessages(RepositoryManagerMessages messages) { 290 _messages = messages; 291 } 292 293 protected RepositoryManagerMessages getMessages() { 294 return _messages; 295 } 296 297 protected void generateFromTemplate(TokenValueSet tokens, 298 File template, File destinationFile) throws IOException 299 { 300 LineTokenReplacer replacer = new LineTokenReplacer(tokens); 301 replacer.replace(template, destinationFile); 302 } 303 304 protected boolean repositoryExists(RepositoryConfig config) 305 { 306 return FileUtils.safeGetCanonicalFile(getRepositoryDir(config)).exists(); 307 } 308 309 protected boolean isValidRepository(File f) 310 { 311 return new File (new File (f, PEFileLayout.BIN_DIR), 312 PEFileLayout.START_SERV_OS).exists(); 313 } 314 315 protected boolean isValidRepository(RepositoryConfig config) { 316 return getFileLayout(config).getStartServ().exists(); 317 } 318 319 protected File getRepositoryDir(RepositoryConfig config) 320 { 321 return getFileLayout(config).getRepositoryDir(); 322 } 323 324 protected File getRepositoryRootDir(RepositoryConfig config) 325 { 326 return getFileLayout(config).getRepositoryRootDir(); 327 } 328 329 protected void checkRepository(RepositoryConfig config) 330 throws RepositoryException 331 { 332 checkRepository(config, true, true); 333 } 334 335 public void checkRepository(RepositoryConfig config, 336 boolean existingRepository) 337 throws RepositoryException 338 { 339 checkRepository(config, existingRepository, true); 340 } 341 342 345 public void checkRepository(RepositoryConfig config, 346 boolean existingRepository, 347 boolean checkRootDir) 348 throws RepositoryException 349 { 350 String repositoryName = config.getDisplayName(); 351 352 new RepositoryNameValidator(getMessages().getRepositoryNameMessage()). 354 validate(repositoryName); 355 356 if (checkRootDir || existingRepository) { 357 new FileValidator(getMessages().getRepositoryRootMessage(), "drw").validate( 359 config.getRepositoryRoot()); 360 } 361 362 new FileValidator(_strMgr.getString("installRoot"), "dr").validate( 364 config.getInstallRoot()); 365 366 if (existingRepository) { 368 if (!repositoryExists(config)) { 369 throw new RepositoryException( 370 getMessages().getNoExistsMessage(repositoryName, 371 getRepositoryRootDir(config).getAbsolutePath())); 372 } else if (!isValidRepository(config)) { 373 throw new RepositoryException( 374 getMessages().getRepositoryNotValidMessage( 375 getRepositoryDir(config).getAbsolutePath())); 376 } 377 } else { 378 if (repositoryExists(config)) { 379 throw new RepositoryException( 380 getMessages().getExistsMessage(repositoryName, 381 getRepositoryRootDir(config).getAbsolutePath())); 382 } 383 } 384 } 385 386 390 protected void setPermissions(RepositoryConfig repositoryConfig) throws RepositoryException 391 { 392 final PEFileLayout layout = getFileLayout(repositoryConfig); 393 final File domainDir = layout.getRepositoryDir(); 394 try { 395 chmod("-R 755", domainDir); 396 } catch (Exception e) { 397 throw new RepositoryException( 398 _strMgr.getString("setPermissionError"), e); 399 } 400 } 401 402 405 protected void deleteRepository( 406 RepositoryConfig config) 407 throws RepositoryException 408 { 409 deleteRepository(config, true); 410 } 411 412 413 419 protected void deleteRepository( 420 RepositoryConfig config, boolean deleteJMSProvider) 421 throws RepositoryException 422 { 423 checkRepository(config, true); 424 425 final int status = getInstancesManager(config).getInstanceStatus(); 427 if (status != Status.kInstanceNotRunningCode) { 428 throw new RepositoryException( 429 getMessages().getCannotDeleteInstanceInvalidState( 430 config.getDisplayName(), 431 Status.getStatusString(status))); 432 } 433 434 if (deleteJMSProvider) { 441 deleteJMSProviderInstance(config); 442 } 443 444 File repository = getRepositoryDir(config); 446 try { 447 FileUtils.liquidate(repository); 448 } catch (Exception e) { 449 throw new RepositoryException(getMessages().getCannotDeleteMessage( 450 repository.getAbsolutePath()), e); 451 } 452 453 if (repositoryExists(config)) { 455 throw new RepositoryException( 456 getMessages().getCannotDeleteMessage(repository.getAbsolutePath())); 457 } 458 } 459 460 463 protected String [] listRepository(RepositoryConfig config) throws RepositoryException 464 { 465 File repository = getRepositoryRootDir(config); 466 String [] dirs = new String [0]; 467 try { 468 File f = repository.getCanonicalFile(); 469 if (!f.isDirectory()) { 470 throw new RepositoryException(getMessages().getInvalidPathMessage( 471 f.getAbsolutePath())); 472 } 473 dirs = f.list(new FilenameFilter () { 474 public boolean accept(File dir, String name) { 476 File f = new File (dir, name); 477 if (!f.isDirectory()) { 478 return false; 479 } else { 480 return isValidRepository(f); 481 } 482 } 483 }); 484 if (dirs == null) { 485 dirs = new String [0]; 486 } 487 } catch (Exception e) { 488 throw new RepositoryException(e); 489 } 490 return dirs; 491 } 492 493 public InstancesManager getInstancesManager(RepositoryConfig config) 494 { 495 return new PEInstancesManager(config); 496 } 497 498 503 protected String [] listDomainsAndStatusAsString( 504 RepositoryConfig config) throws RepositoryException 505 { 506 try { 507 RuntimeStatusList statusList = getRuntimeStatus(config); 508 RuntimeStatus status = null; 509 String [] result = new String [statusList.size()]; 510 for (int i = 0; i < statusList.size(); i++) { 511 status = statusList.getStatus(i); 512 result[i] = getMessages().getListRepositoryElementMessage( 513 status.getName(), status.toShortString()); 514 } 515 return result; 516 } catch (Exception e) { 517 throw new RepositoryException(e); 518 } 519 } 520 521 protected RepositoryConfig getConfigForRepositoryStatus(RepositoryConfig config, 522 String repository) 523 { 524 return new RepositoryConfig(repository, config.getRepositoryRoot()); 526 } 527 528 532 public RuntimeStatusList getRuntimeStatus( 533 RepositoryConfig config) throws RepositoryException 534 { 535 String [] repositories = listRepository(config); 536 RuntimeStatusList result = new RuntimeStatusList(repositories.length); 537 int status; 538 for (int i = 0; i < repositories.length; i++) { 539 final InstancesManager mgr = getInstancesManager( 540 getConfigForRepositoryStatus(config, repositories[i])); 541 result.add(RuntimeStatus.getRuntimeStatus(repositories[i], mgr)); 542 } 543 return result; 544 } 545 546 551 protected void createAdminKeyFile(final RepositoryConfig config, final String 552 user, final String clearPwd) throws RepositoryException { 553 final PEFileLayout layout = getFileLayout(config); 554 final File src = layout.getKeyFileTemplate(); 555 final File dest = layout.getAdminKeyFile(); 556 try { 557 FileUtils.copy(src, dest); 558 modifyKeyFile(dest, user, clearPwd); 559 } catch (final Exception e) { 560 throw new RepositoryException(_strMgr.getString("keyFileNotCreated"), e); 561 } 562 } 563 564 567 protected void createKeyFile(RepositoryConfig config, String user, 568 String password) throws RepositoryException 569 { 570 final PEFileLayout layout = getFileLayout(config); 571 final File src = layout.getKeyFileTemplate(); 572 final File dest = layout.getKeyFile(); 573 try { 574 FileUtils.copy(src, dest); 575 579 } catch (Exception e) { 580 throw new RepositoryException(_strMgr.getString("keyFileNotCreated"), e); 581 } 582 } 583 584 585 589 private void modifyKeyFile(File keyFile, String user, String password) throws 590 IOException , BadRealmException, IASSecurityException, 591 NoSuchRealmException 592 { 593 final String keyFilePath = keyFile.getAbsolutePath(); 594 final FileRealm fileRealm = new FileRealm(keyFilePath); 595 final String [] group = 596 new String []{AdminConstants.DOMAIN_ADMIN_GROUP_NAME}; 597 fileRealm.addUser(user, password, group); 598 fileRealm.writeKeyFile(keyFilePath); 599 appendKeyFileComment(keyFilePath); 600 } 601 602 private void appendKeyFileComment(String fileName) 603 { 604 final String commentLine = NEW_LINE + _strMgr.getString("adminUserComment"); 605 FileUtils.appendText(fileName, commentLine); 606 } 607 608 611 protected void createServerPolicyFile(RepositoryConfig config) throws RepositoryException 612 { 613 final PEFileLayout layout = getFileLayout(config); 614 final File src = layout.getPolicyFileTemplate(); 615 final File dest = layout.getPolicyFile(); 616 try { 617 FileUtils.copy(src, dest); 618 } catch (IOException ioe) { 619 throw new RepositoryException( 620 _strMgr.getString("serverPolicyNotCreated"), ioe); 621 } 622 } 623 624 631 public void validateMasterPassword(RepositoryConfig config, 632 String password) throws RepositoryException 633 { 634 final PEFileLayout layout = getFileLayout(config); 635 final File passwordAliases = layout.getPasswordAliasKeystore(); 636 try { 637 PasswordAdapter p = new PasswordAdapter(passwordAliases.getAbsolutePath(), 638 password.toCharArray()); 639 } catch (IOException ex) { 640 throw new RepositoryException(_strMgr.getString("masterPasswordInvalid")); 641 } catch (Exception ex) { 642 throw new RepositoryException( 643 _strMgr.getString("couldNotValidateMasterPassword", passwordAliases), ex); 644 } 645 } 646 647 654 public String getClearPasswordForAlias(RepositoryConfig config, 655 String password,String alias) throws RepositoryException 656 { 657 final PEFileLayout layout = getFileLayout(config); 658 final File passwordAliases = layout.getPasswordAliasKeystore(); 659 try { 660 PasswordAdapter p = new PasswordAdapter(passwordAliases.getAbsolutePath(), 661 password.toCharArray()); 662 String clearPwd = p.getPasswordForAlias(alias); 663 return clearPwd; 664 } catch (Exception ex) { 665 return null; 666 } 667 } 668 669 public void validateAdminUserAndPassword(RepositoryConfig config, 670 String user, String password) throws RepositoryException 671 { 672 try { 673 final PEFileLayout layout = getFileLayout(config); 675 ConfigContext configContext = getConfigContext(config); 676 String dasName = ServerHelper.getDAS(configContext).getName(); 678 JmxConnector conn = ServerHelper.getServerSystemConnector(configContext, 679 dasName); 680 String realmName = conn.getAuthRealmName(); 681 SecurityService security = ServerHelper.getConfigForServer(configContext, 682 dasName).getSecurityService(); 683 String oldRoot = System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY); 688 String oldConf = System.getProperty("java.security.auth.login.config"); 689 System.setProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, 690 layout.getRepositoryDir().getAbsolutePath()); 691 System.setProperty("java.security.auth.login.config", 692 layout.getLoginConf().getAbsolutePath()); 693 RealmConfig.createRealms(realmName, 694 new AuthRealm[] {security.getAuthRealmByName(realmName)}); 695 if (oldRoot != null) { 697 System.setProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, oldRoot); 698 } 699 if (oldConf != null) { 700 System.setProperty("java.security.auth.login.config", oldConf); 701 } 702 final ASJMXAuthenticator authenticator = new ASJMXAuthenticator(); 704 authenticator.setRealmName(realmName); 705 authenticator.setLoginDriver(new ASLoginDriverImpl()); 706 authenticator.authenticate(new String [] {user, password}); 707 } catch (Exception ex) { 708 throw new RepositoryException( 709 _strMgr.getString("couldNotValidateMasterPassword", user), ex); 710 } 711 } 712 713 719 protected void createPasswordAliasKeystore(RepositoryConfig config, 720 String password) throws RepositoryException 721 { 722 final PEFileLayout layout = getFileLayout(config); 723 final File passwordAliases = layout.getPasswordAliasKeystore(); 724 try { 725 PasswordAdapter p = new PasswordAdapter(passwordAliases.getAbsolutePath(), 726 password.toCharArray()); 727 p.writeStore(); 728 } catch (Exception ex) { 729 throw new RepositoryException( 730 _strMgr.getString("passwordAliasKeystoreNotCreated", passwordAliases), ex); 731 } 732 } 733 734 741 protected void changePasswordAliasKeystorePassword(RepositoryConfig config, 742 String oldPassword, String newPassword) throws RepositoryException 743 { 744 final PEFileLayout layout = getFileLayout(config); 745 final File passwordAliases = layout.getPasswordAliasKeystore(); 746 747 if (passwordAliases.exists()) { 749 try { 750 PasswordAdapter p = new PasswordAdapter(passwordAliases.getAbsolutePath(), 751 oldPassword.toCharArray()); 752 p.changePassword(newPassword.toCharArray()); 753 } catch (Exception ex) { 754 throw new RepositoryException( 755 _strMgr.getString("passwordAliasPasswordNotChanged", passwordAliases), ex); 756 } 757 } 758 } 759 760 763 protected void createMQInstance( 764 RepositoryConfig config) throws RepositoryException 765 { 766 final PEFileLayout layout = getFileLayout(config); 767 final File broker = layout.getImqBrokerExecutable(); 768 final File mqVarHome = layout.getImqVarHome(); 769 try { 770 mqVarHome.mkdirs(); 771 final List cmdInput = new ArrayList (); 772 cmdInput.add(broker.getAbsolutePath()); 773 cmdInput.add("-init"); 774 cmdInput.add("-varhome"); 775 cmdInput.add(mqVarHome.getAbsolutePath()); 776 ProcessExecutor pe = new ProcessExecutor 777 ((String [])cmdInput.toArray(new String [cmdInput.size()])); 778 pe.execute(false, false); 779 } catch (Exception ioe) { 780 792 } 793 } 794 795 796 799 protected void createTimerWal( 800 RepositoryConfig config) throws RepositoryException 801 { 802 final PEFileLayout layout = getFileLayout(config); 803 final File src = layout.getTimerWalTemplate(); 804 final File dest = layout.getTimerWal(); 805 try { 806 FileUtils.copy(src, dest); 807 } catch (IOException ioe) { 808 throw new RepositoryException( 809 _strMgr.getString("timerWalNotCreated"), ioe); 810 } 811 } 812 813 816 protected void createTimerDbn( 817 RepositoryConfig config) throws RepositoryException 818 { 819 final PEFileLayout layout = getFileLayout(config); 820 final File src = layout.getTimerDbnTemplate(); 821 final File dest = layout.getTimerDbn(); 822 try { 823 FileUtils.copy(src, dest); 824 } catch (IOException ioe) { 825 throw new RepositoryException( 826 _strMgr.getString("timerDbnNotCreated"), ioe); 827 } 828 } 829 830 protected void handleDerby(final RepositoryConfig config) throws RepositoryException { 831 final String DL = "derby.log"; final File derbyLog = new File (DL); 833 try { 834 final PEFileLayout layout = getFileLayout(config); 835 final File derbySqlFile = layout.getDerbyEjbTimerSqlFile(); 836 final String tableStatement = formatSqlStatement(derbySqlFile); 837 final String dbDir = layout.getDerbyEjbTimerDatabaseDirectory().getAbsolutePath(); 838 createEjbTimerDatabaseTable(tableStatement, dbDir); 839 } catch (final Exception ae) { 840 final String c = readDerbyLogFile(derbyLog); 841 throw new RepositoryException(_strMgr.getString("derbyEjbTimerDBNotCreated", c), ae); 842 } finally { 843 System.gc(); 844 final boolean d = derbyLog.delete(); 845 if (!d) 846 derbyLog.deleteOnExit(); 847 } 848 } 849 853 private String formatSqlStatement(final File sqlf) throws Exception { 854 final StringBuilder sb = new StringBuilder (); final char SQL_DELIMITER = ';'; 856 final BufferedReader br = new BufferedReader (new FileReader (sqlf)); 857 String line = null; 858 try { 859 while ((line = br.readLine())!= null) { 860 line = line.replaceAll("\\t", " "); 861 sb.append(line); 862 if (line.indexOf(SQL_DELIMITER) != -1) 863 break; 864 } 865 } finally { 866 br.close(); 867 } 868 String fs = sb.toString(); 870 final int indexOfSemiColon = fs.indexOf(SQL_DELIMITER); 871 if (indexOfSemiColon != -1) { 872 fs = fs.substring(0, indexOfSemiColon); 873 } 874 if (Boolean.getBoolean(DEBUG)) { 875 System.out.println(fs); 876 } 877 878 return ( fs ); 879 } 880 881 private void createEjbTimerDatabaseTable(final String createStatement, final String dbDir) throws Exception { 882 checkDerbyDriver(); 883 final String url = getDatabaseUrl(dbDir); 884 final Connection conn = DriverManager.getConnection(url); 885 deleteTable(conn); 886 final Statement cs = conn.createStatement(); 887 cs.executeUpdate(createStatement); 888 } 889 890 private void deleteTable(final Connection conn) { 891 try { 892 final Statement ds = conn.createStatement(); 893 final String deleteTable = "delete table " + PEFileLayout.EJB_TIMER_TABLE_NAME; 894 ds.executeUpdate(deleteTable); 895 } catch (final Exception e) {} } 897 private String getDatabaseUrl(final String dbDir) { 898 final StringBuilder sb = new StringBuilder ("jdbc:derby:"); 899 sb.append(FileUtils.makeForwardSlashes(dbDir)); 900 sb.append(";create=true"); 901 return (sb.toString()); 902 } 903 private void checkDerbyDriver() throws Exception { 904 final String DERBY_DRIVER_CLASS_NAME = "org.apache.derby.jdbc.EmbeddedDriver"; 905 Class.forName(DERBY_DRIVER_CLASS_NAME); 906 } 907 908 916 private String readDerbyLogFile(final File log) { 917 final StringBuilder sb = new StringBuilder (); 918 try { 919 final String s = FileUtils.readSmallFile(log); 920 sb.append(s); 921 } catch (final Exception e) { 922 final String msg = _strMgr.getString("noDerbyLog"); 923 sb.append(msg); 924 } 925 return ( sb.toString() ); 926 } 927 932 protected synchronized void resetConfigContext() 933 { 934 _configContext = null; 935 } 936 937 protected synchronized ConfigContext getConfigContext(RepositoryConfig config) 938 throws ConfigException 939 { 940 if (_configContext == null) { 941 final PEFileLayout layout = getFileLayout(config); 942 _configContext = ConfigFactory.createConfigContext( 943 layout.getDomainConfigFile().getAbsolutePath()); 944 } 945 return _configContext; 946 } 947 948 958 void deleteJMSProviderInstance(RepositoryConfig config) 959 throws RepositoryException 960 { 961 final PEFileLayout layout = getFileLayout(config); 962 final String repositoryName = config.getRepositoryName(); 963 try 964 { 965 final JMSAdmin jmsAdmin = IASJmsUtil.getJMSAdminFactory(). 966 getJMSAdmin(); 967 final ConfigContext ctx = getConfigContext(config); 968 final Server[] servers = getServers(ctx); 969 for (int i = 0; i < servers.length; i++) 970 { 971 final String mqInstanceName = IASJmsUtil.getBrokerInstanceName( 972 repositoryName, 973 servers[i].getName(), 974 getJmsService(servers[i], ctx)); 975 final String javaHome = getJavaHome(servers[i], ctx); 976 977 try 978 { 979 String iMQBin = System.getProperty( 980 SystemPropertyConstants.IMQ_BIN_PROPERTY, 981 layout.getImqBinDir().getAbsolutePath()); 982 String iMQInstances = layout.getRepositoryDir() + File.separator + 983 IASJmsUtil.MQ_DIR_NAME; 984 String [] optArgs = new String [4]; 985 986 optArgs[0] = "-javahome"; 987 optArgs[1] = javaHome; 988 optArgs[2] = "-varhome"; 989 optArgs[3] = iMQInstances; 990 991 jmsAdmin.deleteProviderInstance( 993 iMQBin, 994 optArgs, 995 mqInstanceName); 996 } 998 catch (JMSException jmse) 999 { 1000 1006 } 1007 } 1008 } 1009 catch (Exception e) 1010 { 1011 throw new RepositoryException( 1012 _strMgr.getString("cannotDeleteJmsProviderInstance"), e); 1013 } 1014 } 1015 1016 protected String [] getInteractiveOptions(String user, String password, 1017 String masterPassword, HashMap extraPasswords) 1018 { 1019 int numKeys = extraPasswords == null ? 0 : extraPasswords.size(); 1020 String [] options = new String [3 + numKeys]; 1021 options[0] = user; 1023 options[1] = password; 1024 options[2] = masterPassword; 1025 if (extraPasswords != null) { 1026 Iterator it = extraPasswords.keySet().iterator(); 1027 String key = null; 1028 for (int i = 0; i < numKeys; i++) { 1029 key = (String )it.next(); 1030 options[3 + i] = key + "=" + (String )extraPasswords.get(key); 1031 } 1032 } 1033 return options; 1034 } 1035 1036 private static Server[] getServers(ConfigContext ctx) 1037 throws ConfigException 1038 { 1039 return ServerHelper.getServersInDomain(ctx); 1040 } 1041 1042 private static String getJavaHome(Server server, ConfigContext ctx) 1043 throws ConfigException 1044 { 1045 final JavaConfig javaConfig = getConfig(server, ctx).getJavaConfig(); 1046 return javaConfig.getJavaHome(); 1047 } 1048 1049 private static JmsService getJmsService(Server server, ConfigContext ctx) 1050 throws ConfigException 1051 { 1052 return getConfig(server, ctx).getJmsService(); 1053 } 1054 1055 private static Config getConfig(Server server, ConfigContext ctx) 1056 throws ConfigException 1057 { 1058 return ServerHelper.getConfigForServer(ctx, server.getName()); 1059 } 1060} 1061 | Popular Tags |