001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.core.notification; 021 022 import org.apache.commons.io.IOUtils; 023 import org.apache.commons.lang.builder.ReflectionToStringBuilder; 024 import org.apache.commons.lang.builder.ToStringStyle; 025 import org.sonar.api.notifications.Notification; 026 import org.sonar.api.utils.SonarException; 027 028 import javax.persistence.*; 029 import java.io.*; 030 import java.util.Date; 031 032 @Entity 033 @Table(name = "notifications") 034 public class NotificationQueueElement { 035 036 @Id 037 @Column(name = "id") 038 @GeneratedValue 039 private Integer id; 040 041 @Column(name = "created_at") 042 private Date createdAt; 043 044 @Column(name = "data", updatable = true, nullable = true, length = 167772150) 045 private byte[] data; 046 047 public Integer getId() { 048 return id; 049 } 050 051 public void setId(Integer id) { 052 this.id = id; 053 } 054 055 public Date getCreatedAt() { 056 return createdAt; 057 } 058 059 public void setCreatedAt(Date createdAt) { 060 this.createdAt = createdAt; 061 } 062 063 public void setNotification(Notification notification) { 064 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 065 try { 066 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); 067 objectOutputStream.writeObject(notification); 068 objectOutputStream.close(); 069 this.data = byteArrayOutputStream.toByteArray(); 070 071 } catch (IOException e) { 072 throw new SonarException(e); 073 074 } finally { 075 IOUtils.closeQuietly(byteArrayOutputStream); 076 } 077 } 078 079 public Notification getNotification() { 080 if (this.data == null) { 081 return null; 082 } 083 ByteArrayInputStream byteArrayInputStream = null; 084 try { 085 byteArrayInputStream = new ByteArrayInputStream(this.data); 086 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); 087 Object result = objectInputStream.readObject(); 088 objectInputStream.close(); 089 return (Notification) result; 090 091 } catch (IOException e) { 092 throw new SonarException(e); 093 094 } catch (ClassNotFoundException e) { 095 throw new SonarException(e); 096 097 } finally { 098 IOUtils.closeQuietly(byteArrayInputStream); 099 } 100 } 101 102 @Override 103 public String toString() { 104 return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); 105 } 106 }