Author: jbonofre
Date: Sat Oct 22 13:18:35 2011
New Revision: 1187714

URL: http://svn.apache.org/viewvc?rev=1187714&view=rev
Log:
Add log and update in the Kalumet model.

Added:
    incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/
    
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Event.java
    
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Journal.java
    
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/
    
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateLog.java
    
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateMessage.java
Removed:
    incubator/kalumet/trunk/common/common.iml

Added: 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Event.java
URL: 
http://svn.apache.org/viewvc/incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Event.java?rev=1187714&view=auto
==============================================================================
--- 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Event.java
 (added)
+++ 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Event.java
 Sat Oct 22 13:18:35 2011
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.kalumet.model.log;
+
+import org.apache.xerces.dom.CDATASectionImpl;
+import org.apache.xerces.dom.CoreDocumentImpl;
+import org.apache.xerces.dom.ElementImpl;
+import org.w3c.dom.Element;
+
+/**
+ * Represents the <code>event</code> tag in the journal DOM.
+ */
+public class Event {
+
+   private String date;
+   private String severity;
+   private String author;
+   private String content;
+
+   public Event() { }
+
+   public String getDate() {
+      return this.date;
+   }
+
+   public void setDate(String date) {
+      this.date = date;
+   }
+
+   public String getSeverity() {
+      return this.severity;
+   }
+
+   public void setSeverity(String severity) {
+      this.severity = severity;
+   }
+
+   public String getAuthor() {
+      return this.author;
+   }
+
+   public void setAuthor(String author) {
+      this.author = author;
+   }
+
+   public String getContent() {
+      return this.content;
+   }
+
+   public void setContent(String content) {
+      this.content = content;
+   }
+
+   /**
+    * Transforms the <code>Event</code> POJO to a DOM element.
+    * 
+    * @param document the DOM document.
+    * @return the DOM element.
+    */
+   protected Element toDOMElement(CoreDocumentImpl document) {
+      ElementImpl element = new ElementImpl(document, "event");
+      element.setAttribute("date", this.getDate());
+      element.setAttribute("severity", this.getSeverity());
+      element.setAttribute("author", this.getAuthor());
+      CDATASectionImpl content = new CDATASectionImpl(document, 
this.getContent());
+      element.appendChild(content);
+      return element;
+   }
+
+}

Added: 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Journal.java
URL: 
http://svn.apache.org/viewvc/incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Journal.java?rev=1187714&view=auto
==============================================================================
--- 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Journal.java
 (added)
