KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > project > ProvidesJ2eeModule


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
21 package org.netbeans.modules.j2ee.archive.project;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
29 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
30 import org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter;
31 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
32 import org.netbeans.modules.schema2beans.BaseBean;
33 import org.netbeans.spi.project.support.ant.AntProjectHelper;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileLock;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileSystem;
38 import org.openide.filesystems.FileUtil;
39
40 public class ProvidesJ2eeModule extends J2eeModuleProvider {
41     
42     private J2eeModuleProvider inner;
43     
44     private J2eeModule innerModule;
45     
46     private AntProjectHelper helper;
47         
48     private ArchiveProject project;
49     
50     ProvidesJ2eeModule(AntProjectHelper helper, ArchiveProject proj) {
51         this.helper = helper;
52         inner = null;
53         project = proj;
54     }
55     
56     public void setInner(J2eeModuleProvider inner) {
57         this.inner = inner;
58         innerModule = new InnerModule(inner.getJ2eeModule());
59     }
60     
61     public J2eeModule getJ2eeModule() {
62         return innerModule;
63         
64     }
65     
66     public ModuleChangeReporter getModuleChangeReporter() {
67         ModuleChangeReporter retVal = null;
68         if (null != inner) {
69             retVal = inner.getModuleChangeReporter();
70         }
71         return retVal;
72     }
73     
74     public File JavaDoc getDeploymentConfigurationFile(final String JavaDoc name) {
75         File JavaDoc retVal = null;
76         if (null != inner) {
77             retVal = inner.getDeploymentConfigurationFile(name);
78         }
79         if (null == retVal) {
80            final String JavaDoc dir = (String JavaDoc)project.getArchiveProjectProperties().
81                    get(ArchiveProjectProperties.PROXY_PROJECT_DIR);
82            final File JavaDoc proxyProjectDir =
83                    FileUtil.toFile(helper.getProjectDirectory().getFileObject(dir));
84            retVal = new File JavaDoc(proxyProjectDir,
85                    SRC_LIT + File.separator + "conf" + File.separator + name); // NOI18N
86
}
87         return retVal;
88     }
89     
90     private static final String JavaDoc SRC_LIT = "src"; //NOI18N
91

92     public FileObject findDeploymentConfigurationFile(String JavaDoc name) {
93         FileObject retVal = null;
94         String JavaDoc dir = (String JavaDoc)project.getArchiveProjectProperties().
95                 get(ArchiveProjectProperties.PROXY_PROJECT_DIR);
96         
97         if (null != inner) {
98             retVal = inner.findDeploymentConfigurationFile(name);
99         } else {
100             try {
101                 FileObject parent;
102                 if ("sun-application.xml".equals(name)) { // NOI18N
103
retVal = helper.getProjectDirectory().getFileObject(dir).
104                             getFileObject(SRC_LIT).getFileObject("conf"). // NOI18N
105
getFileObject(name);
106                     if (retVal == null) {
107                         parent = helper.getProjectDirectory().getFileObject(dir).
108                                 getFileObject(SRC_LIT).getFileObject("conf"); // NOI18N
109
retVal = makeDescriptorFromTemplate(parent,name);
110                     }
111                 }
112                 if ("sun-ra.xml".equals(name)) { // NOI18N
113
retVal = helper.getProjectDirectory().getFileObject(dir).
114                             getFileObject(SRC_LIT).getFileObject("conf"). // NOI18N
115
getFileObject(name);
116                     if (null == retVal) {
117                         retVal = helper.getProjectDirectory().getFileObject(dir).
118                             getFileObject(SRC_LIT).getFileObject(name);
119                     }
120                     if (retVal == null) {
121                         parent = helper.getProjectDirectory().getFileObject(dir).
122                             getFileObject(SRC_LIT);
123                         retVal = makeDescriptorFromTemplate(parent,name);
124                     }
125                 }
126             } catch (IOException JavaDoc ex) {
127                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
128             }
129         }
130         return retVal;
131     }
132     
133     private FileObject makeDescriptorFromTemplate(/*final String dir, */ final FileObject parent, final String JavaDoc name) throws IOException JavaDoc {
134         FileObject retVal = null;
135         final InputStream JavaDoc is = ProvidesJ2eeModule.class.getResourceAsStream("template-"+name); // NOI18N;
136
try {
137             FileSystem fs = parent.getFileSystem();
138             fs.runAtomicAction(new AtomicCreate(is, parent, name)); //FileSystem.AtomicAction() {
139
retVal = parent.getFileObject(name);
140         } finally {
141             if (null != is) {
142                 try {
143                     is.close();
144                 } catch (IOException JavaDoc ioe) {
145                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ioe);
146                 }
147             }
148         }
149         return retVal;
150     }
151     
152     private static class AtomicCreate implements FileSystem.AtomicAction {
153         
154         private InputStream JavaDoc is;
155         private String JavaDoc name;
156         private FileObject parent;
157         AtomicCreate(InputStream JavaDoc is, FileObject parent, String JavaDoc name) {
158             this.is = is;
159             this.name = name;
160             this.parent = parent;
161         }
162         public void run() throws IOException JavaDoc {
163             FileLock flock = null;
164             OutputStream JavaDoc os = null;
165             FileObject retVal = null;
166             try {
167                 retVal = parent.createData(name);
168                 flock = retVal.lock();
169                 os = retVal.getOutputStream(flock);
170                 FileUtil.copy(is,os);
171             } finally {
172                 if (null != flock) {
173                     flock.releaseLock();
174                 }
175                 if (is == null || os == null) {
176                     // the write probably did not happen.. so delete the file
177
// if it is there.
178
if (null != retVal) {
179                         try {
180                             retVal.delete();
181                         } catch (IOException JavaDoc ex) {
182                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
183                         }
184                     }
185                 }
186                 if (null != os) {
187                     try {
188                         os.close();
189                     } catch (IOException JavaDoc ioe) {
190                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ioe);
191                     }
192                 }
193                 
194             }
195         }
196     }
197     
198     public void setServerInstanceID(String JavaDoc severInstanceID) {
199         if (null != inner) {
200             inner.setServerInstanceID(severInstanceID);
201         }
202     }
203     
204     public String JavaDoc getServerInstanceID() {
205         String JavaDoc retVal;// = super.getServerInstanceID();
206
if (null != inner) {
207             retVal = inner.getServerInstanceID();
208         } else {
209             retVal = helper.getStandardPropertyEvaluator().getProperty(ArchiveProjectProperties.J2EE_SERVER_INSTANCE);
210         }
211         return retVal;
212     }
213     
214     public String JavaDoc getServerID() {
215         String JavaDoc inst = getServerInstanceID();
216         String JavaDoc retVal = null;
217         if (inst != null) {
218             String JavaDoc id = Deployment.getDefault().getServerID(inst);
219             if (id != null) {
220                 retVal = id;
221             }
222         }
223         return null!=retVal?retVal:helper.getStandardPropertyEvaluator().getProperty(ArchiveProjectProperties.J2EE_SERVER_TYPE);
224     }
225     
226     private FileObject getFileObject(String JavaDoc propname) {
227         String JavaDoc prop = helper.getStandardPropertyEvaluator().getProperty(propname);
228         FileObject retVal = null;
229         if (prop != null) {
230             retVal = helper.resolveFileObject(prop);
231         }
232         return retVal;
233     }
234     
235     /**
236      * Returns directory containing definition for enterprise resources needed for
237      * the module execution; return null if not supported
238      */

