KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > config > EjbDeploymentConfiguration


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.oc4j.config;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
26 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
27 import org.netbeans.modules.j2ee.oc4j.config.gen.OrionEjbJar;
28 import org.openide.ErrorManager;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.loaders.DataObjectNotFoundException;
31
32 /**
33  * EJB module deployment configuration handles orion-ejb-jar.xml configuration file creation.
34  *
35  * @author sherold
36  */

37 public class EjbDeploymentConfiguration extends OC4JDeploymentConfiguration {
38     
39     private File JavaDoc file;
40     private OrionEjbJar orionEjbJar;
41         
42     /**
43      * Creates a new instance of EjbDeploymentConfiguration
44      */

45     public EjbDeploymentConfiguration(DeployableObject JavaDoc deployableObject) {
46         super(deployableObject);
47     }
48     
49     /**
50      * EjbDeploymentConfiguration initialization. This method should be called before
51      * this class is being used.
52      *
53      * @param file orion-ejb-jar.xml file.
54      */

55     public void init(File JavaDoc file, File JavaDoc resourceDir) {
56         super.init(resourceDir);
57         this.file = file;
58         getOC4JEjbJar();
59         if (dataObject == null) {
60             try {
61                 dataObject = dataObject.find(FileUtil.toFileObject(file));
62             } catch(DataObjectNotFoundException donfe) {
63                 ErrorManager.getDefault().notify(donfe);
64             }
65         }
66     }
67        
68     /**
69      * Return OrionEjbJar graph. If it was not created yet, load it from the file
70      * and cache it. If the file does not exist, generate it.
71      *
72      * @return OrionEjbJar graph or null if the orion-ejb-jar.xml file is not parseable.
73      */

74     public synchronized OrionEjbJar getOC4JEjbJar() {
75         if (orionEjbJar == null) {
76             try {
77                 if (file.exists()) {
78                     // load configuration if already exists
79
try {
80                         orionEjbJar = orionEjbJar.createGraph(file);
81                     } catch (IOException JavaDoc ioe) {
82                         ErrorManager.getDefault().notify(ioe);
83                     } catch (RuntimeException JavaDoc re) {
84                         // orion-ejb-jar.xml is not parseable, do nothing
85
}
86                 } else {
87                     // create orion-ejb-jar.xml if it does not exist yet
88
orionEjbJar = genereateOC4JEjbJar();
89                     writefile(file, orionEjbJar);
90                 }
91             } catch (ConfigurationException JavaDoc ce) {
92                 ErrorManager.getDefault().notify(ce);
93             }
94         }
95         return orionEjbJar;
96     }
97     
98     // JSR-88 methods ---------------------------------------------------------
99

100     public void save(OutputStream JavaDoc os) throws ConfigurationException JavaDoc {
101         OrionEjbJar orionEjbJar = getOC4JEjbJar();
102         if (orionEjbJar == null) {
103             throw new ConfigurationException JavaDoc("Cannot read configuration, it is probably in an inconsistent state."); // NOI18N
104
}
105         try {
106             orionEjbJar.write(os);
107         } catch (IOException JavaDoc ioe) {
108             throw new ConfigurationException JavaDoc(ioe.getLocalizedMessage());
109         }
110     }
111     
112     // private helper methods -------------------------------------------------
113

114     /**
115      * Genereate OrionEjbJar graph.
116      */

117     private OrionEjbJar genereateOC4JEjbJar() {
118         return new OrionEjbJar();
119     }
120 }
Popular Tags