+++ 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/log/Journal.java
 Sat Oct 22 13:18:35 2011
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.kalumet.model.log;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock;
+import org.apache.kalumet.KalumetException;
+
+import org.apache.commons.digester.Digester;
+import org.apache.xerces.dom.CoreDocumentImpl;
+import org.apache.xerces.dom.ElementImpl;
+import org.apache.xml.serialize.OutputFormat;
+import org.apache.xml.serialize.XMLSerializer;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+/**
+ * Manages the environment log file and represents the <code>journal</code>
+ * root tag.
+ */
+public class Journal {
+
+   private LinkedList events;
+
+   private WriterPreferenceReadWriteLock lock = new 
WriterPreferenceReadWriteLock();
+
+   public Journal() {
+      this.events = new LinkedList();
+   }
+
+   /**
+    * Adds a new <code>Event</code> in the <code>Journal</code> container.
+    * 
+    * @param event the <code>Event</code> to add.
+    */
+   public void addEvent(Event event) {
+      this.events.add(event);
+   }
+
+   /**
+    * Gets the <code>Event</code> list in the <code>Journal</code>
+    * container.
+    * 
+    * @return the <code>Event</code> list.
+    */
+   public List getEvents() {
+      return this.events;
+   }
+
+   /**
+    * Transforms the <code>Journal</code> POJO to a DOM element.
+    * 
+    * @param document the DOM document.
+    * @return the DOM element.
+    */
+   protected Element toDOMElement(CoreDocumentImpl document) {
+      ElementImpl element = new ElementImpl(document, "journal");
+      // events
+      for(Iterator eventIterator = this.getEvents().iterator(); 
eventIterator.hasNext();) {
+         Event event = (Event) eventIterator.next();
+         element.appendChild(event.toDOMElement(document));
+      }
+      return element;
+   }
+
+   /**
+    * Parses and loads a given XML log file and return the environment journal 
root tag.
+    * 
+    * @param path the environment log XML to parse.
+    * @return the environment <code>Journal</code> corresponding with the 
journal root tag.
+    */
+   public static Journal digeste(String path) throws KalumetException {
+      if (!path.startsWith("http:") && !path.startsWith("HTTP:") && 
!path.startsWith("file:") && !path.startsWith("FILE:")) {
+         path = "file:" + path;
+      }
+      Journal journal = null;
+      try {
+
+         // init the digester with no validation on the XML file (no DTD)
+         Digester digester = new Digester();
+         digester.setValidating(false);
+
+         // journal tag rules
+         digester.addObjectCreate("journal", 
"org.apache.kalumet.model.log.Journal");
+         digester.addSetProperties("journal");
+
+         // event tag rules
+         digester.addObjectCreate("journal/event", 
"org.apache.kalumet.model.log.Event");
+         digester.addSetProperties("journal/event");
+
+         // event content
+         digester.addCallMethod("journal/event", "setContent", 0);
+
+         // add event to journal
+         digester.addSetNext("journal/event", "addEvent", 
"org.apache.kalumet.model.log.Event");
+
+         // parse the XML file
+         journal = (Journal) digester.parse(path);
+      } 
+      catch (IOException ioException) {
+         // most of the time IOException occurs because the journal file path
+         // doesn't exist, try to create it
+         journal = new Journal();
+         journal.writeXMLFile(path);
+      } 
+      catch (Exception e) {
+         throw new KalumetException("Can't read journal", e);
+      }
+      return journal;
+   }
+
+   /**
+    * Writes the environment journal log XML file with the in-memory DOM.
+    * 
+    * @param path the path to the file to write.
+    */
+   public void writeXMLFile(String path) throws KalumetException {
+      try {
+         lock.writeLock().acquire();
+         OutputFormat format = new OutputFormat();
+         format.setLineWidth(72);
+         format.setIndenting(true);
+         format.setIndent(3);
+         format.setEncoding("ISO-8859-1");
+         if (path.startsWith("http:") || path.startsWith("http:")) {
+            throw new KalumetException("Can't write journal file over a HTTP 
URL");
+         }
+         if (path.startsWith("file:") || path.startsWith("FILE:")) {
+            path = path.substring(5);
+         }
+         XMLSerializer serializer = new XMLSerializer(new 
FileOutputStream(path), format);
+         serializer.serialize(this.toDOMElement(new CoreDocumentImpl(true)));
+      } 
+      catch (Exception e) {
+         throw new KalumetException("Can't write journal", e);
+      } finally {
+          lock.writeLock().release();
+      }
+   }
+
+}
\ No newline at end of file

Added: 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateLog.java
URL: 
http://svn.apache.org/viewvc/incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateLog.java?rev=1187714&view=auto
==============================================================================
--- 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateLog.java
 (added)
+++ 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateLog.java
 Sat Oct 22 13:18:35 2011
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.kalumet.model.update;
+
+import java.io.FileOutputStream;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.kalumet.KalumetException;
+import org.apache.kalumet.FileManipulator;
+import org.apache.kalumet.model.Environment;
+
+import org.apache.commons.lang.time.DateFormatUtils;
+import org.apache.commons.lang.time.FastDateFormat;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.xerces.dom.CoreDocumentImpl;
+import org.apache.xerces.dom.ElementImpl;
+import org.apache.xml.serialize.OutputFormat;
+import org.apache.xml.serialize.XMLSerializer;
+import org.w3c.dom.Element;
+
+/**
+ * Represents the main tag of the Kalumet update log DOM.
+ */
+public class UpdateLog {
+
+   private final static transient Log LOG = LogFactory.getLog(UpdateLog.class);
+   
+   public final static String MAIN_LOG_FILE = "log.xml";
+
+   private String status;
+   private String time;
+   private String title;
+   private String basedir;
+   private boolean updated = false;
+   private LinkedList updateMessages;
+   
+   public UpdateLog() {
+       this.updateMessages = new LinkedList();
+   }
+
+   /**
+    * Create a <code>UpdateLog</code> defining basic attributes.
+    * 
+    * @param status the status.
+    * @param time the time.
+    * @param title the title.
+    * @param environment the environment associated to this update log.
+    * @param updated the updated flag.
+    */
+   public UpdateLog(String status, String time, String title, Environment 
environment, boolean updated) throws KalumetException {
+      this.updateMessages = new LinkedList();
+      this.status = status;
+      this.time = time;
+      this.title = title;
+      this.basedir = FileManipulator.initEnvironmentCacheDir(environment);
+      this.updated = updated;
+   }
+   
+   /**
+    * Create a <code>UpdateLog</code>.
+    * 
+    * @param status the current update log status.
+    * @param title the current update log title.
+    * @param environment the update log environment linked.
+    * @throws KalumetException in case of update log creation failure.
+    */
+   public UpdateLog(String status, String title, Environment environment) 
throws KalumetException {
+       this(status, ((FastDateFormat) 
DateFormatUtils.ISO_DATETIME_FORMAT).format(new Date()), title, environment, 
false);
+   }
+
+   public String getStatus() {
+      return this.status;
+   }
+
+   public void setStatus(String status) {
+      this.status = status;
+   }
+
+   public String getTime() {
+      return this.time;
+   }
+
+   public void setTime(String time) {
+      this.time = time;
+   }
+
+   public String getTitle() {
+      return this.title;
+   }
+
+   public void setTitle(String title) {
+      this.title = title;
+   }
+
+   public boolean isUpdated() {
+      return this.updated;
+   }
+
+   public void setUpdated(boolean updated) {
+      this.updated = updated;
+   }
+
+   /**
+    * Adds a new <code>UpdateMessage</code> in the <code>UpdateLog</code>.
+    * 
+    * @param updateMessage the <code>UpdateMessage</code> to add.
+    */
+   public void addUpdateMessage(UpdateMessage updateMessage) {
+      this.updateMessages.add(updateMessage);
+      this.writeXMLFile();
+   }
+
+   /**
+    * Gets <code>UpdateMessage</code> list in the <code>UpdateLog</code>.
+    * 
+    * @return the <code>UpdateMessage</code> list.
+    */
+   public List getUpdateMessages() {
+      return this.updateMessages;
+   }
+
+   /**
+    * Transforms the <code>UpdateLog</code> POJO to a DOM element.
+    * 
+    * @param document the DOM document.
+    * @return the DOM element.
+    */
+   protected Element toDOMElement(CoreDocumentImpl document) {
+      ElementImpl element = new ElementImpl(document, "updatelog");
+      element.setAttribute("status", this.getStatus());
+      element.setAttribute("time", this.getTime());
+      element.setAttribute("title", this.getTitle());
+      element.setAttribute("updated", new 
Boolean(this.isUpdated()).toString());
+      // add update message child nodes
+      for (Iterator updateMessageIterator = 
this.getUpdateMessages().iterator(); updateMessageIterator.hasNext();) {
+         UpdateMessage updateMessage = (UpdateMessage) 
updateMessageIterator.next();
+         element.appendChild(updateMessage.toDOMElement(document));
+      }
+      return element;
+   }
+
+   /**
+    * Writes the Kalumet agent XML log file using in-memory DOM.
+    */
+   public synchronized void writeXMLFile() {
+      try {
+         OutputFormat format = new OutputFormat();
+         format.setLineWidth(72);
+         format.setIndenting(true);
+         format.setIndent(3);
+         format.setEncoding("ISO-8859-1");
+         XMLSerializer serializer = new XMLSerializer(new 
FileOutputStream(this.basedir + "/" + MAIN_LOG_FILE), format);
+         serializer.serialize(this.toDOMElement(new CoreDocumentImpl(true)));
+      } catch (Exception e) {
+         LOG.error("Can't write update log file.", e);
+      }
+   }
+
+}

Added: 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateMessage.java
URL: 
http://svn.apache.org/viewvc/incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateMessage.java?rev=1187714&view=auto
==============================================================================
--- 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateMessage.java
 (added)
+++ 
incubator/kalumet/trunk/common/src/main/java/org/apache/kalumet/model/update/UpdateMessage.java
 Sat Oct 22 13:18:35 2011
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.kalumet.model.update;
+
+import org.apache.xerces.dom.CDATASectionImpl;
+import org.apache.xerces.dom.CoreDocumentImpl;
+import org.apache.xerces.dom.ElementImpl;
+import org.w3c.dom.Element;
+
+/**
+ * Represents a <code>updatemessage</code> tag in the Kalumet update log DOM.
+ */
+public class UpdateMessage {
+
+   private String priority;
+   private String message;
+
+   public UpdateMessage() { }
+
+   /**
+    * Creates a UpdateMessage with a defined message content.
+    * 
+    * @param priority the message priority (info, error, ...).
+    * @param message the message content.
+    */
+   public UpdateMessage(String priority, String message) {
+      this.priority = priority;
+      this.message = message;
+   }
+
+   public String getPriority() {
+      return this.priority;
+   }
+
+   public void setPriority(String priority) {
+      this.priority = priority;
+   }
+
+   public String getMessage() {
+      return this.message;
+   }
+
+   public void setMessage(String message) {
+      this.message = message;
+   }
+
+   /**
+    * Transforms the <code>UpdateMessage</code> POJO to a DOM element.
+    * 
+    * @param document the DOM document.
+    * @return the DOM element.
+    */
+   protected Element toDOMElement(CoreDocumentImpl document) {
+      ElementImpl element = new ElementImpl(document, "updatemessage");
+      element.setAttribute("priority", this.getPriority());
+      CDATASectionImpl message = new CDATASectionImpl(document, 
this.getMessage());
+      element.appendChild(message);
+      return element;
+   }
+
+}


Reply via email to