239     public File JavaDoc getEnterpriseResourceDirectory() {
240         File JavaDoc retValue = null;
241         // TODO vbk -- should have a listener on this file and use it to update
242
// a property in the project. not today zurg!
243
try {
244             FileObject fo = FileUtil.createFolder(helper.getProjectDirectory(),ArchiveProjectProperties.SETUP_DIR_VALUE);
245             retValue = FileUtil.toFile(fo);
246         } catch (IOException JavaDoc ex) {
247             ErrorManager.getDefault().log(helper.getProjectDirectory().getPath());
248             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
249         }
250         
251         return retValue;
252     }
253     
254     void setJ2eeModule(J2eeModule j2eeModule) {
255         innerModule = j2eeModule;
256     }
257     
258     private class InnerModule implements J2eeModule {
259         
260         private J2eeModule inner;
261         
262         InnerModule(J2eeModule inner) {
263             this.inner = inner;
264         }
265         
266         public String JavaDoc getModuleVersion() {
267             return inner.getModuleVersion();
268         }
269         
270         public Object JavaDoc getModuleType() {
271             return inner.getModuleType();
272         }
273         
274         public String JavaDoc getUrl() {
275             return inner.getUrl();
276         }
277         
278         public void setUrl(String JavaDoc url) {
279             inner.setUrl(url);
280         }
281         
282         public FileObject getArchive() throws IOException JavaDoc {
283             return getFileObject("dist.archive"); // NOI18N
284
}
285         
286         // TODO - this is not correct. But it works. Investigate.
287
public Iterator JavaDoc getArchiveContents() throws IOException JavaDoc {
288             return inner.getArchiveContents();
289         }
290         
291         public FileObject getContentDirectory() throws IOException JavaDoc {
292             return null;
293         }
294         
295         public BaseBean getDeploymentDescriptor(String JavaDoc location) {
296             return inner.getDeploymentDescriptor(location);
297         }
298         
299         public void addVersionListener(J2eeModule.VersionListener listener) {
300             inner.addVersionListener(listener);
301         }
302         
303         public void removeVersionListener(J2eeModule.VersionListener listener) {
304             inner.removeVersionListener(listener);
305         }
306         
307     }
308 }
309
Popular Tags