Source Code Cross Referenced for J2EEServerMBean.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » server » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.server 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999-2005 Bull S.A.
004:         * Contact: jonas-team@objectweb.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         * --------------------------------------------------------------------------
021:         * $Id: J2EEServerMBean.java 8109 2006-03-10 12:45:31Z benoitf $
022:         * --------------------------------------------------------------------------
023:         */package org.objectweb.jonas.server;
024:
025:        // Java imports
026:        import java.io.File;
027:        import java.io.FileOutputStream;
028:        import java.io.IOException;
029:        import java.net.MalformedURLException;
030:        import java.net.URL;
031:        import java.net.URLClassLoader;
032:        import java.util.ArrayList;
033:        import java.util.Arrays;
034:
035:        import javax.enterprise.deploy.shared.ModuleType;
036:        import javax.management.MBeanException;
037:        import javax.management.Notification;
038:        import javax.management.NotificationFilter;
039:        import javax.management.NotificationListener;
040:
041:        import org.apache.commons.modeler.BaseModelMBean;
042:
043:        import org.objectweb.common.Cmd;
044:
045:        import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
046:        import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;
047:        import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper;
048:
049:        import org.objectweb.jonas_ejb.genic.wrapper.GenicServiceWrapper;
050:
051:        import org.objectweb.jonas_lib.files.FileUtils;
052:        import org.objectweb.jonas_lib.genclientstub.wrapper.ClientGenStubWrapper;
053:
054:        import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException;
055:        import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper;
056:
057:        import org.objectweb.jonas_ws.wsgen.wrapper.WsGenWrapper;
058:
059:        import org.objectweb.jonas.common.JProp;
060:        import org.objectweb.jonas.common.Log;
061:        import org.objectweb.jonas.container.EJBService;
062:        import org.objectweb.jonas.ear.EarService;
063:        import org.objectweb.jonas.ear.EarServiceException;
064:        import org.objectweb.jonas.resource.ResourceService;
065:        import org.objectweb.jonas.service.ServiceException;
066:        import org.objectweb.jonas.service.ServiceManager;
067:        import org.objectweb.jonas.web.JWebContainerService;
068:
069:        import org.objectweb.util.monolog.api.BasicLevel;
070:        import org.objectweb.util.monolog.api.Logger;
071:
072:        /**
073:         * MBean class for Server management. Two classes are used to provide all
074:         * management services : J2EEServer and J2EEServerMBean. This class provide the
075:         * notification's process when add or remove resources.
076:         * @author Adriana Danes
077:         * @author Michel-Ange Anton
078:         */
079:
080:        public class J2EEServerMBean extends BaseModelMBean {
081:
082:            /**
083:             * Logger
084:             */
085:            private static Logger mgtLogger = Log
086:                    .getLogger(Log.JONAS_MANAGEMENT_PREFIX);
087:
088:            /**
089:             * Default constructor
090:             * @throws MBeanException if super constructor fails
091:             */
092:            public J2EEServerMBean() throws MBeanException {
093:                super ();
094:            }
095:
096:            // ------------------------------------------------------------- Public
097:            // methods
098:
099:            /**
100:             * All The MBean names corresponding to the deployed J2EEModules (ear, jar,
101:             * war, rar).
102:             * @return The String's array of deployed modules objects names
103:             */
104:            public String[] getDeployedObjects() {
105:                ArrayList al = (ArrayList) ((J2EEServer) (this .resource))
106:                        .getDeployedObjects();
107:                return (String[]) al.toArray(new String[al.size()]);
108:            }
109:
110:            /**
111:             * All The MBean names corresponding to the deployed J2EEResources.
112:             * @return The String's array of deployed resources objects names
113:             */
114:            public String[] getResources() {
115:                ArrayList al = (ArrayList) ((J2EEServer) (this .resource))
116:                        .getResources();
117:                return (String[]) al.toArray(new String[al.size()]);
118:            }
119:
120:            /**
121:             * All The MBean names corresponding to the used JVMs.
122:             * @return The String's array of used JVMs objects names
123:             */
124:            public String[] getJavaVMs() {
125:                ArrayList al = (ArrayList) ((J2EEServer) (this .resource))
126:                        .getJavaVMs();
127:                return (String[]) al.toArray(new String[al.size()]);
128:            }
129:
130:            /**
131:             * Send a notification to the listener.
132:             * @param pNotification The notification to send
133:             */
134:            public void sendNotification(Notification pNotification) {
135:                ((J2EEServer) (this .resource)).sendNotification(pNotification);
136:            }
137:
138:            /**
139:             * Add a new listener.
140:             * @param pListner Listener to notify
141:             * @param pFilter Notification filter
142:             * @param pHandback ??
143:             * @throws java.lang.IllegalArgumentException if notification is not done
144:             */
145:            public void addNotificationListener(NotificationListener pListner,
146:                    NotificationFilter pFilter, java.lang.Object pHandback)
147:                    throws java.lang.IllegalArgumentException {
148:                ((J2EEServer) (this .resource)).addNotificationListener(
149:                        pListner, pFilter, pHandback);
150:            }
151:
152:            /**
153:             * Deploy a stand-alone J2EE module packaged in a JAR file
154:             * @param fileName file name
155:             * @return The ObjectName of the MBean associated to the deployed module
156:             * @throws Exception Management operation failed
157:             */
158:            public String deployJar(String fileName) throws Exception {
159:                return ((J2EEServer) (this .resource)).deployJar(fileName);
160:            }
161:
162:            /**
163:             * Deploy a stand-alone J2EE module packaged in a War file
164:             * @param fileName file name
165:             * @throws Exception Management operation failed
166:             */
167:            public void deployWar(String fileName) throws Exception {
168:                ((J2EEServer) (this .resource)).deployWar(fileName);
169:            }
170:
171:            /**
172:             * Deploy a J2EE application packaged in a EAR file
173:             * @param fileName file name
174:             * @return The ObjectName of the MBean associated to the deployed J2EE
175:             *         Application
176:             * @throws Exception Management operation failed
177:             */
178:            public String deployEar(String fileName) throws Exception {
179:                return ((J2EEServer) (this .resource)).deployEar(fileName);
180:            }
181:
182:            /**
183:             * Deploy a J2EE application packaged in a RAR file
184:             * @param fileName file name
185:             * @return The ObjectName of the MBean associated to the deployed J2EE
186:             *         Application
187:             * @throws Exception Management operation failed
188:             */
189:            public String deployRar(String fileName) throws Exception {
190:                return ((J2EEServer) (this .resource)).deployRar(fileName);
191:            }
192:
193:            /**
194:             * Test if the specified filename is already deployed or not.
195:             * @param fileName the name of the ear file.
196:             * @return true if the ear is deployed, otherwise false.
197:             * @throws Exception Management operation failed
198:             */
199:            public Boolean isEarDeployed(String fileName) throws Exception {
200:                return ((J2EEServer) (this .resource)).isEarDeployed(fileName);
201:            }
202:
203:            /**
204:             * Test if the specified filename is already deployed or not.
205:             * @param fileName the name of the rar file.
206:             * @return true if the rar is deployed, otherwise false.
207:             * @throws Exception Management operation failed
208:             */
209:            public Boolean isRarDeployed(String fileName) throws Exception {
210:                return ((J2EEServer) (this .resource)).isRarDeployed(fileName);
211:            }
212:
213:            /**
214:             * Test if the specified filename is already deployed or not.
215:             * @param fileName the name of the jar file.
216:             * @return true if the jar is deployed, otherwise false.
217:             * @throws Exception Management operation failed
218:             */
219:            public Boolean isJarDeployed(String fileName) throws Exception {
220:                return ((J2EEServer) (this .resource)).isJarDeployed(fileName);
221:            }
222:
223:            /**
224:             * Test if the specified filename is already deployed or not.
225:             * @param fileName the name of the War file.
226:             * @return true if the jar is deployed, otherwise false.
227:             * @throws Exception Management operation failed
228:             */
229:            public Boolean isWarDeployed(String fileName) throws Exception {
230:                return ((J2EEServer) (this .resource)).isWarDeployed(fileName);
231:            }
232:
233:            /**
234:             * Deploy local file and generate classes with Genic tool
235:             * @param pathname : local path name to the application
236:             * @param genicArgs : list of genic's parameters
237:             */
238:            public void deployLocalFile(String pathname, String[] genicArgs) {
239:                try {
240:                    callGenic(genicArgs, pathname);
241:                } catch (Exception e) {
242:                    mgtLogger.log(BasicLevel.WARN,
243:                            "Cannot generate classes for this application : '"
244:                                    + pathname + ". " + e.getMessage() + "'.");
245:                }
246:            }
247:
248:            /**
249:             * Apply WsGen on a  local file and generate webservices artifacts
250:             * @param pathname : local path name to the component
251:             * @param unpacked true if we want to have an unpacked directory as result
252:             * @return Returns the path to the modified archive
253:             */
254:            public String wsgenLocalFile(String pathname, Boolean unpacked) {
255:                try {
256:                    WsGenWrapper wsgen = new WsGenWrapper();
257:                    String outputFilename = wsgen.callWsGenExecute(pathname,
258:                            unpacked);
259:                    boolean modified = wsgen.callWsGenIsInputModifed();
260:                    if (modified) {
261:                        return outputFilename;
262:                    } else {
263:                        return null;
264:                    }
265:                } catch (Exception e) {
266:                    mgtLogger.log(BasicLevel.WARN,
267:                            "Cannot generate web services for this component : '"
268:                                    + pathname + ".", e);
269:                    return null;
270:                }
271:            }
272:
273:            /**
274:             * Deploy file (GenIC), needed for Ishmael to work. The file is in the
275:             * directory specified by the property "jonas.service.deployment.directory"
276:             * @param typeparam type of the file (EJB, WAR, EAR, RAR, CAR)
277:             * @param bfile bytes array of the file
278:             * @param filename basename of the file to be deployed (if moveIntoDeployableDirectory full path name because local file)
279:             * @param genicArgs arguments for GenIC
280:             * @param moveIntoDeployableDirectory true to copy the file into the deployable JOnAS_BASE directory
281:             * @throws RemoteException
282:             * @throws EarServiceException
283:             * @throws ResourceServiceException
284:             * @throws JWebContainerServiceException
285:             * @return absolute path of the file
286:             * @author Dean Jennings. <da_jennings@junta.com.au>
287:             */
288:            public String deployFile(Integer typeparam, java.lang.Byte[] bfile,
289:                    String filename, String[] genicArgs,
290:                    Boolean moveIntoDeployableDirectory) {
291:                String directory = "";
292:                int type = typeparam.intValue();
293:
294:                ServiceManager serviceManager = null;
295:                try {
296:                    serviceManager = ServiceManager.getInstance();
297:                } catch (Exception e) {
298:                    // TODO
299:                    mgtLogger.log(BasicLevel.ERROR,
300:                            "Cannot get the instance of the Service Manager : '"
301:                                    + e.getMessage() + "'.");
302:                }
303:
304:                boolean isJar = false;
305:                if (type == ModuleType.EJB.getValue()) {
306:                    directory = ((EJBService) serviceManager.getEjbService())
307:                            .getEjbjarsDirectory();
308:                    isJar = true;
309:                } else if (type == ModuleType.EAR.getValue()) {
310:                    directory = ((EarService) serviceManager.getEarService())
311:                            .getAppsDirectory();
312:                } else if (type == ModuleType.WAR.getValue()) {
313:                    directory = ((JWebContainerService) serviceManager
314:                            .getWebContainerService()).getWebappsDirectory();
315:                } else if (type == ModuleType.RAR.getValue()) {
316:                    directory = ((ResourceService) serviceManager
317:                            .getResourceService()).getRarsDirectory();
318:                } else if (type == ModuleType.CAR.getValue()) {
319:                    // do nothing
320:                }
321:                File file;
322:                if (moveIntoDeployableDirectory.booleanValue()) {
323:                    file = new File(directory + filename);
324:                } else {
325:                    file = new File(filename);
326:                }
327:                try {
328:                    FileOutputStream out = new FileOutputStream(file);
329:                    byte[] bfileused = new byte[bfile.length];
330:                    for (int i = 0; i < bfile.length; i++) {
331:                        bfileused[i] = bfile[i].byteValue();
332:                    }
333:                    out.write(bfileused);
334:                    out.close();
335:                    if (type == ModuleType.EJB.getValue()) {
336:                        try {
337:                            callGenic(genicArgs, file.getAbsolutePath());
338:                        } catch (ServiceException e) {
339:                            mgtLogger.log(BasicLevel.WARN, "deploy File error "
340:                                    + filename + e.getClass().getName() + " "
341:                                    + e.getMessage());
342:                        }
343:                    } else if (type == ModuleType.EAR.getValue()) {
344:                        unpackAndCompileEar(file, genicArgs);
345:                    } else if (type == ModuleType.WAR.getValue()) {
346:                        checkWebAppDeploymentDesc(file);
347:                    } else {
348:                        if (mgtLogger.isLoggable(BasicLevel.DEBUG)) {
349:                            mgtLogger.log(BasicLevel.DEBUG,
350:                                    "Deployment not yet implemented for type '"
351:                                            + type + ".");
352:                        }
353:                    }
354:                } catch (IOException ioe) {
355:                    mgtLogger.log(BasicLevel.ERROR,
356:                            "Cannot dump to outputstream the file '" + file
357:                                    + "' : '" + ioe.getMessage() + "'.", ioe);
358:                    // TODO
359:                }
360:
361:                // Ugly but don't work without it ...
362:                try {
363:                    Thread.sleep(1000);
364:                } catch (InterruptedException e1) {
365:                    e1.printStackTrace();
366:                }
367:
368:                if (!filename.endsWith(".rar")) {
369:                    String modifiedArchive = null;
370:                    ClientGenStubWrapper clientWrapper = new ClientGenStubWrapper();
371:                    try {
372:                        modifiedArchive = clientWrapper
373:                                .callClientGenStubExecute(file.getPath());
374:                    } catch (Exception e) {
375:                        mgtLogger.log(BasicLevel.ERROR,
376:                                "Cannot launch ClientGenStub on the file '"
377:                                        + file + "' : '"
378:                                        + e.getCause().getMessage() + "'.", e);
379:                    }
380:
381:                    boolean modified = false;
382:                    try {
383:                        modified = clientWrapper
384:                                .callClientGenStubIsInputModifed();
385:                    } catch (Exception e) {
386:                        mgtLogger.log(BasicLevel.ERROR,
387:                                "Cannot launch ClientGenStub.isInputModified '"
388:                                        + file + "' : '"
389:                                        + e.getCause().getMessage() + "'.", e);
390:                        // assume file has been modified
391:                        modified = true;
392:                    }
393:
394:                    if (modified && modifiedArchive != null) {
395:                        // assume also that we can call WsGen after
396:                        File newFile = new File(modifiedArchive);
397:                        try {
398:                            FileUtils.copyFile(newFile, file);
399:                        } catch (Exception e) {
400:                            mgtLogger.log(BasicLevel.ERROR,
401:                                    "Error when copying file '" + newFile
402:                                            + "'. : " + e.getMessage());
403:                        }
404:
405:                        // Ugly but don't work without it ...
406:                        try {
407:                            Thread.sleep(1000);
408:                        } catch (InterruptedException e1) {
409:                            e1.printStackTrace();
410:                        }
411:
412:                        WsGenWrapper ww = new WsGenWrapper();
413:                        try {
414:                            modifiedArchive = ww.callWsGenExecute(file
415:                                    .getPath());
416:                        } catch (Exception e) {
417:                            mgtLogger.log(BasicLevel.ERROR,
418:                                    "Cannot launch WsGen on the file '" + file
419:                                            + "' : '"
420:                                            + e.getCause().getMessage() + "'.",
421:                                    e);
422:                        }
423:
424:                        try {
425:                            modified = ww.callWsGenIsInputModifed();
426:                        } catch (Exception e) {
427:                            mgtLogger.log(BasicLevel.ERROR,
428:                                    "Cannot launch WsGen on the file '" + file
429:                                            + "' : '"
430:                                            + e.getCause().getMessage() + "'.",
431:                                    e);
432:                        }
433:
434:                        if (modified) {
435:                            // assume also that we can call WsGen after
436:                            File newFile2 = new File(modifiedArchive);
437:                            // Check for modifiedArchive suffix
438:                            if (modifiedArchive.endsWith(".ear")) {
439:                                // check if input file is a jar
440:                                if (isJar) {
441:                                    // this case is special: we got a JAR in input and generate a EAR in output
442:                                    // the output goes to apps directory
443:                                    String modifiedDirectory = ((EarService) serviceManager
444:                                            .getEarService())
445:                                            .getAppsDirectory();
446:                                    file = new File(modifiedDirectory
447:                                            + newFile2.getName());
448:                                }
449:                            }
450:                            try {
451:                                FileUtils.copyFile(newFile2, file);
452:                            } catch (Exception e) {
453:                                mgtLogger.log(BasicLevel.ERROR,
454:                                        "Error when copying file '" + newFile2
455:                                                + "'. : " + e.getMessage());
456:                            }
457:                            return file.getPath();
458:                        }
459:
460:                        return modifiedArchive;
461:                    } else {
462:                        // don't call wsgen as it will fail with DTD too
463:                        return file.getPath();
464:                    }
465:                } else {
466:                    return file.getPath();
467:                }
468:            }
469:
470:            /**
471:             * Call GenIC by using a wrapper
472:             * @param genicArgs arguments for GenIC
473:             * @param jarPath path of the jar file
474:             */
475:            private void callGenic(String[] genicArgs, String jarPath) {
476:                String[] args;
477:                if (genicArgs != null) {
478:                    args = new String[genicArgs.length + 1];
479:                    for (int i = 0; i < genicArgs.length; i++) {
480:                        args[i] = genicArgs[i];
481:                    }
482:                    args[genicArgs.length] = jarPath;
483:                } else {
484:                    args = new String[1];
485:                    args[0] = jarPath;
486:                }
487:                if (mgtLogger.isLoggable(BasicLevel.DEBUG)) {
488:                    mgtLogger.log(BasicLevel.DEBUG,
489:                            "Calling GenIC with arguments :"
490:                                    + Arrays.asList(args));
491:                }
492:                GenicServiceWrapper.callGenic(args);
493:            }
494:
495:            /**
496:             * Unpack and then launch GenIC on each ejb jar files
497:             * @param file ear file
498:             * @param genicArgs arguments for GenIC
499:             * @throws ServiceException if this step fails
500:             */
501:            private void unpackAndCompileEar(File file, String[] genicArgs)
502:                    throws ServiceException {
503:                URL[] earUrl = new URL[1];
504:
505:                try {
506:                    earUrl[0] = file.toURL();
507:                } catch (MalformedURLException e) {
508:                    // TODO
509:                    mgtLogger.log(BasicLevel.ERROR, "The url for the file '"
510:                            + file + "' cannot be built : '" + e.getMessage()
511:                            + "'.");
512:                }
513:
514:                //Create classLoader
515:                //parent classloader is the current classloader
516:                ClassLoader currentLoader = Thread.currentThread()
517:                        .getContextClassLoader();
518:                URLClassLoader loaderCls = new URLClassLoader(earUrl,
519:                        currentLoader);
520:
521:                EarDeploymentDesc desc = null;
522:                try {
523:                    desc = EarManagerWrapper.getDeploymentDesc(earUrl[0]
524:                            .getFile(), loaderCls);
525:                } catch (EarDeploymentDescException e) {
526:                    // TODO
527:                    mgtLogger.log(BasicLevel.ERROR,
528:                            "Cannot get deployment descriptor for the Ear file '"
529:                                    + file + "' : '" + e.getMessage() + "'.");
530:                }
531:
532:                String[] ejbTags = desc.getEjbTags();
533:                if (ejbTags.length != 0) {
534:                    //  need to call GENIC
535:                    /*
536:                     * Example of a jar command: jar -uf ../output/ejbjars/sb.jar -C
537:                     * /tmp/genic1547.tmp a/b/C1.class -C /tmp/genic1547.tmp
538:                     * a/b/C2.class .....
539:                     */
540:                    String javaHomeBin = System.getProperty("java.home", "");
541:                    if (!("".equals(javaHomeBin))) {
542:                        javaHomeBin = javaHomeBin + File.separator + ".."
543:                                + File.separator + "bin" + File.separator;
544:                    }
545:
546:                    Cmd cmd = null;
547:                    cmd = new Cmd(javaHomeBin + "jar");
548:                    cmd.addArgument("-xf");
549:                    cmd.addArgument(file.getAbsolutePath());
550:
551:                    for (int i = 0; i < ejbTags.length; i++) {
552:                        cmd.addArgument(ejbTags[i]);
553:                    }
554:
555:                    boolean exitCmd = cmd.run();
556:                    // Analyse the result of the jar command
557:                    if (!exitCmd) {
558:                        throw new EarServiceException(
559:                                "Failed when extracting the the ejb jar "
560:                                        + "in the given jar file '" + file
561:                                        + "'.");
562:                    }
563:
564:                    for (int i = 0; i < ejbTags.length; i++) {
565:                        try {
566:                            callGenic(genicArgs, ejbTags[i]);
567:                        } catch (ServiceException e) {
568:                            throw (e);
569:                        }
570:                    }
571:                    // add modified jar into ear
572:                    cmd = new Cmd(javaHomeBin + "jar");
573:                    cmd.addArgument("-uf");
574:                    cmd.addArgument(file.getAbsolutePath());
575:
576:                    for (int i = 0; i < ejbTags.length; i++) {
577:                        cmd.addArgument(ejbTags[i]);
578:                    }
579:                    exitCmd = cmd.run();
580:                    // Analyse the result of the jar command
581:                    if (!exitCmd) {
582:                        throw new EarServiceException(
583:                                "Failed when extracting the the ejb jar "
584:                                        + "in the given jar file '" + file
585:                                        + "'.");
586:                    }
587:
588:                    for (int i = 0; i < ejbTags.length; i++) {
589:                        File del = new File(ejbTags[i]);
590:                        del.delete();
591:                    }
592:                }
593:                desc = null;
594:            }
595:
596:            /**
597:             * Validate deployment descriptor of the War file
598:             * @param file given file
599:             */
600:            private void checkWebAppDeploymentDesc(File file) {
601:                URL warUrl = null;
602:
603:                try {
604:                    warUrl = file.toURL();
605:                } catch (MalformedURLException mue) {
606:                    mgtLogger.log(BasicLevel.ERROR, "The url for the file '"
607:                            + file + "' cannot be built : '" + mue.getMessage()
608:                            + "'.");
609:                    throw new RuntimeException(mue);
610:                }
611:
612:                URLClassLoader warCl = new URLClassLoader(new URL[] { warUrl });
613:
614:                try {
615:                    WebManagerWrapper.getDeploymentDesc(warUrl, warCl, null);
616:                } catch (WebContainerDeploymentDescException e) {
617:                    String err = "Cannot read the deployment descriptors '"
618:                            + warUrl.getFile() + "'";
619:                    mgtLogger.log(BasicLevel.ERROR, err + ": " + e);
620:                    e.printStackTrace(System.err);
621:                    throw new RuntimeException(err, e);
622:                }
623:
624:            }
625:
626:            /**
627:             * Add an object name to the <code>resources</code> list.
628:             * @param pObjectName Object name correspondig to a J2EEResource MBean
629:             */
630:            //
631:            //    public static void addResource(String pObjectName) {
632:            //        try {
633:            //            Server oServer = Server.getInstance();
634:            //            J2EEServer oJ2EEServer = oServer.getJ2EEServer();
635:            //            oJ2EEServer.addResource(pObjectName);
636:            //        } catch (Exception e) {
637:            //            mgtLogger.log(BasicLevel.WARN
638:            //                , "Add resource error (object=" + pObjectName + ") : "
639:            //                + e.getClass().getName() + " " + e.getMessage());
640:            //        }
641:            //    }
642:            /**
643:             * Remove an object name from the <code>resources</code> list.
644:             * @param pObjectName Object name correspondig to a J2EEResource MBean
645:             * @return Object name to the removed J2EEResource MBean
646:             */
647:
648:            //    public static String removeResource(String pObjectName) {
649:            //        String sRet = null;
650:            //        try {
651:            //            Server oServer = Server.getInstance();
652:            //            J2EEServer oJ2EEServer = oServer.getJ2EEServer();
653:            //            sRet = oJ2EEServer.removeResource(pObjectName);
654:            //        } catch (Exception e) {
655:            //            mgtLogger.log(BasicLevel.WARN
656:            //                , "Remove resource object error (object=" + pObjectName + ") : "
657:            //                + e.getClass().getName() + " " + e.getMessage());
658:            //        }
659:            //        return sRet;
660:            //    }
661:
662:            /**
663:             * Send a file to JOnAS Base with the given input stream and write it
664:             * in the corresponding directory, ie JONAS_BASE/apps, JONAS_BASE/ejbjars, etc.
665:             * It is based on the filename extension
666:             * @param fileContent the content (bytes) of the file
667:             * @param fileName name of the file
668:             * @param replaceExisting replace existing file if any
669:             * @return the path of the file
670:             * @throws Exception if file is already present and if the user don't want to replace existing file.
671:             */
672:            public String sendFile(byte[] fileContent, String fileName,
673:                    boolean replaceExisting) throws Exception {
674:
675:                File directoryUploadedFile = null;
676:                FileOutputStream fos = null;
677:                try {
678:                    // Get directory where to upload file, based on extension
679:                    String dir = getFolderDir(fileName);
680:
681:                    // set the dest file
682:                    directoryUploadedFile = new File(dir, fileName);
683:                    // check, by default we can't overwrite an existing file.
684:                    if (directoryUploadedFile.exists() && !replaceExisting) {
685:                        throw new Exception("File '" + directoryUploadedFile
686:                                + "' already exists on the server.");
687:                    }
688:
689:                    // write the bytes to the given file
690:                    fos = new FileOutputStream(directoryUploadedFile);
691:                    fos.write(fileContent);
692:                } finally {
693:                    if (fos != null) {
694:                        try {
695:                            // close the output stream
696:                            fos.close();
697:                        } catch (IOException ioe) {
698:                            mgtLogger.log(BasicLevel.DEBUG,
699:                                    "Cannot close the output stream", ioe);
700:                        }
701:                    }
702:
703:                }
704:                if (directoryUploadedFile != null) {
705:                    return directoryUploadedFile.getPath();
706:                } else {
707:                    return "error, no uploaded file";
708:                }
709:            }
710:
711:            /**
712:             * Remove a specified J2EE module
713:             * @param fileName Name of file to remove
714:             * @return true if file has been removed
715:             * @throws Exception if remove fails
716:             */
717:            public Boolean removeModuleFile(String fileName) throws Exception {
718:
719:                // File could exists (absolute file)
720:                File searchedFile = new File(fileName);
721:                if (!searchedFile.exists()) {
722:                    // Get directory where to search given file
723:                    String dir = getFolderDir(fileName);
724:                    searchedFile = new File(dir, fileName);
725:                }
726:                if (searchedFile.exists()) {
727:                    return Boolean.valueOf(searchedFile.delete());
728:                } else {
729:                    throw new Exception(
730:                            "File '"
731:                                    + searchedFile
732:                                    + "' was not found on the JOnAS server. Cannot remove it");
733:                }
734:
735:            }
736:
737:            /**
738:             * Get directory based on a filename
739:             * @param fileName name of the file
740:             * @return folder of type JONAS_BASE/XXXX/
741:             * @throws Exception if file is not a J2EE archive
742:             */
743:            private String getFolderDir(String fileName) throws Exception {
744:                String jBase = JProp.getJonasBase();
745:                // based on extension
746:                String dir = null;
747:                // EJB
748:                if (fileName.toLowerCase().endsWith(".jar")) {
749:                    dir = jBase + File.separator + "ejbjars";
750:                } else if (fileName.toLowerCase().endsWith(".war")) {
751:                    // War file
752:                    dir = jBase + File.separator + "webapps";
753:                } else if (fileName.toLowerCase().endsWith(".ear")) {
754:                    // ear file
755:                    dir = jBase + File.separator + "apps";
756:                } else if (fileName.toLowerCase().endsWith(".rar")) {
757:                    // rar file
758:                    dir = jBase + File.separator + "rars";
759:                } else {
760:                    // invalid type
761:                    throw new Exception("Invalid extension for the file '"
762:                            + fileName + "'. Valid are .jar, .war, .ear, .rar");
763:                }
764:                return dir;
765:            }
766:
767:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.