1 22 package org.jboss.deployment.spi; 23 24 import java.io.IOException ; 25 import java.io.StringWriter ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.dom4j.Document; 31 import org.dom4j.DocumentHelper; 32 import org.dom4j.Element; 33 import org.dom4j.io.OutputFormat; 34 import org.dom4j.io.XMLWriter; 35 import org.jboss.logging.Logger; 36 37 43 public class DeploymentMetaData 44 { 45 private static Logger log = Logger.getLogger(DeploymentMetaData.class); 47 48 49 public static final String ENTRY_NAME = "deployment-plan.xml"; 50 51 private String deploymentName; 52 private List entryList = new ArrayList (); 53 54 public DeploymentMetaData(String deploymentName) 55 { 56 this.deploymentName = deploymentName; 57 } 58 59 public DeploymentMetaData(Document document) 60 { 61 init(document); 62 } 63 64 public String getDeploymentName() 65 { 66 return deploymentName; 67 } 68 69 public void setDeploymentName(String deploymentName) 70 { 71 this.deploymentName = deploymentName; 72 } 73 74 77 public String addEntry(String archiveName, String descriptorName) 78 { 79 entryList.add(new Entry(archiveName, descriptorName)); 80 81 String entryId = "entry_"; 82 int count = entryList.size(); 83 if (count < 100) 84 entryId += "0"; 85 if (count < 10) 86 entryId += "0"; 87 88 return entryId + count; 89 } 90 91 public boolean hasEntry(String archiveName, String descriptorName) 92 { 93 return entryList.contains(new Entry(archiveName, descriptorName)); 94 } 95 96 public List getEntryList() 97 { 98 return new ArrayList (entryList); 99 } 100 101 public Document getDocument() 102 { 103 Document document = DocumentHelper.createDocument(); 104 Element root = document.addElement("jboss-deployment-plan"); 105 106 root.addElement("deployment-name").addText(deploymentName); 107 108 root.addComment("Note, deployment-entry elements are not used by the DeploymentManager"); 109 root.addComment("The DeploymentManager relies on the the entry nameing convention"); 110 111 Iterator it = entryList.iterator(); 112 while (it.hasNext()) 113 { 114 Entry entry = (Entry)it.next(); 115 Element element = root.addElement("deployment-entry"); 116 element.addElement("archive-name").addText(entry.archiveName); 117 element.addElement("descriptor-name").addText(entry.descriptorName); 118 } 119 120 return document; 121 } 122 123 public String toXMLString() 124 { 125 try 126 { 127 OutputFormat format = OutputFormat.createPrettyPrint(); 128 StringWriter strWriter = new StringWriter (1024); 129 XMLWriter metaWriter = new XMLWriter(strWriter, format); 130 metaWriter.write(getDocument()); 131 metaWriter.close(); 132 return strWriter.toString(); 133 } 134 catch (IOException e) 135 { 136 log.error("Cannot get XML string", e); 137 return null; 138 } 139 } 140 141 private void init(Document document) 142 { 143 Element root = document.getRootElement(); 144 deploymentName = root.elementTextTrim("deployment-name"); 145 Iterator it = root.elementIterator("deployment-entry"); 146 while (it.hasNext()) 147 { 148 Element element = (Element)it.next(); 149 String archiveName = element.elementTextTrim("archive-name"); 150 String descriptorName = element.elementTextTrim("descriptor-name"); 151 addEntry(archiveName, descriptorName); 152 } 153 } 154 155 158 public static class Entry 159 { 160 private String archiveName; 161 private String descriptorName; 162 163 public Entry(String archiveName, String descriptorName) 164 { 165 this.archiveName = archiveName; 166 this.descriptorName = (descriptorName != null ? descriptorName : ""); 167 } 168 169 public String getArchiveName() 170 { 171 return archiveName; 172 } 173 174 public String getDescriptorName() 175 { 176 return descriptorName; 177 } 178 179 public boolean equals(Object obj) 180 { 181 if (obj instanceof Entry) 182 { 183 Entry other = (Entry)obj; 184 return archiveName.equals(other.archiveName) && descriptorName.equals(other.descriptorName); 185 186 } 187 return false; 188 } 189 190 public int hashCode() 191 { 192 return new String (archiveName + descriptorName).hashCode(); 193 } 194 } 195 } 196 | Popular Tags |