KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > spi > DeploymentMetaData


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.deployment.spi;
23
24 import java.io.IOException JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
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 /**
38  * MetaData to the JBoss deployment plan.
39  *
40  * @author Thomas.Diesler@jboss.org
41  * @since 09-Apr-2004
42  */

43 public class DeploymentMetaData
44 {
45    // provide logging
46
private static Logger log = Logger.getLogger(DeploymentMetaData.class);
47
48    /** The entry name in the deployment plan archive */
49    public static final String JavaDoc ENTRY_NAME = "deployment-plan.xml";
50
51    private String JavaDoc deploymentName;
52    private List JavaDoc entryList = new ArrayList JavaDoc();
53
54    public DeploymentMetaData(String JavaDoc deploymentName)
55    {
56       this.deploymentName = deploymentName;
57    }
58
59    public DeploymentMetaData(Document document)
60    {
61       init(document);
62    }
63
64    public String JavaDoc getDeploymentName()
65    {
66       return deploymentName;
67    }
68
69    public void setDeploymentName(String JavaDoc deploymentName)
70    {
71       this.deploymentName = deploymentName;
72    }
73
74    /**
75     * Add an entry and return an id for that entry
76     */

77    public String JavaDoc addEntry(String JavaDoc archiveName, String JavaDoc descriptorName)
78    {
79       entryList.add(new Entry(archiveName, descriptorName));
80
81       String JavaDoc 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 JavaDoc archiveName, String JavaDoc descriptorName)
92    {
93       return entryList.contains(new Entry(archiveName, descriptorName));
94    }
95
96    public List JavaDoc getEntryList()
97    {
98       return new ArrayList JavaDoc(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 JavaDoc 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 JavaDoc toXMLString()
124    {
125       try
126       {
127          OutputFormat format = OutputFormat.createPrettyPrint();
128          StringWriter JavaDoc strWriter = new StringWriter JavaDoc(1024);
129          XMLWriter metaWriter = new XMLWriter(strWriter, format);
130          metaWriter.write(getDocument());
131          metaWriter.close();
132          return strWriter.toString();
133       }
134       catch (IOException JavaDoc 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 JavaDoc it = root.elementIterator("deployment-entry");
146       while (it.hasNext())
147       {
148          Element element = (Element)it.next();
149          String JavaDoc archiveName = element.elementTextTrim("archive-name");
150          String JavaDoc descriptorName = element.elementTextTrim("descriptor-name");
151          addEntry(archiveName, descriptorName);
152       }
153    }
154
155    /**
156     * An entry in the deployment plan
157     */

158    public static class Entry
159    {
160       private String JavaDoc archiveName;
161       private String JavaDoc descriptorName;
162
163       public Entry(String JavaDoc archiveName, String JavaDoc descriptorName)
164       {
165          this.archiveName = archiveName;
166          this.descriptorName = (descriptorName != null ? descriptorName : "");
167       }
168
169       public String JavaDoc getArchiveName()
170       {
171          return archiveName;
172       }
173
174       public String JavaDoc getDescriptorName()
175       {
176          return descriptorName;
177       }
178
179       public boolean equals(Object JavaDoc 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 JavaDoc(archiveName + descriptorName).hashCode();
193       }
194    }
195 }
196
Popular Tags