Source Code Cross Referenced for EnhydraServerXML.java in  » J2EE » Enhydra-Application-Framework » org » enhydra » server » conf » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Application Framework » org.enhydra.server.conf 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.enhydra.server.conf;
002:
003:        import java.io.FileNotFoundException;
004:        import java.io.FileOutputStream;
005:        import java.io.IOException;
006:        import java.util.ArrayList;
007:        import java.util.HashMap;
008:        import java.util.Iterator;
009:        import java.util.Set;
010:
011:        import org.apache.xerces.parsers.DOMParser;
012:        import org.apache.xml.serialize.LineSeparator;
013:        import org.apache.xml.serialize.Method;
014:        import org.apache.xml.serialize.OutputFormat;
015:        import org.apache.xml.serialize.XMLSerializer;
016:        import org.w3c.dom.Attr;
017:        import org.w3c.dom.Document;
018:        import org.w3c.dom.Element;
019:        import org.w3c.dom.NamedNodeMap;
020:        import org.w3c.dom.Node;
021:        import org.w3c.dom.NodeList;
022:        import org.xml.sax.SAXException;
023:
024:        /**
025:         * @author Tweety
026:         *
027:         * To change this generated comment edit the template variable "typecomment":
028:         * Window>Preferences>Java>Templates.
029:         * To enable and disable the creation of type comments go to
030:         * Window>Preferences>Java>Code Generation.
031:         */
032:        public class EnhydraServerXML {
033:
034:            //xml document
035:            public Document document;
036:
037:            //parser
038:            DOMParser parser;
039:
040:            //xml document name
041:            private String xmlFileName;
042:
043:            //Node that represents service tag with the name "EnhydraServer"
044:            private Node serverNode;
045:
046:            private String lineSep;
047:
048:            private EnhydraServerXML() {
049:            }
050:
051:            public EnhydraServerXML(String fileName) {
052:                this .xmlFileName = fileName;
053:
054:                try {
055:                    parser = new DOMParser();
056:                    parser.parse(this .xmlFileName);
057:                    document = parser.getDocument();
058:
059:                    this .serverNode = this .getServerNode();
060:                    this .lineSep = this .determineLineSeparator();
061:
062:                } catch (SAXException e) {
063:                    e.printStackTrace();
064:                } catch (IOException e) {
065:                    e.printStackTrace();
066:                }
067:            }
068:
069:            /*
070:             * Determines which line separator to use in the xml document,
071:             * on the basis of the operating system name.
072:             */
073:            String determineLineSeparator() {
074:
075:                String os = System.getProperty("os.name");
076:
077:                if (os.toLowerCase().indexOf("windows") != -1)
078:                    return LineSeparator.Windows;
079:                if ((os.toLowerCase().indexOf("unix") != -1)
080:                        || (os.toLowerCase().indexOf("linux") != -1))
081:                    return LineSeparator.Unix;
082:                //for all other systems set Web line separator ("\n")
083:                return LineSeparator.Web;
084:            }
085:
086:            /*
087:             * Saves the formatted xml document.
088:             */
089:            public void saveDocument() {
090:
091:                try {
092:
093:                    FileOutputStream os = new FileOutputStream(this .xmlFileName);
094:                    OutputFormat of = new OutputFormat();
095:
096:                    //of.setIndenting(true);
097:                    of.setIndent(4);
098:
099:                    of.setMethod(Method.XML);
100:                    of.setPreserveSpace(true);
101:
102:                    XMLSerializer out = new XMLSerializer(os, of);
103:                    out.serialize(this .document);
104:
105:                } catch (FileNotFoundException e) {
106:                    e.printStackTrace();
107:                } catch (IOException e) {
108:                    e.printStackTrace();
109:                }
110:            }
111:
112:            /*
113:             * Returns array of HashMaps, where each HashMap contains
114:             * attribute values (key anda value) of one application tag
115:             * in the server tag node.
116:             */
117:            public HashMap[] getApplications() {
118:                ArrayList hashArray = null;
119:                try {
120:                    NodeList childs = this .serverNode.getChildNodes();
121:                    hashArray = new ArrayList(childs.getLength());
122:
123:                    for (int i = 0; i < childs.getLength(); i++) {
124:                        if (childs.item(i).getNodeName().equalsIgnoreCase(
125:                                "Application")) {
126:                            HashMap hash = new HashMap();
127:                            NamedNodeMap attributes = childs.item(i)
128:                                    .getAttributes();
129:                            for (int j = 0; j < attributes.getLength(); j++)
130:                                hash.put(attributes.item(j).getNodeName(),
131:                                        attributes.item(j).getNodeValue());
132:                            hashArray.add(hash);
133:                        }
134:                    }
135:
136:                    HashMap[] hashes = new HashMap[hashArray.size()];
137:                    for (int i = 0; i < hashArray.size(); i++)
138:                        hashes[i] = (HashMap) hashArray.get(i);
139:
140:                    return hashes;
141:
142:                } catch (Exception e) {
143:                    return null;
144:                }
145:            }
146:
147:            /*
148:             * Adds new application tag node to the end of all applications in the server tag node.
149:             * New application tag node attributes are contained in the hash argument.
150:             */
151:            public void addApplication(HashMap hash) {
152:                try {
153:                    Element application = this .document
154:                            .createElement("application");
155:                    //application.appendChild(this.document.createTextNode(this.lineSep));
156:                    Set keys = hash.keySet();
157:                    Iterator it = keys.iterator();
158:                    while (it.hasNext()) {
159:                        String key = it.next().toString();
160:                        Attr attr = this .document.createAttribute(key);
161:                        attr.setNodeValue(hash.get(key).toString());
162:                        application.setAttributeNode(attr);
163:                    }
164:                    //attach application node to the end of all applications of the server tag node
165:                    this .serverNode.appendChild(this .document
166:                            .createTextNode(this .lineSep + "\t"));
167:                    this .serverNode.appendChild(application);
168:                    this .serverNode.appendChild(this .document
169:                            .createTextNode(this .lineSep));
170:                } catch (Exception e) {
171:                    System.err.println("New application could not be added!");
172:                }
173:            }
174:
175:            /*
176:             * Removes the application tag node with the given context path.
177:             */
178:            public void removeApplication(String contextPath) {
179:                try {
180:                    Node application = this .findApplication(contextPath);
181:
182:                    //determine spaces (ext nodes) before and after application node that is going to be removed
183:                    Node spaceBefore = null;
184:                    Node spaceAfter = null;
185:                    if (application.getPreviousSibling() != null
186:                            && application.getPreviousSibling().getNodeType() == Node.TEXT_NODE)
187:                        spaceBefore = application.getPreviousSibling();
188:                    if (application.getNextSibling() != null
189:                            && application.getNextSibling().getNodeType() == Node.TEXT_NODE)
190:                        spaceAfter = application.getNextSibling();
191:
192:                    //remove application node
193:                    this .serverNode.removeChild(application);
194:
195:                    //remove spaces
196:                    if (spaceBefore != null)
197:                        this .serverNode.removeChild(spaceBefore);
198:                    if (spaceAfter != null)
199:                        this .serverNode.removeChild(spaceAfter);
200:                } catch (Exception e) {
201:                    System.err.println("Application with context path \""
202:                            + contextPath + "\" could not be removed!");
203:                }
204:            }
205:
206:            //DACHA add 17.03.2003
207:            /**
208:             * Return attributes of <application> tag for given context path.
209:             * @param contextPath value of <code>contextPath</code> attribute.
210:             * @return attributes of <application> tag if exist, otherwise <code>null</code>.
211:             */
212:            public HashMap getApplication(String contextPath) {
213:                Node appNode = findApplication(contextPath);
214:                if (appNode == null) {
215:                    return null; //Application don't exist
216:                }
217:                HashMap hash = new HashMap();
218:                NamedNodeMap attributes = appNode.getAttributes();
219:                for (int j = 0; j < attributes.getLength(); j++)
220:                    hash.put(attributes.item(j).getNodeName(), attributes.item(
221:                            j).getNodeValue());
222:                return hash;
223:            }
224:
225:            /*
226:             * Returns node that represents server tag node (document root node).
227:             */
228:            private Node getServerNode() {
229:                Element root = this .document.getDocumentElement(); //server tag
230:                if (root.getNodeName().equalsIgnoreCase("server"))
231:                    return root;
232:
233:                System.err.println("Tag node Server cannot be found !");
234:                return null;
235:            }
236:
237:            /*
238:             * Returns node that represents application tag with the given context path.
239:             */
240:            private Node findApplication(String contextPath) {
241:                NodeList childs = this .serverNode.getChildNodes();
242:                for (int i = 0; i < childs.getLength(); i++) {
243:                    if (childs.item(i).getNodeName().equalsIgnoreCase(
244:                            "application")) {
245:                        if (childs.item(i).getAttributes().getNamedItem(
246:                                "contextPath").getNodeValue().equalsIgnoreCase(
247:                                contextPath))
248:                            return childs.item(i);
249:                    }
250:                }
251:                return null;
252:            }
253:
254:            public static void main(String[] args) {
255:                try {
256:                    EnhydraServerXML test = new EnhydraServerXML(
257:                            "EnhydraServer.xml");
258:
259:                    /*get*/
260:                    //			HashMap[] hashes = test.getApplications();
261:                    //			for(int i = 0; i < hashes.length; i++)
262:                    //				System.out.println("hashes[" + i + "] = " + hashes[i].toString());
263:                    //			test.saveDocument();
264:                    /*add*/
265:                    HashMap hash = new HashMap();
266:                    hash.put("name", "zzz application");
267:                    hash.put("contextPath", "cp");
268:                    hash.put("connections", "2233, 4455");
269:                    hash.put("running", "false");
270:                    hash.put("URLPath", "C:/...");
271:                    hash.put("description", "zzz app");
272:                    test.addApplication(hash);
273:
274:                    test.saveDocument();
275:
276:                    /*remove*/
277:                    //			test.removeApplication("cp");
278:                    //			test.saveDocument();
279:                    System.out.println("Happy end");
280:                } catch (Exception e) {
281:                    System.out.println("NOOOOOOOOOO");
282:                }
283:            }
284:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.