[jboss-svn-commits] JBL Code SVN: r6798 - in labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta: src/org/jboss/internal/soa/esb/addressing src/org/jboss/soa/esb/addressing src/org/jboss/soa/esb/addressing/helpers src/org/jboss/soa/esb/addressing/util tests/src/org/jboss/soa/esb/addressing tests/src/org/jboss/soa/esb/addressing/util tests/src/org/jboss/soa/esb/addressing/util/tests

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Oct 13 17:34:13 EDT 2006


Author: mark.little at jboss.com
Date: 2006-10-13 17:33:59 -0400 (Fri, 13 Oct 2006)
New Revision: 6798

Added:
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/EPRManager.java
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/addressing/util/
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/addressing/util/tests/
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/addressing/util/tests/EPRManagerUnitTest.java
Modified:
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/PortReferenceHelper.java
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/PortReference.java
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/EmailEpr.java
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/FTPEpr.java
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/HTTPEpr.java
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/JDBCEpr.java
   labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/JMSEpr.java
Log:
Added EPRManager to allow EPRs to be saved to and loaded from files.

Modified: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/PortReferenceHelper.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/PortReferenceHelper.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/PortReferenceHelper.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -50,7 +50,8 @@
  * This needs rewriting after the interoperability workshop!
  */
 
-public class PortReferenceHelper {
+public class PortReferenceHelper
+{
 	/**
 	 * WS-Addressing is broken in that the To field is a URI, even if the From
 	 * field was an EPR! You have to use the EPR bits separately. So, the
@@ -63,16 +64,21 @@
 			org.w3c.dom.Document document,
 			org.w3c.dom.Element portReferenceElement,
 			PortReference portReference, boolean toField)
-			throws MarshalException {
-		try {
-			if (!toField) {
+			throws MarshalException
+	{
+		try
+		{
+			if (!toField)
+			{
 				Element addressElement = document.createElementNS(
 						XMLUtil.WSA_NAMESPACE_URI, XMLUtil.ADDRESS_TAG);
 				addressElement.setPrefix(XMLUtil.WSA_PREFIX);
 				addressElement.appendChild(document
 						.createTextNode(portReference.getAddress()));
 				portReferenceElement.appendChild(addressElement);
-			} else {
+			}
+			else
+			{
 				portReferenceElement.appendChild(document
 						.createTextNode(portReference.getAddress()));
 			}
@@ -80,7 +86,8 @@
 			Iterator extensions = portReference.getExtensions();
 			Element referenceProperties = null;
 
-			while (extensions.hasNext()) {
+			while (extensions.hasNext())
+			{
 				PortReference.Extension extension = (PortReference.Extension) extensions
 						.next();
 
@@ -90,39 +97,48 @@
 				 * separately. Doh!
 				 */
 
-				if (!toField) {
-					if (referenceProperties == null) {
+				if (!toField)
+				{
+					if (referenceProperties == null)
+					{
 						referenceProperties = document.createElementNS(
 								XMLUtil.WSA_NAMESPACE_URI,
 								XMLUtil.REFERENCE_PROPERTIES_TAG);
 						referenceProperties.setPrefix(XMLUtil.WSA_PREFIX);
 						portReferenceElement.appendChild(referenceProperties);
 					}
-				} else
+				}
+				else
 					referenceProperties = header;
 
 				extensionToXML(referenceProperties, document, extension);
 			}
-		} catch (Exception exception) {
+		}
+		catch (Exception exception)
+		{
 			exception.printStackTrace();
 			throw new MarshalException("Marshal failure: " + exception);
 		}
 	}
 
 	/** **************************** */
-	public static PortReference fromXML(org.w3c.dom.Element portReferenceElement, boolean toField)
-			throws MarshalException {
-		PortReference portReference = new PortReference();;
-		
+	public static PortReference fromXML(
+			org.w3c.dom.Element portReferenceElement, boolean toField)
+			throws MarshalException
+	{
+		PortReference portReference = new PortReference();
+
 		if (toField)
 			portReference.setAddress(portReferenceElement.getTextContent());
 		
 		org.w3c.dom.NodeList elements = portReferenceElement.getChildNodes();
 
-		for (int i = 1; i < elements.getLength(); i++) {
+		for (int i = 1; i < elements.getLength(); i++)
+		{
 			final Object extensionObject = elements.item(i);
-			
-			if (extensionObject instanceof Element) {
+
+			if (extensionObject instanceof Element)
+			{
 				int parentNodeType = PortReference.Extension.NEITHER;
 				Element extensionElement = (Element) extensionObject;
 				NodeList children = extensionElement.getChildNodes();
@@ -131,31 +147,40 @@
 
 				if (parentName.equals(XMLUtil.REFERENCE_PROPERTIES_TAG))
 					parentNodeType = PortReference.Extension.REFERENCE_PROPERTIES;
-				else {
+				else
+				{
 					if (parentName.equals(XMLUtil.REFERENCE_PARAMETERS_TAG))
 						parentNodeType = PortReference.Extension.REFERENCE_PARAMETERS;
 					else
 					{
-						if (!toField && parentName.equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.ADDRESS_TAG))
+						if (!toField
+								&& parentName.equals(XMLUtil.WSA_PREFIX + ":"
+										+ XMLUtil.ADDRESS_TAG))
 						{
-							portReference.setAddress(extensionElement.getTextContent());
+							portReference.setAddress(extensionElement
+									.getTextContent());
 						}
 					}
 				}
 
 				final int numChildren = children.getLength();
-				for (int count = 0; count < numChildren; count++) {
+				
+				for (int count = 0; count < numChildren; count++)
+				{
 					final Object childObject = children.item(count);
 
-					if (childObject instanceof Element) {
+					if (childObject instanceof Element)
+					{
 						Element childElement = (Element) childObject;
+
 						portReference
 								.addExtension(childrenFromXML(childElement));
 						haveChildren = true;
 					}
 				}
 
-				if (!haveChildren) {
+				if (!haveChildren && !parentName.equals(XMLUtil.WSA_PREFIX + ":"+ XMLUtil.ADDRESS_TAG))
+				{
 					PortReference.Extension ext = new PortReference.Extension(
 							extensionElement.getNodeName(), null, null,
 							extensionElement.getNodeValue(), parentNodeType);
@@ -164,12 +189,15 @@
 
 					final NamedNodeMap attrs = extensionElement.getAttributes();
 					final int numAttrs = attrs.getLength();
-					if (numAttrs > 0) {
+					if (numAttrs > 0)
+					{
 						final HashMap<ArjunaName, String> p = new HashMap<ArjunaName, String>();
-						for (int count = 0; count < numAttrs; count++) {
+						for (int count = 0; count < numAttrs; count++)
+						{
 							final Attr attr = (Attr) attrs.item(count);
 							if (!"http://www.w3.org/2000/xmlns/".equals(attr
-									.getNamespaceURI())) {
+									.getNamespaceURI()))
+							{
 								final ArjunaName name = generateName(attr);
 								p.put(name, attr.getValue());
 							}
@@ -185,27 +213,37 @@
 	}
 
 	private final static PortReference.Extension childrenFromXML(
-			Element childRoot) {
+			Element childRoot)
+	{
 		final NodeList children = childRoot.getChildNodes();
 		final int numChildNodes = children.getLength();
 
 		final PortReference.Extension extension;
-		if (numChildNodes > 0) {
+		if (numChildNodes > 0)
+		{
 			Object childObject = children.item(0);
-			if (childObject instanceof Element) {
-				extension = new PortReference.Extension(childRoot
-						.getNodeName(), childRoot.getPrefix(), childRoot
-						.getNamespaceURI());
-				for (int count = 1; count < numChildNodes; count++) {
+			if (childObject instanceof Element)
+			{
+				extension = new PortReference.Extension(
+						childRoot.getNodeName(), childRoot.getPrefix(),
+						childRoot.getNamespaceURI());
+				
+				for (int count = 1; count < numChildNodes; count++)
+				{
 					extension.addChild(childrenFromXML((Element) childObject));
 					childObject = children.item(count);
 				}
-			} else {
-				extension = new PortReference.Extension(childRoot
-						.getNodeName(), childRoot.getPrefix(), childRoot
-						.getNamespaceURI(), ((Node) childObject).getNodeValue());
 			}
-		} else {
+			else
+			{
+				extension = new PortReference.Extension(
+						childRoot.getNodeName(), childRoot.getPrefix(),
+						childRoot.getNamespaceURI(), ((Node) childObject)
+								.getNodeValue());
+			}
+		}
+		else
+		{
 			extension = new PortReference.Extension(childRoot.getNodeName(),
 					childRoot.getPrefix(), childRoot.getNamespaceURI(),
 					childRoot.getNodeValue());
@@ -213,12 +251,15 @@
 
 		final NamedNodeMap attrs = childRoot.getAttributes();
 		final int numAttrs = attrs.getLength();
-		if (numAttrs > 0) {
+		if (numAttrs > 0)
+		{
 			final HashMap<ArjunaName, String> p = new HashMap<ArjunaName, String>();
-			for (int count = 0; count < numAttrs; count++) {
+			for (int count = 0; count < numAttrs; count++)
+			{
 				final Attr attr = (Attr) attrs.item(count);
 				if (!"http://www.w3.org/2000/xmlns/".equals(attr
-						.getNamespaceURI())) {
+						.getNamespaceURI()))
+				{
 					final ArjunaName name = generateName(attr);
 					p.put(name, attr.getValue());
 				}
@@ -231,12 +272,16 @@
 
 	private final static Element extensionToXML(Element packInto,
 			org.w3c.dom.Document document, PortReference.Extension toPack)
-			throws MarshalException {
+			throws MarshalException
+	{
 		final String uri = toPack.getURI();
 		final Element element;
-		if (uri == null) {
+		if (uri == null)
+		{
 			element = document.createElement(toPack.getTag());
-		} else {
+		}
+		else
+		{
 			final String prefix = toPack.getPrefix();
 			element = document.createElementNS(uri, toPack.getTag());
 			element.setPrefix(prefix);
@@ -245,15 +290,18 @@
 		}
 		packInto.appendChild(element);
 
-		if (toPack.getValue() != null) {
+		if (toPack.getValue() != null)
+		{
 			final Text text = document.createTextNode(toPack.getValue());
 			element.appendChild(text);
 		}
 
 		LinkedList extensions = toPack.getChildren();
 
-		if (extensions != null) {
-			for (int i = 0; i < extensions.size(); i++) {
+		if (extensions != null)
+		{
+			for (int i = 0; i < extensions.size(); i++)
+			{
 				PortReference.Extension ext = (PortReference.Extension) extensions
 						.get(i);
 
@@ -263,18 +311,23 @@
 
 		HashMap attrs = toPack.getAttributes();
 
-		if (attrs != null) {
+		if (attrs != null)
+		{
 			Iterator names = attrs.entrySet().iterator();
 
-			while (names.hasNext()) {
+			while (names.hasNext())
+			{
 				Map.Entry entry = (Map.Entry) names.next();
 				ArjunaName name = (ArjunaName) entry.getKey();
 				final String value = (String) entry.getValue();
 				final String attrURI = name.toString();
-				if (attrURI != null) {
+				if (attrURI != null)
+				{
 					element.setAttributeNS(attrURI, name.getQualifiedName(),
 							value);
-				} else {
+				}
+				else
+				{
 					element.setAttribute(name.getQualifiedName(), value);
 				}
 			}
@@ -290,7 +343,8 @@
 	 *            The current attribute.
 	 * @return The name
 	 */
-	private static ArjunaName generateName(final Attr attr) {
+	private static ArjunaName generateName(final Attr attr)
+	{
 		final String localName = attr.getNodeName();
 		final String uri = attr.getNamespaceURI();
 		final String prefix = attr.getPrefix();
@@ -326,7 +380,8 @@
 		 *            The local name.
 		 */
 		public ArjunaName(final String uri, final String prefix,
-				final String localName) {
+				final String localName)
+		{
 			this.uri = uri;
 			this.prefix = prefix;
 			this.localName = localName;
@@ -337,7 +392,8 @@
 		 * 
 		 * @return the uri.
 		 */
-		public String getURI() {
+		public String getURI()
+		{
 			return uri;
 		}
 
@@ -346,7 +402,8 @@
 		 * 
 		 * @return the prefix.
 		 */
-		public String getPrefix() {
+		public String getPrefix()
+		{
 			return prefix;
 		}
 
@@ -355,7 +412,8 @@
 		 * 
 		 * @return the local name.
 		 */
-		public String getLocalName() {
+		public String getLocalName()
+		{
 			return localName;
 		}
 
@@ -364,7 +422,8 @@
 		 * 
 		 * @return the qualified name.
 		 */
-		public String getQualifiedName() {
+		public String getQualifiedName()
+		{
 			return (prefix == null ? localName : prefix + ":" + localName);
 		}
 	}

Modified: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/PortReference.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/PortReference.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/PortReference.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -231,6 +231,23 @@
 			_uri = uri;
 			_value = value;
 			_parent = parent;
+			
+			/*
+			 * If XML was loaded via DOM1 then prefix and namespace won't work!
+			 */
+			
+			if (_uri == null)
+				_uri = XMLUtil.WSARJADDR_NAMESPACE_URI;
+			
+			if (_prefix == null)
+				_prefix = XMLUtil.WSARJADDR_PREFIX;
+			
+			if (_tag.contains(":"))
+			{
+				int colon = _tag.indexOf(':');
+				
+				_tag = _tag.substring(colon+1);
+			}
 		}
 
 		public int getParent()

Modified: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/EmailEpr.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/EmailEpr.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/EmailEpr.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -55,6 +55,11 @@
 	private static final String PROTOCOL_SEPARATOR = "://";
 	private static final String PORT_SEPARATOR = ":";
 	
+	public EmailEpr (EPR epr)
+	{
+		copy(epr);
+	}
+	
 	/**
 	 * Create a new email EPR. The port number will be assumed to be 25,
 	 * and there are no values for username and password.

Modified: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/FTPEpr.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/FTPEpr.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/FTPEpr.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -47,6 +47,11 @@
 	public static final String USERNAME_TAG = "username";
 	public static final String PASSWORD_TAG = "password";
 	
+	public FTPEpr (EPR epr)
+	{
+		copy(epr);
+	}
+	
 	public FTPEpr (URL url) throws URISyntaxException
 	{
 		super(new URI(url.toString()));

Modified: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/HTTPEpr.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/HTTPEpr.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/HTTPEpr.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -44,6 +44,11 @@
 public class HTTPEpr extends EPR
 {
 
+	public HTTPEpr (EPR epr)
+	{
+		copy(epr);
+	}
+	
 	public HTTPEpr (URL url) throws URISyntaxException
 	{
 		super(new URI(url.toString()));

Modified: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/JDBCEpr.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/JDBCEpr.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/JDBCEpr.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -49,6 +49,11 @@
 	public static final String SQL_TAG = "sql";
 	public static final String DRIVER_TAG = "driver";
 	
+	public JDBCEpr (EPR epr)
+	{
+		copy(epr);
+	}
+	
 	public JDBCEpr (String url, String sql) throws URISyntaxException
 	{
 		super(new URI(url));

Modified: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/JMSEpr.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/JMSEpr.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/helpers/JMSEpr.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -57,6 +57,11 @@
 	public static final String QUEUE_TYPE = "queue";
 	public static final String TOPIC_TYPE = "topic";
 	
+	public JMSEpr (EPR epr)
+	{
+		copy(epr);
+	}
+	
 	/**
 	 * Create a new JMS EPR. The protocol version is assumed to be 1.1.
 	 * 

Added: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/EPRManager.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/EPRManager.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/EPRManager.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -0,0 +1,230 @@
+package org.jboss.soa.esb.addressing.util;
+
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated 
+ * by the @authors tag. All rights reserved. 
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors. 
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A 
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+ * MA  02110-1301, USA.
+ * 
+ * (C) 2005-2006,
+ * @author mark.little at jboss.com
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.net.URISyntaxException;
+import java.util.Hashtable;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.jboss.internal.soa.esb.addressing.PortReferenceHelper;
+import org.jboss.soa.esb.MarshalException;
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.addressing.PortReference;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+import com.sun.org.apache.xml.internal.serialize.OutputFormat;
+import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
+
+/**
+ * Allows EPRs to be saved to and loaded from files. Mainly for testing purposes.
+ * 
+ * @author marklittle
+ *
+ */
+
+public class EPRManager
+{
+	private static final String ELEMENT_NAME = "EPR";
+	
+	/**
+	 * All EPRs are saves in files within the current working directory.
+	 * 
+	 * @return the manager for the cwd.
+	 */
+	
+	public static final EPRManager getInstance ()
+	{
+		return getInstance("");
+	}
+	
+	/**
+	 * All EPRs are saves in files within a defined directory. Get the right manager
+	 * for that directory.
+	 * 
+	 * @param domain the name of the directory. If <code>null</code> then the null String
+	 * is assumed.
+	 * @return the manager for the directory. If it does not exist, then one will be created.
+	 */
+	
+	public static final EPRManager getInstance (String domain)
+	{
+		if (domain == null)
+			domain = "";
+		
+		synchronized (_instances)
+		{
+			EPRManager theInstance = _instances.get(domain);
+			
+			if (theInstance == null)
+			{
+				theInstance = new EPRManager(domain);
+				
+				_instances.put(domain, theInstance);
+			}
+			
+			return theInstance;
+		}
+	}
+	
+	public final String getDomain ()
+	{
+		return _directory;
+	}
+	
+	public boolean equals (Object manager)
+	{
+		if (manager instanceof EPRManager)
+		{
+			EPRManager comp = (EPRManager) manager;
+			
+			if (_directory.equals(comp.getDomain()))
+				return true;
+		}
+		
+		return false;
+	}
+	
+	/**
+	 * Save the EPR into the specified file.
+	 * 
+	 * @param name the name of the file to use (the logical service name).
+	 * @param address the EPR to save.
+	 * 
+	 * @throws IOException thrown if there is an error.
+	 */
+	
+	public final void saveEPR (String name, EPR address) throws IOException
+	{
+		if ((name == null) || (address == null))
+			throw new IllegalArgumentException();
+		
+		try
+		{
+			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+			DocumentBuilder builder = factory.newDocumentBuilder();
+			Document doc = builder.newDocument();
+			Element portReferenceElement = doc.createElement(ELEMENT_NAME);
+			
+			doc.appendChild(portReferenceElement);
+			
+			PortReferenceHelper.toXML(null, doc, portReferenceElement, address.getAddr(), false);
+
+			StringWriter sWriter = new StringWriter();
+			OutputFormat format = new OutputFormat();
+			format.setIndenting(true);
+
+			XMLSerializer xmlS = new XMLSerializer(sWriter, format);
+
+			xmlS.asDOMSerializer();
+			xmlS.serialize(doc);
+
+			String documentAsString = sWriter.toString();
+
+			FileOutputStream output = new FileOutputStream(_directory+File.separator+name);
+			
+			output.write(documentAsString.getBytes());
+			output.flush();
+			output.getFD().sync(); // make sure it's on disk!
+		}
+		catch (ParserConfigurationException ex)
+		{
+			throw new IOException(ex.toString());
+		}
+		catch (URISyntaxException ex)
+		{
+			throw new IllegalArgumentException(ex.toString());
+		}
+		catch (MarshalException ex)
+		{
+			throw new IOException(ex.toString());
+		}
+	}
+	
+	/**
+	 * Get the EPR specified by the logical name.
+	 * 
+	 * @param name the service name.
+	 * @return the EPR, or <code>null</code> if none exists.
+	 * @throws IOException thrown if there is an error.
+	 */
+	
+	public final EPR loadEPR (String name) throws IOException
+	{
+		if (name == null)
+			throw new IllegalArgumentException();
+		
+		File theFile = new File(_directory+File.separator+name);
+		
+		try
+		{
+			if (theFile.exists())
+			{
+				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+				DocumentBuilder builder = factory.newDocumentBuilder();
+				Document doc = builder.parse(theFile);
+				Element rootElement = doc.getDocumentElement();
+				
+				if (rootElement == null)
+					throw new IOException("Cannot locate "+ELEMENT_NAME);
+				
+				PortReference addr = PortReferenceHelper.fromXML(rootElement, false);
+
+				return new EPR(addr);
+			}
+			else
+				throw new FileNotFoundException(theFile.toString());
+		}
+		catch (SAXException ex)
+		{
+			throw new IOException(ex.toString());
+		}
+		catch (ParserConfigurationException ex)
+		{
+			throw new IllegalArgumentException(ex.toString());
+		}
+		catch (MarshalException ex)
+		{
+			throw new IOException(ex.toString());
+		}
+	}
+
+	protected EPRManager (String domain)
+	{
+		_directory = domain;
+	}
+	
+	private String _directory;
+	
+	private static Hashtable<String, EPRManager> _instances = new Hashtable<String, EPRManager>();
+	
+} 
\ No newline at end of file

Added: labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/addressing/util/tests/EPRManagerUnitTest.java
===================================================================
--- labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/addressing/util/tests/EPRManagerUnitTest.java	2006-10-13 20:17:53 UTC (rev 6797)
+++ labs/jbossesb/workspace/eschifman/trunk/product/core/rosetta/tests/src/org/jboss/soa/esb/addressing/util/tests/EPRManagerUnitTest.java	2006-10-13 21:33:59 UTC (rev 6798)
@@ -0,0 +1,74 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.soa.esb.addressing.util.tests;
+
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.addressing.helpers.EmailEpr;
+import org.jboss.soa.esb.addressing.util.EPRManager;
+import org.jboss.soa.esb.helpers.Email;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for the Class class.
+ * 
+ * @author Mark Little
+ */
+
+public class EPRManagerUnitTest extends TestCase
+{
+	
+	public void testCreate ()
+	{
+		EPRManager manager1 = EPRManager.getInstance(null);
+		EPRManager manager2 = EPRManager.getInstance("");
+		EPRManager manager3 = EPRManager.getInstance("/tmp");
+		
+		assertEquals(manager1.equals(manager2), true);
+		assertEquals(manager1.equals(manager3), false);
+	}
+	
+	public void testInputOutput ()
+	{
+		EmailEpr email = new EmailEpr(EmailEpr.SMTP_PROTOCOL, "foobar", "25", "me", "letmein");
+		
+		EPRManager manager = EPRManager.getInstance("/tmp");
+
+		try
+		{
+			manager.saveEPR("MyEmail", email);
+
+			EmailEpr loaded = new EmailEpr(manager.loadEPR("MyEmail"));
+		
+			assertEquals(loaded.getHost().equals("foobar"), true);
+			assertEquals(loaded.getPassword().equals("letmein"), true);
+		}
+		catch (Exception ex)
+		{
+			ex.printStackTrace();
+			
+			fail(ex.toString());
+		}
+	}
+	
+}




More information about the jboss-svn-commits mailing list