Author: alessio.soldano(a)jboss.com
Date: 2010-03-04 15:53:09 -0500 (Thu, 04 Mar 2010)
New Revision: 11715
Added:
spi/trunk/src/main/java/org/jboss/wsf/spi/deployment/WritableUnifiedVirtualFile.java
spi/trunk/src/main/java/org/jboss/wsf/spi/util/URLLoaderAdapter.java
Modified:
spi/trunk/src/main/java/org/jboss/wsf/spi/metadata/j2ee/serviceref/UnifiedServiceRefMetaData.java
Log:
[JBWS-2021][JBWS-2948] Adding WritableUnifiedVirtualFile interface, redefining
serialization of UnifiedServiceRefMetaData (check if the UnifiedVirtualFile is actually
usable, if not get file content from the reference when available), adding
URLLoaderAdapter to spi.
Added:
spi/trunk/src/main/java/org/jboss/wsf/spi/deployment/WritableUnifiedVirtualFile.java
===================================================================
--- spi/trunk/src/main/java/org/jboss/wsf/spi/deployment/WritableUnifiedVirtualFile.java
(rev 0)
+++
spi/trunk/src/main/java/org/jboss/wsf/spi/deployment/WritableUnifiedVirtualFile.java 2010-03-04
20:53:09 UTC (rev 11715)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.wsf.spi.deployment;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * @author alessio.soldano(a)jboss.com
+ * @since 04-Mar-2010
+ */
+public interface WritableUnifiedVirtualFile extends UnifiedVirtualFile
+{
+ public void writeContent(OutputStream os) throws IOException;
+
+ public void writeContent(OutputStream os, NameFilter filter) throws IOException;
+
+ public interface NameFilter
+ {
+ public boolean accept(String fileName);
+ }
+}
Property changes on:
spi/trunk/src/main/java/org/jboss/wsf/spi/deployment/WritableUnifiedVirtualFile.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified:
spi/trunk/src/main/java/org/jboss/wsf/spi/metadata/j2ee/serviceref/UnifiedServiceRefMetaData.java
===================================================================
---
spi/trunk/src/main/java/org/jboss/wsf/spi/metadata/j2ee/serviceref/UnifiedServiceRefMetaData.java 2010-03-04
14:30:50 UTC (rev 11714)
+++
spi/trunk/src/main/java/org/jboss/wsf/spi/metadata/j2ee/serviceref/UnifiedServiceRefMetaData.java 2010-03-04
20:53:09 UTC (rev 11715)
@@ -1,6 +1,6 @@
/*
* JBoss, Home of Professional Open Source.
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
@@ -21,36 +21,49 @@
*/
package org.jboss.wsf.spi.metadata.j2ee.serviceref;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+import javax.xml.ws.WebServiceException;
+
import org.jboss.logging.Logger;
import org.jboss.wsf.spi.SPIProvider;
import org.jboss.wsf.spi.SPIProviderResolver;
import org.jboss.wsf.spi.deployment.UnifiedVirtualFile;
+import org.jboss.wsf.spi.deployment.WritableUnifiedVirtualFile;
import org.jboss.wsf.spi.serviceref.ServiceRefMetaData;
+import org.jboss.wsf.spi.util.URLLoaderAdapter;
import org.w3c.dom.Element;
-import javax.xml.namespace.QName;
-import javax.xml.ws.WebServiceException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-
/**
* The metdata data from service-ref element in web.xml, ejb-jar.xml, and
* application-client.xml.
*
* @author Thomas.Diesler(a)jboss.org
+ * @author alessio.soldano(a)jboss.com
*/
public class UnifiedServiceRefMetaData extends ServiceRefMetaData
{
- private static final long serialVersionUID = -926464174132493951L;
+ private static final long serialVersionUID = -926464174132493952L;
// provide logging
private static Logger log = Logger.getLogger(UnifiedServiceRefMetaData.class);
- private UnifiedVirtualFile vfsRoot;
+ private transient UnifiedVirtualFile vfsRoot;
// Standard properties
@@ -426,7 +439,77 @@
ServiceRefMetaDataParserFactory factory =
provider.getSPI(ServiceRefMetaDataParserFactory.class);
factory.getServiceRefMetaDataParser().importJBossXml(root, this);
}
+
+ private void writeObject(ObjectOutputStream out) throws IOException
+ {
+ out.defaultWriteObject();
+ out.writeObject(vfsRoot);
+ if (vfsRoot instanceof WritableUnifiedVirtualFile)
+ {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ ((WritableUnifiedVirtualFile)vfsRoot).writeContent(bos, new
WritableUnifiedVirtualFile.NameFilter() {
+ public boolean accept(String fileName)
+ {
+ boolean result = fileName.contains("META-INF");
+ result = result || fileName.endsWith(".wsdl");
+ result = result || fileName.endsWith(".xsd");
+ result = result || fileName.endsWith(".xml");
+ return result;
+ }
+ });
+ out.writeObject(bos.toByteArray());
+ out.writeObject(vfsRoot.getName());
+ }
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException
+ {
+ in.defaultReadObject();
+ UnifiedVirtualFile obj = (UnifiedVirtualFile)in.readObject();
+ if (obj.toURL() == null && (obj instanceof WritableUnifiedVirtualFile))
+ {
+ //the virtual file has been created in a different VM (or is even pointing to a
different filesystem), try getting the serialized contents
+ byte[] bytes = (byte[])in.readObject();
+ String vfName = (String)in.readObject();
+ ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
+ File tempFile = File.createTempFile("jbossws-vf-", "-" +
vfName);
+ tempFile.deleteOnExit();
+ FileOutputStream fos = new FileOutputStream(tempFile);
+ copyStreamAndClose(fos, bis);
+ this.vfsRoot = new URLLoaderAdapter(tempFile.toURI().toURL());
+ }
+ else
+ {
+ this.vfsRoot = (UnifiedVirtualFile)obj;
+ }
+ }
+ private static void copyStreamAndClose(OutputStream outs, InputStream ins) throws
IOException
+ {
+ try
+ {
+ byte[] bytes = new byte[1024];
+ int r = ins.read(bytes);
+ while (r > 0)
+ {
+ outs.write(bytes, 0, r);
+ r = ins.read(bytes);
+ }
+ }
+ catch (IOException e)
+ {
+ throw e;
+ }
+ finally{
+ try {
+ ins.close();
+ } catch (Exception e) {}
+ try {
+ outs.close();
+ } catch (Exception e) {}
+ }
+ }
+
public String toString()
{
StringBuilder str = new StringBuilder();
Added: spi/trunk/src/main/java/org/jboss/wsf/spi/util/URLLoaderAdapter.java
===================================================================
--- spi/trunk/src/main/java/org/jboss/wsf/spi/util/URLLoaderAdapter.java
(rev 0)
+++ spi/trunk/src/main/java/org/jboss/wsf/spi/util/URLLoaderAdapter.java 2010-03-04
20:53:09 UTC (rev 11715)
@@ -0,0 +1,251 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.wsf.spi.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import org.jboss.logging.Logger;
+import org.jboss.util.NotImplementedException;
+import org.jboss.wsf.spi.deployment.UnifiedVirtualFile;
+
+/**
+ * Load resources through a URLClassLoader.<br>
+ * NOTE: The associated classloader doesn't do parent delegation.
+ *
+ *
+ * @author Heiko.Braun(a)jboss.org
+ * @author alessio.soldano(a)jboss.com
+ * @since 25.01.2007
+ */
+public class URLLoaderAdapter implements UnifiedVirtualFile
+{
+ private static final long serialVersionUID = 8263115387770740414L;
+
+ private URL rootURL;
+ private URL resourceURL;
+ private transient URLClassLoader loader;
+ private static Logger log = Logger.getLogger(URLLoaderAdapter.class);
+ private static final String jarFileSeparator = "/";
+
+ public URLLoaderAdapter(URL rootURL)
+ {
+ this.rootURL = rootURL;
+ }
+
+ private URLLoaderAdapter(URL rootURL, URLClassLoader loader, URL resourceURL)
+ {
+ this.rootURL = rootURL;
+ this.resourceURL = resourceURL;
+ this.loader = loader;
+ }
+
+ public UnifiedVirtualFile findChild(String resourcePath) throws IOException
+ {
+ URL resourceURL = null;
+ if (resourcePath != null)
+ {
+ // Try the child as URL
+ try
+ {
+ resourceURL = new URL(resourcePath);
+ }
+ catch (MalformedURLException ex)
+ {
+ // ignore
+ }
+
+ // Try the filename as File
+ if (resourceURL == null)
+ {
+ try
+ {
+ File file = new File(resourcePath);
+ if (file.exists())
+ resourceURL = file.toURL();
+ }
+ catch (MalformedURLException e)
+ {
+ // ignore
+ }
+ }
+
+ // Try the filename as Resource
+ if (resourceURL == null)
+ {
+ try
+ {
+ resourceURL = getResourceLoader().getResource(resourcePath);
+ }
+ catch (Exception ex)
+ {
+ // ignore
+ }
+ }
+ }
+
+ if (resourceURL == null)
+ throw new IOException("Cannot get URL for: " + resourcePath);
+
+ return new URLLoaderAdapter(rootURL, loader, resourceURL);
+ }
+
+ public URL toURL()
+ {
+ if (resourceURL != null)
+ return resourceURL;
+ else
+ return rootURL;
+ }
+
+ private URLClassLoader getResourceLoader()
+ {
+ if (loader == null)
+ {
+ loader = new URLClassLoader(new URL[] { rootURL });
+ }
+ return loader;
+ }
+
+ public List<UnifiedVirtualFile> getChildren() throws IOException
+ {
+ List<UnifiedVirtualFile> list = new LinkedList<UnifiedVirtualFile>();
+
+ URL url = toURL();
+
+ if (url.getProtocol().equals("jar"))
+ {
+ String urlString = url.toExternalForm();
+ String jarRoot = urlString.substring(4, urlString.indexOf("ar!") +
2);
+ String path = urlString.contains("!") ?
urlString.substring(urlString.lastIndexOf("!") + 2) : "";
+ if (path.endsWith(jarFileSeparator))
+ path = path.substring(0, path.lastIndexOf(jarFileSeparator));
+
+ try
+ {
+ String folder = jarRoot.substring(5,jarRoot.lastIndexOf(File.separator));
+ String filename = jarRoot.substring(jarRoot.lastIndexOf(File.separator)+1);
+ final File jar = new File(folder, filename);
+
+ PrivilegedAction<JarFile> action = new
PrivilegedAction<JarFile>()
+ {
+ public JarFile run()
+ {
+ try
+ {
+ return new JarFile(jar);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+ JarFile jarFile = AccessController.doPrivileged(action);
+
+ if (jar.canRead())
+ {
+ Enumeration<JarEntry> entries = jarFile.entries();
+ List<String> pathMatch = new LinkedList<String>();
+ List<String> finalMatch = new LinkedList<String>();
+ while (entries.hasMoreElements())
+ {
+ JarEntry entry = entries.nextElement();
+ String name = entry.getName();
+ //keep entries starting with the current resource path (exclude inner
classes and the current file)
+ if (name.startsWith(path + jarFileSeparator) && (name.length()
> path.length() + 1) && !name.contains("$"))
+ pathMatch.add(name.substring(path.length() + 1));
+ }
+
+ for (String s : pathMatch)
+ {
+ //do not go deeper than the current dir
+ if (!s.contains(jarFileSeparator) || s.indexOf(jarFileSeparator) ==
s.length() - 1)
+ finalMatch.add(s);
+ }
+ for (String s : finalMatch)
+ {
+ URL sUrl = new URL(urlString + jarFileSeparator + s);
+ list.add(new URLLoaderAdapter(rootURL, loader, sUrl));
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ log.error("Cannot get children for resource: " + url);
+ }
+ }
+ else //std file/dir
+ {
+ try
+ {
+ File file = new File(url.toURI());
+ if (file.exists() && file.isDirectory())
+ {
+ File[] files = file.listFiles();
+ if (files != null)
+ {
+ for (File f : files)
+ {
+ list.add(new URLLoaderAdapter(rootURL, loader, f.toURL()));
+ }
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ log.error("Cannot get children for resource: " + url, e);
+ }
+ }
+ return list;
+ }
+
+ public String getName()
+ {
+ String name = null;
+ try
+ {
+ String filename = toURL().getFile();
+ File f = new File(filename);
+ name = f.getName();
+ if (f.isDirectory() || (toURL().getProtocol().equals("jar") &&
filename.endsWith(jarFileSeparator)))
+ name = name + jarFileSeparator;
+ }
+ catch (Exception e)
+ {
+ log.error("Cannot get name for resource: " + toURL(), e);
+ }
+ return name;
+ }
+}
Property changes on: spi/trunk/src/main/java/org/jboss/wsf/spi/util/URLLoaderAdapter.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF