[jboss-cvs] JBossAS SVN: r81825 - 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
Sun Nov 30 09:32:52 EST 2008


Author: emuckenhuber
Date: 2008-11-30 09:32:52 -0500 (Sun, 30 Nov 2008)
New Revision: 81825

Modified:
   trunk/server/src/etc/conf/default/bootstrap/profile-repository.xml
   trunk/system/src/main/org/jboss/system/server/profileservice/attachments/LazyPredeterminedManagedObjects.java
   trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractAttachmentStore.java
   trunk/system/src/main/org/jboss/system/server/profileservice/repository/JAXBAttachmentSerializer.java
Log:
[JBAS-3768] switch to use the MO persistence format

Modified: trunk/server/src/etc/conf/default/bootstrap/profile-repository.xml
===================================================================
--- trunk/server/src/etc/conf/default/bootstrap/profile-repository.xml	2008-11-30 14:29:54 UTC (rev 81824)
+++ trunk/server/src/etc/conf/default/bootstrap/profile-repository.xml	2008-11-30 14:32:52 UTC (rev 81825)
@@ -39,6 +39,7 @@
         <bean class="org.jboss.system.server.profile.basic.XmlIncludeVirtualFileFilter"/>
       </property>
       <property name="mainDeployer"><inject bean="MainDeployer"/></property>
+      <depends>ProfileServicePersistenceDeployer</depends>
    </bean>
    <bean name="AttachmentsSerializer" class="org.jboss.system.server.profileservice.repository.JAXBAttachmentSerializer" />
 
@@ -49,6 +50,8 @@
       <property name="mof"><inject bean="ManagedObjectFactory"/></property>
       <property name="mgtDeploymentCreator"><inject bean="ManagedDeploymentCreator"/></property>
    </bean>
+   
+   <bean name="ProfileServicePersistenceDeployer" class="org.jboss.system.server.profileservice.persistence.deployer.ProfileServicePersistenceDeployer" />
 	
    <!-- A filter for excluding files from the scanner -->
    <bean name="DeploymentFilter" class="org.jboss.virtual.plugins.vfs.helpers.ExtensibleFilter">

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-30 14:29:54 UTC (rev 81824)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/attachments/LazyPredeterminedManagedObjects.java	2008-11-30 14:32:52 UTC (rev 81825)
@@ -22,21 +22,19 @@
 package org.jboss.system.server.profileservice.attachments;
 
 import java.io.File;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 import java.util.Collection;
 import java.util.Map;
 
 import org.jboss.deployers.spi.attachments.AttachmentsFactory;
 import org.jboss.deployers.spi.attachments.MutableAttachments;
 import org.jboss.profileservice.spi.AttachmentsSerializer;
+import org.jboss.system.server.profileservice.persistence.deployer.ProfileServicePersistenceDeployer;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
 
 /**
  * Basic wrapper for MutableAttachmets. This maintains a list of associated
- * metadata, which can be loaded lazily and delegates the attachments to AttachmentsImpl.
+ * metadata, which is loaded on demand.
  * 
- * TODO we might want to use a specific ClassLoader for loading a class.
- * 
  * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
  * @version $Revision$
  */
@@ -45,16 +43,16 @@
    /** The serialVersionUID */
    private static final long serialVersionUID = 1L;
 
-   /** The attachments */
+   /** The attachments. */
    private final MutableAttachments delegate;
    
-   /** The attachmentsSerializer */
+   /** The attachmentsSerializer. */
    private final AttachmentsSerializer serializer;
    
-   /** The relative path */
+   /** The relative path. */
    private final String relativePath;
    
-   /** The available attachments */
+   /** The available attachments. */
    private final Collection<String> attachments;
    
    public LazyPredeterminedManagedObjects(AttachmentsSerializer serializer, String relativePath, Collection<String> attachments)
