[jboss-cvs] JBossAS SVN: r81275 - in trunk/system/src/main/org/jboss/system/server/profileservice: repository and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 19 03:56:20 EST 2008


Author: emuckenhuber
Date: 2008-11-19 03:56:19 -0500 (Wed, 19 Nov 2008)
New Revision: 81275

Modified:
   trunk/system/src/main/org/jboss/system/server/profileservice/attachments/AttachmentMetaData.java
   trunk/system/src/main/org/jboss/system/server/profileservice/attachments/DeploymentClassPathMetaData.java
   trunk/system/src/main/org/jboss/system/server/profileservice/attachments/LazyPredeterminedManagedObjects.java
   trunk/system/src/main/org/jboss/system/server/profileservice/attachments/RepositoryAttachmentMetaDataFactory.java
   trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractAttachmentStore.java
Log:
[JBAS-6037]

Modified: trunk/system/src/main/org/jboss/system/server/profileservice/attachments/AttachmentMetaData.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/attachments/AttachmentMetaData.java	2008-11-19 08:52:00 UTC (rev 81274)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/attachments/AttachmentMetaData.java	2008-11-19 08:56:19 UTC (rev 81275)
@@ -25,6 +25,9 @@
 import javax.xml.bind.annotation.XmlTransient;
 
 /**
+ * The AttachmentMetaData, containing the information for storing and 
+ * restoring the persisted Attachment.
+ * 
  * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
  * @version $Revision$
  */

Modified: trunk/system/src/main/org/jboss/system/server/profileservice/attachments/DeploymentClassPathMetaData.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/attachments/DeploymentClassPathMetaData.java	2008-11-19 08:52:00 UTC (rev 81274)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/attachments/DeploymentClassPathMetaData.java	2008-11-19 08:56:19 UTC (rev 81275)
@@ -24,6 +24,9 @@
 import javax.xml.bind.annotation.XmlElement;
 
 /**
+ * The DeploymentClassPathMetaData is the xml representation
+ * of the ClassPathEntry in ContextInfo.
+ * 
  * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
  * @version $Revision$
  */

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-19 08:52:00 UTC (rev 81274)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/attachments/LazyPredeterminedManagedObjects.java	2008-11-19 08:56:19 UTC (rev 81275)
@@ -22,6 +22,8 @@
 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;
 
@@ -33,8 +35,7 @@
  * Basic wrapper for MutableAttachmets. This maintains a list of associated
  * metadata, which can be loaded lazily and delegates the attachments to AttachmentsImpl.
  * 
- * TODO this should use a AttachmentSerializer which stores the meta data
- * as XML files.  
+ * 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$
@@ -136,14 +137,14 @@
    public <T> T getAttachment(Class<T> type)
    {
       T o = delegate.getAttachment(type); 
-      if(o == null) o = (T) loadAttachment(type.getName());
+      if(o == null) o = (T) loadAttachment(type.getName(), type);
       return o;  
    }
 
    public <T> T getAttachment(String name, Class<T> expectedType)
    {
       T o = delegate.getAttachment(name, expectedType);
-      if(o == null) o = (T) loadAttachment(name);
+      if(o == null) o = (T) loadAttachment(name, expectedType);
       return o;
    }
 
@@ -181,9 +182,14 @@
    {
       return attachments.contains(name);
    }
-
+   
    private Object loadAttachment(String name)
    {
+      return loadAttachment(name, null);
+   }
+
+   private Object loadAttachment(String name, Class<?> clazz)
+   {
       if(! attachments.contains(name))
          return null;
       
@@ -191,10 +197,10 @@
       {
          // deploy/deployment/child/attachmentName
          String attachmentName = relativePath + name;
-         // use the con
-         ClassLoader cl = Thread.currentThread().getContextClassLoader();
          // Load class
-         Class<?> expected = cl.loadClass(name);
+         Class<?> expected = clazz;
+         if(expected == null)
+            expected = loadClass(name);
          // Load attachment
          Object o = serializer.loadAttachment(attachmentName, expected);
          // Add attachment
@@ -209,4 +215,23 @@
       }
    }
    
+   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/attachments/RepositoryAttachmentMetaDataFactory.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/attachments/RepositoryAttachmentMetaDataFactory.java	2008-11-19 08:52:00 UTC (rev 81274)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/attachments/RepositoryAttachmentMetaDataFactory.java	2008-11-19 08:56:19 UTC (rev 81275)
@@ -105,6 +105,21 @@
       metaData.setDeploymentStructure(structure);
    }
    
+   public static AttachmentMetaData findAttachment(String name, List<AttachmentMetaData> attachments)
+   {
+      if(name == null)
+         return null;
+      if(attachments == null)
+         return null;
+      
+      for(AttachmentMetaData attachment : attachments)
+      {
+         if(name.equals(attachment.getName()))
+            return attachment;
+      }
+      return null;
+   }
+   
    protected static List<DeploymentClassPathMetaData> getDeploymentClassPathMetaData(ContextInfo info)
    {
       if(info == null)

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-19 08:52:00 UTC (rev 81274)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/repository/AbstractAttachmentStore.java	2008-11-19 08:56:19 UTC (rev 81275)
@@ -36,6 +36,7 @@
 import org.jboss.deployers.structure.spi.main.MainDeployerStructure;
 import org.jboss.deployers.vfs.spi.client.VFSDeployment;
 import org.jboss.deployers.vfs.spi.client.VFSDeploymentFactory;
+import org.jboss.deployers.vfs.spi.structure.VFSDeploymentContext;
 import org.jboss.logging.Logger;
 import org.jboss.managed.api.ManagedCommon;
 import org.jboss.managed.api.ManagedComponent;
@@ -52,6 +53,8 @@
 import org.jboss.virtual.VirtualFile;
 
 /**
+ * The AbstractAttachmentStore updates and restores the persisted attachments.
+ * 
  * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
  * @version $Revision$
  */
@@ -67,6 +70,9 @@
    /** The MainDeployerStructure. */
    protected MainDeployerStructure mainDeployer;
    
+   /** The metadata name */
+   public static final String METADATA_NAME = "metadata";
+   
    /** The logger. */
    private static final Logger log = Logger.getLogger(AbstractAttachmentStore.class);
    
@@ -163,9 +169,9 @@
       
       try
       {
-         // If the deployment has changes we skip this.
+         // If the deployment has changes we skip restoring the persisted metadata.
          // TODO delete attachments ?
-         if(attachmentMetaData.getLastModified() < file.getLastModified())
+         if(attachPredeterminedObject(file, attachmentMetaData) == false)
          {
             log.debug("Not using the persisted metadata, as the deployment was modified.");
             return deployment;
@@ -173,7 +179,7 @@
       }
       catch(IOException e)
       {
-         log.error("failed to get LastModified date for file, no using persisted metadata: "+ file.getPathName());
+         log.error("failed to get LastModified date for file, not using persisted metadata: "+ file.getPathName());
          return deployment;
       }
       
@@ -183,6 +189,37 @@
       return deployment;
    }
    
+   /**
+    * Determine whether to attach the PredeterminedManagedObjects or not.
+    * 
+    * TODO this should also check the metadata path for the deployment. 
+    * 
+    * @param root the path of the deployment
+    * @param metaData 
+    * @return
+    * @throws IOException
+    */
+   protected boolean attachPredeterminedObject(VirtualFile root, RepositoryAttachmentMetaData metaData)
+      throws IOException
+   {
+      boolean attach = true;
+      if(metaData.getLastModified() < root.getLastModified())
+      {
+         attach = false;
+      }
+      // TODO check metaData locations
+      return attach;
+   }
+   
+   /**
+    * 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
+    * @throws Exception
+    */
    protected void updateDeployment(VFSDeployment deployment, DeploymentPhase phase, ManagedComponent comp)
       throws Exception
    {
@@ -240,7 +277,7 @@
       }
       
       if(currentContextMetaData == null)
-         throw new IllegalStateException("Could not create child metadata");
+         throw new IllegalStateException("Could not create metadata");
       
       // Get the currentTimeMillis
       long lastModified = System.currentTimeMillis();
@@ -257,9 +294,14 @@
          Object o = managedObject.getAttachment();
          if(o != null)
          {
-            AttachmentMetaData attachment = new AttachmentMetaData();
+            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(managedObject.getAttachmentName());
+            attachment.setName(attachmentName);
             attachment.setClassName(o.getClass().getName());
             // Set attachment
             attachment.setAttachment(o);
@@ -270,10 +312,14 @@
          }
       }
       
