1 23 24 package com.sun.enterprise.admin.common; 25 26 27 import java.io.File ; 28 import java.io.FileNotFoundException ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.RandomAccessFile ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 import java.util.Random ; 35 import javax.management.InstanceNotFoundException ; 36 import javax.management.ObjectName ; 37 import javax.management.remote.JMXConnector ; 38 import javax.management.remote.JMXConnectorFactory ; 39 import javax.management.remote.JMXServiceURL ; 40 import javax.management.MBeanServerConnection ; 41 import javax.management.MBeanException ; 42 import javax.management.ReflectionException ; 43 import javax.management.MalformedObjectNameException ; 44 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 45 46 import com.sun.enterprise.admin.util.ArgChecker; 47 import com.sun.enterprise.admin.util.StringValidator; 48 import com.sun.enterprise.admin.common.constant.DeploymentConstants; 49 import com.sun.enterprise.admin.common.exception.*; 50 51 import com.sun.enterprise.admin.common.constant.AdminConstants; 52 import com.sun.enterprise.util.SystemPropertyConstants; 53 import com.sun.enterprise.util.i18n.StringManager; 54 55 58 public class JMXFileTransfer implements FileTransfer { 59 60 61 public static final String S1ASHTTP_PROVIDER_PACKAGES = "com.sun.enterprise.admin.jmx.remote.protocol"; 62 private static final String UPLOAD_OPERATION = "uploadToServer"; 63 private static final String GET_STUB_FILE_LOCATION = "getClientStubJarLocation"; 64 private static final String GET_WSDL_FILE_LOCATION = "getWsdlFileLocation"; 65 private static final String PREPARE_DOWNLOAD = "prepareDownload"; 66 private static final String MCPREPARE_DOWNLOAD = "mcPrepareDownload"; 67 private static final String DOWNLOAD_FILE = "downloadFile"; 68 private static final String MCDOWNLOAD_FILE = "mcDownloadFile"; 69 70 private JMXServiceURL url ; 71 private String user; 72 private String password; 73 private MBeanServerConnection mbsc; 74 private String targetServer = SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME; 75 private static final StringManager _localStrMgr = 76 StringManager.getManager(JMXFileTransfer.class); 77 78 85 public JMXFileTransfer(JMXServiceURL url, String user, String password) throws IOException { 86 this.url = url; 87 this.user = user; 88 this.password = password; 89 setConnection(); 90 } 91 92 100 public JMXFileTransfer(JMXServiceURL url, String user, String password, boolean doConnect) throws IOException { 101 this.url = url; 102 this.user = user; 103 this.password = password; 104 if (doConnect == true) 105 setConnection(); 106 } 107 108 113 public JMXFileTransfer(MBeanServerConnection mbsc) throws IOException { 114 this.mbsc = mbsc; 115 } 116 117 123 public MBeanServerConnection getMBeanServerConnection () { 124 return mbsc; 125 } 126 127 132 public void setMBeanServerConnection(MBeanServerConnection mbsc) { 133 this.mbsc = mbsc; 134 } 135 136 143 public void setConnection() throws IOException { 144 final Map env = new HashMap (); 145 env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, S1ASHTTP_PROVIDER_PACKAGES); 146 env.put(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME, user); 147 env.put(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME, password); 148 env.put(DefaultConfiguration.HTTP_AUTH_PROPERTY_NAME, DefaultConfiguration.DEFAULT_HTTP_AUTH_SCHEME); 149 env.put(JMXConnector.CREDENTIALS, new String [] {user, password}); 150 JMXConnector conn = JMXConnectorFactory.connect(url, env); 151 mbsc = conn.getMBeanServerConnection(); 152 } 153 154 159 public void setTargetServer(final String s) { 160 this.targetServer = s; 161 } 162 public String uploadFile(String filePath) throws IOException { 164 165 return uploadFile(filePath, null); 166 } 167 169 public String uploadFile (String filePath, String targetDir) 170 throws IOException { 171 172 final File f = new File (filePath); 173 if(!f.exists() || !f.canRead()) 174 throw new FileNotFoundException (filePath); 175 176 return uploadFile(f, targetDir); 177 } 178 179 public String uploadFile(File f, String targetDir ) 180 throws IOException { 181 182 183 if( !f.isFile() || (mbsc==null)) 184 throw new IllegalArgumentException (); 185 186 String remoteLocation = null; 187 188 ObjectName mbeanName = ObjectNames.getPerInstanceSystemServicesObjectName(targetServer); 189 String operationName = UPLOAD_OPERATION; 190 191 final RandomAccessFile file = new RandomAccessFile (f, "r"); 192 byte[] bytes = new byte[ByteChunk.kChunkMaxSize]; 193 int bytesRead = 0; 194 long totalBytesRead = 0; 195 boolean isFirstChunk = true; 196 boolean isLastChunk = false; 197 final long fileSize = file.length(); 198 Random random = new Random (); 199 final String id = String.valueOf(random.nextInt(9999999)); 200 201 try { 202 int index = 0; 203 while ((bytesRead = file.read(bytes)) != -1) 204 { 205 totalBytesRead += bytesRead; 206 if (bytesRead < bytes.length) 207 { 208 byte[] realBytes = new byte[bytesRead]; 209 for (int i = 0; i < bytesRead; i++) 210 { 211 realBytes[i] = bytes[i]; 212 } 213 bytes = realBytes; 214 } 215 if (totalBytesRead == fileSize) 216 { 217 isLastChunk = true; 218 } 219 ByteChunk aChunk = new ByteChunk(bytes, f.getName(), 220 isFirstChunk, isLastChunk, id, fileSize); 221 aChunk.setTargetDir(targetDir); 223 225 ParamInfo paramInfo = new ParamInfo(operationName, aChunk); 226 try { 227 remoteLocation = (String )mbsc.invoke(mbeanName, 228 paramInfo.getOperationName(), 229 paramInfo.getParams(), 230 paramInfo.getSignature()); 231 }catch(Exception e){ 232 e.printStackTrace(); 233 throw (IOException )(new IOException (e.getLocalizedMessage()).initCause(e)); 234 } 235 index++; 236 isFirstChunk = false; 237 } 238 } 239 catch(Exception e) { 240 e.printStackTrace(); 241 throw new IOException (e.getClass().getName() + ":" + e.getMessage()); 242 } 243 finally { 244 try { 245 file.close(); 246 } 247 catch(Exception e){} 248 } 249 250 return remoteLocation; 251 } 252 253 254 public String downloadClientStubs(String appName, String destDir) 255 throws IOException 256 { 257 258 if (mbsc == null ) 259 throw new IllegalArgumentException ("MBean Server connection not set"); 260 261 ArgChecker.checkValid(appName, "appName", StringValidator.getInstance()); 263 String msg=""; 264 File f = new File (destDir); 265 if (!f.exists()) 266 { 267 throw new FileNotFoundException ( msg ); 269 } 270 else if (!f.isDirectory()) 271 { 272 throw new IllegalArgumentException ( msg ); 274 } 275 else if (!f.canWrite()) 276 { 277 throw new IllegalArgumentException ( msg ); 279 } 280 281 Object [] params = new Object [] {appName}; 282 String [] signature = new String [] {"java.lang.String"}; 283 ObjectName mbeanName = ObjectNames.getPerInstanceSystemServicesObjectName(targetServer); 284 try { 285 String filePath = (String ) mbsc.invoke(mbeanName, GET_STUB_FILE_LOCATION, params, signature); 286 downloadFile(filePath, destDir); 288 String exportedFileLocation = new File (destDir, new File (filePath).getName()).getAbsolutePath(); 289 return exportedFileLocation; 290 }catch(Exception e) { 291 throw (IOException )(new IOException (e.getLocalizedMessage()).initCause(e)); 292 } 293 } 294 295 307 public String exportWsdlFile(String appName, 308 String moduleName, 309 String wsdlFileUri, 310 String destDir) 311 throws IOException 312 { 313 if (mbsc == null ) 314 throw new IllegalArgumentException ("MBean Server connection not set"); 315 316 ArgChecker.checkValid(appName, "appName", StringValidator.getInstance()); 318 File f = new File (destDir); 319 String msg=""; 320 String exportedFileLocation; 321 f.mkdirs(); 322 if (!f.exists()) { 323 throw new FileNotFoundException ( f.toString() ); 324 } 325 else if (!f.isDirectory()) 326 { 327 throw new IllegalArgumentException ( f.toString() ); 328 } 329 else if (!f.canWrite()) 330 { 331 throw new IllegalArgumentException ( f.toString() ); 332 } 333 String filePath = null; 334 335 336 Object [] params = new Object [] {appName, moduleName, wsdlFileUri}; 337 String [] signature = new String [] {"java.lang.String", "java.lang.String","java.lang.String"}; 338 ObjectName mbeanName = ObjectNames.getPerInstanceSystemServicesObjectName(targetServer); 339 try{ 340 filePath = (String ) mbsc.invoke(mbeanName, GET_WSDL_FILE_LOCATION, params, signature); 341 if (wsdlFileUri.lastIndexOf('/')!=-1) { 342 String wsdlDir ; 343 if (wsdlFileUri.startsWith("META-INF/wsdl")) { 344 wsdlDir = "META-INF/wsdl/"; 345 } else { 346 wsdlDir = "WEB-INF/wsdl/"; 347 } 348 File absolutePath; 349 if (wsdlDir.length()<wsdlFileUri.lastIndexOf('/')) { 350 String intermediateDirs = wsdlFileUri.substring(wsdlDir.length(), wsdlFileUri.lastIndexOf('/')); 351 absolutePath = new File (destDir, intermediateDirs); 352 } else { 353 absolutePath = new File (destDir); 354 } 355 absolutePath.mkdirs(); 356 destDir = absolutePath.getAbsolutePath(); 357 wsdlFileUri = wsdlFileUri.substring(wsdlFileUri.lastIndexOf('/')+1); 358 } 359 downloadFile(filePath, destDir, wsdlFileUri); 360 exportedFileLocation = new File (destDir, wsdlFileUri).getAbsolutePath(); 361 }catch(Exception e) { 362 throw (IOException )(new IOException (e.getLocalizedMessage()).initCause(e)); 363 } 364 365 return exportedFileLocation; 366 } 367 368 public String downloadFile(String filePath, String destinationDirPath) 369 throws IOException 370 { 371 return downloadFile(filePath, destinationDirPath, null) ; 372 } 373 374 public String downloadFile(String filePath, String destinationDirPath, String appName) 375 throws IOException 376 { 377 File destDir = new File (destinationDirPath); 378 File destPath = null; 379 380 if(appName == null) 381 destPath = new File (destinationDirPath, new File (filePath).getName()); 382 else 383 destPath = new File (destinationDirPath,appName); 384 385 return downloadFile(filePath, destPath); 386 } 387 388 public String downloadFile(String filePath,File destPath ) 389 throws IOException 390 { 391 if (mbsc == null ) 392 throw new IllegalArgumentException ("MBean Server connection not set"); 393 394 395 String msg=""; 396 final File destDir = destPath.getParentFile(); 397 if (!destDir.exists()) 398 { 399 throw new FileNotFoundException (destDir.getName()); 400 } 401 FileOutputStream fos = null; 402 try 403 { 404 ObjectName mbeanName = ObjectNames.getPerInstanceSystemServicesObjectName(targetServer); 405 mbsc.invoke(mbeanName, PREPARE_DOWNLOAD, 406 new Object [] {filePath}, 407 new String [] {"java.lang.String"}); 408 fos = new FileOutputStream (destPath); 409 boolean lastChunk = false; 410 int chunkIndex = 0; 411 int curSize = 0; 412 long totalFileSize = 0; 413 while (!lastChunk) 414 { 415 Object [] params = new Object [] {new Integer (chunkIndex)}; 416 String [] signature = new String [] {"java.lang.Integer"}; 417 ByteChunk chunk = (ByteChunk)mbsc.invoke(mbeanName, 418 DOWNLOAD_FILE, 419 params, 420 signature); 421 ++chunkIndex; 422 lastChunk = chunk.isLast(); 423 byte[] bytes = chunk.getBytes(); 424 fos.write(bytes, 0, bytes.length); 425 curSize =+ bytes.length; 426 totalFileSize = chunk.getTotalFileSize(); 427 } 428 429 if ( curSize < totalFileSize) 430 throw new IOException ("Checksum error, download incomplete, total file size is " + totalFileSize + " only gotton " + curSize + " bytes."); 431 } catch (Exception e) { 432 throw (IOException )(new IOException (e.getLocalizedMessage()).initCause(e)); 433 } 434 finally 435 { 436 if (fos != null) 437 { 438 try { fos.close(); } 439 catch (Exception e) {} 440 } 441 442 } 443 return destPath.getPath(); 444 } 445 446 public synchronized String mcDownloadFile(String filePath, File destPath) 447 throws IOException 448 { 449 if (mbsc == null ) 451 { 452 String msg = _localStrMgr.getString("admin.common.nombsc"); 453 throw new IllegalArgumentException (msg); 454 } 455 456 final File destDir = destPath.getParentFile(); 458 if (!destDir.exists()) 459 { 460 throw new FileNotFoundException (destDir.getName()); 461 } 462 463 FileOutputStream fos = null; 464 try 465 { 466 ObjectName mbeanName = 467 ObjectNames.getPerInstanceSystemServicesObjectName(targetServer); 468 469 DownloadRequestInfo info = (DownloadRequestInfo) 470 mbsc.invoke(mbeanName, MCPREPARE_DOWNLOAD, 471 new Object [] {filePath}, 472 new String [] {"java.lang.String"}); 473 474 fos = new FileOutputStream (destPath); 475 boolean lastChunk = false; 476 int chunkIndex = 0; 477 long curSize = 0; 478 long totalFileSize = info.getTotalFileSize(); 479 480 while (!lastChunk) 481 { 482 Object [] params = new Object [] {info}; 483 String [] signature = new String [] { 484 "com.sun.enterprise.admin.common.DownloadRequestInfo"}; 485 486 info = (DownloadRequestInfo) mbsc.invoke(mbeanName, 487 MCDOWNLOAD_FILE, params, signature); 488 489 if (chunkIndex != info.getChunkIndex()) { 490 String msg = _localStrMgr.getString("admin.common.chunkidx", 491 Integer.toString(chunkIndex), 492 Integer.toString(info.getChunkIndex())); 493 throw new IOException (msg); 494 } 495 496 ByteChunk chunk = info.getChunk(); 497 498 ++chunkIndex; 500 info.setChunkIndex(chunkIndex); 501 502 lastChunk = chunk.isLast(); 503 504 byte[] bytes = chunk.getBytes(); 505 fos.write(bytes, 0, bytes.length); 506 507 curSize = (long) (curSize + bytes.length); 509 510 long tFileSize = chunk.getTotalFileSize(); 512 if (tFileSize != totalFileSize) { 513 String msg = _localStrMgr.getString("admin.common.totalfs", 514 Long.toString(totalFileSize), 515 Long.toString(tFileSize)); 516 throw new IOException (msg); 517 } 518 519 if (curSize != info.getNumberOfBytesSent()) 521 { 522 String msg = _localStrMgr.getString("admin.common.curfs", 523 Long.toString(curSize), 524 Long.toString(info.getNumberOfBytesSent())); 525 throw new IOException (msg); 526 } 527 } 528 529 if ( curSize < totalFileSize) 531 { 532 String msg=_localStrMgr.getString("admin.common.checksumerror", 533 Long.toString(totalFileSize), 534 Long.toString(curSize)); 535 throw new IOException (msg); 536 } 537 538 } 539 catch (Exception e) 540 { 541 throw (IOException )(new IOException (e.getLocalizedMessage()) 542 .initCause(e)); 543 } 544 finally 545 { 546 if (fos != null) 547 { 548 try { fos.close(); } 549 catch (Exception e) {} 550 } 551 552 } 553 554 return destPath.getPath(); 555 } 556 } 557 | Popular Tags |