@@ -129,34 +127,28 @@
 
    public Object getAttachment(String name)
    {
-      Object o = delegate.getAttachment(name);
-      if(o == null) o = loadAttachment(name);
-      return o; 
+      if(ishandleAttachment(name))
+         return loadAttachment(name);
+      return delegate.getAttachment(name); 
    }
 
    public <T> T getAttachment(Class<T> type)
    {
-      T o = delegate.getAttachment(type); 
-      if(o == null) o = (T) loadAttachment(type.getName(), type);
-      return o;  
+      return delegate.getAttachment(type);  
    }
 
    public <T> T getAttachment(String name, Class<T> expectedType)
    {
-      T o = delegate.getAttachment(name, expectedType);
-      if(o == null) o = (T) loadAttachment(name, expectedType);
-      return o;
+      return delegate.getAttachment(name, expectedType);
    }
 
    public Map<String, Object> getAttachments()
    {
-      // TODO - load all attachments ? 
       return delegate.getAttachments();
    }
 
    public boolean hasAttachments()
    {
-      if(! this.attachments.isEmpty()) return true;
       return delegate.hasAttachments();
    }
 
@@ -180,58 +172,30 @@
    
    private boolean ishandleAttachment(String name)
    {
-      return attachments.contains(name);
+      if(name == null)
+         return false;
+      if(! name.startsWith(ProfileServicePersistenceDeployer.PERSISTED_ATTACHMENT_PREFIX))
+         return false;
+      
+      return attachments.contains(name.substring(ProfileServicePersistenceDeployer.PERSISTED_ATTACHMENT_PREFIX.length()));
    }
-   
-   private Object loadAttachment(String name)
-   {
-      return loadAttachment(name, null);
-   }
 
-   private Object loadAttachment(String name, Class<?> clazz)
+   private PersistedManagedObject loadAttachment(String name)
    {
-      if(! attachments.contains(name))
+      if(! ishandleAttachment(name))
          return null;
-      
+    
+      String attachmentClassName = name.substring(ProfileServicePersistenceDeployer.PERSISTED_ATTACHMENT_PREFIX.length());
       try
       {
          // deploy/deployment/child/attachmentName
-         String attachmentName = relativePath + name;
-         // Load class
-         Class<?> expected = clazz;
-         if(expected == null)
-            expected = loadClass(name);
+         String attachmentName = relativePath + attachmentClassName;
          // Load attachment
-         Object o = serializer.loadAttachment(attachmentName, expected);
-         // Add attachment
-         if(o != null)
-            this.delegate.addAttachment(name, o);
-         // Return attachment
-         return o;
+         return serializer.loadAttachment(attachmentName, PersistedManagedObject.class);
       }
       catch(Exception e)
       {
          throw new RuntimeException(e);
       }
    }
-   
-   private Class<?> loadClass(String name) throws Exception
-   {
-      ClassLoader loader;
-      if (System.getSecurityManager() == null)
-      {
-         loader = Thread.currentThread().getContextClassLoader();
-      }
-      else
-      {
-         loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
-         {
-            public ClassLoader run()
-            {
-               return Thread.currentThread().getContextClassLoader();
-            }
-         });
-      }
-      return loader.loadClass(name);
-   }
- }
+}

Modified: trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractAttachmentStore.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractAttachmentStore.java	2008-11-30 14:29:54 UTC (rev 81824)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractAttachmentStore.java	2008-11-30 14:32:52 UTC (rev 81825)
@@ -50,6 +50,8 @@
 import org.jboss.system.server.profileservice.attachments.LazyPredeterminedManagedObjects;
 import org.jboss.system.server.profileservice.attachments.RepositoryAttachmentMetaData;
 import org.jboss.system.server.profileservice.attachments.RepositoryAttachmentMetaDataFactory;
+import org.jboss.system.server.profileservice.persistence.ManagedObjectPeristenceHandler;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
 import org.jboss.virtual.VirtualFile;
 
 /**
@@ -73,6 +75,9 @@
    /** The metadata name */
    public static final String METADATA_NAME = "metadata";
    
+   /** The managed object persistence handler. */
+   private static final ManagedObjectPeristenceHandler handler = new ManagedObjectPeristenceHandler();
+   
    /** The logger. */
    private static final Logger log = Logger.getLogger(AbstractAttachmentStore.class);
    
@@ -192,11 +197,11 @@
    /**
     * Determine whether to attach the PredeterminedManagedObjects or not.
     * 
-    * TODO this should also check the metadata path for the deployment. 
+    * TODO this should also check the metadata paths for the deployment. 
     * 
     * @param root the path of the deployment
-    * @param metaData 
-    * @return
+    * @param metaData the repository meta data
+    * @return hasBeenModified.
     * @throws IOException
     */
    protected boolean attachPredeterminedObject(VirtualFile root, RepositoryAttachmentMetaData metaData)
@@ -215,9 +220,9 @@
     * Persist the updated metadata for a managedComponent and generate a metadata describing
     * the repository, if it does not exist already.
     * 
-    * @param deployment
-    * @param phase
-    * @param comp
+    * @param deployment the deployment.
+    * @param phase the deployment phase.
+    * @param comp the managed component.
     * @throws Exception
     */
    protected void updateDeployment(VFSDeployment deployment, DeploymentPhase phase, ManagedComponent comp)
@@ -287,29 +292,28 @@
       while(parent.getParent() != null)
          parent = parent.getParent();
 
-      // Get the managed object
+      // Get the managed object, as a component can also be a child of a managedObject
       ManagedObject managedObject = comp.getDeployment().getManagedObject(parent.getName());
+      // Create a AttachmentMetaData for the MO
       if(managedObject != null)
       {
-         Object o = managedObject.getAttachment();
-         if(o != null)
+         String attachmentName = managedObject.getAttachmentName(); 
+         // Create attachmentMetaData if needed
+         AttachmentMetaData attachment = RepositoryAttachmentMetaDataFactory.findAttachment(attachmentName, currentContextMetaData.getAttachments());
+         if(attachment == null)
          {
-            String attachmentName = managedObject.getAttachmentName(); 
-            // Create attachmentMetaData if needed
-            AttachmentMetaData attachment = RepositoryAttachmentMetaDataFactory.findAttachment(attachmentName, currentContextMetaData.getAttachments());
-            if(attachment == null)
-               attachment = new AttachmentMetaData();
-            
-            // Is attachmentName the same as the className ?
-            attachment.setName(attachmentName);
-            attachment.setClassName(o.getClass().getName());
-            // Set attachment
-            attachment.setAttachment(o);
-            // Add attachment
+            // Add a new attachment
+            attachment = new AttachmentMetaData();
             RepositoryAttachmentMetaDataFactory.addAttachment(currentContextMetaData, attachment);
-            // Update lastModified
-            currentContextMetaData.setLastModified(lastModified);
          }
+         
+         // Is attachmentName the same as the className ?
+         attachment.setName(attachmentName);
+         attachment.setClassName(managedObject.getAttachment().getClass().getName());
+         // Set attachment, this is transient - as it will get persisted in it's own file.
+         attachment.setAttachment(managedObject);
+         // Update lastModified
+         currentContextMetaData.setLastModified(lastModified);
       }
       
       // Save the attachment for the root
@@ -371,12 +375,12 @@
       return childMetaData;
    }
    
+
    /**
     * Save the attachments based on the RepositoryAttachmentMetaData.
     * 
-    * @param d
-    * @param phase
-    * @param metaData
+    * @param deploymentPath the deploymentPath
+    * @param metaData the repository meta data.
     * @throws Exception
     */
    private void saveAttachmentMetaData(String deploymentPath, RepositoryAttachmentMetaData metaData)
@@ -393,9 +397,13 @@
             if(attachment.getAttachment() == null)
                continue;
             
+            // Create xml meta data for persistence.
+            ManagedObject mo = (ManagedObject) attachment.getAttachment();
+            PersistedManagedObject root = createPersistedMetaData(mo);
+            
             String attachmentPath = deploymentPath + attachment.getName();
             // Serialize the attachment
-            serializer.saveAttachment(attachmentPath, attachment.getAttachment());
+            serializer.saveAttachment(attachmentPath, root);
             
             if(trace)
                log.trace("Stored attachment to : " + attachmentPath);
