00001
00024 package org.objectweb.jonas.server;
00025
00026
00027 import java.io.File;
00028 import java.io.FileInputStream;
00029 import java.io.FileOutputStream;
00030 import java.io.IOException;
00031 import java.io.InputStream;
00032 import java.io.OutputStream;
00033 import java.net.MalformedURLException;
00034 import java.net.URL;
00035 import java.net.URLClassLoader;
00036 import java.util.ArrayList;
00037 import java.util.Arrays;
00038
00039 import javax.enterprise.deploy.shared.ModuleType;
00040 import javax.management.MBeanException;
00041 import javax.management.Notification;
00042 import javax.management.NotificationFilter;
00043 import javax.management.NotificationListener;
00044
00045 import org.apache.commons.modeler.BaseModelMBean;
00046
00047 import org.objectweb.common.Cmd;
00048
00049 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
00050 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;
00051 import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper;
00052
00053 import org.objectweb.jonas_ejb.genic.wrapper.GenicServiceWrapper;
00054
00055 import org.objectweb.jonas_lib.genclientstub.wrapper.ClientGenStubWrapper;
00056
00057 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException;
00058 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper;
00059
00060 import org.objectweb.jonas_ws.wsgen.wrapper.WsGenWrapper;
00061
00062 import org.objectweb.jonas.common.Log;
00063 import org.objectweb.jonas.container.EJBService;
00064 import org.objectweb.jonas.ear.EarService;
00065 import org.objectweb.jonas.ear.EarServiceException;
00066 import org.objectweb.jonas.resource.ResourceService;
00067 import org.objectweb.jonas.service.ServiceException;
00068 import org.objectweb.jonas.service.ServiceManager;
00069 import org.objectweb.jonas.web.JWebContainerService;
00070
00071 import org.objectweb.util.monolog.api.BasicLevel;
00072 import org.objectweb.util.monolog.api.Logger;
00073
00082 public class J2EEServerMBean extends BaseModelMBean {
00083
00087 private static final int BUFFER_SIZE = 1024;
00088
00092 private static Logger mgtLogger = Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX);
00093
00098 public J2EEServerMBean() throws MBeanException {
00099 super();
00100 }
00101
00102
00103
00104
00110 public String[] getDeployedObjects() {
00111 ArrayList al = (ArrayList) ((J2EEServer) (this.resource)).getDeployedObjects();
00112 return (String[]) al.toArray(new String[al.size()]);
00113 }
00114
00119 public String[] getResources() {
00120 ArrayList al = (ArrayList) ((J2EEServer) (this.resource)).getResources();
00121 return (String[]) al.toArray(new String[al.size()]);
00122 }
00123
00128 public String[] getJavaVMs() {
00129 ArrayList al = (ArrayList) ((J2EEServer) (this.resource)).getJavaVMs();
00130 return (String[]) al.toArray(new String[al.size()]);
00131 }
00132
00137 public void sendNotification(Notification pNotification) {
00138 ((J2EEServer) (this.resource)).sendNotification(pNotification);
00139 }
00140
00148 public void addNotificationListener(NotificationListener pListner, NotificationFilter pFilter,
00149 java.lang.Object pHandback) throws java.lang.IllegalArgumentException {
00150 ((J2EEServer) (this.resource)).addNotificationListener(pListner, pFilter, pHandback);
00151 }
00152
00159 public String deployJar(String fileName) throws Exception {
00160 return ((J2EEServer) (this.resource)).deployJar(fileName);
00161 }
00162
00168 public void deployWar(String fileName) throws Exception {
00169 ((J2EEServer) (this.resource)).deployWar(fileName);
00170 }
00171
00179 public String deployEar(String fileName) throws Exception {
00180 return ((J2EEServer) (this.resource)).deployEar(fileName);
00181 }
00182
00190 public String deployRar(String fileName) throws Exception {
00191 return ((J2EEServer) (this.resource)).deployRar(fileName);
00192 }
00193
00200 public Boolean isEarDeployed(String fileName) throws Exception {
00201 return ((J2EEServer) (this.resource)).isEarDeployed(fileName);
00202 }
00203
00210 public Boolean isRarDeployed(String fileName) throws Exception {
00211 return ((J2EEServer) (this.resource)).isRarDeployed(fileName);
00212 }
00213
00220 public Boolean isJarDeployed(String fileName) throws Exception {
00221 return ((J2EEServer) (this.resource)).isJarDeployed(fileName);
00222 }
00223
00230 public Boolean isWarDeployed(String fileName) throws Exception {
00231 return ((J2EEServer) (this.resource)).isWarDeployed(fileName);
00232 }
00233
00248 public String deployFile(Integer typeparam, java.lang.Byte[] bfile, String filename, String[] genicArgs) {
00249 String directory = "";
00250 int type = typeparam.intValue();
00251
00252 ServiceManager serviceManager = null;
00253 try {
00254 serviceManager = ServiceManager.getInstance();
00255 } catch (Exception e) {
00256
00257 mgtLogger.log(BasicLevel.ERROR, "Cannot get the instance of the Service Manager : '" + e.getMessage()
00258 + "'.");
00259 }
00260
00261 boolean isJar = false;
00262 if (type == ModuleType.EJB.getValue()) {
00263 directory = ((EJBService) serviceManager.getEjbService()).getEjbjarsDirectory();
00264 isJar = true;
00265 } else if (type == ModuleType.EAR.getValue()) {
00266 directory = ((EarService) serviceManager.getEarService()).getAppsDirectory();
00267 } else if (type == ModuleType.WAR.getValue()) {
00268 directory = ((JWebContainerService) serviceManager.getWebContainerService()).getWebappsDirectory();
00269 } else if (type == ModuleType.RAR.getValue()) {
00270 directory = ((ResourceService) serviceManager.getResourceService()).getRarsDirectory();
00271 } else if (type == ModuleType.CAR.getValue()) {
00272
00273 }
00274
00275 File file = new File(directory + filename);
00276 try {
00277 FileOutputStream out = new FileOutputStream(file);
00278 byte[] bfileused = new byte[bfile.length];
00279 for (int i = 0; i < bfile.length; i++) {
00280 bfileused[i] = bfile[i].byteValue();
00281 }
00282 out.write(bfileused);
00283 out.close();
00284 if (type == ModuleType.EJB.getValue()) {
00285 try {
00286 callGenic(genicArgs, directory + filename);
00287 } catch (ServiceException e) {
00288 mgtLogger.log(BasicLevel.WARN, "deploy File error " + filename + e.getClass().getName() + " "
00289 + e.getMessage());
00290 }
00291 } else if (type == ModuleType.EAR.getValue()) {
00292 unpackAndCompileEar(file, genicArgs);
00293 } else if (type == ModuleType.WAR.getValue()) {
00294 checkWebAppDeploymentDesc(file);
00295 } else {
00296 mgtLogger.log(BasicLevel.DEBUG, "Deployment not yet implemented for type '" + type + ".");
00297 }
00298 } catch (IOException ioe) {
00299 mgtLogger.log(BasicLevel.ERROR, "Cannot dump to outputstream the file '" + file + "' : '"
00300 + ioe.getMessage() + "'.", ioe);
00301
00302 }
00303
00304
00305 try {
00306 Thread.sleep(1000);
00307 } catch (InterruptedException e1) {
00308 e1.printStackTrace();
00309 }
00310
00311 if (!filename.endsWith(".rar")) {
00312 String modifiedArchive = null;
00313 ClientGenStubWrapper clientWrapper = new ClientGenStubWrapper();
00314 try {
00315 modifiedArchive = clientWrapper.callClientGenStubExecute(file.getPath());
00316 } catch (Exception e) {
00317 mgtLogger.log(BasicLevel.ERROR, "Cannot launch ClientGenStub on the file '" + file + "' : '"
00318 + e.getCause().getMessage() + "'.", e);
00319 }
00320
00321 boolean modified = false;
00322 try {
00323 modified = clientWrapper.callClientGenStubIsInputModifed();
00324 } catch (Exception e) {
00325 mgtLogger.log(BasicLevel.ERROR, "Cannot launch ClientGenStub.isInputModified '" + file + "' : '"
00326 + e.getCause().getMessage() + "'.", e);
00327
00328 modified = true;
00329 }
00330
00331 if (modified) {
00332
00333 File newFile = new File(modifiedArchive);
00334 try {
00335 copy(newFile, file);
00336 } catch (Exception e) {
00337 mgtLogger.log(BasicLevel.ERROR, "Error when copying file '" + newFile + "'. : " + e.getMessage());
00338 }
00339
00340
00341 try {
00342 Thread.sleep(1000);
00343 } catch (InterruptedException e1) {
00344 e1.printStackTrace();
00345 }
00346
00347 WsGenWrapper ww = new WsGenWrapper();
00348 try {
00349 modifiedArchive = ww.callWsGenExecute(file.getPath());
00350 } catch (Exception e) {
00351 mgtLogger.log(BasicLevel.ERROR, "Cannot launch WsGen on the file '" + file + "' : '"
00352 + e.getCause().getMessage() + "'.", e);
00353 }
00354
00355 try {
00356 modified = ww.callWsGenIsInputModifed();
00357 } catch (Exception e) {
00358 mgtLogger.log(BasicLevel.ERROR, "Cannot launch WsGen on the file '" + file + "' : '"
00359 + e.getCause().getMessage() + "'.", e);
00360 }
00361
00362 if (modified) {
00363
00364 File newFile2 = new File(modifiedArchive);
00365
00366 if (modifiedArchive.endsWith(".ear")) {
00367
00368 if (isJar) {
00369
00370
00371 String modifiedDirectory = ((EarService) serviceManager.getEarService()).getAppsDirectory();
00372 file = new File(modifiedDirectory + newFile2.getName());
00373 }
00374 }
00375 try {
00376 copy(newFile2, file);
00377 } catch (Exception e) {
00378 mgtLogger.log(BasicLevel.ERROR, "Error when copying file '" + newFile2 + "'. : " + e.getMessage());
00379 }
00380 return file.getPath();
00381 }
00382
00383 return modifiedArchive;
00384 } else {
00385
00386 return file.getPath();
00387 }
00388 } else {
00389 return file.getPath();
00390 }
00391 }
00392
00399 void copy(File src, File dst) throws IOException {
00400 InputStream is = new FileInputStream(src);
00401 OutputStream os = new FileOutputStream(dst);
00402
00403
00404 byte[] buf = new byte[BUFFER_SIZE];
00405 int len;
00406 while ((len = is.read(buf)) > 0) {
00407 os.write(buf, 0, len);
00408 }
00409 is.close();
00410 os.close();
00411 }
00412
00418 private void callGenic(String[] genicArgs, String jarPath) {
00419 String[] args;
00420 if (genicArgs != null) {
00421 args = new String[genicArgs.length + 1];
00422 for (int i = 0; i < genicArgs.length; i++) {
00423 args[i] = genicArgs[i];
00424 }
00425 args[genicArgs.length] = jarPath;
00426 } else {
00427 args = new String[1];
00428 args[0] = jarPath;
00429 }
00430 mgtLogger.log(BasicLevel.DEBUG, "Calling GenIC with arguments :" + Arrays.asList(args));
00431 GenicServiceWrapper.callGenic(args);
00432 }
00433
00440 private void unpackAndCompileEar(File file, String[] genicArgs) throws ServiceException {
00441 URL[] earUrl = new URL[1];
00442
00443 try {
00444 earUrl[0] = file.toURL();
00445 } catch (MalformedURLException e) {
00446
00447 mgtLogger.log(BasicLevel.ERROR, "The url for the file '" + file + "' cannot be built : '" + e.getMessage()
00448 + "'.");
00449 }
00450
00451
00452
00453 ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
00454 URLClassLoader loaderCls = new URLClassLoader(earUrl, currentLoader);
00455
00456 EarDeploymentDesc desc = null;
00457 try {
00458 desc = EarManagerWrapper.getDeploymentDesc(earUrl[0].getFile(), loaderCls);
00459 } catch (EarDeploymentDescException e) {
00460
00461 mgtLogger.log(BasicLevel.ERROR, "Cannot get deployment descriptor for the Ear file '" + file + "' : '"
00462 + e.getMessage() + "'.");
00463 }
00464
00465 String[] ejbTags = desc.getEjbTags();
00466 if (ejbTags.length != 0) {
00467
00468
00469
00470
00471
00472
00473 String javaHomeBin = System.getProperty("java.home", "");
00474 if (!("".equals(javaHomeBin))) {
00475 javaHomeBin = javaHomeBin + File.separator + ".." + File.separator + "bin" + File.separator;
00476 }
00477
00478 Cmd cmd = null;
00479 cmd = new Cmd(javaHomeBin + "jar");
00480 cmd.addArgument("-xf");
00481 cmd.addArgument(file.getAbsolutePath());
00482
00483 for (int i = 0; i < ejbTags.length; i++) {
00484 cmd.addArgument(ejbTags[i]);
00485 }
00486
00487 boolean exitCmd = cmd.run();
00488
00489 if (!exitCmd) {
00490 throw new EarServiceException("Failed when extracting the the ejb jar " + "in the given jar file '"
00491 + file + "'.");
00492 }
00493
00494 for (int i = 0; i < ejbTags.length; i++) {
00495 try {
00496 callGenic(genicArgs, ejbTags[i]);
00497 } catch (ServiceException e) {
00498 throw (e);
00499 }
00500 }
00501
00502 cmd = new Cmd(javaHomeBin + "jar");
00503 cmd.addArgument("-uf");
00504 cmd.addArgument(file.getAbsolutePath());
00505
00506 for (int i = 0; i < ejbTags.length; i++) {
00507 cmd.addArgument(ejbTags[i]);
00508 }
00509 exitCmd = cmd.run();
00510
00511 if (!exitCmd) {
00512 throw new EarServiceException("Failed when extracting the the ejb jar " + "in the given jar file '"
00513 + file + "'.");
00514 }
00515
00516 for (int i = 0; i < ejbTags.length; i++) {
00517 File del = new File(ejbTags[i]);
00518 del.delete();
00519 }
00520 }
00521 }
00522
00527 private void checkWebAppDeploymentDesc(File file) {
00528 URL warUrl = null;
00529
00530 try {
00531 warUrl = file.toURL();
00532 } catch (MalformedURLException mue) {
00533 mgtLogger.log(BasicLevel.ERROR, "The url for the file '" + file + "' cannot be built : '"
00534 + mue.getMessage() + "'.");
00535 throw new RuntimeException(mue);
00536 }
00537
00538 URLClassLoader warCl = new URLClassLoader(new URL[] {warUrl});
00539
00540 try {
00541 WebManagerWrapper.getDeploymentDesc(warUrl, warCl, null);
00542 } catch (WebContainerDeploymentDescException e) {
00543 String err = "Cannot read the deployment descriptors '" + warUrl.getFile() + "'";
00544 mgtLogger.log(BasicLevel.ERROR, err + ": " + e);
00545 e.printStackTrace(System.err);
00546 throw new RuntimeException(err, e);
00547 }
00548
00549 }
00550
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586 }