-      // Plus set the last modified on the root metadata
+      // Save the attachment for the root
+      saveAttachmentMetaData(deploymentPath, savedMetaData);
+
+      // Set the last modified on the root metadata
       savedMetaData.setLastModified(lastModified);
-      // Save the attachment
-      saveAttachmentMetaData(deploymentPath, savedMetaData);
+      
+      // Save the repository meta data
+      this.serializer.saveAttachment(getMetaDataPathName(deploymentPath), savedMetaData);
    }
    
    /**
@@ -305,13 +351,16 @@
          // If it is not the root path
          if(! "".equals(info.getPath()))
          {
+            String childContextName = fixName(info.getPath());
+            // TODO we might need to check the context itself contains a subdeployment? 
+            
             RepositoryAttachmentMetaData newChild = RepositoryAttachmentMetaDataFactory.createInstance();
-            newChild.setDeploymentName(info.getPath());
+            newChild.setDeploymentName(childContextName);
             
             RepositoryAttachmentMetaDataFactory.applyStructureContext(newChild, info);
             RepositoryAttachmentMetaDataFactory.addChild(parentMetaData, newChild);
             
-            if(childPath.equals(info.getPath()))
+            if(childPath.equals(childContextName))
                childMetaData = newChild;
          }
       }
@@ -323,25 +372,8 @@
    }
    
    /**
-    * Get the structure meta data for a deployment.
+    * Save the attachments based on the RepositoryAttachmentMetaData.
     * 
-    * @param vfsDeploymentName the vfs deployment name
-    * @return the StructureMetaData
-    * @throws Exception
-    */
-   protected StructureMetaData getStructureMetaData(String vfsDeploymentName)
-      throws Exception
-    {
-      // Get the StructureMetaData;
-      DeploymentContext deploymentContext = mainDeployer.getDeploymentContext(vfsDeploymentName, false);
-      return deploymentContext.getDeploymentUnit().getAttachment(StructureMetaData.class);
-    }
-   
-   /**
-    * Update a deployment based on it's RepositoryAttachmentMetaData
-    * 
-    * TODO use a xml serializer, to drop the use of the childAttachment maps. 
-    * 
     * @param d
     * @param phase
     * @param metaData
@@ -370,34 +402,15 @@
          }
       }
       
-      // Save attachments for the children
-      List<RepositoryAttachmentMetaData> children = metaData.getChildren();
-      if(children != null && !children.isEmpty())
+      // Save the attachments for the childs
+      if(metaData.getChildren() != null && ! metaData.getChildren().isEmpty())
       {
-         for(RepositoryAttachmentMetaData child : children)
+         for(RepositoryAttachmentMetaData child : metaData.getChildren())
          {
-            // The relative child path 
-            String childPath = deploymentPath + child.getDeploymentName() + File.separator;
-            if(child.getAttachments() != null && ! child.getAttachments().isEmpty())
-            {
-               for(AttachmentMetaData attachment : child.getAttachments())
-               {
-                  if(attachment.getAttachment() == null)
-                     continue;
-                  
-                  String attachmentPath = childPath + attachment.getName();
-                  // Serialize the attachment
-                  serializer.saveAttachment(attachmentPath, attachment.getAttachment());
-                  
-                  if(trace)
-                     log.trace("Stored attachment to : " + attachmentPath);
-               }
-            }
+            String childDeploymentPath = deploymentPath + File.separator + child.getDeploymentName() + File.separator;
+            saveAttachmentMetaData(childDeploymentPath, child);
          }
       }
-      
-      // Finally store the repository metadata
-      this.serializer.saveAttachment(deploymentPath + "metadata", metaData);
    }
    
    /**
@@ -406,7 +419,7 @@
     * 
     * @param deployment the VFSDeployment
     * @param contextName the structure context path
-    * @param deploymentPath the path to the attachement
+    * @param deploymentPath the path to the attachment
     * @param attachmentMetaData the meta data
     */
    protected void rebuildStructureContext(VFSDeployment deployment,
@@ -497,19 +510,30 @@
             rebuildStructureContext(deployment, fixName(childContextName), relativePath, childMetaData, trace);
          }
       }
