LifeCycleEvent.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.wsdm.notifications;
022 
023 import org.apache.muse.util.xml.XmlSerializable;
024 import org.apache.muse.util.xml.XmlUtils;
025 import org.apache.qpid.management.Names;
026 import org.w3c.dom.Document;
027 import org.w3c.dom.Element;
028 
029 /**
030  * Object representation of a QMan entity lifecycle event notification.
031  * Note that with entity we mean both object(s) and event(s). 
032  
033  * At the moment there are only two types of lifecycle events : CREATE and REMOVE.
034  * The first one if fired when a new instance (event or object) is created, while the second
035  * one is fired when an object instance (events are transient objects so they are not destroyed) 
036  * is removed.
037  
038  * Developer Note : The marshal & unmarshal ops could be handled using JAXB but
039  * we are not sure about the running environment (JAXB libs were included only 
040  * starting from 1.6)
041  
042  * This is the event XML representation :
043  
044  <LifecycleEvent Type="created" timemillis="">
045     <Resource>
046       <ResourceId>16038bd5-b62b-4e86-9833-7560ed57b474</id>
047       <Package>org.qpid.apache.broker</package>
048       <Name>session</name>
049     </Resource>
050   </lifecycle-event>
051   
052  @author Andrea Gazzarini
053  */
054 public class LifeCycleEvent implements XmlSerializable
055 {  
056   private final String _resourceId;
057   private final String _packageName;
058   private final String _name;
059   
060   private final LifeCycleEventType _type;
061   
062   /**
063    * Builds a new event with the given data.
064    
065    @param resourceId resource identifier.
066    @param packageName resource package name.
067    @param name resource name.
068    @param type event type.
069    */
070   private LifeCycleEvent(String resourceId, String packageName, String name, LifeCycleEventType type)
071   {
072     this._resourceId = resourceId;
073     this._packageName = packageName;
074     this._name = name;
075     this._type = type;
076   }
077 
078   /**
079    * Factory method for a new "CREATE" event. 
080    * Builds a new "CREATE" event with the given data.
081    
082    @param resourceId resource identifier.
083    @param packageName resource package name.
084    @param name resource name.
085    */
086   public static LifeCycleEvent newCreateEvent(String resourceId, String packageName, String name)
087   {
088     return new LifeCycleEvent(resourceId, packageName, name, LifeCycleEventType.CREATED);
089   }
090 
091   /**
092    * Factory method for a new "REMOVE" event. 
093    * Builds a new "REMOVE" event with the given data.
094    
095    @param resourceId resource identifier.
096    @param packageName resource package name.
097    @param name resource name.
098    */
099   public static LifeCycleEvent newRemoveEvent(String resourceId, String packageName, String name)
100   {
101     return new LifeCycleEvent(resourceId, packageName, name, LifeCycleEventType.REMOVED);
102   }
103 
104   /**
105    * Returns an XML representation of this event.
106    
107    @return an XML representation of this event.
108    */
109   public Element toXML()
110   {
111     return toXML(XmlUtils.EMPTY_DOC);
112   }
113 
114   /**
115    * Returns an XML representation of this event using the given 
116    * input document as owner.
117    
118    @return an XML representation of this event.
119    */
120   public Element toXML(Document factory)
121   {
122     Element lifeCycleEvent = XmlUtils.createElement(factory, Names.LIFECYCLE_EVENT_QNAME);
123     
124     lifeCycleEvent.setAttribute(
125         "Type"
126         _type.toString());
127     
128     lifeCycleEvent.setAttribute(
129         Names.TIMEMILLIS_ATTRIBUTE_NAME, 
130         String.valueOf(System.currentTimeMillis()));
131 
132     Element resource = XmlUtils.createElement(factory, Names.RESOURCE_QNAME);
133     lifeCycleEvent.appendChild(resource);
134     
135     Element id = XmlUtils.createElement(factory, Names.RES_ID_QNAME);
136     id.setTextContent(_resourceId);
137     resource.appendChild(id);
138     
139     Element packageName = XmlUtils.createElement(factory, Names.PACKAGE_NAME_QNAME);
140     packageName.setTextContent(_packageName);
141     resource.appendChild(packageName);
142     
143     Element name = XmlUtils.createElement(factory, Names.ENTITY_NAME_QNAME);
144     name.setTextContent(_name);
145     resource.appendChild(name);
146     
147     return lifeCycleEvent;
148   }
149 }