Attachments

Before we start coding the new Deployers there is another concept that we still need to have a look at. What is the way to store the information between different deployers? We keep this information in so called Attachments. There are two types of attachments, predetermined and transient. e.g. predetermined can be set by ProfileService, where we would get the transient one's from parsing the XML file. You must be aware that a predetermined overriddes transient. This is a simple API to get a hold of the Attachments reference from the underlying DeploymentUnit instance.

         public interface DeploymentUnit extends MutableAttachments
         {
         /**
         * Get all the metadata for the expected type
         *
         * @param <T> the type to get
         * @param type the type
         * @return a set of metadata matching the type
         * @throws IllegalArgumentException if the type is null
         */
         <T> Set<? extends T> getAllMetaData(Class<T> type);

         /**
         * Get the transient managed objects
         *
         * @return the managed objects
         */
         MutableAttachments getTransientManagedObjects();

         ...
         }

         public interface MutableAttachments extends Attachments
         {
         /**
         * Add attachment
         *
         * @param name the name of the attachment
         * @param attachment the attachment
         * @return any previous attachment
         * @throws IllegalArgumentException for a null name or attachment
         * @throws UnsupportedOperationException when not supported by the implementation
         */
         Object addAttachment(String name, Object attachment);

         /**
         * Add attachment
         *
         * @param <T> the expected type
         * @param name the name of the attachment
         * @param attachment the attachment
         * @param expectedType the expected type
         * @return any previous attachment
         * @throws IllegalArgumentException for a null name, attachment or expectedType
         * @throws UnsupportedOperationException when not supported by the implementation
         */
         <T> T addAttachment(String name, T attachment, Class<T> expectedType);

         /**
         * Add attachment
         *
         * @param <T> the expected type
         * @param attachment the attachment
         * @param type the type
         * @return any previous attachment
         * @throws IllegalArgumentException for a null name, attachment or type
         * @throws UnsupportedOperationException when not supported by the implementation
         */
         <T> T addAttachment(Class<T> type, T attachment);

         /**
         * Remove attachment
         *
         * @param name the name of the attachment
         * @return the attachment or null if not present
         * @throws IllegalArgumentException for a null name
         * @throws UnsupportedOperationException when not supported by the implementation
         */
         Object removeAttachment(String name);

         /**
         * Remove attachment
         *
         * @param <T> the expected type
         * @param name the name of the attachment
         * @return the attachment or null if not present
         * @param expectedType the expected type
         * @throws IllegalArgumentException for a null name or expectedType
         * @throws UnsupportedOperationException when not supported by the implementation
         */
         <T> T removeAttachment(String name, Class<T> expectedType);

         /**
         * Remove attachment
         *
         * @param <T> the expected type
         * @return the attachment or null if not present
         * @param type the type
         * @throws IllegalArgumentException for a null name or type
         */
         <T> T removeAttachment(Class<T> type);

         /**
         * Set the attachments
         *
         * @param map the new attachments a map of names to attachments
         * @throws IllegalArgumentException for a null map
         */
         void setAttachments(Map<String, Object> map);

         /**
         * Clear the attachments
         *
         * @throws UnsupportedOperationException when not supported by the implementation
         */
         void clear();

         /**
         * Get the number of changes that have happened.
         *
         * @return number of adds/removes that have happened since creation or clearChangeCount.
         */
         int getChangeCount();

         /**
         * Reset the change count to zero.
         */
         void clearChangeCount();
         }

         public interface Attachments extends Serializable
         {
         /**
         * Get all the attachments
         *
         * @return the unmodifiable attachments
         */
         Map<String, Object> getAttachments();

         /**
         * Get attachment
         *
         * @param name the name of the attachment
         * @return the attachment or null if not present
         * @throws IllegalArgumentException for a null name
         */
         Object getAttachment(String name);

         /**
         * Get attachment
         *
         * @param <T> the expected type
         * @param name the name of the attachment
         * @param expectedType the expected type
         * @return the attachment or null if not present
         * @throws IllegalArgumentException for a null name or expectedType
         */
         <T> T getAttachment(String name, Class<T> expectedType);

         /**
         * Get attachment
         *
         * @param <T> the expected type
         * @param type the type
         * @return the attachment or null if not present
         * @throws IllegalArgumentException for a null name or type
         */
         <T> T getAttachment(Class<T> type);

         /**
         * Is the attachment present
         *
         * @param name the name of the attachment
         * @return true when the attachment is present
         * @throws IllegalArgumentException for a null name
         */
         boolean isAttachmentPresent(String name);

         /**
         * Is the attachment present
         *
         * @param name the name of the attachment
         * @param expectedType the expected type
         * @return true when the attachment is present
         * @throws IllegalArgumentException for a null name or expectedType
         */
         boolean isAttachmentPresent(String name, Class<?> expectedType);

         /**
         * Is the attachment present
         *
         * @param type the type
         * @return true when the attachment is present
         * @throws IllegalArgumentException for a null name or type
         */
         boolean isAttachmentPresent(Class<?> type);

         /**
         * Are there any attachments
         *
         * @return true if there are any attachments, false otherwise.
         */
         boolean hasAttachments();
         }