KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > TargetModuleConverter


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.deployment.impl;
21
22 import org.w3c.dom.Element JavaDoc;
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
25
26 import org.netbeans.spi.settings.DOMConvertor;
27 import org.openide.ErrorManager;
28 import org.openide.util.NbBundle;
29 import org.openide.filesystems.*;
30
31 import java.util.List JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.io.*;
35
36 /**
37  * @author nn136682
38  */

39 public class TargetModuleConverter extends DOMConvertor {
40
41     private static final String JavaDoc PUBLIC_ID = "-//org_netbeans_modules_j2ee//DTD TargetModule 1.0//EN"; // NOI18N
42
private static final String JavaDoc SYSTEM_ID = "nbres:/org/netbeans/modules/j2ee/deployment/impl/target-module.dtd"; // NOI18N
43
private static final String JavaDoc E_TARGET_MODULE_LIST = "target-module-list";
44
45     private static final String JavaDoc E_TARGET_MODULE = "target-module";
46     private static final String JavaDoc A_ID = "id";
47     private static final String JavaDoc A_INSTANCE_URL = "instance-url";
48     private static final String JavaDoc A_TARGET_NAME = "target-name";
49     private static final String JavaDoc A_TIMESTAMP = "timestamp";
50     private static final String JavaDoc A_CONTENT_DIR = "content-dir";
51     private static final String JavaDoc A_CONTEXT_ROOT = "context-root";
52     
53     public static DOMConvertor create() {
54         return new TargetModuleConverter();
55     }
56     
57     /** Creates a new instance of TargetModuleConverter */
58     protected TargetModuleConverter() {
59         super(PUBLIC_ID, SYSTEM_ID, E_TARGET_MODULE_LIST);
60     }
61     
62     protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
63         NodeList JavaDoc targetModuleElements = element.getElementsByTagName(E_TARGET_MODULE);
64         TargetModule[] targetModules = new TargetModule[targetModuleElements.getLength()];
65         for (int i=0; i<targetModules.length; i++) {
66             Element JavaDoc te = (Element JavaDoc) targetModuleElements.item(i);
67             String JavaDoc id = te.getAttribute(A_ID);
68             String JavaDoc url = te.getAttribute(A_INSTANCE_URL);
69             String JavaDoc targetName = te.getAttribute(A_TARGET_NAME);
70             String JavaDoc timestamp = te.getAttribute(A_TIMESTAMP);
71             String JavaDoc contentDir = te.getAttribute(A_CONTENT_DIR);
72             String JavaDoc contextRoot = te.getAttribute(A_CONTEXT_ROOT);
73
74             if (id == null || url == null || targetName == null)
75                 throw new IOException(NbBundle.getMessage(TargetModuleConverter.class, "MSG_TargetModuleParseError"));
76                 
77             try {
78                 targetModules[i] = new TargetModule(id, url, targetName, Long.parseLong(timestamp), contentDir, contextRoot);
79             } catch (NumberFormatException JavaDoc nfe) {
80                 throw (IOException) ErrorManager.getDefault().annotate(new IOException(), nfe);
81             }
82         }
83         return new TargetModule.List(targetModules);
84     }
85     
86     protected void writeElement(org.w3c.dom.Document JavaDoc doc, org.w3c.dom.Element JavaDoc element, Object JavaDoc obj) throws IOException, DOMException JavaDoc {
87         if (obj == null)
88             return;
89         
90         if (! (obj instanceof TargetModule.List))
91             throw new DOMException JavaDoc(
92             DOMException.NOT_SUPPORTED_ERR,
93             NbBundle.getMessage(TargetModuleConverter.class, "MSG_NotSupportedObject", obj.getClass()));
94         
95         TargetModule.List tmList = (TargetModule.List) obj;
96         TargetModule[] targetModules = tmList.getTargetModules();
97         for (int i=0; i<targetModules.length; i++) {
98             Element JavaDoc tmElement = doc.createElement (E_TARGET_MODULE);
99             tmElement.setAttribute(A_ID, targetModules[i].getId());
100             tmElement.setAttribute(A_INSTANCE_URL, targetModules[i].getInstanceUrl());
101             tmElement.setAttribute(A_TARGET_NAME, targetModules[i].getTargetName());
102             tmElement.setAttribute(A_TIMESTAMP, String.valueOf(targetModules[i].getTimestamp()));
103             tmElement.setAttribute(A_CONTENT_DIR, targetModules[i].getContentDirectory());
104             tmElement.setAttribute(A_CONTEXT_ROOT, targetModules[i].getContextRoot());
105             element.appendChild (tmElement);
106         }
107     }
108
109     public void registerSaver(Object JavaDoc inst, org.netbeans.spi.settings.Saver s) {
110         // Not needed: there is not editing of TargetModule
111
}
112     public void unregisterSaver(Object JavaDoc inst, org.netbeans.spi.settings.Saver s) {
113         // Not needed: there is not editing of TargetModule
114
}
115
116     private static final String JavaDoc DIR_TARGETMODULES = "TargetModules";
117     private static FileObject targetModulesDir = null;
118     private static FileObject getTargetModulesDir() throws IOException {
119         if (targetModulesDir == null) {
120             FileObject j2eeDir = Repository.getDefault().getDefaultFileSystem().findResource("/J2EE");
121             targetModulesDir = j2eeDir.getFileObject(DIR_TARGETMODULES);
122             if (targetModulesDir == null)
123                 targetModulesDir = j2eeDir.createFolder(DIR_TARGETMODULES);
124         }
125         return targetModulesDir;
126     }
127
128     public static boolean writeTargetModule(TargetModule instance, String JavaDoc managerDir, String JavaDoc targetDir, String JavaDoc tmFileName) {
129         FileLock lock = null;
130         Writer writer = null;
131         try {
132             FileObject managerDirFO = getTargetModulesDir().getFileObject(managerDir);
133             if (managerDirFO == null)
134                 managerDirFO = getTargetModulesDir().createFolder(managerDir);
135             FileObject targetDirFO = managerDirFO.getFileObject(targetDir);
136             if (targetDirFO == null)
137                 targetDirFO = managerDirFO.createFolder(targetDir);
138             FileObject fo = FileUtil.createData(targetDirFO, tmFileName);
139             lock = fo.lock();
140             writer = new OutputStreamWriter(fo.getOutputStream(lock));
141             create().write(writer, new TargetModule.List(instance));
142             return true;
143             
144         } catch(Exception JavaDoc ioe) {
145             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ioe);
146             return false;
147         }
148         finally {
149             try {
150             if (lock != null) lock.releaseLock();
151             if (writer != null) writer.close();
152             } catch (Exception JavaDoc e) {
153                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, e);
154             }
155         }
156     }
157
158     public static TargetModule readTargetModule(String JavaDoc managerDir, String JavaDoc targetDir, String JavaDoc tmFileName) {
159         Reader reader = null;
160         try {
161             FileObject dir = getTargetModulesDir().getFileObject(managerDir);
162             if (dir != null) {
163                 dir = dir.getFileObject (targetDir);
164                 if (dir != null) {
165                     FileObject fo = dir.getFileObject(tmFileName);
166                     if (fo != null) {
167                         reader = new InputStreamReader(fo.getInputStream());
168                         TargetModule.List tml = (TargetModule.List) create().read(reader);
169                         if (tml == null || tml.getTargetModules().length < 1)
170                             return null;
171                         return tml.getTargetModules()[0];
172                     }
173                 }
174             }
175             return null;
176         } catch(Exception JavaDoc ioe) {
177             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ioe);
178             return null;
179         } finally {
180             try { if (reader != null) reader.close(); } catch(Exception JavaDoc e) {
181                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
182             }
183         }
184     }
185
186     public static List JavaDoc getTargetModulesByContextRoot(String JavaDoc managerDir, String JavaDoc targetDir, String JavaDoc contextRoot) {
187         Reader reader = null;
188         try {
189             FileObject dir = getTargetModulesDir().getFileObject(managerDir);
190             if (dir != null) {
191                 dir = dir.getFileObject (targetDir);
192                 if (dir != null) {
193                     java.util.Enumeration JavaDoc fos = dir.getChildren(false);
194                     ArrayList JavaDoc result = new ArrayList JavaDoc();
195                     while (fos.hasMoreElements()){
196                         FileObject fo = (FileObject) fos.nextElement();
197                         reader = new InputStreamReader(fo.getInputStream());
198                         TargetModule.List tml = (TargetModule.List) create().read(reader);
199                         if (tml != null && tml.getTargetModules().length > 0) {
200                             TargetModule tm = tml.getTargetModules()[0];
201                             if (contextRoot.equals(tm.getContextRoot()))
202                                 result.add(tm);
203                         }
204                     }
205                     return result;
206                 }
207             }
208             return Collections.EMPTY_LIST;
209         } catch(Exception JavaDoc ioe) {
210             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ioe);
211             return Collections.EMPTY_LIST;
212         } finally {
213             try { if (reader != null) reader.close(); } catch(Exception JavaDoc e) {
214                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
215             }
216         }
217     }
218
219     public static TargetModule remove(String JavaDoc managerDir, String JavaDoc targetDir, String JavaDoc tmFileName) {
220         FileLock lock = null;
221         try {
222             FileObject dir = getTargetModulesDir().getFileObject(managerDir);
223             if (dir != null) {
224                 dir = dir.getFileObject (targetDir);
225                 if (dir != null) {
226                     FileObject fo = dir.getFileObject(tmFileName);
227                     if (fo != null) {
228                         lock = fo.lock();
229                         fo.delete(lock);
230                     }
231                 }
232             }
233             return null;
234         } catch(Exception JavaDoc ioe) {
235             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ioe);
236             return null;
237         } finally {
238             try { if (lock != null) lock.releaseLock(); } catch(Exception JavaDoc e) {
239                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
240             }
241         }
242     }
243
244 }
245
Popular Tags