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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Oct 27 12:32:34 EDT 2006


Author: mark.little at jboss.com
Date: 2006-10-27 12:32:30 -0400 (Fri, 27 Oct 2006)
New Revision: 7176

Added:
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/EPRHelper.java
Modified:
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/EPRManager.java
Log:
Added EPRHelper.

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/EPRHelper.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/EPRHelper.java	2006-10-27 12:39:57 UTC (rev 7175)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/EPRHelper.java	2006-10-27 16:32:30 UTC (rev 7176)
@@ -0,0 +1,173 @@
+package org.jboss.internal.soa.esb.addressing;
+
+/*
+ * 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.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.net.URISyntaxException;
+import java.security.InvalidParameterException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.jboss.soa.esb.MarshalException;
+import org.jboss.soa.esb.addressing.EPR;
+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;
+
+public class EPRHelper
+{
+    public static final String ELEMENT_NAME = "EPR";
+    
+    public static final Element toXML (EPR epr, Document doc, Element header)
+    {
+	if (epr == null)
+	    throw new InvalidParameterException();
+
+	try
+	{
+        	PortReferenceHelper.toXML(null, doc, header, epr.getAddr(), false);
+        
+        	return header;
+	}
+	catch (URISyntaxException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+	catch (MarshalException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+    }
+
+    public static final String toXMLString (EPR epr)
+    {
+	if (epr == null)
+	    throw new InvalidParameterException();
+	
+	try
+	{
+	    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+	    DocumentBuilder builder = factory.newDocumentBuilder();
+	    Document doc = builder.newDocument();
+	    Element portReferenceElement = doc.createElement(ELEMENT_NAME);
+        
+	    doc.appendChild(portReferenceElement);
+        	
+	    toXML(epr, doc, portReferenceElement);
+	    
+	    StringWriter sWriter = new StringWriter();
+	    OutputFormat format = new OutputFormat();
+	    format.setIndenting(true);
+
+	    XMLSerializer xmlS = new XMLSerializer(sWriter, format);
+
+	    xmlS.asDOMSerializer();
+	    xmlS.serialize(doc);
+
+	    return sWriter.toString();
+	}
+	catch (IOException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+	catch (ParserConfigurationException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+    }
+    
+    public static final EPR fromXML (Element header)
+    {
+	if (header == null)
+	    throw new InvalidParameterException();
+	
+	try
+	{
+	    if (header.getNodeName().equals(ELEMENT_NAME))
+		return new EPR(PortReferenceHelper.fromXML(header, false));
+	    else
+		return null;
+	}
+	catch (MarshalException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+    }
+    
+    public static final EPR fromXMLString (String xml)
+    {
+	if (xml == null)
+	    throw new InvalidParameterException();
+	
+	try
+	{
+        	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        	DocumentBuilder builder = factory.newDocumentBuilder();
+        	Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
+        	Element rootElement = doc.getDocumentElement();
+        	
+        	return new EPR(PortReferenceHelper.fromXML(rootElement, false));
+	}
+	catch (MarshalException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+	catch (SAXException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+	catch (IOException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+	catch (ParserConfigurationException ex)
+	{
+	    ex.printStackTrace();
+	    
+	    return null;
+	}
+    }
+   
+}
\ No newline at end of file

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/EPRManager.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/EPRManager.java	2006-10-27 12:39:57 UTC (rev 7175)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/util/EPRManager.java	2006-10-27 16:32:30 UTC (rev 7176)
@@ -25,28 +25,21 @@
 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.internal.soa.esb.addressing.EPRHelper;
 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.
+ * purposes. This version assumes XML as the output format.
  * 
  * @author marklittle
  * 
@@ -54,209 +47,173 @@
 
 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.
-	 */
+    /**
+         * All EPRs are saves in files within the current working directory.
+         * 
+         * @return the manager for the cwd.
+         */
 
-	public static final EPRManager getInstance()
-	{
-		return getInstance("");
-	}
+    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.
-	 */
+    /**
+         * 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)
+    public static final EPRManager getInstance (String domain)
+    {
+	if (domain == null)
+	    domain = "";
+
+	synchronized (_instances)
 	{
-		if (domain == null)
-			domain = "";
+	    EPRManager theInstance = _instances.get(domain);
 
-		synchronized (_instances)
-		{
-			EPRManager theInstance = _instances.get(domain);
+	    if (theInstance == null)
+	    {
+		theInstance = new EPRManager(domain);
 
-			if (theInstance == null)
-			{
-				theInstance = new EPRManager(domain);
+		_instances.put(domain, theInstance);
+	    }
 
-				_instances.put(domain, theInstance);
-			}
-
-			return theInstance;
-		}
+	    return theInstance;
 	}
+    }
 
-	public final String getDomain()
-	{
-		return _directory;
-	}
+    public final String getDomain ()
+    {
+	return _directory;
+    }
 
-	public boolean equals(Object manager)
+    public boolean equals (Object manager)
+    {
+	if (manager instanceof EPRManager)
 	{
-		if (manager instanceof EPRManager)
-		{
-			EPRManager comp = (EPRManager) manager;
+	    EPRManager comp = (EPRManager) manager;
 
-			if (_directory.equals(comp.getDomain()))
-				return true;
-		}
-
-		return false;
+	    if (_directory.equals(comp.getDomain()))
+		return true;
 	}
 
-	/**
-	 * 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.
-	 */
+	return false;
+    }
 
