1 19 20 package org.netbeans.modules.j2ee.deployment.impl; 21 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.DOMException ; 24 import org.w3c.dom.NodeList ; 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 ; 32 import java.util.Collections ; 33 import java.util.ArrayList ; 34 import java.io.*; 35 36 39 public class TargetModuleConverter extends DOMConvertor { 40 41 private static final String PUBLIC_ID = "-//org_netbeans_modules_j2ee//DTD TargetModule 1.0//EN"; private static final String SYSTEM_ID = "nbres:/org/netbeans/modules/j2ee/deployment/impl/target-module.dtd"; private static final String E_TARGET_MODULE_LIST = "target-module-list"; 44 45 private static final String E_TARGET_MODULE = "target-module"; 46 private static final String A_ID = "id"; 47 private static final String A_INSTANCE_URL = "instance-url"; 48 private static final String A_TARGET_NAME = "target-name"; 49 private static final String A_TIMESTAMP = "timestamp"; 50 private static final String A_CONTENT_DIR = "content-dir"; 51 private static final String A_CONTEXT_ROOT = "context-root"; 52 53 public static DOMConvertor create() { 54 return new TargetModuleConverter(); 55 } 56 57 58 protected TargetModuleConverter() { 59 super(PUBLIC_ID, SYSTEM_ID, E_TARGET_MODULE_LIST); 60 } 61 62 protected Object readElement(org.w3c.dom.Element element) throws java.io.IOException , ClassNotFoundException { 63 NodeList targetModuleElements = element.getElementsByTagName(E_TARGET_MODULE); 64 TargetModule[] targetModules = new TargetModule[targetModuleElements.getLength()]; 65 for (int i=0; i<targetModules.length; i++) { 66 Element te = (Element ) targetModuleElements.item(i); 67 String id = te.getAttribute(A_ID); 68 String url = te.getAttribute(A_INSTANCE_URL); 69 String targetName = te.getAttribute(A_TARGET_NAME); 70 String timestamp = te.getAttribute(A_TIMESTAMP); 71 String contentDir = te.getAttribute(A_CONTENT_DIR); 72 String 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 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 doc, org.w3c.dom.Element element, Object obj) throws IOException, DOMException { 87 if (obj == null) 88 return; 89 90 if (! (obj instanceof TargetModule.List)) 91 throw new DOMException ( 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 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 inst, org.netbeans.spi.settings.Saver s) { 110 } 112 public void unregisterSaver(Object inst, org.netbeans.spi.settings.Saver s) { 113 } 115 116 private static final String 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 managerDir, String targetDir, String 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 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 e) { 153 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, e); 154 } 155 } 156 } 157 158 public static TargetModule readTargetModule(String managerDir, String targetDir, String 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 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 e) { 181 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 182 } 183 } 184 } 185 186 public static List getTargetModulesByContextRoot(String managerDir, String targetDir, String 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 fos = dir.getChildren(false); 194 ArrayList result = new ArrayList (); 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 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 e) { 214 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 215 } 216 } 217 } 218 219 public static TargetModule remove(String managerDir, String targetDir, String 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 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 e) { 239 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 240 } 241 } 242 } 243 244 } 245 | Popular Tags |