@@ -414,6 +422,22 @@
    }
    
    /**
+    * create the xml meta data for persisting the managed object.
+    * 
+    * @param mo the managed object.
+    * @return the xml metadata.
+    */
+   protected PersistedManagedObject createPersistedMetaData(ManagedObject mo)
+   {
+      // TODO - load already persisted attachment.
+      PersistedManagedObject root = new PersistedManagedObject();
+      // Process
+      handler.processManagedObject(root, mo);
+      // Return
+      return root;
+   }
+   
+   /**
     * Rebuild the StructureMetaData based on the RepositoryAttachmentMetaData
     * and add predeterminedManagedObjects.
     * 
@@ -471,7 +495,7 @@
          for(AttachmentMetaData attachment : attachmentMetaData.getAttachments())
             availableAttachments.add(attachment.getClassName());
 
-          MutableAttachments mutable =  new LazyPredeterminedManagedObjects(this.serializer, deploymentPath, availableAttachments);
+          MutableAttachments mutable =  createPredeterminedAttachment(deploymentPath, availableAttachments);
           
           // TODO is there a better way to do this ?
           if(isRoot)
@@ -510,10 +534,21 @@
             rebuildStructureContext(deployment, fixName(childContextName), relativePath, childMetaData, trace);
          }
       }
-
    }
    
    /**
+    * Create a predetermined managedObject attachment.
+    * 
+    * @param deploymentPath the relative deployment path
+    * @param availableAttachments the available attachments
+    * @return
+    */
+   protected MutableAttachments createPredeterminedAttachment(String deploymentPath, Set<String> availableAttachments)
+   {
+      return new LazyPredeterminedManagedObjects(this.serializer, deploymentPath, availableAttachments);
+   }
+   
+   /**
     * Get the metadata path, based on a relative path.
     * 
     * @param deploymentPath the relative path to the deployment

Modified: trunk/system/src/main/org/jboss/system/server/profileservice/repository/JAXBAttachmentSerializer.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/repository/JAXBAttachmentSerializer.java	2008-11-30 14:29:54 UTC (rev 81824)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/repository/JAXBAttachmentSerializer.java	2008-11-30 14:32:52 UTC (rev 81825)
@@ -24,13 +24,19 @@
 import java.io.File;
 
 import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
+import javax.xml.namespace.QName;
 
 import org.jboss.logging.Logger;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedCompositeValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedProperty;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedValue;
 
 /**
- * A basic JAXB attachment serializer.
+ * A basic JAXB attachment Serializer.
  * 
  * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
  * @version $Revision$
@@ -41,6 +47,9 @@
    /** The logger */
    private static final Logger log = Logger.getLogger(JAXBAttachmentSerializer.class);
    
+   /** The QNAME for the root element */
+   private static final QName ROOT_NAME = new QName("attachment");
+   
    /** The attachment suffix. */
    private static final String ATTACHMENT_SUFFIX = ".attachment.xml";
    
@@ -49,6 +58,11 @@
    {
       JAXBContext ctx = JAXBContext.newInstance(expected);
       Unmarshaller unmarshaller = ctx.createUnmarshaller();
+      // FIXME this is a hack
+      if(PersistedManagedObject.class.getName().equals(expected.getName()))
+      {
+         hack(unmarshaller);
+      }
       return (T) unmarshaller.unmarshal(attachmentsStore);
    }
 
@@ -61,6 +75,11 @@
       marshaller.marshal(attachment, attachmentsStore);
    }
    
+   protected static <T> JAXBElement<T> createRootElement(Class<T> expected, Object o)
+   {
+      return new JAXBElement<T>(ROOT_NAME, expected, (T) o);
+   }
+   
    @Override
    protected File getAttachmentPath(String baseName)
    {
@@ -68,5 +87,36 @@
       return new File(getAttachmentsStoreDir(), vfsPath);
    }
    
+   private void hack(Unmarshaller unmarshaller)
+   {
+      unmarshaller.setListener(new PeristedManagedObjectHandler());
+   }
+   
+   /**
+    * Hack handler for JAXB.
+    */
+   private static class PeristedManagedObjectHandler extends Unmarshaller.Listener
+   {
+      @Override
+      public void afterUnmarshal(Object target, Object parent)
+      {
+         if(parent instanceof PersistedManagedObject)
+         {
+            if(target instanceof PersistedProperty)
+            {
+               PersistedProperty p = (PersistedProperty) target;
+               ((PersistedManagedObject) parent).put(p.getName(), p);
+            }
+         }
+         else if(parent instanceof PersistedCompositeValue)
+         {
+            if(target instanceof PersistedValue)
+            {
+               PersistedValue v = (PersistedValue) target;
+               ((PersistedCompositeValue) parent).put(v.getName(), v);
+            }
+         }
+      }
+   }
+   
 }
-




More information about the jboss-cvs-commits mailing list