1 23 24 25 package com.sun.enterprise.admin.mbeans; 26 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.OutputStream ; 30 import java.io.IOException ; 31 import java.io.RandomAccessFile ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import java.util.Map ; 35 import java.util.Hashtable ; 36 import java.lang.reflect.Method ; 37 38 import javax.management.MBeanInfo ; 39 import javax.management.MBeanException ; 40 import javax.management.MBeanServer ; 41 import javax.management.ObjectName ; 42 import javax.management.DynamicMBean ; 43 import javax.management.Attribute ; 44 import javax.management.AttributeList ; 45 import javax.management.AttributeNotFoundException ; 46 import javax.management.MBeanAttributeInfo ; 47 import javax.management.MBeanConstructorInfo ; 48 import javax.management.MBeanOperationInfo ; 49 import javax.management.MBeanNotificationInfo ; 50 import javax.management.MBeanParameterInfo ; 51 import javax.management.MBeanException ; 52 53 import com.sun.enterprise.admin.common.ByteChunk; 54 import com.sun.enterprise.admin.common.DownloadRequestInfo; 55 import com.sun.enterprise.admin.server.core.AdminService; 56 import com.sun.enterprise.util.io.FileUtils; 57 import com.sun.enterprise.admin.common.constant.AdminConstants; 58 import com.sun.enterprise.admin.server.core.mbean.meta.MBeanInfoBuilder; 59 import com.sun.enterprise.admin.common.exception.*; 60 import com.sun.enterprise.admin.common.MBeanServerFactory; 61 import com.sun.enterprise.util.i18n.StringManager; 63 64 import com.sun.enterprise.admin.server.core.AdminService; 65 66 67 import com.sun.enterprise.config.*; 68 import com.sun.enterprise.config.serverbeans.*; 69 import com.sun.enterprise.admin.common.constant.AdminConstants; 70 import com.sun.enterprise.deployment.phasing.DeploymentServiceUtils; 71 import com.sun.enterprise.instance.InstanceEnvironment; 72 import com.sun.enterprise.instance.AppsManager; 73 import com.sun.enterprise.instance.EjbModulesManager; 74 import com.sun.enterprise.instance.WebModulesManager; 75 import com.sun.enterprise.instance.AppclientModulesManager; 76 import com.sun.enterprise.util.SystemPropertyConstants; 77 78 import com.sun.enterprise.admin.mbeanapi.ISystemServicesMBean; 79 80 import com.sun.enterprise.util.RelativePathResolver; 81 82 86 public class SystemServicesMBean implements DynamicMBean , ISystemServicesMBean { 87 88 private static final Logger sLogger = Logger.getLogger(AdminConstants.kLoggerName); 89 90 private Map mStreamTable = new Hashtable (); 91 92 private MBeanInfo info = null; 93 private static StringManager localStrings = StringManager.getManager( SystemServicesMBean.class ); 94 95 96 105 106 public String uploadToServer(ByteChunk byteChunk) throws MBeanException { 107 if (byteChunk == null) { 108 throw new IllegalArgumentException (); 109 } 110 String fileName = byteChunk.getChunkedFileName(); 111 String id = byteChunk.getId(); 112 113 File localDir = new File (AdminService.getAdminService().getTempDirPath(),id); 114 localDir.mkdirs(); 115 String localPath = FileUtils.safeGetCanonicalPath(localDir); 116 117 119 String targetDirName = byteChunk.getTargetDir(); 121 if (targetDirName != null) { 122 File targetDir = new File (targetDirName); 124 125 if (!targetDir.isAbsolute()) { 127 String instanceRoot = System.getProperty( 128 SystemPropertyConstants.INSTANCE_ROOT_PROPERTY); 129 130 targetDir = new File (instanceRoot, targetDirName); 131 } 132 else { 133 String msg = localStrings.getString( "admin.mbeans.ssmb.absolute_dirs_not_allowed" ); 134 throw new MBeanException ( new IllegalAccessException (), msg ); 135 136 } 137 if (!targetDir.exists()) { 138 targetDir.mkdir(); 139 } 140 localPath = FileUtils.safeGetCanonicalPath(targetDir); 141 } 142 143 145 File uploadFile = new File (localPath, fileName); 146 localPath = FileUtils.safeGetCanonicalPath(uploadFile); 147 if (byteChunk.isFirst()) { 148 if (uploadFile.exists()) { 149 sLogger.log(Level.INFO, "mbean.temp_upload_file_exists", localPath); 150 boolean couldDelete = uploadFile.delete(); 151 if (couldDelete) { 152 sLogger.log(Level.FINE, "mbean.delete_temp_file_ok", localPath); 153 } 154 else { 155 sLogger.log(Level.INFO, "mbean.delete_temp_file_failed", localPath); 156 } 157 } 158 OutputStream outStream = createOutputStream(localPath); 159 mStreamTable.put(id, outStream); 160 sLogger.log(Level.INFO, "mbean.begin_upload", localPath); 161 } 162 saveFile(localPath, byteChunk); 163 165 return ( localPath ); 166 } 167 168 175 public DownloadRequestInfo mcPrepareDownload(String filePath) 176 throws MBeanException 177 { 178 179 filePath = RelativePathResolver.resolvePath(filePath); 180 181 File dFile = new File (filePath); 182 183 if (!dFile.isAbsolute()) 187 { 188 String instanceRoot = System.getProperty( 189 SystemPropertyConstants.INSTANCE_ROOT_PROPERTY); 190 sLogger.log(Level.CONFIG, "mbean.prep_download", 191 instanceRoot + filePath); 192 dFile = new File (instanceRoot, filePath); 193 } 194 else 195 { 196 sLogger.log(Level.CONFIG, "mbean.prep_download", filePath); 197 } 198 199 if (!dFile.exists()) 201 { 202 String msg = localStrings.getString( 203 "admin.mbeans.ssmb.file_doesnot_exist", 204 dFile.getAbsolutePath() ); 205 throw new MBeanException (new java.io.FileNotFoundException (), msg); 206 } 207 208 return new DownloadRequestInfo(dFile); 209 } 210 211 221 public Object prepareDownload(String filePath) throws MBeanException 222 { 223 filePath = RelativePathResolver.resolvePath(filePath); 225 226 File downloadFile = new File (filePath); 227 if (!downloadFile.isAbsolute()) 230 { 231 String instanceRoot = System.getProperty( 232 SystemPropertyConstants.INSTANCE_ROOT_PROPERTY); 233 sLogger.log(Level.CONFIG, "mbean.prep_download", 234 instanceRoot + filePath); 235 downloadFile = new File (instanceRoot, filePath); 236 } 237 else { 238 sLogger.log(Level.CONFIG, "mbean.prep_download", filePath); 239 } 240 if (!downloadFile.exists()) 241 { 242 String msg = localStrings.getString( "admin.mbeans.ssmb.file_doesnot_exist", downloadFile.getAbsolutePath() ); 243 throw new MBeanException ( new java.io.FileNotFoundException (), msg ); 244 } 245 try 246 { 247 if (lock == null) 248 { 249 lock = new Lock(); 250 } 251 lock.attempt(1000); 252 } 253 catch (Exception ie) 254 { 255 String msg = localStrings.getString( "admin.server.core.mbean.config.could_not_acquire_lock", ie.toString() ); 256 throw new MBeanException ( ie, msg ); 257 } 258 if (downloadInfo == null) 259 { 260 downloadInfo = new DownloadInfo(); 261 } 262 downloadInfo.downloadFile = downloadFile; 263 long size = downloadInfo.downloadFile.length(); 264 downloadInfo.numChunks = Math.round(size/ByteChunk.kChunkMaxSize); 265 if (downloadInfo.numChunks * ByteChunk.kChunkMaxSize < size) 266 { 267 downloadInfo.numChunks += 1; 268 } 269 274 downloadInfo.isPrepared = true; 275 return null; 276 } 277 278 289 public ByteChunk downloadFile(Integer chunkInteger) throws MBeanException 290 { 291 int chunkIndex = chunkInteger.intValue(); 292 sLogger.log(Level.FINE, "mbean.begin_download"); 293 if (downloadInfo == null) 294 { 295 String msg = localStrings.getString( "admin.mbeans.ssmb.call_preparedownload_first" ); 296 throw new MBeanException ( new java.lang.IllegalStateException ( msg ) ); 297 } 298 else if (!downloadInfo.isPrepared) 299 { 300 String msg = localStrings.getString( "admin.mbeans.ssmb.call_preparedownload_first" ); 301 throw new MBeanException ( new java.lang.IllegalStateException ( msg ) ); 302 } 303 ByteChunk byteChunk = null; 305 if (downloadInfo.downloadFile.length() == 0 ) { 306 byte[] bytes = new byte[ByteChunk.kChunkMinSize]; 307 byteChunk = new ByteChunk(bytes, 308 downloadInfo.downloadFile.getAbsolutePath(), 309 true, true); 310 unlockAndReset(); 311 return byteChunk; 312 } 313 314 if ((chunkIndex >= downloadInfo.numChunks) || (chunkIndex < 0)) 315 { 316 String msg = localStrings.getString( "admin.mbeans.ssmb.invalid_chunk_index" ); 317 unlockAndReset(); 318 throw new MBeanException ( new java.lang.IllegalStateException ( msg ) ); 319 } 320 RandomAccessFile raf = null; 321 try 322 { 323 raf = new RandomAccessFile (downloadInfo.downloadFile, "r"); 324 byte[] bytes = new byte[ByteChunk.kChunkMaxSize]; 325 raf.seek(downloadInfo.numBytesRead); 326 int actualBytesRead = raf.read(bytes, 0, ByteChunk.kChunkMaxSize); 327 331 if (actualBytesRead < bytes.length) 332 { 333 byte[] newBytes = new byte[actualBytesRead]; 334 System.arraycopy(bytes, 0, newBytes, 0, newBytes.length); 335 bytes = newBytes; 336 } 337 downloadInfo.numBytesRead += actualBytesRead; 338 boolean isFirstChunk = (chunkIndex == 0); 339 boolean isLastChunk = 340 (chunkIndex == (downloadInfo.numChunks - 1)); 341 sLogger.log(Level.FINEST, "chunkIndex = " + chunkIndex + 342 " isFirstChunk = " + isFirstChunk + 343 " isLastChunk = " + isLastChunk); 344 byteChunk = new ByteChunk(bytes, 345 downloadInfo.downloadFile.getAbsolutePath(), 346 isFirstChunk, isLastChunk); 347 } 348 catch (IOException ioe) 349 { 350 sLogger.log(Level.FINE, "mbean.download_failed", ioe); 351 unlockAndReset(); 352 throw new MBeanException (ioe); 353 } 354 finally 355 { 356 if (raf != null) 357 { 358 try { raf.close(); } 359 catch (IOException ioe) {} 360 } 361 if ((byteChunk != null) && (byteChunk.isLast())) 362 { 363 unlockAndReset(); 364 } 365 } 366 return byteChunk; 367 } 368 369 380 public synchronized DownloadRequestInfo mcDownloadFile( 381 DownloadRequestInfo info) throws MBeanException 382 { 383 sLogger.log(Level.FINE, "mbean.begin_download"); 384 385 if (info == null) 387 { 388 String msg = localStrings.getString( 389 "admin.mbeans.ssmb.call_preparedownload_first"); 390 throw new MBeanException (new java.lang.IllegalStateException (msg)); 391 } 392 else if (!info.isPrepared()) 393 { 394 String msg = localStrings.getString( 395 "admin.mbeans.ssmb.call_preparedownload_first"); 396 throw new MBeanException (new java.lang.IllegalStateException (msg)); 397 } 398 399 File targetFile = new File (info.getDownloadFilePath()); 401 402 ByteChunk byteChunk = null; 404 if (targetFile.length() == 0) 405 { 406 byte[] bytes = new byte[ByteChunk.kChunkMinSize]; 407 String fPath = info.getDownloadFilePath(); 408 byteChunk = new ByteChunk(bytes, fPath, true, true, fPath, 0); 409 info.setChunk(byteChunk); 410 411 return info; 412 } 413 414 if ( (info.getChunkIndex() >= info.getNumberOfChunks()) 416 || (info.getChunkIndex() < 0) ) 417 { 418 String msg = localStrings.getString( 419 "admin.mbeans.ssmb.invalid_chunk_index" ); 420 throw new MBeanException (new java.lang.IllegalStateException (msg)); 421 } 422 423 RandomAccessFile raf = null; 425 try 426 { 427 raf = new RandomAccessFile (targetFile, "r"); 428 byte[] bytes = new byte[ByteChunk.kChunkMaxSize]; 429 raf.seek(info.getNumberOfBytesSent()); 430 431 int actualBytesRead = raf.read(bytes, 0, ByteChunk.kChunkMaxSize); 432 if (actualBytesRead < bytes.length) 433 { 434 byte[] newBytes = new byte[actualBytesRead]; 435 System.arraycopy(bytes, 0, newBytes, 0, newBytes.length); 436 bytes = newBytes; 437 } 438 439 info.incrementNumberOfBytesSent(bytes.length); 441 442 String path = info.getDownloadFilePath(); 443 byteChunk = new ByteChunk(bytes, 444 path, info.isFirstChunk(), info.isLastChunk(), 445 path, targetFile.length()); 446 } 447 catch (IOException ioe) 448 { 449 sLogger.log(Level.FINE, "mbean.download_failed", ioe); 450 throw new MBeanException (ioe); 451 } 452 finally 453 { 454 if (raf != null) 455 { 456 try { raf.close(); } 457 catch (IOException ioe) {} 458 } 459 } 460 info.setChunk(byteChunk); 461 462 return info; 463 } 464 465 private void unlockAndReset() 468 { 469 try 470 { 471 downloadInfo.reset(); 472 lock.release(); 473 } 474 catch (Exception e) 475 { 476 sLogger.log(Level.FINEST, "lock could not be released"); 477 } 478 } 479 480 488 public String getClientStubJarLocation(String appName) 489 throws MBeanException 490 { 491 String clientJarLocation = null; 492 493 try 494 { 495 final com.sun.enterprise.server.ServerContext sc = com.sun.enterprise.server.ApplicationServer.getServerContext(); 496 InstanceEnvironment iEnv = sc.getInstanceEnvironment(); 497 ObjectName appsObjName = new ObjectName ("com.sun.appserv:type=applications,category=config"); 498 String appLocation = null; 499 ObjectName appObjName = null; 500 try { 501 appObjName = (ObjectName )getMBeanServer().invoke(appsObjName, "getJ2eeApplicationByName", new Object [] {appName}, new String [] {"java.lang.String"}); 502 AppsManager appsManager = new AppsManager(iEnv); 503 appLocation = appsManager.getGeneratedXMLLocation(appName); 504 } catch(Exception ee) { 505 } 507 if(appObjName == null) { 508 try { 509 appObjName = (ObjectName )getMBeanServer().invoke(appsObjName, "getEjbModuleByName", new Object [] {appName}, new String [] {"java.lang.String"}); 510 EjbModulesManager ejbManager = new EjbModulesManager(iEnv); 511 appLocation = ejbManager.getGeneratedXMLLocation(appName); 512 } catch (Exception ejbe) { 513 } 515 if(appObjName == null) { 516 try { 517 appObjName = (ObjectName )getMBeanServer().invoke(appsObjName, "getAppclientModuleByName", new Object [] {appName}, new String [] {"java.lang.String" }); 518 AppclientModulesManager appClientManager = 519 new AppclientModulesManager(iEnv); 520 appLocation = 521 appClientManager.getGeneratedXMLLocation(appName); 522 } catch(Exception ace) { 523 } 525 } 526 if(appObjName == null) { 529 throw new IllegalArgumentException (localStrings.getString( "admin.mbeans.ssmb.invalid_appname", appName )); 530 } 531 } 532 533 if (appLocation == null || 535 !FileUtils.safeIsDirectory(appLocation)) { 536 appLocation = (String )getMBeanServer().getAttribute(appObjName, 537 "location"); 538 } 539 clientJarLocation = appLocation + java.io.File.separator + 540 DeploymentServiceUtils.getClientJarPath(appName); 541 542 sLogger.log(Level.INFO, "mbean.cl_jar_loc", clientJarLocation); 543 } 544 catch (Exception e) 545 { 546 sLogger.log(Level.WARNING, e.toString(), e); 547 throw new MBeanException (e); 548 } 549 return clientJarLocation; 550 } 551 552 protected MBeanServer getMBeanServer() 553 { 554 return com.sun.enterprise.admin.common.MBeanServerFactory.getMBeanServer(); 555 } 556 557 protected String getDomainName() { 558 return com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getDomainName(); 559 } 560 561 562 public Object getAttribute(String str) throws javax.management.AttributeNotFoundException , javax.management.MBeanException , javax.management.ReflectionException { 563 throw new AttributeNotFoundException (str); 564 } 565 566 public javax.management.AttributeList getAttributes(String [] str) { 567 return new AttributeList (); 568 } 569 570 585 public String getHostServerSystemPropertyValue(final String name) { 586 return ( System.getProperty(name) ); 587 } 588 589 public javax.management.MBeanInfo getMBeanInfo() { 590 MBeanConstructorInfo [] constructors = new MBeanConstructorInfo [ 1 ]; 591 MBeanNotificationInfo [] notifications = null; 592 MBeanAttributeInfo [] attributes = null; 593 MBeanOperationInfo [] operations = new MBeanOperationInfo [ 4 ]; 594 595 try{ 596 MBeanConstructorInfo cinfo = new MBeanConstructorInfo ( 597 "Main constructor", 598 this.getClass().getConstructor() ); 599 constructors[ 0 ] = cinfo; 600 }catch(Exception e){ 601 sLogger.log(Level.WARNING, e.getLocalizedMessage() ); 602 } 603 604 MBeanParameterInfo [] parameters = new MBeanParameterInfo [ 1 ]; 605 parameters[ 0 ] = new MBeanParameterInfo ( "byteChunk", ByteChunk.class.getName(), 606 "byte chunk" ); 607 operations[ 0 ] = new MBeanOperationInfo ( "uploadToServer", "upload file to admin server", 608 parameters, String .class.getName(), MBeanOperationInfo.ACTION ); 609 610 MBeanParameterInfo [] parameters1 = new MBeanParameterInfo [ 1 ]; 611 parameters1[ 0 ] = new MBeanParameterInfo ( "fileName", String .class.getName(), 612 "file name" ); 613 operations[ 1 ] = new MBeanOperationInfo ( "prepareDownload", "prepare file download from admin server", 614 parameters1, Object .class.getName(), MBeanOperationInfo.ACTION ); 615 616 MBeanParameterInfo [] parameters2 = new MBeanParameterInfo [ 1 ]; 617 parameters2[ 0 ] = new MBeanParameterInfo ( "chunkIndex", "java.lang.Integer", 618 "chunk index" ); 619 operations[ 2 ] = new MBeanOperationInfo ( "downloadFile", "download file from admin server", 620 parameters2, ByteChunk.class.getName(), MBeanOperationInfo.ACTION ); 621 622 MBeanParameterInfo [] parameters3 = new MBeanParameterInfo [ 1 ]; 623 parameters3[ 0 ] = new MBeanParameterInfo ( "appName", String .class.getName(), 624 "application name" ); 625 operations[ 3 ] = new MBeanOperationInfo ( "getClientStubJarLocation", "get stub file location from admin server", 626 parameters3, String .class.getName(), MBeanOperationInfo.ACTION ); 627 628 return new MBeanInfo ( this.getClass().getName(), 629 "Manages System Services" , attributes, constructors, operations, notifications ); 630 } 631 632 public Object invoke(String str, Object [] args, String [] sig) throws javax.management.MBeanException , javax.management.ReflectionException { 633 try { 634 String methodName = str; 635 Class types[] = new Class [ sig.length ]; 636 for( int i=0; i<types.length; i++ ) 637 types[i] = Class.forName(sig[i]); 638 639 Method m = this.getClass().getMethod(methodName, types); 640 return m.invoke( this, args ); 641 } catch ( Exception e ) { 642 sLogger.log(Level.WARNING,e.toString(),e); 643 throw new MBeanException ( e ); 644 } 645 } 646 647 public void setAttribute(javax.management.Attribute attribute) throws javax.management.AttributeNotFoundException , javax.management.InvalidAttributeValueException , javax.management.MBeanException , javax.management.ReflectionException { 648 throw new AttributeNotFoundException (); 649 } 650 651 public javax.management.AttributeList setAttributes(javax.management.AttributeList attributeList) { 652 return new AttributeList (); 653 } 654 655 658 private void saveFile(String filePath, ByteChunk aChunk) 659 throws MBeanException { 660 OutputStream sOut = null; 661 String id = aChunk.getId(); 662 try { 663 sOut = (OutputStream ) mStreamTable.get(id); 664 byte[] bytes = aChunk.getBytes(); 665 sOut.write(bytes); 666 } 667 catch(Exception e) { 668 sLogger.log(Level.WARNING,e.getLocalizedMessage(),e); 669 throw new MBeanException (e); 670 } 671 finally { 672 try { 673 if (aChunk.isLast()) { 674 sOut.close(); 675 mStreamTable.remove(id); 676 if(aChunk.getTotalFileSize()>0) { 677 if ( new File (filePath).length() != aChunk.getTotalFileSize() ) { 678 sLogger.log(Level.WARNING, "mbean.upload_failed", filePath); 679 String msg = localStrings.getString( "admin.mbeans.upload_failed", filePath ); 680 throw new MBeanException ( new java.lang.RuntimeException (msg) ); 681 } 682 } else { 683 sLogger.log(Level.INFO, "mbean.filesize_notverified", filePath); 684 } 685 sLogger.log(Level.INFO, "mbean.upload_done", filePath); 686 } 687 } 688 catch(Exception fe) { 689 sLogger.log(Level.WARNING,fe.toString(),fe); 690 throw new MBeanException (fe); 691 } 692 } 693 } 694 695 696 private OutputStream createOutputStream(String filePath) throws MBeanException { 697 OutputStream fOut = null; 698 699 try { 700 fOut = new FileOutputStream (filePath); 701 } 702 catch(Exception e) { 703 try { 704 if (fOut != null) { 705 fOut.close(); 706 } 707 } 708 catch (Exception ce) { 709 sLogger.log(Level.WARNING, "mbean.upload_failed", filePath); 710 } 711 712 throw new MBeanException (e); 713 } 714 return ( fOut ); 715 } 716 717 private final class Lock 718 { 719 private boolean inUse; 720 721 private Lock() 722 { 723 inUse = false; 724 } 725 726 private synchronized void acquire() 727 throws InterruptedException , IllegalAccessException 728 { 729 while (inUse) 730 { 731 wait(); 732 } 733 inUse = true; 734 } 735 736 private synchronized void release() throws IllegalAccessException 737 { 738 inUse = false; 739 notify(); 740 } 741 742 private synchronized void attempt(long milliseconds) 743 throws InterruptedException , IllegalAccessException 744 { 745 if (inUse) 746 { 747 wait(milliseconds); 748 if (inUse) { 750 String msg = localStrings.getString( "admin.server.core.mbean.config.another_thread_holding_lock" ); 751 throw new IllegalAccessException ( msg ); 752 } 753 } 754 inUse = true; 755 } 756 } 757 758 private static final class DownloadInfo 759 { 760 private File downloadFile; 761 private int numChunks; 762 private long numBytesRead; 763 private boolean isPrepared; 764 765 private DownloadInfo() 766 { 767 reset(); 768 } 769 770 private synchronized void reset() 771 { 772 downloadFile = null; 773 numChunks = 0; 774 numBytesRead = 0; 775 isPrepared = false; 776 } 777 } 778 779 private Lock lock; 780 private DownloadInfo downloadInfo; 781 782 783 } 784 785 | Popular Tags |