[jboss-cvs] JBossAS SVN: r80859 - in trunk/system/src/main/org/jboss: system/server/profileservice/attachments and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Nov 12 14:35:50 EST 2008
Author: emuckenhuber
Date: 2008-11-12 14:35:50 -0500 (Wed, 12 Nov 2008)
New Revision: 80859
Added:
trunk/system/src/main/org/jboss/system/server/profileservice/repository/JAXBAttachmentSerializer.java
Modified:
trunk/system/src/main/org/jboss/profileservice/spi/AttachmentsSerializer.java
trunk/system/src/main/org/jboss/system/server/profileservice/attachments/LazyPredeterminedManagedObjects.java
trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractFileAttachmentsSerializer.java
trunk/system/src/main/org/jboss/system/server/profileservice/repository/JavaBeanXmlAttachmentsSerializer.java
trunk/system/src/main/org/jboss/system/server/profileservice/repository/JavaSerializationAttachmentsSerializer.java
trunk/system/src/main/org/jboss/system/server/profileservice/repository/SerializableDeploymentRepository.java
Log:
[JBAS-3768] add JAXBAttachmentSerializer
Modified: trunk/system/src/main/org/jboss/profileservice/spi/AttachmentsSerializer.java
===================================================================
--- trunk/system/src/main/org/jboss/profileservice/spi/AttachmentsSerializer.java 2008-11-12 19:31:47 UTC (rev 80858)
+++ trunk/system/src/main/org/jboss/profileservice/spi/AttachmentsSerializer.java 2008-11-12 19:35:50 UTC (rev 80859)
@@ -21,7 +21,6 @@
*/
package org.jboss.profileservice.spi;
-import java.util.Map;
/**
* An interface that allows externalization of the attachment
@@ -32,9 +31,9 @@
*/
public interface AttachmentsSerializer
{
- public void saveAttachments(String baseName,
- Map<String, Object> attachments)
- throws Exception;
- public Map<String, Object> loadAttachments(String baseName, ClassLoader loader)
- throws Exception;
+
+ public void saveAttachment(String baseName, Object attachment) throws Exception;
+
+ public <T> T loadAttachment(String baseName, Class<T> expected) throws Exception;
+
}
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/attachments/LazyPredeterminedManagedObjects.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/attachments/LazyPredeterminedManagedObjects.java 2008-11-12 19:31:47 UTC (rev 80858)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/attachments/LazyPredeterminedManagedObjects.java 2008-11-12 19:35:50 UTC (rev 80859)
@@ -193,14 +193,14 @@
String attachmentName = relativePath + name;
// use the con
ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ // Load class
+ Class<?> expected = cl.loadClass(name);
// Load attachment
- // TODO use different attachment serializer - we actually don't need a Map<String, Object> ?
- Map<String, Object> map = serializer.loadAttachments(attachmentName, cl);
- if(map == null)
- return null;
-
- Object o = map.get(name);
- this.delegate.addAttachment(name, o);
+ Object o = serializer.loadAttachment(attachmentName, expected);
+ // Add attachment
+ if(o != null)
+ this.delegate.addAttachment(name, o);
+ // Return attachment
return o;
}
catch(Exception e)
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractFileAttachmentsSerializer.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractFileAttachmentsSerializer.java 2008-11-12 19:31:47 UTC (rev 80858)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractFileAttachmentsSerializer.java 2008-11-12 19:35:50 UTC (rev 80859)
@@ -24,8 +24,6 @@
import java.io.File;
import java.io.IOException;
import java.io.NotSerializableException;
-import java.util.Map;
-import java.util.Map.Entry;
import org.jboss.logging.Logger;
import org.jboss.profileservice.spi.AttachmentsSerializer;
@@ -51,34 +49,29 @@
{
this.attachmentsStoreDir = attachmentsStoreDir;
}
-
- public Map<String, Object> loadAttachments(String baseName, ClassLoader loader)
+
+ public <T> T loadAttachment(String baseName, Class<T> expected)
throws Exception
{
if( attachmentsStoreDir == null )
throw new IllegalStateException("attachmentsStoreDir has not been set");
- String vfsPath = baseName;
- vfsPath += ".attachments";
- File attachmentsStore = new File(attachmentsStoreDir, vfsPath);
+ File attachmentsStore = getAttachmentPath(baseName);
if( attachmentsStore.exists() == false )
{
return null;
}
- return loadAttachments(attachmentsStore, loader);
+ return loadAttachment(attachmentsStore, expected);
}
- public void saveAttachments(String baseName,
- Map<String, Object> attachments)
+ public void saveAttachment(String baseName, Object attachment)
throws Exception
{
if( attachmentsStoreDir == null )
throw new IllegalStateException("attachmentsStoreDir has not been set");
- String vfsPath = baseName;
- vfsPath += ".attachments";
- File attachmentsStore = new File(attachmentsStoreDir, vfsPath);
+ File attachmentsStore = getAttachmentPath(baseName);
File attachmentsParent = attachmentsStore.getParentFile();
if( attachmentsParent.exists() == false )
{
@@ -86,30 +79,31 @@
throw new IOException("Failed to create attachmentsParent: "+attachmentsParent.getAbsolutePath());
}
- if( attachments != null )
+ if( attachment != null )
{
try
{
- saveAttachments(attachmentsStore, attachments);
+ saveAttachment(attachmentsStore, attachment);
}
catch(NotSerializableException e)
{
// Log what is in the attachments
StringBuilder tmp = new StringBuilder("Save failed with NSE, attachments contents: ");
- for(Entry<String, Object> entry : attachments.entrySet())
- {
- tmp.append(entry.getKey());
- tmp.append('=');
- tmp.append(entry.getValue());
- }
+ tmp.append(attachment).append(" to: ").append(attachmentsStore);
log.error(tmp.toString());
throw e;
}
}
}
+
+ protected File getAttachmentPath(String baseName)
+ {
+ String vfsPath = baseName;
+ vfsPath += ".attachment";
+ return new File(attachmentsStoreDir, vfsPath);
+ }
- protected abstract Map<String, Object> loadAttachments(File attachmentsStore, ClassLoader loader)
- throws Exception;
- protected abstract void saveAttachments(File attachmentsStore, Map<String, Object> attachments)
- throws Exception;
+ protected abstract <T> T loadAttachment(File attachmentsStore, Class<T> expected) throws Exception;
+
+ protected abstract void saveAttachment(File attachmentsStore, Object attachment) throws Exception;
}
Added: trunk/system/src/main/org/jboss/system/server/profileservice/repository/JAXBAttachmentSerializer.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/repository/JAXBAttachmentSerializer.java (rev 0)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/repository/JAXBAttachmentSerializer.java 2008-11-12 19:35:50 UTC (rev 80859)
@@ -0,0 +1,62 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.system.server.profileservice.repository;
+
+import java.io.File;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+import org.jboss.logging.Logger;
+import org.jboss.system.server.profileservice.attachments.JAXBAttachmentHelper;
+
+/**
+ * A basic JAXB attachment serializer.
+ *
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class JAXBAttachmentSerializer extends AbstractFileAttachmentsSerializer
+{
+
+ /** The logger */
+ private static final Logger log = Logger.getLogger(JAXBAttachmentHelper.class);
+
+ @SuppressWarnings("unchecked")
+ protected <T> T loadAttachment(File attachmentsStore, Class<T> expected) throws Exception
+ {
+ JAXBContext ctx = JAXBContext.newInstance(expected);
+ Unmarshaller unmarshaller = ctx.createUnmarshaller();
+ return (T) unmarshaller.unmarshal(attachmentsStore);
+ }
+
+ protected void saveAttachment(File attachmentsStore, Object attachment) throws Exception
+ {
+ log.trace("saveAttachments, attachmentsStore="+attachmentsStore+ ", attachment="+attachment);
+ JAXBContext ctx = JAXBContext.newInstance(attachment.getClass());
+ Marshaller marshaller = ctx.createMarshaller();
+ marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
+ marshaller.marshal(attachment, attachmentsStore);
+ }
+}
+
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/repository/JavaBeanXmlAttachmentsSerializer.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/repository/JavaBeanXmlAttachmentsSerializer.java 2008-11-12 19:31:47 UTC (rev 80858)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/repository/JavaBeanXmlAttachmentsSerializer.java 2008-11-12 19:35:50 UTC (rev 80859)
@@ -26,8 +26,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
-import java.io.Serializable;
-import java.util.Map;
import org.jboss.logging.Logger;
@@ -43,16 +41,15 @@
private static final Logger log = Logger.getLogger(JavaBeanXmlAttachmentsSerializer.class);
@Override
- protected Map<String, Object> loadAttachments(File attachmentsStore,
- ClassLoader loader) throws Exception
+ @SuppressWarnings("unchecked")
+ protected <T> T loadAttachment(File attachmentsStore, Class<T> expected) throws Exception
{
- Thread.currentThread().setContextClassLoader(loader);
+ T object = null;
FileInputStream fis = new FileInputStream(attachmentsStore);
XMLDecoder ois = new XMLDecoder(fis);
- Map<String, Object> map = null;
try
{
- map = (Map<String, Object>) ois.readObject();
+ object = (T) ois.readObject();
}
finally
{
@@ -71,27 +68,18 @@
{
}
}
- return map;
+ return object;
}
@Override
- protected void saveAttachments(File attachmentsStore,
- Map<String, Object> map) throws Exception
+ protected void saveAttachment(File attachmentsStore, Object attachment) throws Exception
{
- log.debug("saveAttachments, attachmentsStore="+attachmentsStore);
- if( log.isTraceEnabled() )
- {
- log.trace("Attachments.size: "+map.size());
- for(String key : map.keySet())
- {
- log.trace(key+", "+map.get(key));
- }
- }
+ log.trace("saveAttachments, attachmentsStore="+attachmentsStore+ ", attachment="+attachment);
FileOutputStream fos = new FileOutputStream(attachmentsStore);
XMLEncoder oos = new XMLEncoder(fos);
try
{
- oos.writeObject(map);
+ oos.writeObject(attachment);
}
finally
{
@@ -111,5 +99,4 @@
}
}
}
-
}
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/repository/JavaSerializationAttachmentsSerializer.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/repository/JavaSerializationAttachmentsSerializer.java 2008-11-12 19:31:47 UTC (rev 80858)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/repository/JavaSerializationAttachmentsSerializer.java 2008-11-12 19:35:50 UTC (rev 80859)
@@ -26,11 +26,9 @@
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import java.io.Serializable;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Arrays;
-import java.util.Map;
import org.jboss.logging.Logger;
@@ -44,23 +42,21 @@
extends AbstractFileAttachmentsSerializer
{
private static final Logger log = Logger.getLogger(JavaSerializationAttachmentsSerializer.class);
-
+
+ @Override
@SuppressWarnings("unchecked")
- protected Map<String, Object> loadAttachments(File attachmentsStore, ClassLoader loader)
- throws Exception
+ protected <T> T loadAttachment(File attachmentsStore, Class<T> expected) throws Exception
{
- log.debug("loadAttachments, attachmentsStore="+attachmentsStore);
- Thread.currentThread().setContextClassLoader(loader);
FileInputStream fis = new FileInputStream(attachmentsStore);
- ObjectInputStream ois = new TCLObjectInputStream(fis, loader);
- Map<String, Object> map = null;
+ ObjectInputStream ois = new ObjectInputStream(fis);
+ T object = null;
try
{
- map = (Map<String, Object>) ois.readObject();
+ object = (T) ois.readObject();
}
catch(ClassNotFoundException e)
{
- URL[] path = getClassLoaderURLs(loader);
+ URL[] path = getClassLoaderURLs(Thread.currentThread().getContextClassLoader());
log.debug("ClassNotFoundException: "+e.getMessage()+", path: "+Arrays.asList(path));
}
finally
@@ -80,27 +76,18 @@
{
}
}
- return map;
+ return object;
}
- protected void saveAttachments(File attachmentsStore,
- Map<String, Object> map)
- throws Exception
+ @Override
+ protected void saveAttachment(File attachmentsStore, Object attachment) throws Exception
{
- log.debug("saveAttachments, attachmentsStore="+attachmentsStore);
- if( log.isTraceEnabled() )
- {
- log.trace("Attachments.size: "+map.size());
- for(String key : map.keySet())
- {
- log.trace(key+", "+map.get(key));
- }
- }
+ log.trace("saveAttachments, attachmentsStore="+attachmentsStore+ ", attachment="+attachment);
FileOutputStream fos = new FileOutputStream(attachmentsStore);
ObjectOutputStream oos = new ObjectOutputStream(fos);
try
{
- oos.writeObject(map);
+ oos.writeObject(attachment);
}
finally
{
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/repository/SerializableDeploymentRepository.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/repository/SerializableDeploymentRepository.java 2008-11-12 19:31:47 UTC (rev 80858)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/repository/SerializableDeploymentRepository.java 2008-11-12 19:35:50 UTC (rev 80859)
@@ -120,12 +120,6 @@
private boolean failIfAlreadyExists = false;
/** The deployment factory */
private VFSDeploymentFactory factory = VFSDeploymentFactory.getInstance();
-
- /**
- * A helper for xml serialization
- * TODO this should be removed.
- */
- private JAXBAttachmentHelper xmlSerializer;
public SerializableDeploymentRepository(File root, URI[] appURIs, ProfileKey key)
{
@@ -720,8 +714,8 @@
}
adminEditsRoot = new File(profileRoot, "attachments");
- // TODO the xmlSerializer should be removed
- this.xmlSerializer = new JAXBAttachmentHelper(adminEditsRoot);
+ if(this.serializer instanceof AbstractFileAttachmentsSerializer)
+ ((AbstractFileAttachmentsSerializer) this.serializer).setAttachmentsStoreDir(adminEditsRoot);
if( bootstrapDir != null )
{
@@ -1131,11 +1125,8 @@
for(AttachmentMetaData attachment : metaData.getAttachments())
{
String attachmentPath = deploymentPath + attachment.getName();
- // TODO
- Map<String, Object> childAttachment = new HashMap<String, Object>();
- childAttachment.put(attachment.getName(), attachment.getAttachment());
// Serialize the attachment
- serializer.saveAttachments(attachmentPath, childAttachment);
+ serializer.saveAttachment(attachmentPath, attachment.getAttachment());
if(trace)
log.trace("Stored attachment to : " + attachmentPath);
@@ -1155,11 +1146,8 @@
for(AttachmentMetaData attachment : child.getAttachments())
{
String attachmentPath = childPath + attachment.getName();
- // TODO
- Map<String, Object> childAttachment = new HashMap<String, Object>();
- childAttachment.put(attachment.getName(), attachment.getAttachment());
// Serialize the attachment
- serializer.saveAttachments(attachmentPath, childAttachment);
+ serializer.saveAttachment(attachmentPath, attachment.getAttachment());
if(trace)
log.trace("Stored attachment to : " + attachmentPath);
@@ -1169,7 +1157,7 @@
}
// Store the repository metadata
- xmlSerializer.saveAttachment(deploymentPath + "metadata", metaData);
+ this.serializer.saveAttachment(deploymentPath + "metadata", metaData);
//
this.lastModified = System.currentTimeMillis();
@@ -1347,7 +1335,7 @@
try
{
// Try to load metadata Attachment
- return xmlSerializer.loadAttachment(fixedMetadataPath, RepositoryAttachmentMetaData.class);
+ return this.serializer.loadAttachment(fixedMetadataPath, RepositoryAttachmentMetaData.class);
}
catch(Exception e)
{
More information about the jboss-cvs-commits
mailing list