-	public final void saveEPR(String name, EPR address) throws IOException
-	{
-		if ((name == null) || (address == null))
-			throw new IllegalArgumentException();
+    /**
+         * 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.
+         */
 
-		try
-		{
-			DocumentBuilderFactory factory = DocumentBuilderFactory
-					.newInstance();
-			DocumentBuilder builder = factory.newDocumentBuilder();
-			Document doc = builder.newDocument();
-			Element portReferenceElement = doc.createElement(ELEMENT_NAME);
+    public final void saveEPR (String name, EPR address) throws IOException
+    {
+	if ((name == null) || (address == null))
+	    throw new IllegalArgumentException();
 
-			doc.appendChild(portReferenceElement);
+	try
+	{
+	    String documentAsString = EPRHelper.toXMLString(address);
 
-			PortReferenceHelper.toXML(null, doc, portReferenceElement, address
-					.getAddr(), false);
+	    FileOutputStream output = new FileOutputStream(_directory
+		    + File.separator + name);
 
-			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());
-		}
+	    output.write(documentAsString.getBytes());
+	    output.flush();
+	    output.getFD().sync(); // make sure it's on disk!
 	}
-
-	/**
-	 * Remove the EPR-to-file association.
-	 * 
-	 * @param name the logical name for the service.
-	 * @throws IOException thrown if there are any errors.
-	 */
-	
-	public final void removeEPR(String name) throws IOException
+	catch (Exception ex)
 	{
-		if (name == null)
-			throw new IllegalArgumentException();
+	    ex.printStackTrace();
+	}
+    }
 
-		File theFile = new File(_directory + File.separator + name);
+    /**
+         * Remove the EPR-to-file association.
+         * 
+         * @param name
+         *                the logical name for the service.
+         * @throws IOException
+         *                 thrown if there are any errors.
+         */
 
-		if (theFile.exists())
-			theFile.delete();
-		else
-			throw new FileNotFoundException();
-	}
+    public final void removeEPR (String name) throws IOException
+    {
+	if (name == null)
+	    throw new IllegalArgumentException();
 
-	/**
-	 * 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.
-	 */
+	File theFile = new File(_directory + File.separator + name);
 
-	public final EPR loadEPR(String name) throws IOException
-	{
-		if (name == null)
-			throw new IllegalArgumentException();
+	if (theFile.exists())
+	    theFile.delete();
+	else
+	    throw new FileNotFoundException();
+    }
 
-		File theFile = new File(_directory + File.separator + name);
+    /**
+         * 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.
+         */
 
-		try
-		{
-			if (theFile.exists())
-			{
-				DocumentBuilderFactory factory = DocumentBuilderFactory
-						.newInstance();
-				DocumentBuilder builder = factory.newDocumentBuilder();
-				Document doc = builder.parse(theFile);
-				Element rootElement = doc.getDocumentElement();
+    public final EPR loadEPR (String name) throws IOException
+    {
+	if (name == null)
+	    throw new IllegalArgumentException();
 
-				if (rootElement == null)
-					throw new IOException("Cannot locate " + ELEMENT_NAME);
+	File theFile = new File(_directory + File.separator + name);
 
-				PortReference addr = PortReferenceHelper.fromXML(rootElement,
-						false);
+	try
+	{
+	    if (theFile.exists())
+	    {
+		DocumentBuilderFactory factory = DocumentBuilderFactory
+			.newInstance();
+		DocumentBuilder builder = factory.newDocumentBuilder();
+		Document doc = builder.parse(theFile);
+		Element rootElement = doc.getDocumentElement();
 
-				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());
-		}
+		return EPRHelper.fromXML(rootElement);
+	    }
+	    else
+		throw new FileNotFoundException(theFile.toString());
 	}
-
-	protected EPRManager(String domain)
+	catch (SAXException ex)
 	{
-		_directory = domain;
+	    throw new IOException(ex.toString());
 	}
+	catch (ParserConfigurationException ex)
+	{
+	    throw new IllegalArgumentException(ex.toString());
+	}
+    }
 
-	private String _directory;
+    protected EPRManager(String domain)
+    {
+	_directory = domain;
+    }
 
-	private static Hashtable<String, EPRManager> _instances = new Hashtable<String, EPRManager>();
+    private String _directory;
 
+    private static Hashtable<String, EPRManager> _instances = new Hashtable<String, EPRManager>();
+
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list