+
    }
    
    /**
+    * Get the metadata path, based on a relative path.
+    * 
+    * @param deploymentPath the relative path to the deployment
+    * @return
+    */
+   protected String getMetaDataPathName(String deploymentPath)
+   {
+      return deploymentPath.endsWith(File.separator) ? deploymentPath + METADATA_NAME : deploymentPath + File.separator + METADATA_NAME;
+   }
+   
+   /**
     * Load the attachment metadata for a deployment.
     * 
-    * @param relativeDeploymentPath the relatative path to the metadata
+    * @param relativeDeploymentPath the relative path
     * @return the attachment metadata
     */
    protected RepositoryAttachmentMetaData loadAttachmentMetaData(String relativeDeploymentPath)
    {
-      final String metaDataName = "metadata";
-      // attachments/deploy/deployment/metadata
-      String fixedMetadataPath = relativeDeploymentPath.endsWith(File.separator) ? relativeDeploymentPath + metaDataName : relativeDeploymentPath + File.separator + metaDataName;   
+      // attachments/deploy/deployment/metadata.xml
+      String fixedMetadataPath = getMetaDataPathName(relativeDeploymentPath);   
                
       try
       {  // Try to load the repository attachment metadata
@@ -522,7 +546,44 @@
       return null;
    }
    
+   
    /**
+    * Get the structure meta data for a deployment.
+    * 
+    * @param vfsDeploymentName the vfs deployment name
+    * @return the StructureMetaData
+    * @throws Exception
+    */
+   protected StructureMetaData getStructureMetaData(String vfsDeploymentName)
+   {
+      // Get the StructureMetaData;
+      DeploymentContext deploymentContext = getDeploymentContext(vfsDeploymentName);
+      if(deploymentContext == null)
+         throw new IllegalStateException("Could not find deployment context for name: "+ vfsDeploymentName);
+      
+      return deploymentContext.getDeploymentUnit().getAttachment(StructureMetaData.class);
+    }
+   
+   /**
+    * Get deployment context.
+    *
+    * @param name the deployment context name
+    * @return vfs deployment context or null if doesn't exist or not vfs based
+    */
+   @SuppressWarnings("deprecation")
+   protected VFSDeploymentContext getDeploymentContext(String name)
+   {
+      if (mainDeployer == null)
+         throw new IllegalStateException("Null main deployer.");
+
+      DeploymentContext deploymentContext = mainDeployer.getDeploymentContext(name);
+      if (deploymentContext == null || deploymentContext instanceof VFSDeploymentContext == false)
+         return null;
+
+      return (VFSDeploymentContext)deploymentContext;
+   }
+   
+   /**
     * Make sure that the name does not start or end with /
     * 
     * @param name




More information about the jboss-cvs-commits mailing list